Skip to content

Commit

Permalink
add: DBfun, command make:dbfun , setting config and add class Config
Browse files Browse the repository at this point in the history
  • Loading branch information
justalinko committed Oct 18, 2021
1 parent 8ce7cb6 commit 190e5e4
Show file tree
Hide file tree
Showing 10 changed files with 261 additions and 32 deletions.
22 changes: 22 additions & 0 deletions app/config/config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
/**
* REIKO FRAMEWORK
*
* @package ReiKo Framework
*
* @author alinko <[email protected]>
* @author ReiYan <[email protected]>
* @copyright (c) 2021
*
* @license MIT
*
*/




/** load functions */
$config['load_functions'] = array('common' , 'form');

/** default language */
$config['default_lang'] = 'en';
2 changes: 1 addition & 1 deletion app/config/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

/** set router here */
$router->get('/' ,'IndexHandler@index');

$router->get('/db-test' , 'IndexHandler@get_db');



Expand Down
9 changes: 7 additions & 2 deletions app/handler/IndexHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,24 @@
namespace Reiko\App;

use Reiko\Libraries\Handler;
use Reiko\Libraries\DB;
use Reiko\Domains;

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());
}

}
50 changes: 26 additions & 24 deletions core/Bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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';


}

}
2 changes: 1 addition & 1 deletion core/functions/common.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ function assets($files)
}

function dd($data){
$data = var_dump($data);
$data = ($data);
highlight_string("<?php\n " . var_export($data, true) . "?>");
echo '<script>document.getElementsByTagName("code")[0].getElementsByTagName("span")[1].remove() ;document.getElementsByTagName("code")[0].getElementsByTagName("span")[document.getElementsByTagName("code")[0].getElementsByTagName("span").length - 1].remove() ; </script>';
die();
Expand Down
3 changes: 2 additions & 1 deletion core/functions/constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
const REQUEST_PATH = APP_PATH . SEPARATOR . 'request'. SEPARATOR;

54 changes: 54 additions & 0 deletions core/libraries/Config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

/**
* REIKO FRAMEWORK
*
* @package ReiKo Framework
*
* @author alinko <[email protected]>
* @author ReiYan <[email protected]>
* @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;
}
}
31 changes: 28 additions & 3 deletions core/libraries/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -35,7 +34,9 @@ public function __construct()

public function run(){

$this->load_appHandler();
$this->load_DBfun();
$this->load_appHandler();

}
public function view($view , $params = [])
{
Expand All @@ -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){
Expand All @@ -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';
}
});
}
}
118 changes: 118 additions & 0 deletions dev/command/MakeDbfun.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<?php
namespace Dev\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;

class MakeDbfun extends Command
{
// the name of the command (the part after "bin/console")
protected static $defaultName = 'make:dbfun';
protected static $defaultDescription = 'Automation command for make DB classes';
protected function configure(): void
{

$this->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("<comment>Generating $name please wait ... </comment>");
$this->templateFun($name , $input->getOption('methods') , $input->getOption('table'));
$output->write("<info>Successfully !</info>");
return Command::SUCCESS;
}
protected function templateFun($filename,$methods,$table){

if($table == null)
{
$table = strtolower($filename);
}
$tmpl = "<?php
namespace Reiko;
/**
* REIKO FRAMEWORK
*
* @package ReiKo Framework
*
* @author alinko <[email protected]>
* @author ReiYan <[email protected]>
* @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);
}
}
Loading

0 comments on commit 190e5e4

Please sign in to comment.