This commit is contained in:
2024-07-29 15:57:20 +03:00
parent 95f56a04d3
commit 200763725e
34 changed files with 936 additions and 72 deletions

48
kernel/App.php Normal file
View File

@ -0,0 +1,48 @@
<?php
namespace kernel;
use app\helpers\Debug;
use Phroute\Phroute\Dispatcher;
class App
{
static string $responseType = ResponseType::TEXT_HTML;
static CgRouteCollector $collector;
static Header $header;
public static Database $db;
public function run(): void
{
$dispatcher = new Dispatcher(App::$collector->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();
}
}

44
kernel/CgRouteCollector.php Executable file
View File

@ -0,0 +1,44 @@
<?php
namespace kernel;
use Phroute\Phroute\Route;
use Phroute\Phroute\RouteCollector;
class CgRouteCollector extends RouteCollector
{
public function crud($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::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);
}
}

45
kernel/Database.php Executable file
View File

@ -0,0 +1,45 @@
<?php
/**
* Created by PhpStorm.
* User: kirill
* Date: 03.08.19
* Time: 23:51
*/
namespace kernel;
use Illuminate\Database\Capsule\Manager as Capsule;
use Illuminate\Database\Schema\Builder;
use Illuminate\Container\Container;
use Phroute\Phroute\Dispatcher;
class Database
{
/** @var Builder $capsule */
public Builder $schema;
/** @var Capsule $capsule */
public Capsule $capsule;
function __construct()
{
$this->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();
}
}

28
kernel/Header.php Executable file
View File

@ -0,0 +1,28 @@
<?php
/**
* Created by PhpStorm.
* User: kirill
* Date: 16.06.19
* Time: 0:22
*/
namespace kernel;
class Header
{
protected $list = [];
public function add($key, $value)
{
$this->list[$key] = $value;
}
public function set()
{
foreach ($this->list as $key=>$v){
header($key . ": " . $v);
}
}
}

19
kernel/ResponseType.php Executable file
View File

@ -0,0 +1,19 @@
<?php
/**
* Created by PhpStorm.
* User: kirill
* Date: 15.06.19
* Time: 0:44
*/
namespace kernel;
class ResponseType
{
const TEXT_HTML = 'text/html';
const APPLICATION_JSON = 'application/json';
const MULTIPART_FORM_DATA = 'multipart/form-data';
}

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

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

View 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");
}
}

View 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');
}
}
}

View 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()
{
//
}
}

View 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');
}
}

View 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 }}');
}
};

View 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
{
//
}
};

View 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) {
//
});
}
};

View 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) {
//
});
}
}

View 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']);
});