diff --git a/App/Controllers/WelcomeController.php b/App/Controllers/WelcomeController.php index fb68b1a..07a88a8 100644 --- a/App/Controllers/WelcomeController.php +++ b/App/Controllers/WelcomeController.php @@ -27,6 +27,7 @@ public function welcome() public function demo() { - return View::make('welcome')->withComponent($data);; + $data = []; + return View::make('welcome')->withComponent($data); } } diff --git a/App/Routes.php b/App/Routes.php index 744cde4..6fffbd9 100644 --- a/App/Routes.php +++ b/App/Routes.php @@ -15,3 +15,4 @@ use Silver\Core\Route; Route::get('/', 'Welcome@welcome', 'home', 'public'); +Route::get('/demo', 'Welcome@demo', 'home', 'public'); diff --git a/App/Templates/Controller.ghost.tpl b/App/Templates/Controller.ghost.tpl index 3e31e2f..fa1c130 100644 --- a/App/Templates/Controller.ghost.tpl +++ b/App/Templates/Controller.ghost.tpl @@ -3,6 +3,7 @@ namespace App\Controllers; use Silver\Core\Controller; +use Silver\Http\View; /** * {{{$name}}} controller @@ -11,21 +12,22 @@ class {{{ucfirst($name)}}}Controller extends Controller { public function get() { - return "Welcome in {{$name}} controller. This file is on App/Controllers/"; + echo "Welcome in {{$name}} controller. This file is on App/Controllers/"; + //return View::make(''); } public function post() { - echo 'Methode: post'; + echo 'Method: post'; } public function put() { - echo 'Methode: put'; + echo 'Method: put'; } public function delete() { - echo 'Methode: delete'; + echo 'Method: delete'; } } diff --git a/App/Templates/View.ghost.tpl b/App/Templates/View.ghost.tpl index 5390165..c5cc0f0 100644 --- a/App/Templates/View.ghost.tpl +++ b/App/Templates/View.ghost.tpl @@ -1,7 +1,5 @@ +{{ extends('layouts.master') }} - - - - - - Welcome in {{{$name}}}. \ No newline at end of file +#set[content] + Welcome in {{{$name}}}. +#end diff --git a/App/Views/components/demo.ghost.php b/App/Views/components/demo.ghost.tpl similarity index 100% rename from App/Views/components/demo.ghost.php rename to App/Views/components/demo.ghost.tpl diff --git a/App/Views/errors/.gitkeep b/App/Views/errors/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/App/Views/layouts/master.ghost.php b/App/Views/layouts/master.ghost.tpl similarity index 99% rename from App/Views/layouts/master.ghost.php rename to App/Views/layouts/master.ghost.tpl index ac44166..149131d 100644 --- a/App/Views/layouts/master.ghost.php +++ b/App/Views/layouts/master.ghost.tpl @@ -13,12 +13,9 @@ - -#block(content) - welcome +#block(content) {{ js('app') }} - diff --git a/App/Views/test2.ghost.tpl b/App/Views/test2.ghost.tpl new file mode 100644 index 0000000..7d3b2ad --- /dev/null +++ b/App/Views/test2.ghost.tpl @@ -0,0 +1,5 @@ +{{ extends('layouts.master') }} + +#set[content] + Welcome to {{ test2 }} page +#end \ No newline at end of file diff --git a/App/Views/welcome.ghost.php b/App/Views/welcome.ghost.tpl similarity index 93% rename from App/Views/welcome.ghost.php rename to App/Views/welcome.ghost.tpl index 1e7f672..6c0cd9f 100644 --- a/App/Views/welcome.ghost.php +++ b/App/Views/welcome.ghost.tpl @@ -1,7 +1,5 @@ {{ extends('layouts.master') }} #set[content] - Welcome to SilverEngine demo page - #end diff --git a/Database/Migrations/Migrations.php b/Database/Migrations/Migrations.php index 530a05b..469caae 100644 --- a/Database/Migrations/Migrations.php +++ b/Database/Migrations/Migrations.php @@ -17,7 +17,6 @@ class Migrations { - private static $table = 'migrations'; public static function up() @@ -29,12 +28,10 @@ public static function up() $q->varchar('model_name', 100); $q->datetime("create_at", 255)->default(new Raw('CURRENT_TIMESTAMP')); })->execute(); - } public static function down() { Query::drop(static::$table)->ifExists()->execute(); } - } diff --git a/Database/Migrations/UsersMigrate.php b/Database/Migrations/UsersMigrate.php index fe352c5..7719eb8 100644 --- a/Database/Migrations/UsersMigrate.php +++ b/Database/Migrations/UsersMigrate.php @@ -18,7 +18,7 @@ class UsersMigrate { - private static $table = 'users'; + protected static $table = 'users'; public static function up() { diff --git a/Database/Seeds/DefaultSeed.php b/Database/Seeds/DefaultSeed.php index 6112156..d5c65b2 100644 --- a/Database/Seeds/DefaultSeed.php +++ b/Database/Seeds/DefaultSeed.php @@ -17,41 +17,26 @@ class DefaultSeed { - private static $table; + public static $table; /** * Run the database seeds. * * @return void */ - public static function run($model, $table = false) + public static function run() { - if ($model) { - if ($table) - self::$table = $table; - else - self::$table = $model; - - self::{$model}(); - } else - return false; + self::Users(); } - /** - * Run the database Users seed. - * - * @return void - */ - protected static function users() + public static function Users($table = 'users') { - Seed::insert(static::$table, [ + Seed::insert('users', [ 'username' => 'admin', 'password' => md5('admin'), 'salt' => 'ht4h4', - 'email' => 'admin@badget.com', + 'email' => 'admin@admin.local', 'active' => 1, ])->execute(); - } - } diff --git a/System/App/Controllers/MigrationsController.php b/System/App/Controllers/MigrationsController.php new file mode 100644 index 0000000..02e64b3 --- /dev/null +++ b/System/App/Controllers/MigrationsController.php @@ -0,0 +1,123 @@ +from('migrations')->where('model_name',$model)->fetch(); + + if($check->count == 0) + DB::insert('migrations', ['model_name'=> ucfirst($modelName).'Migrate'])->execute(); + } + else{ + $models = ['UsersMigrate','Migrations']; + $path = ROOT. 'Database/Migrations/'; + $files = array_diff(scandir($path), array('.', '..')); + + foreach ($files as $row) { + $row = preg_replace('/.php/', '', $row); + + if($row != 'Migrations') { + DB::insert('Migrations', ['model_name'=> $row])->execute(); + } + + $namespace = "Database\\Migrations\\"; + $migrate = $namespace . ucfirst($row); + $migrate::up(); + $list[] = $row; + } + } + print_r($list); + return '- Migrations created!'; + } + + public function down($modelName = false) + { + if($modelName){ + if($modelName == 'migrations') + return 'This is GLOBAL SYSTEM Name!'; + + if(! is_file(ROOT.'Database'.DS.'Migrations'.DS.$modelName.'Migrate'.EXT)) + return "File or namespace in: ".ROOT."Database".DS."Migrations".DS.ucfirst($modelName)."Migrate".EXT." not exists!"; + + $namespace = "Database\\Migrations\\"; + $migrate = $namespace . ucfirst($modelName).'Migrate'; + $migrate::down(); + $list[] = $modelName; + } + else{ + $path = ROOT. 'Database/Migrations/'; + $files = array_diff(scandir($path), array('.', '..')); + + foreach ($files as $row) { + $row = preg_replace('/.php/', '', $row); + + $namespace = "Database\\Migrations\\"; + $migrate = $namespace . ucfirst($row); + $migrate::down(); + $list[] = $row; + } + } + print_r($list); + return '- Migrations created!'; + } + + public function all() + { + + $pathMigrations = ROOT. 'Database/Migrations/'; + $filesMigrations = array_diff(scandir($pathMigrations), array('.', '..')); + + foreach ($filesMigrations as $row) { + $row = preg_replace('/.php/', '', $row); + $namespace = "Database\\Migrations\\"; + $migrate = $namespace . ucfirst($row); + $migrate::up(); + $list[] = $row; + } + + $pathSeeds = ROOT. 'Database/Seeds/'; + $filesSeeds = array_diff(scandir($pathSeeds), array('.', '..')); + + foreach ($filesSeeds as $row) { + $row = preg_replace('/.php/', '', $row); + $namespace = "Database\\Seeds\\"; + $seed = $namespace . ucfirst($row); + $seed::run(); + $listSeeds[] = $row; + } + + print_r($list); + print_r($listSeeds); + return '- Migrations created!'; + } +} diff --git a/System/App/Controllers/TerminalController.php b/System/App/Controllers/TerminalController.php deleted file mode 100644 index 9e42293..0000000 --- a/System/App/Controllers/TerminalController.php +++ /dev/null @@ -1,186 +0,0 @@ -setHeader("Cache-Control", "no-store, no-cache, must-revalidate, max-age=0, post-check=0, pre-check=0"); - $res->setHeader("Pragma", "no-cache"); // HTTP/1.0 - } - - public function index() - { - return View::make('terminal.index') - ->withUser(Session::get('terminal.user')); - } - - public function manifest() - { - $this->nocache(); - - $m = new Manifest; - return [ - 'commands' => $m->getCommands(), - 'aliases' => $m->getAliases(), - 'command_arguments' => $m->getArguments(), - 'services' => $m->getServices(), - 'messages' => $m->getMessages() - ]; - } - - public function resource(Req $req) - { - $this->nocache(); - - try { - $command = $req->param('command'); - $resource = $req->param('resource'); - $target = Manifest::resource($command, $resource); - - // We have /manifest for this purpose - if (String::endsWith($target, 'manifest.json')) { - return; - } - - // Dont have permission for this command - if (!self::hasPermission($command)) { - return; - } - - $ext = pathinfo($target, PATHINFO_EXTENSION); - if(in_array($ext, ['php', 'js'])) { - return new Template($target); - } else { - return file_get_contents($target); - } - } catch (Exception $e) { - $pos = json_encode($e->getFile() . ':' . $e->getLine()); - $msg = json_encode($e->getMessage()); - return <<param('action'); - $env = json_decode($req->param('env')); - $class = '\\Silver\\Engine\\Terminal\\Commands\\' . $program; - - if(!self::hasPermission($program)) { - throw new Exception("Permission denied."); - } - - if(!self::commandExists($program, $command)) { - throw new Exception("Invalid command: $program::$command"); - } - - switch($action) { - case 'execute': - $opts = json_decode($req->param('opts')); - $args = json_decode($req->param('args')); - - $object = new $class($env); - $object->$command($opts, $args); // Ignore return value - return $object->_getActions(); - case 'continue': - $input_values = json_decode($req->param('input_values')); - $object = Command::_process($input_values, $env); - return $object->_getActions(); - default: - return [[ - 'type' => 'output', - 'content' => (string) Html::make('span', [ - 'style' => 'color: red' - ], "Terminal error: unknown action '$action'"), - 'html' => true, - ], [ - 'type' => 'output', - 'content' => "\n", - 'html' => false - ]]; - } - } catch(\Exception $e) { - return [[ - 'type' => 'output', - 'content' => (string) Html::make('span', [ - 'style' => 'color: red' - ], 'Exception: ' . $e->getMessage()), - 'html' => true, - ], [ - 'type' => 'output', - 'content' => "\n", - 'html' => false - ]]; - } - } - - public function service(Req $req, $program, $service) - { - $args = json_decode($req->param('args')); - $class = '\\Silver\\Engine\\Terminal\\Commands\\' . $program; - - if(!self::hasPermission($program)) { - throw new Exception("Permission denied."); - } - - if(!self::serviceExists($program, $service)) { - throw new Exception("Invalid service: $program::$service"); - } - - - $result = call_user_func_array([$class, $service], $args); - return json_encode($result); - } - - private function hasPermission($program) - { - $manifest = Manifest::get($program); - - if (isset($manifest->auth) and $manifest->auth) { - return !!Session::get('terminal.user', false); - } - - return true; - } - - private function commandExists($program, $command) - { - $manifest = Manifest::get($program); - return in_array($command, $manifest->provide); - } - - private function serviceExists($program, $service) - { - $manifest = Manifest::get($program); - return in_array($service, $manifest->services); - } -} diff --git a/System/App/Routes.php b/System/App/Routes.php index 8fa58b8..82c07ad 100644 --- a/System/App/Routes.php +++ b/System/App/Routes.php @@ -15,13 +15,11 @@ use Silver\Core\Route; use Silver\Core\Env; - if (Env::name() == 'local') { - - Route::get('/terminal', 'Terminal@index', 'terminal'); - Route::get('/terminal/manifest', 'Terminal@manifest', 'terminal.manifest'); - Route::get('/terminal/resource', 'Terminal@resource', 'terminal.resource'); - Route::post('/terminal/execute/{program}/{command}', 'Terminal@execute', 'terminal.execute'); - Route::post('/terminal/service/{program}/{service}', 'Terminal@service', 'terminal.service'); - Route::post('/terminal/logout', 'Terminal@logout', 'terminal.logout'); -} \ No newline at end of file + //create migration + Route::get('/migrate/{modelName?}', 'Migrations@up', 'migrate'); + //drop migrations + Route::get('/migrate-down/{modelName?}', 'Migrations@down', 'migrate'); + //run migrations and seeds + Route::get('/migrate-seed', 'Migrations@all', 'migrate'); +} diff --git a/System/Core/Env.php b/System/Core/Env.php index 4d7205b..f1e7794 100644 --- a/System/Core/Env.php +++ b/System/Core/Env.php @@ -29,7 +29,7 @@ public static function construct() $envfile = ROOT . $envtype . '.env' . EXT; if (!is_file($envfile)) - exit("Env '$type' not found!"); + exit("Env {$envtype} not found!"); $config = self::readConfiguration(); $env = include($envfile); diff --git a/System/Core/ErrorHandlers.php b/System/Core/ErrorHandlers.php index a21c309..93cf3dd 100644 --- a/System/Core/ErrorHandlers.php +++ b/System/Core/ErrorHandlers.php @@ -217,4 +217,4 @@ private static function resetCWD() set_exception_handler("Silver\\Core\\ErrorHandler::handle_ex"); register_shutdown_function("Silver\\Core\\ErrorHandler::handle_fatal"); ErrorHandler::setFilter(E_ALL); -error_reporting(~E_ALL); \ No newline at end of file +error_reporting(~E_ALL); diff --git a/System/Core/Route.php b/System/Core/Route.php index c25cfee..105fb31 100644 --- a/System/Core/Route.php +++ b/System/Core/Route.php @@ -266,7 +266,7 @@ public static function group($args, $fn) $args['prefix'] = ''; $old_prefix = self::$_prefix; - self::$_prefix = self::$_prefix . $args['prefix']; + self::$_prefix = self::$_prefix . '/'.$args['prefix']; // Execute $fn(); @@ -375,8 +375,9 @@ public static function resource($route, $action, $name = null, $middleware ='pub self::register( 'get', $route, - $action . '@index', + $action . '@get', $name, + $middleware, 'resources' ); @@ -385,6 +386,7 @@ public static function resource($route, $action, $name = null, $middleware ='pub $route, $action . '@post', $name, + $middleware, 'resources' ); @@ -394,6 +396,7 @@ public static function resource($route, $action, $name = null, $middleware ='pub $route . '/{id}', $action . '@' . $method, $name, + $middleware, 'resources' ); } @@ -416,6 +419,7 @@ public static function any($route, $action, $name = null, $middleware ='public') */ public static function getRoute($name) { + // ndd($name); if (isset(self::$_route_index[ $name ])) return self::$_route_index[ $name ]; throw new \Exception("Route $name not found."); diff --git a/System/Engine/CLI/index.php b/System/Engine/CLI/index.php index 6987cfb..17b71e7 100644 --- a/System/Engine/CLI/index.php +++ b/System/Engine/CLI/index.php @@ -14,7 +14,6 @@ use Silver\Engine\Ghost\Template; - class CLI { private $cmd = []; @@ -44,7 +43,7 @@ private function run() break; case "migrate": - return $this->make(); + return $this->migrate(); break; @@ -54,6 +53,31 @@ private function run() } } + private function migrate(){ + //todo: need to redone this is not oK! + if (empty($this->args[2])) { + $path = ROOT. 'Database/Migrations/';; + $files = array_diff(scandir($path), array('.', '..')); + + foreach ($files as $key) { + $key = preg_replace('/.php/', '', $key); + // exit($path.$key.'.php'); + include_once($path.$key.'.php'); + $namespace = "\\Database\\Migrations\\".$key; + // exit($namespace); + $namespace::up(); + return 'test1'; + } + } + else{ + + } + } + + private function seed(){ + + } + private function make() { @@ -96,8 +120,10 @@ private function generate($type, $name, $force = false) case 'view': $name = strtolower($name); $name = str_replace('.', '/', $name); + // exit(ROOT); $template = ROOT . 'App/Templates/View.ghost.tpl'; - $destination = ROOT . 'World/' . $name . '.ghost.tpl'; + $destination = ROOT . 'App/Views/' . $name . '.ghost.tpl'; + // exit($destination); break; case 'event': $this->createDirIfNorExists('Events', ROOT . 'App/'); @@ -130,29 +156,31 @@ private function generate($type, $name, $force = false) 'controller', 'model', 'view', + 'helper', + 'facade', ]); break; } - if (!file_exists($template)) { + if (!file_exists($template)) $this->error('Template is missing'); - } + + if($type == 'view' || $type == 'v') + return $this->generateView('y', $template, $destination, $type, $name); if (file_exists($destination) and $force === false) - $this->error('File exists!'); + return $this->error('File exists!'); else - $this->generateFile('y', $template, $destination, $type, $name); - + return $this->generateFile('y', $template, $destination, $type, $name); } private function generateFile($yes, $template, $destination, $type, $name) { - // RenderInterface template - $ghost = new Template($template); $ghost->set('type', $type); $ghost->set('name', $name); + file_put_contents($destination, $ghost->render()); if ($type == 'controller') { @@ -162,6 +190,26 @@ private function generateFile($yes, $template, $destination, $type, $name) $this->success("{$type} {$name} successfully created. ({$destination})"); } + private function generateView($yes, $template, $destination, $type, $name) + { + // RenderInterface template + $ghost = new Template($template); + $ghost->set('type', $type); + $ghost->set('name', $name); + + // var_dump($template,$destination); + // die(); + $temp = "{{ extends('layouts.master') }} + +#set[content] + Welcome to {{ $name }} page +#end"; + + file_put_contents($destination, $temp); + + $this->success("{$type} {$name} successfully created. ({$destination})"); + } + private function fix_routes($name, $add = true) { @@ -182,7 +230,7 @@ private function fix_routes($name, $add = true) if ($add === true) { fwrite($fh, "\n"); fwrite($fh, "// Route for {$name} controller.\n"); - fwrite($fh, "Route::get('/{$name}', '{$name}@get', 'unguard');\n"); + fwrite($fh, "Route::get('/".lcfirst($name)."', '{$name}@get', '".lcfirst($name)."', 'public');\n"); fclose($fh); $this->success('Route created!'); } @@ -216,5 +264,3 @@ private function createDirIfNorExists($name, $path) } } - - diff --git a/System/Engine/Ghost/Template.php b/System/Engine/Ghost/Template.php index 16656cd..8ef052f 100644 --- a/System/Engine/Ghost/Template.php +++ b/System/Engine/Ghost/Template.php @@ -239,7 +239,12 @@ protected function parseMaster($body) { $block_content .= $line . "\n"; } + $fullpath = ROOT."App/Views/{$master}.ghost".EXT; + + if(! is_file($fullpath)) + $fullpath = ROOT."App/Views/{$master}.ghost.tpl"; + $ghost = new \Silver\Engine\Ghost\Template($fullpath); if($blocks){ @@ -528,6 +533,10 @@ protected function includeFile($alias) $alias = str_replace('.', '/', $alias); $loadPath = ROOT."App/Views/{$alias}.ghost".EXT; + + if(! is_file($loadPath)) + $loadPath = ROOT."App/Views/{$alias}.ghost.tpl"; + if (file_exists($loadPath)) { $ghost = new self($loadPath); return $ghost->render($alias); @@ -541,6 +550,10 @@ protected function includeComponent($alias) $alias = str_replace('.', '/', $alias); $loadPath = ROOT."App/Views/components/{$alias}.ghost".EXT; + + if(! is_file($loadPath)) + $loadPath = ROOT."App/Views/components/{$alias}.ghost.tpl"; + if (file_exists($loadPath)) { $ghost = new self($loadPath); diff --git a/local.env.php b/local.env.php index 79a3941..7741f9f 100644 --- a/local.env.php +++ b/local.env.php @@ -22,9 +22,9 @@ 'service' => true, 'driver' => 'mysql', 'hostname' => 'localhost', - 'username' => 'silver', - 'password' => 'secret', - 'basename' => 'youtdatabase', + 'username' => 'root', + 'password' => '', + 'basename' => 'test1', 'limit_request' => 25, ] ], @@ -35,4 +35,4 @@ 'name' => 'Your Name', ], -]; \ No newline at end of file +]; diff --git a/silver b/silver index 14686ba..8c77ed9 100644 --- a/silver +++ b/silver @@ -24,7 +24,22 @@ $CWD = str_replace('/System/Engine/CLI', '', $CWD); //ltrim($CWD); define('ROOT', $CWD . '/'); -//die(ROOT); + +/** + * @param $data + * @param bool $param + */ +if (!function_exists('dd')) { + function dd($data, $param = false) + { + if ($param) + var_dump('
', $data, '
');
+        else
+            var_dump($data);
+
+        exit();
+    }
+}
 
 /**
  * Defining php extension.