App]
This commit is contained in:
27
kernel/console/CgMigrationCreator.php
Normal file
27
kernel/console/CgMigrationCreator.php
Normal file
@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace kernel\console;
|
||||
|
||||
|
||||
use Illuminate\Database\Migrations\MigrationCreator;
|
||||
|
||||
class CgMigrationCreator extends MigrationCreator
|
||||
{
|
||||
|
||||
/**
|
||||
* Get the path to the stubs.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function stubPath(): string
|
||||
{
|
||||
return ROOT_DIR . '/kernel/console/migrations/stubs';
|
||||
}
|
||||
|
||||
public static function getCustomStubPath(): string
|
||||
{
|
||||
return ROOT_DIR . '/kernel/console/migrations/stubs';
|
||||
}
|
||||
|
||||
}
|
51
kernel/console/ConsoleApp.php
Executable file
51
kernel/console/ConsoleApp.php
Executable file
@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace kernel\console;
|
||||
|
||||
|
||||
use app\helpers\Debug;
|
||||
use kernel\App;
|
||||
use Phroute\Phroute\Dispatcher;
|
||||
|
||||
class ConsoleApp extends App
|
||||
{
|
||||
public array $argv;
|
||||
|
||||
public function run(): void
|
||||
{
|
||||
if(!$rout = $this->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();
|
||||
}
|
||||
|
||||
|
||||
}
|
28
kernel/console/ConsoleController.php
Executable file
28
kernel/console/ConsoleController.php
Executable file
@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace kernel\console;
|
||||
|
||||
|
||||
use kernel\console\Out;
|
||||
use samejack\PHP\ArgvParser;
|
||||
|
||||
class ConsoleController
|
||||
{
|
||||
public Out $out;
|
||||
protected array $argv;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
78
kernel/console/Out.php
Executable file
78
kernel/console/Out.php
Executable file
@ -0,0 +1,78 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace kernel\console;
|
||||
|
||||
|
||||
class Out
|
||||
{
|
||||
private $foreground_colors = array();
|
||||
private $background_colors = array();
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
// Set up shell colors
|
||||
$this->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);
|
||||
}
|
||||
}
|
15
kernel/console/controllers/MainController.php
Normal file
15
kernel/console/controllers/MainController.php
Normal file
@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
namespace kernel\console\controllers;
|
||||
|
||||
use kernel\console\ConsoleController;
|
||||
|
||||
class MainController extends ConsoleController
|
||||
{
|
||||
|
||||
public function indexAction(): void
|
||||
{
|
||||
$this->out->r("Привет", "green");
|
||||
}
|
||||
|
||||
}
|
95
kernel/console/controllers/MigrationController.php
Normal file
95
kernel/console/controllers/MigrationController.php
Normal file
@ -0,0 +1,95 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace kernel\console\controllers;
|
||||
|
||||
|
||||
use Illuminate\Filesystem\Filesystem;
|
||||
use kernel\App;
|
||||
use kernel\console\CgMigrationCreator;
|
||||
use kernel\console\ConsoleController;
|
||||
use Illuminate\Database\Migrations\DatabaseMigrationRepository;
|
||||
use Illuminate\Database\Migrations\MigrationCreator;
|
||||
use Illuminate\Database\Migrations\Migrator;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Database\Schema\Builder;
|
||||
|
||||
|
||||
class MigrationController extends ConsoleController
|
||||
{
|
||||
//create migrations table
|
||||
public function actionCreateMigrationTable(): void
|
||||
{
|
||||
try {
|
||||
App::$db->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');
|
||||
}
|
||||
}
|
||||
}
|
28
kernel/console/migrations/stubs/blank.stub
Executable file
28
kernel/console/migrations/stubs/blank.stub
Executable file
@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class DummyClass extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
//
|
||||
}
|
||||
}
|
32
kernel/console/migrations/stubs/create.stub
Executable file
32
kernel/console/migrations/stubs/create.stub
Executable file
@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
use core\App;
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class DummyClass extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
App::$db->schema->create('DummyTable', function (Blueprint $table) {
|
||||
$table->bigIncrements('id');
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
App::$db->schema->dropIfExists('DummyTable');
|
||||
}
|
||||
}
|
27
kernel/console/migrations/stubs/migration.create.stub
Executable file
27
kernel/console/migrations/stubs/migration.create.stub
Executable file
@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::create('{{ table }}', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('{{ table }}');
|
||||
}
|
||||
};
|
24
kernel/console/migrations/stubs/migration.stub
Executable file
24
kernel/console/migrations/stubs/migration.stub
Executable file
@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
//
|
||||
}
|
||||
};
|
28
kernel/console/migrations/stubs/migration.update.stub
Executable file
28
kernel/console/migrations/stubs/migration.update.stub
Executable file
@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::table('{{ table }}', function (Blueprint $table) {
|
||||
//
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::table('{{ table }}', function (Blueprint $table) {
|
||||
//
|
||||
});
|
||||
}
|
||||
};
|
33
kernel/console/migrations/stubs/update.stub
Executable file
33
kernel/console/migrations/stubs/update.stub
Executable file
@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
use core\App;
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class DummyClass extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
App::$db->schema->table('DummyTable', function (Blueprint $table) {
|
||||
//
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
App::$db->schema->table('DummyTable', function (Blueprint $table) {
|
||||
//
|
||||
});
|
||||
}
|
||||
}
|
14
kernel/console/routs/cli.php
Normal file
14
kernel/console/routs/cli.php
Normal file
@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
use kernel\App;
|
||||
use Phroute\Phroute\RouteCollector;
|
||||
|
||||
App::$collector->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']);
|
||||
});
|
||||
|
Reference in New Issue
Block a user