From 190e5e451989652f11a09192fc45ca7cc80b0f39 Mon Sep 17 00:00:00 2001 From: justalinko Date: Mon, 18 Oct 2021 14:35:09 +0700 Subject: [PATCH] add: DBfun, command make:dbfun , setting config and add class Config --- app/config/config.php | 22 +++++++ app/config/routes.php | 2 +- app/handler/IndexHandler.php | 9 ++- core/Bootstrap.php | 50 ++++++++------- core/functions/common.php | 2 +- core/functions/constants.php | 3 +- core/libraries/Config.php | 54 ++++++++++++++++ core/libraries/Handler.php | 31 ++++++++- dev/command/MakeDbfun.php | 118 +++++++++++++++++++++++++++++++++++ reiko | 2 + 10 files changed, 261 insertions(+), 32 deletions(-) create mode 100644 app/config/config.php create mode 100644 core/libraries/Config.php create mode 100644 dev/command/MakeDbfun.php diff --git a/app/config/config.php b/app/config/config.php new file mode 100644 index 0000000..2a6c6d9 --- /dev/null +++ b/app/config/config.php @@ -0,0 +1,22 @@ + + * @author ReiYan + * @copyright (c) 2021 + * + * @license MIT + * + */ + + + + +/** load functions */ +$config['load_functions'] = array('common' , 'form'); + +/** default language */ +$config['default_lang'] = 'en'; \ No newline at end of file diff --git a/app/config/routes.php b/app/config/routes.php index 622b51b..cfa312e 100644 --- a/app/config/routes.php +++ b/app/config/routes.php @@ -23,7 +23,7 @@ /** set router here */ $router->get('/' ,'IndexHandler@index'); - +$router->get('/db-test' , 'IndexHandler@get_db'); diff --git a/app/handler/IndexHandler.php b/app/handler/IndexHandler.php index d120c55..1b8c272 100644 --- a/app/handler/IndexHandler.php +++ b/app/handler/IndexHandler.php @@ -3,7 +3,7 @@ namespace Reiko\App; use Reiko\Libraries\Handler; -use Reiko\Libraries\DB; +use Reiko\Domains; class IndexHandler extends Handler { @@ -11,11 +11,16 @@ class IndexHandler extends Handler public function index() { $data['menus'] = ['Home'=> 'https://github.com/justalinko/reikoframework', - 'Documentation' => 'https://github.com/justalinko/reikoframework', + 'Documentation' => 'https://github.com/justalinko/reikoframework/wiki', 'Contribute' => 'https://github.com/justalinko/reikoframework', 'Donate' => 'https://github.com/justalinko/reikoframework']; $data['title'] = 'Reiko Framework'; $this->view('default', $data); } + public function get_db() + { + $domain = new Domains; + dd($domain->getAll()); + } } diff --git a/core/Bootstrap.php b/core/Bootstrap.php index f806d19..644b0b7 100644 --- a/core/Bootstrap.php +++ b/core/Bootstrap.php @@ -12,12 +12,15 @@ * */ +use Reiko\Libraries\Config; +use Reiko\Libraries\DB; use Reiko\Libraries\Handler; class Bootstrap { - public $route; + + private $load; public function __construct() { $dotenv = Dotenv\Dotenv::createImmutable(ROOT_PATH); @@ -31,35 +34,34 @@ public function __construct() ORM::configure("mysql:host=$dbhost;dbname=$dbname"); ORM::configure("username",$dbuser); ORM::configure("password",$dbpass); - - $this->load_library(); - $this->handler = new Handler; - } - public function load_library() - { - - spl_autoload_register(function ($class) { - $ex = explode("\\", $class); - $class = end($ex); - if (file_exists(LIB_PATH . $class . '.php')) { - require_once LIB_PATH . $class . '.php'; - } - }); } - public function load_configFiles() + public function load( $lib) { - $dir = CONFIG_PATH; - $files = glob($dir . '/*.php'); - - foreach ($files as $file) { - require($file); + $exp = explode("\\" , $lib); + $file = end($exp); + if(file_exists(LIB_PATH . $file. '.php')){ + require_once LIB_PATH. $file.'.php'; } } - public function run() + private function register_lib() { - $this->handler->run(); + spl_autoload_register(function($class) + { + $this->load($class); + }); + } + public function run(){ - $this->load_configFiles(); + $this->register_lib(); + + $config = new Config; + $config->init(); + $handler = new Handler; + $handler->run(); + + require CONFIG_PATH . '/routes.php'; + } + } diff --git a/core/functions/common.php b/core/functions/common.php index db47883..ed79fe9 100644 --- a/core/functions/common.php +++ b/core/functions/common.php @@ -53,7 +53,7 @@ function assets($files) } function dd($data){ - $data = var_dump($data); + $data = ($data); highlight_string(""); echo ''; die(); diff --git a/core/functions/constants.php b/core/functions/constants.php index 2657b43..4a4a204 100644 --- a/core/functions/constants.php +++ b/core/functions/constants.php @@ -43,4 +43,5 @@ const VIEW_PATH = APP_PATH . SEPARATOR . 'view' . SEPARATOR; const CONFIG_PATH = APP_PATH . SEPARATOR . 'config' . SEPARATOR; const LANG_PATH = APP_PATH . SEPARATOR . 'language' . SEPARATOR; - const REQUEST_PATH = APP_PATH . SEPARATOR . 'request'. SEPARATOR; \ No newline at end of file + const REQUEST_PATH = APP_PATH . SEPARATOR . 'request'. SEPARATOR; + diff --git a/core/libraries/Config.php b/core/libraries/Config.php new file mode 100644 index 0000000..82449c3 --- /dev/null +++ b/core/libraries/Config.php @@ -0,0 +1,54 @@ + + * @author ReiYan + * @copyright (c) 2021 + * + * @license MIT + * + */ + +namespace Reiko\Libraries; + +class Config +{ + public $config; + private $config_list; + + public function register_config() + { + /** + * You can call the config files in 'app/config/' + * This addtional config besides of .env files. + */ + $this->config_list = [ + 'config', + 'routes' + ]; + } + + public function init() + { + $this->register_config(); + foreach ($this->config_list as $config_file) { + /** check config file exist */ + if (file_exists(CONFIG_PATH . $config_file . '.php')) { + /** include registered config file */ + require_once(CONFIG_PATH . $config_file . '.php'); + } else { + exit('ERROR : Config ' . $config_file . ' not exist !'); + } + } + + /** declare $config to constant */ + define('CONFIG' , $config); + + /** declare $config to $this->config */ + $this->config = $config; + } +} diff --git a/core/libraries/Handler.php b/core/libraries/Handler.php index cc77ddd..9b31c1e 100644 --- a/core/libraries/Handler.php +++ b/core/libraries/Handler.php @@ -23,8 +23,7 @@ class Handler{ public function __construct() { /** load some functions */ - $this->use_fun('common'); - $this->use_fun('form'); + $this->use_fun_array(CONFIG['load_functions']); /** load latte template engine */ $this->latte = new \Latte\Engine; @@ -35,7 +34,9 @@ public function __construct() public function run(){ - $this->load_appHandler(); + $this->load_DBfun(); + $this->load_appHandler(); + } public function view($view , $params = []) { @@ -56,6 +57,17 @@ public function use_fun($functions) exit('FUNCTIONS : '.$functions . ' Doesn\'t exists !'); } } + public function use_fun_array($arr) + { + if(is_array($arr)) + { + foreach($arr as $fun) + { + $this->use_fun($fun); + } + } + } + public function load_appHandler(){ spl_autoload_register(function($class){ @@ -66,4 +78,17 @@ public function load_appHandler(){ } }); } + + public function load_DBfun() + { + spl_autoload_register(function($class) + { + $ex = explode("\\" , $class); + $class = end($ex); + if(file_exists(APP_PATH . $class.'.dbfun.php')) + { + require_once APP_PATH .$class .'.dbfun.php'; + } + }); + } } \ No newline at end of file diff --git a/dev/command/MakeDbfun.php b/dev/command/MakeDbfun.php new file mode 100644 index 0000000..63f571a --- /dev/null +++ b/dev/command/MakeDbfun.php @@ -0,0 +1,118 @@ +addArgument('funName' , InputArgument::REQUIRED , "Name of db class"); + $this->addOption('methods' , null, InputOption::VALUE_OPTIONAL , 'Generate methods in db class'); + $this->addOption('table' ,null , InputOption::VALUE_OPTIONAL , 'Use the table instead , default table is name of fundb'); + } + + protected function execute(InputInterface $input, OutputInterface $output): int + { + $name = $input->getArgument('funName'); + $output->writeln("Generating $name please wait ... "); + $this->templateFun($name , $input->getOption('methods') , $input->getOption('table')); + $output->write("Successfully !"); + return Command::SUCCESS; + } + protected function templateFun($filename,$methods,$table){ + + if($table == null) + { + $table = strtolower($filename); + } +$tmpl = " + * @author ReiYan + * @copyright (c) 2021 + * + * @license MIT + * + */ +use Reiko\Libraries\DB; + +class ".ucfirst($filename)." extends DB +{ + protected \$table = \"{$table}\"; + protected \$id = \"id_{$table}\"; + + public function __construct() + { + parent::__construct(); + \$this->table(\$this->table); + } + + /** + * The reiko framework generated basic function for CRUD + * @method create + * @method readAll + * @method readById + * @method update + * @method delete + * + **/ + public function create(\$data = []) + { + return \$this->save(\$data); + } + + public function readAll() + { + return \$this->select('*')->get(); + } + + public function delete(\$id) + { + return \$this->where([\$this->id => \$id])->delete(); + } + + public function readById(\$id) + { + return \$this->where([\$this->id => \$id])->fetchArray(); + } + + public function update(\$data= [] , \$id) + { + return \$this->set(\$data)->where([\$this->id => \$id])->update(); + } + + /** + * You can add your method bellow + */ + "; +if($methods != null) +{ + foreach(explode("," , $methods) as $method){ +$tmpl.= " + public function $method() + { + // some code here for method : $method + } +"; + } +} +$tmpl.= " +}"; + +file_put_contents( 'app/'.ucfirst($filename) . '.dbfun.php' , $tmpl); + } +} \ No newline at end of file diff --git a/reiko b/reiko index 0437ffa..e13e747 100755 --- a/reiko +++ b/reiko @@ -6,6 +6,7 @@ require 'dev/loadCommand.php'; use Dev\Command\ClearCache; use Symfony\Component\Console\Application; use Dev\Command\Init; +use Dev\Command\MakeDbfun; use Dev\Command\MakeHandler; use Dev\Command\Run; @@ -55,6 +56,7 @@ Class ReikoCLI{ $command_lists = [Run::class, Init::class, MakeHandler::class, + MakeDbfun::class, ClearCache::class];