Merge branch 'master' of https://git.mcnd.ca/mcndave/lean-console
This commit is contained in:
commit
ce2b9399c2
|
@ -39,6 +39,10 @@ return [
|
||||||
]
|
]
|
||||||
],
|
],
|
||||||
|
|
||||||
|
'cli' => [
|
||||||
|
'Lean\\Console\\Cli' => implode(DIRECTORY_SEPARATOR, [ $path, "src", "Cli", "" ]),
|
||||||
|
],
|
||||||
|
|
||||||
'routes' => ! getenv('DEBUG') ? [] : [
|
'routes' => ! getenv('DEBUG') ? [] : [
|
||||||
'Lean\\Console\\Controller' => implode(DIRECTORY_SEPARATOR, [ $path, "src", "Controller", "" ]),
|
'Lean\\Console\\Controller' => implode(DIRECTORY_SEPARATOR, [ $path, "src", "Controller", "" ]),
|
||||||
],
|
],
|
||||||
|
|
|
@ -0,0 +1,31 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Lean\Console\Cli;
|
||||||
|
|
||||||
|
use Notes\CLI\Attribute\{Option, Command};
|
||||||
|
use Psr\Http\Message\{ServerRequestInterface, ResponseInterface};
|
||||||
|
|
||||||
|
use Lean\Console\{Lib, };
|
||||||
|
|
||||||
|
# https://www.asciiart.eu/text-to-ascii-art
|
||||||
|
#[Command(description: PHP_EOL . <<<CLI
|
||||||
|
#[color:gray, bg: black]╔═════════════════════════╗[#]
|
||||||
|
#[color:gray, bg: black]║ _ ║[#]
|
||||||
|
#[color:gray, bg: black]║ | | ___ __ _ _ __ ║[#]
|
||||||
|
#[color:gray, bg: black]║ | | / _ \/ _` | '_ \ ║[#]
|
||||||
|
#[color:gray, bg: black]║ | |__| __/ (_| | | | | ║[#]
|
||||||
|
#[color:gray, bg: black]║ |_____\___|\__,_|_| |_| ║[#]
|
||||||
|
#[color:gray, bg: black]║ ║[#]
|
||||||
|
#[color:gray, bg: black]╚═════════════════════════╝[#]
|
||||||
|
|
||||||
|
Try #[color:gray, bg: black]'lean console --help'[#] for more information.
|
||||||
|
CLI
|
||||||
|
, command: 'console')]
|
||||||
|
#[Option(toggle: ['-h', '--help'], description: "Display help for the given command. When no command is given display help for the list command")]
|
||||||
|
#[Option(toggle: ['-q', '--quiet'], description: "Do not output any message")]
|
||||||
|
#[Option(toggle: ['-n', '--no-interaction'], description: "Run script without user interaction")]
|
||||||
|
#[Option(toggle: ['-v', '-vv', '-vvv', '--verbose'], description: "Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug", argument: Option::ARGUMENT_OPTIONAL)]
|
||||||
|
class Console
|
||||||
|
{
|
||||||
|
use Lib\ConsoleControllerTrait;
|
||||||
|
}
|
|
@ -0,0 +1,41 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Lean\Console\Cli;
|
||||||
|
|
||||||
|
use Notes\CLI\Attribute\{Option, Command};
|
||||||
|
use Psr\Http\Message\{ServerRequestInterface, ResponseInterface};
|
||||||
|
|
||||||
|
use Lean\Console\{Lib, };
|
||||||
|
|
||||||
|
# https://www.asciiart.eu/text-to-ascii-art
|
||||||
|
#[Command(description: PHP_EOL . <<<CLI
|
||||||
|
#[color:gray, bg: black]╔═════════════════════════╗[#]
|
||||||
|
#[color:gray, bg: black]║ _ ║[#]
|
||||||
|
#[color:gray, bg: black]║ | | ___ __ _ _ __ ║[#]
|
||||||
|
#[color:gray, bg: black]║ | | / _ \/ _` | '_ \ ║[#]
|
||||||
|
#[color:gray, bg: black]║ | |__| __/ (_| | | | | ║[#]
|
||||||
|
#[color:gray, bg: black]║ |_____\___|\__,_|_| |_| ║[#]
|
||||||
|
#[color:gray, bg: black]║ ║[#]
|
||||||
|
#[color:gray, bg: black]║ STORAGE ║[#]
|
||||||
|
#[color:gray, bg: black]╚═════════════════════════╝[#]
|
||||||
|
|
||||||
|
Try #[color:gray, bg: black]'lean console storage --help'[#] for more information.
|
||||||
|
CLI
|
||||||
|
, command: 'console storage')]
|
||||||
|
#[Option(toggle: ['-h', '--help'], description: "Display help for the given command. When no command is given display help for the list command")]
|
||||||
|
#[Option(toggle: ['-q', '--quiet'], description: "Do not output any message")]
|
||||||
|
#[Option(toggle: ['-n', '--no-interaction'], description: "Run script without user interaction")]
|
||||||
|
#[Option(toggle: ['-v', '-vv', '-vvv', '--verbose'], description: "Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug", argument: Option::ARGUMENT_OPTIONAL)]
|
||||||
|
class Storage
|
||||||
|
{
|
||||||
|
use Lib\ConsoleControllerTrait;
|
||||||
|
|
||||||
|
#[Command(description: "Generate entities based on an existing connection", command: "entity:generate")]
|
||||||
|
#[Option(toggle: ['-l', '--list'], description: "")]
|
||||||
|
public function generate_entity(ServerRequestInterface $request, array $arguments): ResponseInterface
|
||||||
|
{
|
||||||
|
dump($arguments);
|
||||||
|
|
||||||
|
return $this->renderCLI($request, ":)");
|
||||||
|
}
|
||||||
|
}
|
|
@ -27,7 +27,7 @@ class Storage extends Console {
|
||||||
$migrations = $this->container->has(Lib\DatabaseMigrations::class) ? $this->container->get(Lib\DatabaseMigrations::class) : false;
|
$migrations = $this->container->has(Lib\DatabaseMigrations::class) ? $this->container->get(Lib\DatabaseMigrations::class) : false;
|
||||||
$migrations->getEntities();
|
$migrations->getEntities();
|
||||||
|
|
||||||
$context = (new FormHandler($request, new Form\Storage\DatabaseMigration($migrations)))->context;
|
$context = (new FormHandler($request, new Form\Storage\Database\Migrate($migrations)))->context;
|
||||||
|
|
||||||
return $this->renderView("lean-console/page/storage/database", get_defined_vars());
|
return $this->renderView("lean-console/page/storage/database", get_defined_vars());
|
||||||
}
|
}
|
||||||
|
@ -39,7 +39,7 @@ class Storage extends Console {
|
||||||
$migrations = $this->container->has(Lib\DatabaseMigrations::class) ? $this->container->get(Lib\DatabaseMigrations::class) : false;
|
$migrations = $this->container->has(Lib\DatabaseMigrations::class) ? $this->container->get(Lib\DatabaseMigrations::class) : false;
|
||||||
$migrations->getEntities();
|
$migrations->getEntities();
|
||||||
|
|
||||||
$form = new Form\Storage\DatabaseImport($migrations, $connections);
|
$form = new Form\Storage\Database\Import($migrations, $connections);
|
||||||
|
|
||||||
$context = (new FormHandler($request, $form, $this->pushContext(new Lib\FormContext($request, "database.import"))))->context;
|
$context = (new FormHandler($request, $form, $this->pushContext(new Lib\FormContext($request, "database.import"))))->context;
|
||||||
|
|
||||||
|
@ -74,12 +74,4 @@ class Storage extends Console {
|
||||||
{
|
{
|
||||||
return rtrim(ini_get("session.save_path"), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
|
return rtrim(ini_get("session.save_path"), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function createTable() : void
|
|
||||||
{
|
|
||||||
foreach($migrations->entities as $table) {
|
|
||||||
// $repository = $table
|
|
||||||
// $entity::repository()->createTable();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
|
@ -0,0 +1,34 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Lean\Console\Form\Storage\Database;
|
||||||
|
|
||||||
|
use Picea\Ui\Method\{ FormInterface, FormContext, FormContextInterface };
|
||||||
|
|
||||||
|
use \Lean\Console\{ Lib, Entity };
|
||||||
|
|
||||||
|
use Picea\Compiler\Context;
|
||||||
|
use Psr\Http\Message\ServerRequestInterface;
|
||||||
|
use Ulmus\{ EntityCollection };
|
||||||
|
|
||||||
|
class GenerateEntity implements FormInterface
|
||||||
|
{
|
||||||
|
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public function initialize(FormContextInterface $context) : void
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public function validate(FormContextInterface $context) : bool
|
||||||
|
{
|
||||||
|
|
||||||
|
|
||||||
|
return $context->valid();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function execute(FormContextInterface $context) : void
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,6 +1,6 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Lean\Console\Form\Storage;
|
namespace Lean\Console\Form\Storage\Database;
|
||||||
|
|
||||||
use Picea\Ui\Method\{ FormInterface, FormContext, FormContextInterface };
|
use Picea\Ui\Method\{ FormInterface, FormContext, FormContextInterface };
|
||||||
|
|
||||||
|
@ -10,7 +10,7 @@ use Picea\Compiler\Context;
|
||||||
use Psr\Http\Message\ServerRequestInterface;
|
use Psr\Http\Message\ServerRequestInterface;
|
||||||
use Ulmus\{ EntityCollection };
|
use Ulmus\{ EntityCollection };
|
||||||
|
|
||||||
class DatabaseImport implements FormInterface
|
class Import implements FormInterface
|
||||||
{
|
{
|
||||||
protected ? Lib\DatabaseMigrations $migration;
|
protected ? Lib\DatabaseMigrations $migration;
|
||||||
|
|
|
@ -0,0 +1,163 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Lean\Console\Form\Storage\Database;
|
||||||
|
|
||||||
|
use Picea\Ui\Method\{ FormInterface, FormContext, FormContextInterface };
|
||||||
|
|
||||||
|
use \Lean\Console\Lib;
|
||||||
|
|
||||||
|
use Picea\Compiler\Context;
|
||||||
|
use Psr\Http\Message\ServerRequestInterface;
|
||||||
|
use Ulmus\Common\EntityResolver;
|
||||||
|
use \Ulmus\Entity\InformationSchema\Table;
|
||||||
|
|
||||||
|
class Migrate implements FormInterface
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
protected ? Lib\DatabaseMigrations $migration,
|
||||||
|
protected null|Lib\Database\DefinitionCollection $definition = null,
|
||||||
|
)
|
||||||
|
{
|
||||||
|
$this->definition ??= new Lib\Database\DefinitionCollection();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function initialize(FormContextInterface $context) : void
|
||||||
|
{
|
||||||
|
foreach($this->migration->entities as $entity => $table) {
|
||||||
|
$previous = null;
|
||||||
|
|
||||||
|
$connection = $entity::resolveEntity()->sqlAdapter();
|
||||||
|
$adapter = $connection->adapter();
|
||||||
|
$tableName = $table->tableName();
|
||||||
|
$databaseName = $table->databaseName();
|
||||||
|
|
||||||
|
if ( ! $tableName || ! $databaseName ) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
# Query current table's columns
|
||||||
|
$tableEntity = $adapter->schemaTable($connection, $databaseName, $tableName);
|
||||||
|
|
||||||
|
$this->definition[$entity]['table'] = $tableDefinition = new Lib\Database\Definition(
|
||||||
|
entity: $entity,
|
||||||
|
modifier: new Lib\Database\Modifier( type: Lib\Database\ModifierTypeEnum::None, ),
|
||||||
|
);
|
||||||
|
|
||||||
|
if ( $tableEntity ) {
|
||||||
|
$this->definition[$entity]['columns'] = new Lib\Database\DefinitionCollection();
|
||||||
|
|
||||||
|
foreach($table->fieldList(EntityResolver::KEY_COLUMN_NAME, true) as $field => $definition) {
|
||||||
|
$this->definition[$entity]['columns'][$field] = $def = new Lib\Database\Definition(
|
||||||
|
entity: $entity,
|
||||||
|
field: $field,
|
||||||
|
definition: $definition,
|
||||||
|
modifier: new Lib\Database\Modifier( type: Lib\Database\ModifierTypeEnum::None, ),
|
||||||
|
previous: $def ?? null,
|
||||||
|
);
|
||||||
|
|
||||||
|
$column = $tableEntity->columns->filtersOne(fn($e) => strtolower($e->name) === $field);
|
||||||
|
|
||||||
|
# Column do not exists
|
||||||
|
if ( $column === null ) {
|
||||||
|
$def->action = "add";
|
||||||
|
|
||||||
|
$def->modifier = new Lib\Database\Modifier(
|
||||||
|
type: Lib\Database\ModifierTypeEnum::Alter,
|
||||||
|
query: $entity::repository()
|
||||||
|
->alterSqlQuery([ $def->toArray() ])
|
||||||
|
->getSqlQuery(true)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
elseif ( ! $column->matchFieldDefinition($definition) ) {
|
||||||
|
$def->action = "change";
|
||||||
|
|
||||||
|
$def->modifier = new Lib\Database\Modifier(
|
||||||
|
type: Lib\Database\ModifierTypeEnum::Alter,
|
||||||
|
query: $entity::repository()
|
||||||
|
->alterSqlQuery([ $def->toArray() ])
|
||||||
|
->getSqlQuery(true)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
# ALTER TABLE `ajustements` CHANGE `annee` `annee` INT(11) NULL;
|
||||||
|
$alter = $this->definition[$entity]['columns']->filterAlter();
|
||||||
|
|
||||||
|
if ( $alter->count() ) {
|
||||||
|
if ( $adapter->splitAlterQuery() ) {
|
||||||
|
$query = $alter->map(fn(Lib\Database\Definition $map) => $entity::repository()
|
||||||
|
->alterSqlQuery([ $map->toArray() ])
|
||||||
|
->getSqlQuery(true));
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$query = $entity::repository()
|
||||||
|
->alterSqlQuery($alter->map(fn(Lib\Database\Definition $map) => $map->toArray()))
|
||||||
|
->getSqlQuery(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
$tableDefinition->modifier = new Lib\Database\Modifier(
|
||||||
|
type: Lib\Database\ModifierTypeEnum::Alter,
|
||||||
|
query: $query,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$tableDefinition->modifier = new Lib\Database\Modifier(
|
||||||
|
type: Lib\Database\ModifierTypeEnum::Create,
|
||||||
|
query: $entity::repository()
|
||||||
|
->createSqlQuery()
|
||||||
|
->getSqlQuery(true)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$context->definition = $this->definition;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function validate(FormContextInterface $context) : bool
|
||||||
|
{
|
||||||
|
return $context->valid();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function execute(FormContextInterface $context) : void
|
||||||
|
{
|
||||||
|
$create = $context->createAll ? explode(',', $context->createAll) : [ $context->create ];
|
||||||
|
|
||||||
|
foreach(array_filter($create) as $entity) {
|
||||||
|
if ($this->definition[$entity]['table']->modifier->type === Lib\Database\ModifierTypeEnum::Create) {
|
||||||
|
$entity::repository()->createTable();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$alter = $context->alterAll ? explode(',', $context->alterAll) : [ $context->alter ];
|
||||||
|
|
||||||
|
foreach(array_filter($alter) as $entity) {
|
||||||
|
if ($this->definition[$entity]['table']->modifier->type === Lib\Database\ModifierTypeEnum::Alter) {
|
||||||
|
$adapter = $entity::resolveEntity()->sqlAdapter()->adapter();
|
||||||
|
|
||||||
|
$def = $this->definition[$entity]['columns']->filterAlter();
|
||||||
|
|
||||||
|
if ( $adapter->splitAlterQuery() ) {
|
||||||
|
$def->walk(fn(Lib\Database\Definition $definition) =>
|
||||||
|
$entity::repository()->alterTable([ $definition->toArray() ])
|
||||||
|
);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$entity::repository()->alterTable($def->map( fn(Lib\Database\Definition $e) => $e->toArray() ));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->initialize($context);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function getContext(ServerRequestInterface $request, ? string $formName = null) : FormContextInterface
|
||||||
|
{
|
||||||
|
return new class($request, $formName) extends FormContext {
|
||||||
|
|
||||||
|
public array $status = [];
|
||||||
|
|
||||||
|
public array $actions = [];
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue