From 200763725e9e538ab32d8b7c11549b24846eab46 Mon Sep 17 00:00:00 2001 From: Kavalar Date: Mon, 29 Jul 2024 15:57:20 +0300 Subject: [PATCH] App] --- .../AnswerMigration.php | 2 +- {migrations => _migrations}/PostMigration.php | 2 +- .../QuestionMigration.php | 2 +- {migrations => _migrations}/UserMigration.php | 2 +- {migrations => _migrations}/stubs/blank.stub | 0 {migrations => _migrations}/stubs/create.stub | 0 {migrations => _migrations}/stubs/update.stub | 0 bootstrap.php | 3 + bootstrap/db.php | 17 +- composer.json | 4 +- composer.lock | 202 +++++++++++++++++- console.php | 6 + index.php | 40 +--- kernel/App.php | 48 +++++ kernel/CgRouteCollector.php | 44 ++++ kernel/Database.php | 45 ++++ kernel/Header.php | 28 +++ kernel/ResponseType.php | 19 ++ kernel/console/CgMigrationCreator.php | 27 +++ kernel/console/ConsoleApp.php | 51 +++++ kernel/console/ConsoleController.php | 28 +++ kernel/console/Out.php | 78 +++++++ kernel/console/controllers/MainController.php | 15 ++ .../controllers/MigrationController.php | 95 ++++++++ kernel/console/migrations/stubs/blank.stub | 28 +++ kernel/console/migrations/stubs/create.stub | 32 +++ .../migrations/stubs/migration.create.stub | 27 +++ .../console/migrations/stubs/migration.stub | 24 +++ .../migrations/stubs/migration.update.stub | 28 +++ kernel/console/migrations/stubs/update.stub | 33 +++ kernel/console/routs/cli.php | 14 ++ m.php | 8 +- migrations/2024_07_28_171433_some.php | 27 +++ rout.php | 29 +++ 34 files changed, 936 insertions(+), 72 deletions(-) rename {migrations => _migrations}/AnswerMigration.php (97%) rename {migrations => _migrations}/PostMigration.php (96%) rename {migrations => _migrations}/QuestionMigration.php (97%) rename {migrations => _migrations}/UserMigration.php (97%) rename {migrations => _migrations}/stubs/blank.stub (100%) rename {migrations => _migrations}/stubs/create.stub (100%) rename {migrations => _migrations}/stubs/update.stub (100%) create mode 100644 console.php create mode 100644 kernel/App.php create mode 100755 kernel/CgRouteCollector.php create mode 100755 kernel/Database.php create mode 100755 kernel/Header.php create mode 100755 kernel/ResponseType.php create mode 100644 kernel/console/CgMigrationCreator.php create mode 100755 kernel/console/ConsoleApp.php create mode 100755 kernel/console/ConsoleController.php create mode 100755 kernel/console/Out.php create mode 100644 kernel/console/controllers/MainController.php create mode 100644 kernel/console/controllers/MigrationController.php create mode 100755 kernel/console/migrations/stubs/blank.stub create mode 100755 kernel/console/migrations/stubs/create.stub create mode 100755 kernel/console/migrations/stubs/migration.create.stub create mode 100755 kernel/console/migrations/stubs/migration.stub create mode 100755 kernel/console/migrations/stubs/migration.update.stub create mode 100755 kernel/console/migrations/stubs/update.stub create mode 100644 kernel/console/routs/cli.php create mode 100644 migrations/2024_07_28_171433_some.php create mode 100644 rout.php diff --git a/migrations/AnswerMigration.php b/_migrations/AnswerMigration.php similarity index 97% rename from migrations/AnswerMigration.php rename to _migrations/AnswerMigration.php index 887fa94..8035762 100644 --- a/migrations/AnswerMigration.php +++ b/_migrations/AnswerMigration.php @@ -1,6 +1,6 @@ load(); include_once __DIR__ . "/bootstrap/db.php"; const ROOT_DIR = __DIR__; +const KERNEL_DIR = __DIR__ . "/kernel"; +const CONSOLE_DIR = __DIR__ . "/kernel/console"; + diff --git a/bootstrap/db.php b/bootstrap/db.php index dda61db..2febbe3 100644 --- a/bootstrap/db.php +++ b/bootstrap/db.php @@ -1,18 +1,3 @@ addConnection([ - 'driver' => $_ENV['DB_DRIVER'], - 'host' => $_ENV['DB_HOST'], - 'database' => $_ENV['DB_NAME'], - 'username' => $_ENV['DB_USER'], - 'password' => $_ENV['DB_PASSWORD'], - 'charset' => 'utf8', - 'collation' => 'utf8_unicode_ci', - 'prefix' => '', -]); -// Setup the Eloquent ORM… -$capsule->setAsGlobal(); -$capsule->bootEloquent(); \ No newline at end of file +\kernel\App::$db = new \kernel\Database(); \ No newline at end of file diff --git a/composer.json b/composer.json index d43b17a..966ecb9 100644 --- a/composer.json +++ b/composer.json @@ -4,13 +4,15 @@ "type": "project", "require": { "illuminate/database": "^11.14", + "illuminate/filesystem": "^11.18.1", "craft-group/phroute": "^2.1", "vlucas/phpdotenv": "^5.6", "twig/twig": "^3.0", "twbs/bootstrap": "5.0.2", "itguild/tables": "^0.1.3", "madesimple/php-form-validator": "^2.9", - "itguild/forms": "^0.1.1" + "itguild/forms": "^0.1.1", + "samejack/php-argv": "dev-master" }, "autoload": { "psr-4": { diff --git a/composer.lock b/composer.lock index aa4c290..b463373 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "6f3aaa23fb01733bc2ac85dfaaf0dec3", + "content-hash": "05960b2d73dd4ee5a824e5a9416486b2", "packages": [ { "name": "brick/math", @@ -603,6 +603,73 @@ }, "time": "2024-07-26T06:18:25+00:00" }, + { + "name": "illuminate/filesystem", + "version": "v11.18.1", + "source": { + "type": "git", + "url": "https://github.com/illuminate/filesystem.git", + "reference": "43b8d2af61dd6ca882e192fa4ca803d2294d4507" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/illuminate/filesystem/zipball/43b8d2af61dd6ca882e192fa4ca803d2294d4507", + "reference": "43b8d2af61dd6ca882e192fa4ca803d2294d4507", + "shasum": "" + }, + "require": { + "illuminate/collections": "^11.0", + "illuminate/contracts": "^11.0", + "illuminate/macroable": "^11.0", + "illuminate/support": "^11.0", + "php": "^8.2", + "symfony/finder": "^7.0" + }, + "suggest": { + "ext-fileinfo": "Required to use the Filesystem class.", + "ext-ftp": "Required to use the Flysystem FTP driver.", + "ext-hash": "Required to use the Filesystem class.", + "illuminate/http": "Required for handling uploaded files (^7.0).", + "league/flysystem": "Required to use the Flysystem local driver (^3.0.16).", + "league/flysystem-aws-s3-v3": "Required to use the Flysystem S3 driver (^3.0).", + "league/flysystem-ftp": "Required to use the Flysystem FTP driver (^3.0).", + "league/flysystem-sftp-v3": "Required to use the Flysystem SFTP driver (^3.0).", + "psr/http-message": "Required to allow Storage::put to accept a StreamInterface (^1.0).", + "symfony/filesystem": "Required to enable support for relative symbolic links (^7.0).", + "symfony/mime": "Required to enable support for guessing extensions (^7.0)." + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "11.x-dev" + } + }, + "autoload": { + "files": [ + "functions.php" + ], + "psr-4": { + "Illuminate\\Filesystem\\": "" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Taylor Otwell", + "email": "taylor@laravel.com" + } + ], + "description": "The Illuminate Filesystem package.", + "homepage": "https://laravel.com", + "support": { + "issues": "https://github.com/laravel/framework/issues", + "source": "https://github.com/laravel/framework" + }, + "time": "2024-06-28T20:10:30+00:00" + }, { "name": "illuminate/macroable", "version": "v11.18.1", @@ -1274,6 +1341,59 @@ }, "time": "2020-08-27T05:07:01+00:00" }, + { + "name": "samejack/php-argv", + "version": "dev-master", + "source": { + "type": "git", + "url": "https://github.com/samejack/php-argv.git", + "reference": "0551f0f7fd38a5e9dcb316d5cc9028cdd62b21ac" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/samejack/php-argv/zipball/0551f0f7fd38a5e9dcb316d5cc9028cdd62b21ac", + "reference": "0551f0f7fd38a5e9dcb316d5cc9028cdd62b21ac", + "shasum": "" + }, + "require": { + "php": ">=5.3.0" + }, + "require-dev": { + "php-coveralls/php-coveralls": "*@stable", + "phpunit/phpunit": "~4.8|~5.7", + "squizlabs/php_codesniffer": "*@stable" + }, + "default-branch": true, + "type": "library", + "autoload": { + "psr-4": { + "samejack\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "Apache-2.0" + ], + "authors": [ + { + "name": "SJ", + "email": "sj@toright.com", + "homepage": "http://blog.toright.com", + "role": "Developer" + } + ], + "description": "PHP CLI arguments parse.", + "homepage": "https://github.com/samejack/php-argv", + "keywords": [ + "cli", + "php" + ], + "support": { + "issues": "https://github.com/samejack/php-argv/issues", + "source": "https://github.com/samejack/php-argv/tree/1.0.3" + }, + "time": "2020-07-15T12:28:03+00:00" + }, { "name": "symfony/clock", "version": "v7.1.1", @@ -1415,6 +1535,70 @@ ], "time": "2024-04-18T09:32:20+00:00" }, + { + "name": "symfony/finder", + "version": "v7.1.3", + "source": { + "type": "git", + "url": "https://github.com/symfony/finder.git", + "reference": "717c6329886f32dc65e27461f80f2a465412fdca" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/finder/zipball/717c6329886f32dc65e27461f80f2a465412fdca", + "reference": "717c6329886f32dc65e27461f80f2a465412fdca", + "shasum": "" + }, + "require": { + "php": ">=8.2" + }, + "require-dev": { + "symfony/filesystem": "^6.4|^7.0" + }, + "type": "library", + "autoload": { + "psr-4": { + "Symfony\\Component\\Finder\\": "" + }, + "exclude-from-classmap": [ + "/Tests/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Finds files and directories via an intuitive fluent interface", + "homepage": "https://symfony.com", + "support": { + "source": "https://github.com/symfony/finder/tree/v7.1.3" + }, + "funding": [ + { + "url": "https://symfony.com/sponsor", + "type": "custom" + }, + { + "url": "https://github.com/fabpot", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", + "type": "tidelift" + } + ], + "time": "2024-07-24T07:08:44+00:00" + }, { "name": "symfony/polyfill-ctype", "version": "v1.30.0", @@ -1732,16 +1916,16 @@ }, { "name": "symfony/translation", - "version": "v7.1.1", + "version": "v7.1.3", "source": { "type": "git", "url": "https://github.com/symfony/translation.git", - "reference": "cf5ae136e124fc7681b34ce9fac9d5b9ae8ceee3" + "reference": "8d5e50c813ba2859a6dfc99a0765c550507934a1" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/translation/zipball/cf5ae136e124fc7681b34ce9fac9d5b9ae8ceee3", - "reference": "cf5ae136e124fc7681b34ce9fac9d5b9ae8ceee3", + "url": "https://api.github.com/repos/symfony/translation/zipball/8d5e50c813ba2859a6dfc99a0765c550507934a1", + "reference": "8d5e50c813ba2859a6dfc99a0765c550507934a1", "shasum": "" }, "require": { @@ -1806,7 +1990,7 @@ "description": "Provides tools to internationalize your application", "homepage": "https://symfony.com", "support": { - "source": "https://github.com/symfony/translation/tree/v7.1.1" + "source": "https://github.com/symfony/translation/tree/v7.1.3" }, "funding": [ { @@ -1822,7 +2006,7 @@ "type": "tidelift" } ], - "time": "2024-05-31T14:57:53+00:00" + "time": "2024-07-26T12:41:01+00:00" }, { "name": "symfony/translation-contracts", @@ -2193,7 +2377,9 @@ "packages-dev": [], "aliases": [], "minimum-stability": "stable", - "stability-flags": [], + "stability-flags": { + "samejack/php-argv": 20 + }, "prefer-stable": false, "prefer-lowest": false, "platform": [], diff --git a/console.php b/console.php new file mode 100644 index 0000000..6410f17 --- /dev/null +++ b/console.php @@ -0,0 +1,6 @@ +setArgv($argv)->load()->run(); \ No newline at end of file diff --git a/index.php b/index.php index 52a81e0..0f70325 100644 --- a/index.php +++ b/index.php @@ -6,42 +6,4 @@ ini_set("display_errors", true); require_once "vendor/autoload.php"; require 'bootstrap.php'; -use app\controllers\MainController; -use app\controllers\UserController; -use app\controllers\QuestionController; -use app\controllers\AnswerController; -use app\controllers\PostController; -use app\helpers\Debug; -use Phroute\Phroute\RouteCollector; - -$router = new RouteCollector(); -$router->get('/', [MainController::class, 'actionIndex']); -$router->get('/example', [MainController::class, 'actionExample']); -$router->group(["prefix" => "admin"], function (RouteCollector $router){ - $router->group(["prefix" => "user"], function (RouteCollector $router){ - $router->get('/', [\app\controllers\UserController::class, 'actionIndex']); - $router->get('/create', [\app\controllers\UserController::class, 'actionCreate']); - $router->post("/", [\app\controllers\UserController::class, 'actionAdd']); - $router->get('/{id}', [\app\controllers\UserController::class, 'actionView']); - $router->any('/update/{id}', [\app\controllers\UserController::class, 'actionUpdate']); - $router->any("/edit/{id}", [\app\controllers\UserController::class, 'actionEdit']); - $router->get('/delete/{id}', [\app\controllers\UserController::class, 'actionDelete']); - }); - $router->group(["prefix" => "post"], function (RouteCollector $router){ - $router->get('/', [\app\controllers\PostController::class, 'actionIndex']); - $router->get('/create', [\app\controllers\PostController::class, 'actionCreate']); - $router->post("/", [\app\controllers\PostController::class, 'actionAdd']); - $router->get('/{id}', [\app\controllers\PostController::class, 'actionView']); - $router->any('/update/{id}', [\app\controllers\PostController::class, 'actionUpdate']); - $router->any("/edit/{id}", [\app\controllers\PostController::class, 'actionEdit']); - $router->get('/delete/{id}', [\app\controllers\PostController::class, 'actionDelete']); - }); -}); - -$dispatcher = new Phroute\Phroute\Dispatcher($router->getData()); - -$response = $dispatcher->dispatch($_SERVER['REQUEST_METHOD'], parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)); - - -// Print out the value returned from the dispatched function -echo $response; \ No newline at end of file +\kernel\App::create()->load()->run(); \ No newline at end of file diff --git a/kernel/App.php b/kernel/App.php new file mode 100644 index 0000000..64f6d39 --- /dev/null +++ b/kernel/App.php @@ -0,0 +1,48 @@ +getData()); + $response = $dispatcher->dispatch($_SERVER['REQUEST_METHOD'], parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)); + header('Content-Type: ' . App::$responseType); + App::$header->set(); + echo $response; + } + + public function load(): static + { + App::$collector = new CgRouteCollector(); + $this->setRouting(); + + return $this; + } + + public function setRouting(): void + { + include ROOT_DIR . "/rout.php"; + } + + public static function create(): App + { + return new self(); + } + +} \ No newline at end of file diff --git a/kernel/CgRouteCollector.php b/kernel/CgRouteCollector.php new file mode 100755 index 0000000..4cfa124 --- /dev/null +++ b/kernel/CgRouteCollector.php @@ -0,0 +1,44 @@ +addRoute(Route::GET, $route, array_merge($handler, ['actionIndex']), $filters); + $this->addRoute(Route::GET, $route . '/{id}', array_merge($handler, ['actionView']), $filters); + $this->addRoute(Route::POST, $route, array_merge($handler, ['actionStore']), $filters); + $this->addRoute(Route::DELETE, $route, array_merge($handler, ['actionDelete']), $filters); + return $this->addRoute(Route::ANY, $route . '/update/{id}', array_merge($handler, ['actionEdit']), $filters); + } + + public function gridView($route, $handler, array $filters = []): CgRouteCollector + { + $this->addRoute(Route::GET, $route, array_merge($handler, ['actionIndex']), $filters); + $this->addRoute(Route::GET, $route . '/{id}', array_merge($handler, ['actionView']), $filters); + $this->addRoute(Route::ANY, $route . '/create', array_merge($handler, ['actionStore']), $filters); + $this->addRoute(Route::POST, $route . '/delete', array_merge($handler, ['actionDelete']), $filters); + return $this->addRoute(Route::ANY, $route . '/update/{id}', array_merge($handler, ['actionEdit']), $filters); + } + + public function authorization($route, $handler, array $filters = []) + { + //TODO + } + + public function exclude($action) + { + //TODO + } + + public function console($route, $handler, array $filters = []): void + { + $this->addRoute(Route::GET, $route, $handler, $filters); + } +} \ No newline at end of file diff --git a/kernel/Database.php b/kernel/Database.php new file mode 100755 index 0000000..c7c0f16 --- /dev/null +++ b/kernel/Database.php @@ -0,0 +1,45 @@ +capsule = new Capsule; + $this->capsule->addConnection([ + 'driver' => $_ENV['DB_DRIVER'], + 'host' => $_ENV['DB_HOST'], + 'database' => $_ENV['DB_NAME'], + 'username' => $_ENV['DB_USER'], + 'password' => $_ENV['DB_PASSWORD'], + 'charset' => $_ENV['DB_CHARSET'], + 'collation' => $_ENV['DB_COLLATION'], + 'prefix' => $_ENV['DB_PREFIX'], + ]); + // Setup the Eloquent ORM… + + $this->capsule->setAsGlobal(); + + $this->capsule->bootEloquent(); + + $this->schema = $this->capsule->schema(); + } +} \ No newline at end of file diff --git a/kernel/Header.php b/kernel/Header.php new file mode 100755 index 0000000..12afd12 --- /dev/null +++ b/kernel/Header.php @@ -0,0 +1,28 @@ +list[$key] = $value; + } + + public function set() + { + foreach ($this->list as $key=>$v){ + header($key . ": " . $v); + } + } + +} \ No newline at end of file diff --git a/kernel/ResponseType.php b/kernel/ResponseType.php new file mode 100755 index 0000000..9d2d744 --- /dev/null +++ b/kernel/ResponseType.php @@ -0,0 +1,19 @@ +getRout()){ + echo "Not found \n"; + exit(); + } + $dispatcher = new Dispatcher(App::$collector->getData()); + $response = $dispatcher->dispatch('GET', $rout); + echo $response; + } + + public function setArgv($argv): static + { + $this->argv = $argv; + return $this; + } + + public function setRouting(): void + { + include CONSOLE_DIR . "/routs/cli.php"; + } + + private function getRout() + { + if(isset($this->argv[1])){ + return $this->argv[1]; + } + return null; + } + + public static function create(): ConsoleApp + { + return new self(); + } + + +} \ No newline at end of file diff --git a/kernel/console/ConsoleController.php b/kernel/console/ConsoleController.php new file mode 100755 index 0000000..4d9bf73 --- /dev/null +++ b/kernel/console/ConsoleController.php @@ -0,0 +1,28 @@ +out = new Out(); + $argv = $_SERVER['argv']; + unset($argv[0]); + unset($argv[1]); + if(!empty($argv)){ + $argvParser = new ArgvParser(); + $tmp = implode(" ", $argv); + $this->argv = $argvParser->parseConfigs($tmp); + } + } + +} \ No newline at end of file diff --git a/kernel/console/Out.php b/kernel/console/Out.php new file mode 100755 index 0000000..cc08182 --- /dev/null +++ b/kernel/console/Out.php @@ -0,0 +1,78 @@ +foreground_colors['black'] = '0;30'; + $this->foreground_colors['dark_gray'] = '1;30'; + $this->foreground_colors['blue'] = '0;34'; + $this->foreground_colors['light_blue'] = '1;34'; + $this->foreground_colors['green'] = '0;32'; + $this->foreground_colors['light_green'] = '1;32'; + $this->foreground_colors['cyan'] = '0;36'; + $this->foreground_colors['light_cyan'] = '1;36'; + $this->foreground_colors['red'] = '0;31'; + $this->foreground_colors['light_red'] = '1;31'; + $this->foreground_colors['purple'] = '0;35'; + $this->foreground_colors['light_purple'] = '1;35'; + $this->foreground_colors['brown'] = '0;33'; + $this->foreground_colors['yellow'] = '1;33'; + $this->foreground_colors['light_gray'] = '0;37'; + $this->foreground_colors['white'] = '1;37'; + + $this->background_colors['black'] = '40'; + $this->background_colors['red'] = '41'; + $this->background_colors['green'] = '42'; + $this->background_colors['yellow'] = '43'; + $this->background_colors['blue'] = '44'; + $this->background_colors['magenta'] = '45'; + $this->background_colors['cyan'] = '46'; + $this->background_colors['light_gray'] = '47'; + } + + // Returns colored string + public function get($string, $foreground_color = null, $background_color = null) + { + $colored_string = ""; + + // Check if given foreground color found + if (isset($this->foreground_colors[$foreground_color])) { + $colored_string .= "\033[" . $this->foreground_colors[$foreground_color] . "m"; + } + // Check if given background color found + if (isset($this->background_colors[$background_color])) { + $colored_string .= "\033[" . $this->background_colors[$background_color] . "m"; + } + + // Add string and end coloring + $colored_string .= $string . "\033[0m"; + + return $colored_string; + } + + public function r($string, $foreground_color = null, $background_color = null) + { + echo $this->get($string, $foreground_color, $background_color) . "\n"; + } + + // Returns all foreground color names + public function getForegroundColors() + { + return array_keys($this->foreground_colors); + } + + // Returns all background color names + public function getBackgroundColors() + { + return array_keys($this->background_colors); + } +} \ No newline at end of file diff --git a/kernel/console/controllers/MainController.php b/kernel/console/controllers/MainController.php new file mode 100644 index 0000000..eecee1d --- /dev/null +++ b/kernel/console/controllers/MainController.php @@ -0,0 +1,15 @@ +out->r("Привет", "green"); + } + +} \ No newline at end of file diff --git a/kernel/console/controllers/MigrationController.php b/kernel/console/controllers/MigrationController.php new file mode 100644 index 0000000..b7f5db3 --- /dev/null +++ b/kernel/console/controllers/MigrationController.php @@ -0,0 +1,95 @@ +schema->create('migration', function (Blueprint $table) { + $table->bigIncrements('id'); + $table->string('migration', 255); + $table->integer('batch'); + }); + $this->out->r("Success", 'green'); + } + catch (\Exception $e){ + $this->out->r($e->getMessage(), 'red'); + } + } + + // create migrations + public function actionCreate(): void + { + try { + if (!isset($this->argv['name'])) { + throw new \Exception('Missing migration "--name" specified'); + } + $m = new CgMigrationCreator(new Filesystem(), CgMigrationCreator::getCustomStubPath()); + + $path = $this->argv['path'] ?? 'migrations'; + + $res = $m->create( + $this->argv['name'], + ROOT_DIR . '/' . $path, + $this->argv['table'] ?? null, + !isset($this->argv['update']) + ); + $this->out->r(basename($res) . " created", 'green'); + } catch (\Exception $e) { + $this->out->r('Message: ' .$e->getMessage(), 'red'); + } + } + + //execute migrations + public function actionRun(): void + { + try { + $dmr = new DatabaseMigrationRepository(App::$db->capsule->getDatabaseManager(), 'migration'); + + $m = new Migrator($dmr, App::$db->capsule->getDatabaseManager(), new Filesystem()); + //$migrationPaths = array_merge(App::$migrationsPaths, [ROOT_DIR . '/migrations']); + $migrationPaths = [ROOT_DIR . '/migrations']; + $res = $m->run($migrationPaths); + foreach ($res as $re){ + $this->out->r(basename($re), 'green'); + } + } + catch (\Exception $e){ + $this->out->r('Message: ' .$e->getMessage(), 'red'); + } + } + + public function actionRollback(): void + { + try { + $dmr = new DatabaseMigrationRepository(App::$db->capsule->getDatabaseManager(), 'migration'); + + $m = new Migrator($dmr, App::$db->capsule->getDatabaseManager(), new Filesystem()); + //$migrationPaths = array_merge(App::$migrationsPaths, [WORKSPACE_DIR . '/console/migrations']); + $migrationPaths = [ROOT_DIR . '/console/migrations']; + $res = $m->rollback($migrationPaths); + foreach ($res as $re){ + $this->out->r(basename($re), 'green'); + } + } + catch (\Exception $e){ + $this->out->r('Message: ' .$e->getMessage(), 'red'); + } + } +} \ No newline at end of file diff --git a/kernel/console/migrations/stubs/blank.stub b/kernel/console/migrations/stubs/blank.stub new file mode 100755 index 0000000..5e3b154 --- /dev/null +++ b/kernel/console/migrations/stubs/blank.stub @@ -0,0 +1,28 @@ +schema->create('DummyTable', function (Blueprint $table) { + $table->bigIncrements('id'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + App::$db->schema->dropIfExists('DummyTable'); + } +} diff --git a/kernel/console/migrations/stubs/migration.create.stub b/kernel/console/migrations/stubs/migration.create.stub new file mode 100755 index 0000000..168c622 --- /dev/null +++ b/kernel/console/migrations/stubs/migration.create.stub @@ -0,0 +1,27 @@ +id(); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('{{ table }}'); + } +}; diff --git a/kernel/console/migrations/stubs/migration.stub b/kernel/console/migrations/stubs/migration.stub new file mode 100755 index 0000000..88fa2f3 --- /dev/null +++ b/kernel/console/migrations/stubs/migration.stub @@ -0,0 +1,24 @@ +schema->table('DummyTable', function (Blueprint $table) { + // + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + App::$db->schema->table('DummyTable', function (Blueprint $table) { + // + }); + } +} diff --git a/kernel/console/routs/cli.php b/kernel/console/routs/cli.php new file mode 100644 index 0000000..dff01c6 --- /dev/null +++ b/kernel/console/routs/cli.php @@ -0,0 +1,14 @@ +console("hello", [\kernel\console\controllers\MainController::class, "indexAction"]); + +App::$collector->group(["prefix" => "migration"], callback: function (RouteCollector $router){ + App::$collector->console('run', [\kernel\console\controllers\MigrationController::class, 'actionRun']); + App::$collector->console('init', [\kernel\console\controllers\MigrationController::class, 'actionCreateMigrationTable']); + App::$collector->console('create', [\kernel\console\controllers\MigrationController::class, 'actionCreate']); + App::$collector->console('rollback', [\kernel\console\controllers\MigrationController::class, 'actionRollback']); +}); + diff --git a/m.php b/m.php index e3bca0e..ee82d48 100644 --- a/m.php +++ b/m.php @@ -1,9 +1,9 @@ id(); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('some'); + } +}; diff --git a/rout.php b/rout.php new file mode 100644 index 0000000..243ce53 --- /dev/null +++ b/rout.php @@ -0,0 +1,29 @@ +get('/', [MainController::class, 'actionIndex']); +App::$collector->get('/example', [MainController::class, 'actionExample']); +App::$collector->group(["prefix" => "admin"], function (RouteCollector $router){ + App::$collector->group(["prefix" => "user"], callback: function (RouteCollector $router){ + App::$collector->get('/', [\app\controllers\UserController::class, 'actionIndex']); + App::$collector->get('/create', [\app\controllers\UserController::class, 'actionCreate']); + App::$collector->post("/", [\app\controllers\UserController::class, 'actionAdd']); + App::$collector->get('/{id}', [\app\controllers\UserController::class, 'actionView']); + App::$collector->any('/update/{id}', [\app\controllers\UserController::class, 'actionUpdate']); + App::$collector->any("/edit/{id}", [\app\controllers\UserController::class, 'actionEdit']); + App::$collector->get('/delete/{id}', [\app\controllers\UserController::class, 'actionDelete']); + }); + App::$collector->group(["prefix" => "post"], function (RouteCollector $router){ + App::$collector->get('/', [\app\controllers\PostController::class, 'actionIndex']); + App::$collector->get('/create', [\app\controllers\PostController::class, 'actionCreate']); + App::$collector->post("/", [\app\controllers\PostController::class, 'actionAdd']); + App::$collector->get('/{id}', [\app\controllers\PostController::class, 'actionView']); + App::$collector->any('/update/{id}', [\app\controllers\PostController::class, 'actionUpdate']); + App::$collector->any("/edit/{id}", [\app\controllers\PostController::class, 'actionEdit']); + App::$collector->get('/delete/{id}', [\app\controllers\PostController::class, 'actionDelete']); + }); +}); \ No newline at end of file