From aff8730316c9f75b1bc5320ac7142bc4f7135383 Mon Sep 17 00:00:00 2001 From: Stephan Kasdorf Date: Tue, 28 Aug 2018 11:50:22 +0200 Subject: [PATCH] CORE UPDATE: Minor update concerning the autoloading class in the core, now it is also possible to give a loading order through the configuration Minor update concerning the form factory classes in the core, now some javascript events are implemented as well, another update concerning functinoallity will follow soon. Update on the example configuration file, implementing the autoloading order of interfaces, moduels and traits. Update for multidatabase support, see the documentation on http://www.nibiru-framework.com --- README.md | 31 +-- application/interfaces/users.php | 16 ++ application/module/empty | 0 application/module/users.php | 18 ++ .../settings/config/settings.development.ini | 45 ++-- application/trait/users.php | 16 ++ core/c/autoloader.php | 205 ++++++++++++++++-- core/c/formattributes.php | 6 + core/c/mysql.php | 16 +- core/c/odbc.php | 16 +- core/c/pdo.php | 26 ++- core/c/postgres.php | 19 +- core/c/typecheckbox.php | 11 +- core/f/db.php | 3 +- core/i/form.php | 1 + 15 files changed, 343 insertions(+), 86 deletions(-) create mode 100644 application/interfaces/users.php delete mode 100644 application/module/empty create mode 100644 application/module/users.php create mode 100644 application/trait/users.php diff --git a/README.md b/README.md index 4062c25..336e369 100644 --- a/README.md +++ b/README.md @@ -34,9 +34,10 @@ Engine Implementation. @@ -51,11 +52,13 @@ Engine Implementation. -

Version 0.3 beta 04.02.2018

+ +

Version 0.3 beta 04.02.2018

Previous version

-

Version 0.3.5 beta 14.03.2018

+

Version 0.3.5 beta 14.03.2018

Update

-

Version 0.4.0 beta 09.07.2018

+

Version 0.4.0 beta 28.08.2018

The start is done, have success with PHP prototyping, and always remember to have fun!
- Author: Stephan Kasdorf

\ No newline at end of file diff --git a/application/interfaces/users.php b/application/interfaces/users.php new file mode 100644 index 0000000..af2deba --- /dev/null +++ b/application/interfaces/users.php @@ -0,0 +1,16 @@ +debug( true ); + * @param bool $debug + */ + public static function debug( bool $debug ): void + { + self::$_debug = $debug; + } + + /** + * @desc will sort the array by a given sort order from the configuration file + * just provided in the AUTOLOADER section. + * @param array $modules + * @return array + */ + private static function sortOrderModules( array $modules, $section ): array + { + (bool) $skip = false; + (array) $normal = array(); + + if($section == self::SETTINGS_CLASS_POS) + { + $moduleSortOrder = Config::getInstance()->getConfig()[self::SETTINGS_SECTION][self::SETTINGS_CLASS_POS]; + } + if($section == self::SETTINGS_TRAIT_POS) + { + $moduleSortOrder = Config::getInstance()->getConfig()[self::SETTINGS_SECTION][self::SETTINGS_TRAIT_POS]; + } + if($section == self::SETTINGS_IFACE_POS) + { + $moduleSortOrder = Config::getInstance()->getConfig()[self::SETTINGS_SECTION][self::SETTINGS_IFACE_POS]; + } + if(sizeof($moduleSortOrder)>0) + { + foreach ($modules as $module) + { + foreach ($moduleSortOrder as $sortOrder) + { + if($module['nfilename'] == $sortOrder) + { + $first[] = $module; + } + else + { + $skip = true; + } + } + if($skip) + { + $normal[] = $module; + $skip = false; + } + } + if(array_key_exists(0, $first)) + { + $sorted = array(); + foreach($moduleSortOrder as $item) + { + foreach ($first as $fmodule) + { + if($item==$fmodule['nfilename']) + { + $sorted[] = $fmodule; + } + } + } + } + $modules = array_merge($sorted, $normal); + } + return $modules; + } + /** * @desc includes all database model files in preparation for the * database factory model @@ -39,10 +128,19 @@ class Autoloader { foreach (self::getFilesInFoler() as $file) { + $required[] = $file; require_once $file; } + if(self::isDebug()) + { + View::getInstance()->assign( + array( + 'ndbraw_output' => '
' . print_r($required, true) . '
' + ) + ); + } } - + /** * @return array */ @@ -50,47 +148,106 @@ class Autoloader { return self::$_filesInFoler; } - + + /** + * @return array + */ + protected static function getModules(): array + { + return self::$_modules; + } + /** * @param array $filesInFoler */ private static function _setFilesInFoler( ) { - $modelFolder = Config::getInstance()->getConfig()[View::NIBIRU_SETTINGS][self::DB_MODEL_FOLDER]; - if(is_array($modelFolder)) + /** + * @desc arrays for sorting the module order, so they will load + * alphabetically + */ + $modules = array(); + + if( is_array( Config::getInstance()->getConfig()[View::NIBIRU_SETTINGS][self::DB_MODEL_FOLDER] ) ) { - foreach ($modelFolder as $folder) + foreach ( Config::getInstance()->getConfig()[View::NIBIRU_SETTINGS][self::DB_MODEL_FOLDER] as $modelfolder ) { - $iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator(__DIR__ . $folder )); - foreach ($iterator as $item) + $iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator( __DIR__ . $modelfolder )); + foreach ( $iterator as $item ) { if($item->getFileName()!= self::MY_FILE_NAME && $item->getFileName()!="." && $item->getFileName()!="..") { self::$_filesInFoler[] = $item->getPathName(); } - } + } } } else { - $iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator(__DIR__ . $modelFolder )); - foreach ($iterator as $item) + $iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator(__DIR__ . Config::getInstance()->getConfig()[View::NIBIRU_SETTINGS][self::DB_MODEL_FOLDER] )); + foreach ( $iterator as $item ) { if($item->getFileName()!= self::MY_FILE_NAME && $item->getFileName()!="." && $item->getFileName()!="..") { self::$_filesInFoler[] = $item->getPathName(); } - } - } - $iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator(__DIR__ . Config::getInstance()->getConfig()[View::NIBIRU_SETTINGS][self::MODULE_FOLDER] )); - foreach ($iterator as $item) + } + } + /** + * @desc run check on modules that should provide an interface as well as a trait + */ + $iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator(__DIR__ . Config::getInstance()->getConfig()[View::NIBIRU_SETTINGS][self::INTERFACE_FOLDER] )); + foreach ( $iterator as $item ) { if($item->getFileName()!= self::MY_FILE_NAME && $item->getFileName()!="." && $item->getFileName()!="..") { - self::$_filesInFoler[] = $item->getPathName(); + $interfaces[] = array( + 'nfilename' => str_replace('.php', '', $item->getFileName()), + 'filepathname' => $item->getPath() . '/' . $item->getFileName() + ); } } + asort($interfaces); + $Sinterfaces = self::sortOrderModules($interfaces, self::SETTINGS_IFACE_POS); + foreach ($Sinterfaces as $interface) + { + self::$_filesInFoler[] = $interface['filepathname']; + } + + $iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator(__DIR__ . Config::getInstance()->getConfig()[View::NIBIRU_SETTINGS][self::TRAIT_FOLDER] )); + foreach ( $iterator as $item ) + { + if($item->getFileName()!= self::MY_FILE_NAME && $item->getFileName()!="." && $item->getFileName()!="..") + { + $traits[] = array( + 'nfilename' => str_replace('.php', '', $item->getFileName()), + 'filepathname' => $item->getPath() . '/' . $item->getFileName() + ); + } + } + asort($traits); + $Straits = self::sortOrderModules($traits, self::SETTINGS_TRAIT_POS); + foreach($Straits as $trait) + { + self::$_filesInFoler[] = $trait['filepathname']; + } + + $iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator(__DIR__ . Config::getInstance()->getConfig()[View::NIBIRU_SETTINGS][self::MODULE_FOLDER] )); + foreach ( $iterator as $item ) + { + if($item->getFileName()!= self::MY_FILE_NAME && $item->getFileName()!="." && $item->getFileName()!="..") + { + $modules[] = array( + 'nfilename' => str_replace('.php', '', $item->getFileName()), + 'filepathname' => $item->getPath() . '/' . $item->getFileName() + ); + } + } + asort($modules); + $Smodules = self::sortOrderModules($modules, self::SETTINGS_CLASS_POS); + foreach ($Smodules as $module) + { + self::$_filesInFoler[] = $module['filepathname']; + } } - - } \ No newline at end of file diff --git a/core/c/formattributes.php b/core/c/formattributes.php index 6b19087..2e4fb38 100755 --- a/core/c/formattributes.php +++ b/core/c/formattributes.php @@ -60,17 +60,23 @@ class FormAttributes $this->_element = str_replace(' CLASS', '', $this->_element); $this->_element = str_replace(' enctype="ENCTYPE"', '', $this->_element); $this->_element = str_replace(' onsubmit="ONSUBMIT"', '', $this->_element); + $this->_element = str_replace(' onclick="ONCLICK"', '', $this->_element); $this->_element = str_replace(' action="ACTION"', '', $this->_element); $this->_element = str_replace(' SPEECH', '', $this->_element); $this->_element = str_replace(' FORM', '', $this->_element); $this->_element = str_replace(' placeholder="PLACEHOLDER"', '', $this->_element); $this->_element = str_replace(' required="REQUIRED"', '', $this->_element); $this->_element = str_replace(' value="VALUE"', '', $this->_element); + $this->_element = str_replace(' name="NAME"', '', $this->_element); $this->_element = str_replace(' ', ' ', $this->_element); $this->_element = str_replace(' type="TYPE"', '', $this->_element); $this->_element = str_replace(' onchange="ONCHANGE"', '', $this->_element); + $this->_element = str_replace(' target="TARGET"', '', $this->_element); + $this->_element = str_replace(' method="METHOD"', '', $this->_element); + $this->_element = str_replace(' data-toggle="DATA-TOGGLE"', '', $this->_element); $this->_element = str_replace(' SELECTED', '', $this->_element); $this->_element = str_replace(' CONTEXT', '', $this->_element); + $this->_element = str_replace(' CHECKED', '', $this->_element); } /** diff --git a/core/c/mysql.php b/core/c/mysql.php index a05eaa8..493561a 100755 --- a/core/c/mysql.php +++ b/core/c/mysql.php @@ -24,10 +24,16 @@ class Mysql implements IMysql protected $_conn = self::PLACE_CONNECTION; - protected function __construct( ) + protected function __construct( $section = false ) { - - $settings = Config::getInstance()->getConfig()[self::SETTINGS_DATABASE]; + if($section) + { + $settings = Config::getInstance()->getConfig()[$section]; + } + else + { + $settings = Config::getInstance()->getConfig()[self::SETTINGS_DATABASE]; + } $this->_setUsername($settings[self::PLACE_USERNAME]); $this->_setPassword($settings[self::PLACE_PASSWORD]); $this->_setDbname($settings[self::PLACE_DATABASE]); @@ -38,10 +44,10 @@ class Mysql implements IMysql $this->_setConn(); } - public static function getInstance() + public static function getInstance( $section = false ) { $className = get_called_class(); - if(self::$_instance==null) self::$_instance = new $className(); + if(self::$_instance==null) self::$_instance = new $className( $section ); return self::$_instance; } diff --git a/core/c/odbc.php b/core/c/odbc.php index 7cc2977..a3221d6 100755 --- a/core/c/odbc.php +++ b/core/c/odbc.php @@ -17,10 +17,16 @@ class Odbc extends Mysql implements IOdbc private static $_instance; - protected function __construct( ) + protected function __construct( $section = false ) { - - $settings = Config::getInstance()->getConfig()[self::SETTINGS_DATABASE]; + if($section) + { + $settings = Config::getInstance()->getConfig()[$section]; + } + else + { + $settings = Config::getInstance()->getConfig()[self::SETTINGS_DATABASE]; + } $this->_setUsername($settings[self::PLACE_USERNAME]); $this->_setPassword($settings[self::PLACE_PASSWORD]); $this->_setDbname($settings[self::PLACE_DATABASE]); @@ -32,10 +38,10 @@ class Odbc extends Mysql implements IOdbc $this->_setConn(); } - public static function getInstance() + public static function getInstance( $section = false ) { $className = get_called_class(); - if(self::$_instance==null) self::$_instance = new $className(); + if(self::$_instance==null) self::$_instance = new $className( $section ); return self::$_instance; } diff --git a/core/c/pdo.php b/core/c/pdo.php index 1b0a2a9..2f48490 100644 --- a/core/c/pdo.php +++ b/core/c/pdo.php @@ -10,7 +10,17 @@ namespace Nibiru; */ final class Pdo extends Mysql implements IPdo { - + private static $section = false; + + public static function settingsSection( $section = IOdbc::SETTINGS_DATABASE ) + { + self::$section = $section; + } + + private static function getSettingsSection() + { + return self::$section; + } /** * @param string $string * @@ -18,7 +28,7 @@ final class Pdo extends Mysql implements IPdo */ public static function query( $string = self::PLACE_NO_QUERY ) { - $query = parent::getInstance()->getConn()->query( $string ); + $query = parent::getInstance( self::getSettingsSection() )->getConn()->query( $string ); while($row = $query->fetch()) { $keys = array_keys($row); @@ -35,7 +45,7 @@ final class Pdo extends Mysql implements IPdo public static function selectDatasetByFieldAndValue($tablename = self::PLACE_TABLE_NAME, $fieldAndValue = array() ) { - $result = parent::getInstance()->getConn()->query("SELECT * FROM " . $tablename . " WHERE " . $fieldAndValue['name'] . " = '" . $fieldAndValue['value'] . "';"); + $result = parent::getInstance( self::getSettingsSection() )->getConn()->query("SELECT * FROM " . $tablename . " WHERE " . $fieldAndValue['name'] . " = '" . $fieldAndValue['value'] . "';"); return $result->fetchAll(); } @@ -45,7 +55,7 @@ final class Pdo extends Mysql implements IPdo $field_name = IMysql::PLACE_FIELD_NAME, $where_value = IMysql::PLACE_WHERE_VALUE ) { - $statement = parent::getInstance()->getConn(); + $statement = parent::getInstance( self::getSettingsSection() )->getConn(); $query = "UPDATE " . $tablename . " SET " . $column_name . " = :" . $column_name . " WHERE " . $field_name . " = :". $field_name; $insert = $statement->prepare($query); $insert->bindParam( ':'.$column_name, $parameter_name ); @@ -56,7 +66,7 @@ final class Pdo extends Mysql implements IPdo public static function fetchRowInArrayById($tablename = self::PLACE_TABLE_NAME, $id = self::NO_ID ) { $result = array(); - $statement = parent::getInstance()->getConn(); + $statement = parent::getInstance( self::getSettingsSection() )->getConn(); $describe = $statement->query('DESC ' . $tablename); $describe->execute(); $tableInformation = $describe->fetchAll( \PDO::FETCH_ASSOC ); @@ -93,7 +103,7 @@ final class Pdo extends Mysql implements IPdo $column_name = IMysql::PLACE_COLUMN_NAME, $parameter_name = IMysql::PLACE_SEARCH_TERM) { - $statement = parent::getInstance()->getConn(); + $statement = parent::getInstance( self::getSettingsSection() )->getConn(); $prepare = $statement->prepare("SELECT * FROM " . $tablename . " WHERE " . $column_name . " = :" . $column_name . ";"); $prepare->bindParam(":".$column_name, $parameter_name, \PDO::PARAM_STR); $prepare->execute(); @@ -116,7 +126,7 @@ final class Pdo extends Mysql implements IPdo public static function fetchTableAsArray( $tablename = self::PLACE_TABLE_NAME ) { - $statement = parent::getInstance()->getConn()->query('SElECT * FROM ' . $tablename); + $statement = parent::getInstance( self::getSettingsSection() )->getConn()->query('SElECT * FROM ' . $tablename); $statement->execute(); $result = $statement->fetchAll( \PDO::FETCH_ASSOC ); return $result; @@ -131,7 +141,7 @@ final class Pdo extends Mysql implements IPdo */ public static function insertArrayIntoTable( $tablename = IMysql::PLACE_TABLE_NAME, $array_name = IMysql::PLACE_ARRAY_NAME, $encrypted = IMysql::PLACE_DES_ENCRYPT ) { - $statement = parent::getInstance()->getConn(); + $statement = parent::getInstance( self::getSettingsSection() )->getConn(); if(is_array($array_name)) { diff --git a/core/c/postgres.php b/core/c/postgres.php index 6d1a655..d96005e 100755 --- a/core/c/postgres.php +++ b/core/c/postgres.php @@ -9,6 +9,17 @@ namespace Nibiru; */ class Postgres extends Odbc implements IPostgres { + private static $section = false; + + public static function settingsSection( $section = IOdbc::SETTINGS_DATABASE ) + { + self::$section = $section; + } + + private static function getSettingsSection() + { + return self::$section; + } /** * @desc does a plain SQL query on a postgres database, and returns the * result as an array @@ -18,7 +29,7 @@ class Postgres extends Odbc implements IPostgres public static function query($string = IOdbc::PLACE_NO_QUERY) { $all = array(); - $result = \odbc_exec(parent::getInstance()->getConn(), $string); + $result = \odbc_exec(parent::getInstance( self::getSettingsSection() )->getConn(), $string); for($i=1;$row=\odbc_fetch_object($result,$i);$i++) { $row_values = array(); @@ -54,7 +65,7 @@ class Postgres extends Odbc implements IPostgres public static function fetchTableFieldsAsArray($tablename = IOdbc::PLACE_TABLE_NAME) { $columns = array(); - $result = \odbc_columns(parent::getInstance()->getConn(), null, null, $tablename); + $result = \odbc_columns(parent::getInstance( self::getSettingsSection() )->getConn(), null, null, $tablename); for($i=0;$row=\odbc_fetch_object($result, $i);$i++) { foreach ($row as $key=>$entry) @@ -112,7 +123,7 @@ class Postgres extends Odbc implements IPostgres $row .= " )"; $sql = 'INSERT INTO ' . $tablename . $row . ';'; - \odbc_exec(parent::getInstance()->getConn(), $sql); + \odbc_exec(parent::getInstance( self::getSettingsSection() )->getConn(), $sql); } } else @@ -148,7 +159,7 @@ class Postgres extends Odbc implements IPostgres $row .= " )"; $sql = 'INSERT INTO ' . $tablename . $row . ';'; - \odbc_exec(parent::getInstance()->getConn(), $sql); + \odbc_exec(parent::getInstance( self::getSettingsSection() )->getConn(), $sql); } } diff --git a/core/c/typecheckbox.php b/core/c/typecheckbox.php index 65154c4..82a91fe 100644 --- a/core/c/typecheckbox.php +++ b/core/c/typecheckbox.php @@ -12,10 +12,11 @@ use Nibiru\Adapter; class TypeCheckbox extends FormAttributes implements IForm { private $_attributes = array( - self::FORM_NAME => '', - self::FORM_VALUE => '', - self::FORM_ATTRIBUTE_ID => '', - self::FORM_ATTRIBUTE_CLASS => '' + self::FORM_NAME => '', + self::FORM_VALUE => '', + self::FORM_ATTRIBUTE_ID => '', + self::FORM_ATTRIBUTE_CLASS => '', + self::FORM_ATTRIBUTE_CHECKED => '' ); public function loadElement( $attributes ) @@ -32,7 +33,7 @@ class TypeCheckbox extends FormAttributes implements IForm */ private function _setElement( ) { - $this->_element = '' . 'VALUE
' . "\n"; + $this->_element = '' . 'VALUE
' . "\n"; } diff --git a/core/f/db.php b/core/f/db.php index e0d1ba5..3a88c56 100644 --- a/core/f/db.php +++ b/core/f/db.php @@ -9,7 +9,8 @@ namespace Nibiru\Factory; */ class Db { - protected static $_model = null; + const DATABASE_DRIVER_NS = "driver"; + protected static $_model = null; /** * @desc loads the database model through the correct Factory, diff --git a/core/i/form.php b/core/i/form.php index 39f5488..c1fbab4 100644 --- a/core/i/form.php +++ b/core/i/form.php @@ -39,6 +39,7 @@ interface IForm const FORM_ATTRIBUTE_ONSUBMIT = 'onsubmit'; const FORM_ATTRIBUTE_SELECTED = 'selected'; const FORM_ATTRIBUTE_CONTEXT = 'context'; + const FORM_ATTRIBUTE_CHECKED = 'checked'; /** * @desc loads the current Form element to the form