From 6f9b7c7d5e16f5b58cdd43dfe7e0b7e9e6b7fa7e Mon Sep 17 00:00:00 2001 From: Anton Shevchuk Date: Fri, 3 Nov 2017 18:03:58 +0200 Subject: [PATCH 01/11] Updated GRID generator --- src/Command/Generate/GridCommand.php | 4 +- src/Generator/views/views/crud.phtml | 10 ++-- src/Generator/views/views/grid.phtml | 89 ++++++++++++++++------------ 3 files changed, 59 insertions(+), 44 deletions(-) diff --git a/src/Command/Generate/GridCommand.php b/src/Command/Generate/GridCommand.php index 3cbfe62..92ddfbd 100644 --- a/src/Command/Generate/GridCommand.php +++ b/src/Command/Generate/GridCommand.php @@ -124,7 +124,7 @@ protected function generate(InputInterface $input, OutputInterface $output) } else { $template = new Generator\Template\GridControllerTemplate(); $template->setFilePath($controllerFile); - $template->setTemplateData(['model' => $model]); + $template->setTemplateData(['model' => $model, 'module' => $module]); $generator = new Generator\Generator($template); $generator->make(); @@ -138,7 +138,7 @@ protected function generate(InputInterface $input, OutputInterface $output) } else { $template = new Generator\Template\GridViewTemplate(); $template->setFilePath($viewFile); - $template->setTemplateData(['model' => $model]); + $template->setTemplateData(['model' => $model, 'module' => $module]); $generator = new Generator\Generator($template); $generator->make(); diff --git a/src/Generator/views/views/crud.phtml b/src/Generator/views/views/crud.phtml index 7c1e57a..038176e 100644 --- a/src/Generator/views/views/crud.phtml +++ b/src/Generator/views/views/crud.phtml @@ -1,12 +1,12 @@ /** @var \Application\\Row $row */ -$uid = uniqid('form_'); +$uid = uniqid('form_', false); $url = $this->url('', 'crud'); \n"?> -
- - - + + + +
// need for plain request diff --git a/src/Generator/views/views/grid.phtml b/src/Generator/views/views/grid.phtml index 08a5f36..60ae79e 100644 --- a/src/Generator/views/views/grid.phtml +++ b/src/Generator/views/views/grid.phtml @@ -6,44 +6,59 @@ if (!$grid) return; $uid = $grid->getUid(); \n"?>
- + - partial("grid/total.phtml", ["grid"=>$grid]) ?>'?> - partial("grid/limit.phtml", ["grid"=>$grid]) ?>'?> + - - - - - - - - - - - getData() as $i => $row) : ?>'?> - - - - - - - '?> - -
#Header example
'?>Content example - - - - - - -
+
- partial("grid/empty-rows.phtml", ["grid"=>$grid]) ?>'?> - partial("grid/pagination.phtml", ["grid"=>$grid]) ?>'?> - partial("grid/total.phtml", ["grid"=>$grid]) ?>'?> + partial('grid/total.phtml', ['grid'=>\$grid]) ?>\n"?> + partial('grid/limit.phtml', ['grid'=>\$grid]) ?>\n"?> -
\ No newline at end of file + + + + + + + + + + + getData() as $row) : ?>'?> + + + + + + + '?> + +
order('id') ?: '#' ?>"?>">"?>Header example
"?>Content example + url('$module', 'crud', ['id' => \$row['id']]) ?>"?>" class="btn btn-primary" + data-ajax-dialog data-ajax-method="get" data-modal-style="modal-lg"> + + + url('$module', 'crud', ['id' => \$row['id']]) ?>"?>" class="btn btn-danger" + data-confirm data-ajax data-ajax-method="delete"> + + +
+ + partial('grid/empty-rows.phtml', ['grid'=>\$grid]) ?>\n"?> + partial('grid/pagination.phtml', ['grid'=>\$grid]) ?>\n"?> + partial('grid/total.phtml', ['grid'=>\$grid]) ?>\n"?> + + From 08facbdf473be238a593a96bebfae14be20f134f Mon Sep 17 00:00:00 2001 From: Anton Shevchuk Date: Wed, 8 Nov 2017 11:04:53 +0200 Subject: [PATCH 02/11] Added DB info to output --- src/Command/Db/AbstractDbCommand.php | 7 +++++++ src/Command/Db/StatusCommand.php | 4 ---- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/Command/Db/AbstractDbCommand.php b/src/Command/Db/AbstractDbCommand.php index 96bfe2b..f701cc9 100644 --- a/src/Command/Db/AbstractDbCommand.php +++ b/src/Command/Db/AbstractDbCommand.php @@ -43,6 +43,13 @@ protected function execute(InputInterface $input, OutputInterface $output) $phinxArguments['command'] = $commandName; $phinxArguments['--configuration'] = PATH_APPLICATION . DS . 'configs' . DS . 'phinx.php'; + + // write database name to console + // to avoid any mistakes + $config = include $phinxArguments['--configuration']; + $this->write('host '. $config['environments']['default']['host']); + $this->write('name '. $config['environments']['default']['name']); + if ($command->getDefinition()->hasOption('environment')) { $phinxArguments['--environment'] = 'default'; } diff --git a/src/Command/Db/StatusCommand.php b/src/Command/Db/StatusCommand.php index 831e707..05669e2 100644 --- a/src/Command/Db/StatusCommand.php +++ b/src/Command/Db/StatusCommand.php @@ -30,9 +30,5 @@ protected function configure() // the "--help" option ->setHelp('This command is shorthand to phinx tool') ; - - $env = new InputArgument('--environment', InputArgument::OPTIONAL, 'Environment', 'default'); - - $this->getDefinition()->addArgument($env); } } From d27dc751b31d2c40629229cf70523928a0dac098 Mon Sep 17 00:00:00 2001 From: Anton Shevchuk Date: Mon, 13 Nov 2017 10:07:38 +0200 Subject: [PATCH 03/11] Migrate to PHP 7.1 --- .travis.yml | 2 -- composer.json | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index 700a0c8..d921542 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,12 +1,10 @@ language: php php: - - 7.0 - 7.1 - 7.2 - master matrix: allow_failures: - - php: 7.2 - php: master install: # Composer diff --git a/composer.json b/composer.json index bfb7973..26e4935 100644 --- a/composer.json +++ b/composer.json @@ -11,7 +11,7 @@ } ], "require": { - "php": ">=7.0", + "php": ">=7.1", "codeception/codeception": "~2.3", "composer/composer": "~1.5", "robmorgan/phinx": "~0.9", From e2e4711411fb881a8c4458247756de00ea39433f Mon Sep 17 00:00:00 2001 From: Anton Shevchuk Date: Thu, 16 Nov 2017 18:11:30 +0200 Subject: [PATCH 04/11] Updated generator for PHP 7.1 --- composer.json | 4 ++-- src/Generator/views/models/grid.phtml | 6 ++---- src/Generator/views/models/row.phtml | 4 ++-- tests/Generator/samples/grid.html | 6 ++---- tests/Generator/samples/row.html | 4 ++-- 5 files changed, 10 insertions(+), 14 deletions(-) diff --git a/composer.json b/composer.json index 26e4935..2f31d24 100644 --- a/composer.json +++ b/composer.json @@ -12,6 +12,7 @@ ], "require": { "php": ">=7.1", + "bluzphp/framework": "~7.8", "codeception/codeception": "~2.3", "composer/composer": "~1.5", "robmorgan/phinx": "~0.9", @@ -20,8 +21,7 @@ "symfony/filesystem": "~3.0", "symfony/finder": "~3.0", "symfony/process": "~3.0", - "symfony/yaml": "~3.0", - "bluzphp/framework": "~7.7" + "symfony/yaml": "~3.0" }, "require-dev": { "phpunit/phpunit": "~6.4", diff --git a/src/Generator/views/models/grid.phtml b/src/Generator/views/models/grid.phtml index d4be412..a32b3d1 100644 --- a/src/Generator/views/models/grid.phtml +++ b/src/Generator/views/models/grid.phtml @@ -23,9 +23,9 @@ class Grid extends \Bluz\Grid\Grid protected $uid = ''; /** - * @return self + * @return void */ - public function init() + public function init() : void { // Current table as source of grid $adapter = new SelectSource(); @@ -34,7 +34,5 @@ class Grid extends \Bluz\Grid\Grid $this->setAdapter($adapter); $this->setDefaultLimit(25); $this->setAllowOrders([]); - - return $this; } } diff --git a/src/Generator/views/models/row.phtml b/src/Generator/views/models/row.phtml index 1e5debe..440f365 100644 --- a/src/Generator/views/models/row.phtml +++ b/src/Generator/views/models/row.phtml @@ -20,14 +20,14 @@ class Row extends \Bluz\Db\Row /** * @return void */ - public function beforeInsert() + public function beforeInsert() : void { } /** * @return void */ - public function beforeUpdate() + public function beforeUpdate() : void { } } diff --git a/tests/Generator/samples/grid.html b/tests/Generator/samples/grid.html index c286a29..5650938 100644 --- a/tests/Generator/samples/grid.html +++ b/tests/Generator/samples/grid.html @@ -22,9 +22,9 @@ protected $uid = 'users'; /** - * @return self + * @return void */ - public function init() + public function init() : void { // Current table as source of grid $adapter = new SelectSource(); @@ -33,7 +33,5 @@ $this->setAdapter($adapter); $this->setDefaultLimit(25); $this->setAllowOrders([]); - - return $this; } } diff --git a/tests/Generator/samples/row.html b/tests/Generator/samples/row.html index e1e94f7..c700d05 100644 --- a/tests/Generator/samples/row.html +++ b/tests/Generator/samples/row.html @@ -21,14 +21,14 @@ /** * @return void */ - public function beforeInsert() + public function beforeInsert() : void { } /** * @return void */ - public function beforeUpdate() + public function beforeUpdate() : void { } } From 0002a2fec8d33fd7b4b465ed0f8af4837c889d4e Mon Sep 17 00:00:00 2001 From: Anton Shevchuk Date: Tue, 21 Nov 2017 14:39:23 +0200 Subject: [PATCH 05/11] Added `Scaffold` generator --- src/Application/Application.php | 12 +- src/Command/AbstractCommand.php | 94 ++++++---- .../Generate/AbstractGenerateCommand.php | 165 +++++++++++++++-- src/Command/Generate/ControllerCommand.php | 79 +++----- src/Command/Generate/CrudCommand.php | 126 +++++-------- src/Command/Generate/GridCommand.php | 120 +++++------- src/Command/Generate/ModelCommand.php | 123 +++---------- src/Command/Generate/ModuleCommand.php | 50 ++--- src/Command/Generate/RestCommand.php | 82 ++++----- src/Command/Generate/ScaffoldCommand.php | 173 ++++++++++++++++++ src/Generator/Generator.php | 2 + src/Generator/Template/AbstractTemplate.php | 1 + src/Generator/views/models/grid.phtml | 13 +- src/Generator/views/views/crud.phtml | 34 +++- src/Generator/views/views/grid.phtml | 15 +- tests/Command/Generate/GridCommandTest.php | 10 +- tests/Generator/samples/grid.html | 9 +- 17 files changed, 650 insertions(+), 458 deletions(-) create mode 100644 src/Command/Generate/ScaffoldCommand.php diff --git a/src/Application/Application.php b/src/Application/Application.php index a82222c..0ab4ae0 100644 --- a/src/Application/Application.php +++ b/src/Application/Application.php @@ -37,6 +37,7 @@ public function __construct($name = 'UNKNOWN', $version = 'UNKNOWN') * init * * @return void + * @throws \Bluz\Config\ConfigException */ public function init() { @@ -98,6 +99,7 @@ protected function registerCommands() new Command\Generate\CrudCommand, new Command\Generate\GridCommand, new Command\Generate\RestCommand, + new Command\Generate\ScaffoldCommand, new Command\Module\InstallCommand, new Command\Module\ListCommand, new Command\Module\RemoveCommand, @@ -113,7 +115,7 @@ protected function registerCommands() * * @return string */ - public function getWorkingPath() + public function getWorkingPath() : string { return getcwd(); } @@ -124,7 +126,7 @@ public function getWorkingPath() * @param string $name * @return string */ - public function getModulePath($name) + public function getModulePath($name) : string { return $this->getWorkingPath() . DIRECTORY_SEPARATOR . 'application' . DIRECTORY_SEPARATOR @@ -138,7 +140,7 @@ public function getModulePath($name) * @param string $name * @return string */ - public function getModelPath($name) + public function getModelPath($name) : string { return $this->getWorkingPath() . DIRECTORY_SEPARATOR . 'application' . DIRECTORY_SEPARATOR @@ -150,7 +152,7 @@ public function getModelPath($name) * @param string $name * @return bool */ - public function isModuleExists($name) + public function isModuleExists($name) : bool { return is_dir($this->getModulePath($name)); } @@ -159,7 +161,7 @@ public function isModuleExists($name) * @param string $name * @return bool */ - public function isModelExists($name) + public function isModelExists($name) : bool { return is_dir($this->getModelPath($name)); } diff --git a/src/Command/AbstractCommand.php b/src/Command/AbstractCommand.php index 1fbda26..ac7d4cb 100644 --- a/src/Command/AbstractCommand.php +++ b/src/Command/AbstractCommand.php @@ -6,9 +6,11 @@ namespace Bluzman\Command; -use Bluz\Validator\Validator as v; +use Bluz\Proxy\Config; +use Bluz\Validator\Validator; use Bluzman\Application\Application; use Bluzman\Input\InputArgument; +use Bluzman\Input\InputException; use Symfony\Component\Console; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; @@ -43,7 +45,7 @@ abstract class AbstractCommand extends Console\Command\Command /** * @param InputInterface $input */ - public function setInput(InputInterface $input) + public function setInput(InputInterface $input) : void { $this->input = $input; } @@ -59,7 +61,7 @@ public function getInput() /** * @param OutputInterface $output */ - public function setOutput(OutputInterface $output) + public function setOutput(OutputInterface $output) : void { $this->output = $output; } @@ -75,7 +77,7 @@ public function getOutput() /** * @param \Symfony\Component\Filesystem\Filesystem $fs */ - public function setFs($fs) + public function setFs($fs) : void { $this->fs = $fs; } @@ -92,9 +94,11 @@ public function getFs() } /** - * @param InputInterface $input + * @param InputInterface $input * @param OutputInterface $output + * * @return void + * @throws \Bluz\Config\ConfigException */ final public function initialize(InputInterface $input, OutputInterface $output) { @@ -110,14 +114,14 @@ final public function initialize(InputInterface $input, OutputInterface $output) $config->setEnvironment($input->getOption('env')); $config->init(); - \Bluz\Proxy\Config::setInstance($config); + Config::setInstance($config); } /** * @param $message * @return void */ - public function write($message) + public function write($message) : void { $this->getOutput()->writeln($message); } @@ -126,7 +130,7 @@ public function write($message) * @param $message * @return void */ - public function info($message) + public function info($message) : void { $this->write("$message"); } @@ -135,7 +139,7 @@ public function info($message) * @param $message * @return void */ - public function comment($message) + public function comment($message) : void { $this->write("$message"); } @@ -144,7 +148,7 @@ public function comment($message) * @param $message * @return void */ - public function question($message) + public function question($message) : void { $this->write("$message:"); } @@ -153,7 +157,7 @@ public function question($message) * @param $message * @return void */ - public function error($message) + public function error($message) : void { $this->write("$message"); } @@ -170,42 +174,68 @@ public function callForContribute() } /** - * addModuleArgument + * Add Module Argument * * @param int $required - * @return AbstractCommand + * @return void */ - protected function addModuleArgument($required = InputArgument::REQUIRED) + protected function addModuleArgument($required = InputArgument::REQUIRED) : void { $module = new InputArgument('module', $required, 'Module name is required'); - $module->setValidator( - v::create()->string()->alphaNumeric('-_')->noWhitespace() - ); - $this->getDefinition()->addArgument($module); + } - return $this; + /** + * Validate Module Argument + * + * @return void + * @throws \Bluzman\Input\InputException + */ + protected function validateModuleArgument() : void + { + $module = $this->getInput()->getArgument('module'); + + $validator = Validator::create() + ->string() + ->alphaNumeric('-_') + ->noWhitespace(); + + if ($this->getDefinition()->getArgument('module')->isRequired() + && !$validator->validate($module)) { + throw new InputException($validator->getError()); + } } /** - * addControllerArgument + * Add Controller Argument * * @param int $required - * @return AbstractCommand + * @return void */ - protected function addControllerArgument($required = InputArgument::REQUIRED) + protected function addControllerArgument($required = InputArgument::REQUIRED) : void { - $controller = new InputArgument( - 'controller', - $required, - 'Controller name is required' - ); - $controller->setValidator( - v::create()->string()->alphaNumeric('-_')->noWhitespace() - ); - + $controller = new InputArgument('controller', $required, 'Controller name is required'); $this->getDefinition()->addArgument($controller); + } - return $this; + /** + * Validate Module Argument + * + * @return void + * @throws \Bluzman\Input\InputException + */ + protected function validateControllerArgument() : void + { + $controller = $this->getInput()->getArgument('controller'); + + $validator = Validator::create() + ->string() + ->alphaNumeric('-_') + ->noWhitespace(); + + if ($this->getDefinition()->getArgument('controller')->isRequired() + && !$validator->validate($controller)) { + throw new InputException($validator->getError()); + } } } diff --git a/src/Command/Generate/AbstractGenerateCommand.php b/src/Command/Generate/AbstractGenerateCommand.php index 428c866..dd63329 100644 --- a/src/Command/Generate/AbstractGenerateCommand.php +++ b/src/Command/Generate/AbstractGenerateCommand.php @@ -6,9 +6,14 @@ namespace Bluzman\Command\Generate; -use Bluz\Validator\Validator as v; +use Bluz\Db\Table; +use Bluz\Proxy\Db; +use Bluz\Validator\Validator; use Bluzman\Command\AbstractCommand; +use Bluzman\Generator\Generator; +use Bluzman\Generator\GeneratorException; use Bluzman\Input\InputArgument; +use Bluzman\Input\InputException; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; @@ -24,23 +29,95 @@ abstract class AbstractGenerateCommand extends AbstractCommand * @param OutputInterface $output * @return mixed */ - abstract public function verify(InputInterface $input, OutputInterface $output); + abstract public function verify(InputInterface $input, OutputInterface $output) : void; /** - * addModelArguments + * Add Model Argument * - * @return self + * @return void */ - protected function addModelArgument() + protected function addModelArgument() : void { $model = new InputArgument('model', InputArgument::REQUIRED, 'Model name is required'); - $model->setValidator( - v::create()->string()->alphaNumeric()->noWhitespace() - ); - $this->getDefinition()->addArgument($model); + } + + /** + * Validate Model Argument + * + * @return void + * @throws \Bluzman\Input\InputException + */ + protected function validateModelArgument() : void + { + $model = $this->getInput()->getArgument('model'); + + $validator = Validator::create() + ->string() + ->alphaNumeric() + ->noWhitespace(); + + if ($this->getDefinition()->getArgument('model')->isRequired() + && !$validator->validate($model)) { + throw new InputException($validator->getError()); + } + } + + /** + * Add Table Argument + * + * @return void + */ + protected function addTableArgument() : void + { + $table = new InputArgument('table', InputArgument::REQUIRED, 'Table name is required'); + $this->getDefinition()->addArgument($table); + } + + /** + * Validate Table Argument + * + * @return void + * @throws \Bluzman\Input\InputException + */ + protected function validateTableArgument() : void + { + $table = $this->getInput()->getArgument('table'); + + $validator = Validator::create() + ->string() + ->alphaNumeric('_') + ->noWhitespace(); + + if ($this->getDefinition()->getArgument('table')->isRequired() + && !$validator->validate($table)) { + throw new InputException($validator->getError()); + } + } + + /** + * Get Table instance + * + * @param string $model + * + * @return Table + * @throws \Bluzman\Generator\GeneratorException + */ + protected function getTableInstance($model) : Table + { + $file = $this->getApplication()->getModelPath($model) . DS . 'Table.php'; + if (!file_exists($file)) { + throw new GeneratorException( + "Model $model is not exist, run command `bluzman generate:model $model` before" + ); + } + include_once $file; + $class = '\\Application\\'. ucfirst($model) . '\\Table'; + if (!class_exists($class)) { + throw new GeneratorException("Bluzman can't found `Table` class for model `$model`"); + } + return $class::getInstance(); - return $this; } /** @@ -55,10 +132,33 @@ protected function getTemplate($class) return new $class; } + /** + * Small wrapper for simplify code + * + * @param string $class + * @param string $file + * @param array $data + * + * @return void + */ + protected function generateFile($class, $file, array $data = []) : void + { + if (file_exists($file)) { + $this->comment(" |> File $file already exists"); + } + + $template = $this->getTemplate($class); + $template->setFilePath($file); + $template->setTemplateData($data); + + $generator = new Generator($template); + $generator->make(); + } + /** * @return string */ - protected function getControllerPath($module, $controller) + protected function getControllerPath($module, $controller) : string { return $this->getApplication()->getModulePath($module) . DS . 'controllers' @@ -69,11 +169,52 @@ protected function getControllerPath($module, $controller) /** * @return string */ - protected function getViewPath($module, $controller) + protected function getViewPath($module, $controller) : string { return $this->getApplication()->getModulePath($module) . DS . 'views' . DS . $controller . '.phtml'; } + + /** + * @todo move it to DB class + * @return array + */ + protected function getPrimaryKey($table) + { + $connect = Db::getOption('connect'); + + return Db::fetchColumn( + ' + SELECT COLUMN_NAME + FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE + WHERE TABLE_SCHEMA = ? + AND TABLE_NAME = ? + AND CONSTRAINT_NAME = ? + ', + [$connect['name'], $table, 'PRIMARY'] + ); + } + + /** + * @todo move it to DB class + * @return array + */ + protected function getColumns($table) + { + $connect = Db::getOption('connect'); + + return Db::fetchAll( + ' + SELECT + COLUMN_NAME as name, + COLUMN_TYPE as type + FROM INFORMATION_SCHEMA.COLUMNS + WHERE TABLE_SCHEMA = ? + AND TABLE_NAME = ? + ', + [$connect['name'], $table] + ); + } } diff --git a/src/Command/Generate/ControllerCommand.php b/src/Command/Generate/ControllerCommand.php index 1f69354..a16d786 100644 --- a/src/Command/Generate/ControllerCommand.php +++ b/src/Command/Generate/ControllerCommand.php @@ -33,10 +33,8 @@ protected function configure() ->setHelp('This command allows you to generate controller files') ; - $this - ->addModuleArgument() - ->addControllerArgument() - ; + $this->addModuleArgument(); + $this->addControllerArgument(); } /** @@ -44,39 +42,19 @@ protected function configure() * @param OutputInterface $output * @return void */ - protected function execute(InputInterface $input, OutputInterface $output) + protected function execute(InputInterface $input, OutputInterface $output) : void { + $this->write('Running generate:controller command'); try { - $this->write("Running generate:controller command"); - - $module = $input->getArgument('module'); - $this->getDefinition()->getArgument('module')->validate($module); - - if (!$this->getApplication()->isModuleExists($module)) { - $command = $this->getApplication()->find('generate:module'); - - $arguments = [ - 'command' => 'generate:module', - 'module' => $module - ]; - - $greetInput = new ArrayInput($arguments); - $command->run($greetInput, $output); - } - - $controller = $input->getArgument('controller'); - $this->getDefinition()->getArgument('controller')->validate($controller); + // validate + $this->validateModuleArgument(); + $this->validateControllerArgument(); // generate directories and files $this->generate($input, $output); // verify it $this->verify($input, $output); - - $this->write( - "Controller {$controller} has been successfully created " . - "in the module {$controller}." - ); } catch (\Exception $e) { $this->error("ERROR: {$e->getMessage()}"); } @@ -85,37 +63,18 @@ protected function execute(InputInterface $input, OutputInterface $output) /** * @param InputInterface $input * @param OutputInterface $output - * @return $this + * @return void */ - protected function generate(InputInterface $input, OutputInterface $output) + protected function generate(InputInterface $input, OutputInterface $output) : void { $module = $input->getArgument('module'); $controller = $input->getArgument('controller'); $controllerFile = $this->getControllerPath($module, $controller); - if (file_exists($controllerFile)) { - $this->comment("Controller file $module/$controller already exists"); - } else { - $template = new Generator\Template\ControllerTemplate; - $template->setFilePath($controllerFile); - - $generator = new Generator\Generator($template); - $generator->make(); - } + $this->generateFile('ControllerTemplate', $controllerFile); $viewFile = $this->getViewPath($module, $controller); - if (file_exists($viewFile)) { - $this->comment("View file $module/$controller already exists"); - } else { - $template = new Generator\Template\ViewTemplate; - $template->setFilePath($viewFile); - $template->setTemplateData(['name' => $controller]); - - $generator = new Generator\Generator($template); - $generator->make(); - } - - return $this; + $this->generateFile('ViewTemplate', $viewFile, ['name' => $controller]); } /** @@ -124,16 +83,19 @@ protected function generate(InputInterface $input, OutputInterface $output) * @return void * @throws \Bluzman\Generator\GeneratorException */ - public function verify(InputInterface $input, OutputInterface $output) + public function verify(InputInterface $input, OutputInterface $output) : void { - $modulePath = $this->getApplication()->getModulePath($input->getArgument('module')); + $module = $input->getArgument('module'); + $controller = $input->getArgument('controller'); + + $modulePath = $this->getApplication()->getModulePath($module); $paths = [ $modulePath, $modulePath . DS . 'controllers', - $modulePath . DS . 'controllers' . DS . $input->getArgument('controller') . '.php', + $modulePath . DS . 'controllers' . DS . $controller . '.php', $modulePath . DS . 'views', - $modulePath . DS . 'views' . DS . $input->getArgument('controller') . '.phtml', + $modulePath . DS . 'views' . DS . $controller . '.phtml', ]; @@ -142,5 +104,10 @@ public function verify(InputInterface $input, OutputInterface $output) throw new Generator\GeneratorException("File or directory `$path` is not exists"); } } + + $this->write( + " |> Controller {$controller} has been successfully created " . + "in the module {$module}." + ); } } diff --git a/src/Command/Generate/CrudCommand.php b/src/Command/Generate/CrudCommand.php index d376770..4dc344c 100644 --- a/src/Command/Generate/CrudCommand.php +++ b/src/Command/Generate/CrudCommand.php @@ -33,55 +33,30 @@ protected function configure() // the "--help" option ->setHelp('This command allows you to generate CRUD files'); - $this - ->addModelArgument() - ->addModuleArgument(InputArgument::OPTIONAL) - ; + $this->addModelArgument(); + $this->addModuleArgument(InputArgument::OPTIONAL); } /** - * @param InputInterface $input + * @param InputInterface $input * @param OutputInterface $output + * * @return void + * @throws \Bluzman\Generator\GeneratorException */ - protected function execute(InputInterface $input, OutputInterface $output) + protected function execute(InputInterface $input, OutputInterface $output) : void { + $this->write('Running generate:crud command'); try { - $this->write("Running generate:crud command"); - - $model = $input->getArgument('model'); - $this->getDefinition()->getArgument('model')->validate($model); - - if (!$this->getApplication()->isModelExists($model)) { - throw new InputException( - "Model $model is not exist, " . - "run command bluzman generate:model $model before" - ); - } - - if ($module = $input->getArgument('module')) { - $this->getDefinition()->getArgument('module')->validate($module); - - if (!$this->getApplication()->isModuleExists($module)) { - throw new InputException( - "Module $module is not exist, " . - "run command bluzman generate:module $module before" - ); - } - } + // validate + $this->validateModelArgument(); + $this->validateModuleArgument(); // generate directories and files $this->generate($input, $output); // verify it $this->verify($input, $output); - - $this->write("CRUD for {$model} has been successfully created."); - if ($module = $input->getArgument('module')) { - $this->write( - "Open page /acl in your browser and set permissions for {$module}" - ); - } } catch (InputException $e) { $this->error("ERROR: {$e->getMessage()}"); } @@ -93,61 +68,41 @@ protected function execute(InputInterface $input, OutputInterface $output) * @return void * @throws InputException */ - protected function generate(InputInterface $input, OutputInterface $output) + protected function generate(InputInterface $input, OutputInterface $output) : void { $model = ucfirst($input->getArgument('model')); + $module = $input->getArgument('module'); + + // template data + $data = [ + 'model' => $model, + 'module' => $module + ]; // generate CRUD $crudFile = $this->getApplication()->getModelPath($model) . DS . 'Crud.php'; + $this->generateFile('CrudTemplate', $crudFile, $data); - if (file_exists($crudFile)) { - $this->comment("Crud file $model/Crud.php already exists"); - } else { - $template = $this->getTemplate('CrudTemplate'); - $template->setFilePath($crudFile); - $template->setTemplateData( - [ - 'model' => $model - ] - ); - - $generator = new Generator\Generator($template); - $generator->make(); - } + if ($module) { + if (!$this->getApplication()->isModelExists($model)) { + throw new InputException( + "Model $model is not exist, " . + "run command bluzman generate:model $model before" + ); + } - if ($module = $input->getArgument('module')) { - $this->write("Generate $module/controllers/crud.php"); + $this->write(" |> Generate CRUD controller $module/controllers/crud.php"); $controllerFile = $this->getControllerPath($module, 'crud'); - if (file_exists($controllerFile)) { - $this->comment("Controller file $module/crud already exists"); - } else { - $template = new Generator\Template\CrudControllerTemplate(); - $template->setFilePath($controllerFile); - $template->setTemplateData(['model' => $model]); - - $generator = new Generator\Generator($template); - $generator->make(); - } + $this->generateFile('CrudControllerTemplate', $controllerFile, $data); - $this->write("Generate $module/views/crud.phtml"); + $this->write(" |> Generate CRUD view $module/views/crud.phtml"); - $viewFile = $this->getViewPath($module, 'crud'); - if (file_exists($viewFile)) { - $this->comment("View file $module/crud already exists"); - } else { - $template = new Generator\Template\CrudViewTemplate(); - $template->setFilePath($viewFile); - $template->setTemplateData( - [ - 'model' => $model, - 'module' => $module - ] - ); + $tableInstance = $this->getTableInstance($model); + $data['columns'] = $tableInstance::getMeta(); - $generator = new Generator\Generator($template); - $generator->make(); - } + $viewFile = $this->getViewPath($module, 'crud'); + $this->generateFile('CrudViewTemplate', $viewFile, $data); } } @@ -157,9 +112,12 @@ protected function generate(InputInterface $input, OutputInterface $output) * @return void * @throws \Bluzman\Generator\GeneratorException */ - public function verify(InputInterface $input, OutputInterface $output) + public function verify(InputInterface $input, OutputInterface $output) : void { - $modelPath = $this->getApplication()->getModelPath($input->getArgument('model')); + $model = $input->getArgument('model'); + $module = $input->getArgument('module'); + + $modelPath = $this->getApplication()->getModelPath($model); $paths = [ $modelPath . DS . 'Crud.php', @@ -170,5 +128,13 @@ public function verify(InputInterface $input, OutputInterface $output) throw new Generator\GeneratorException("File `$path` is not exists"); } } + + $this->write(" |> CRUD for {$model} has been successfully created."); + if ($module) { + $this->write( + " |> Open page /acl in your browser ". + "and set permission Management for {$module} module" + ); + } } } diff --git a/src/Command/Generate/GridCommand.php b/src/Command/Generate/GridCommand.php index 92ddfbd..c2b6f2e 100644 --- a/src/Command/Generate/GridCommand.php +++ b/src/Command/Generate/GridCommand.php @@ -34,56 +34,30 @@ protected function configure() ->setHelp('This command allows you to generate GRID files') ; - $this - ->addModelArgument() - ->addModuleArgument(InputArgument::OPTIONAL) - ; + $this->addModelArgument(); + $this->addModuleArgument(InputArgument::OPTIONAL); } /** - * @param InputInterface $input + * @param InputInterface $input * @param OutputInterface $output + * * @return void + * @throws \Bluzman\Generator\GeneratorException */ - protected function execute(InputInterface $input, OutputInterface $output) + protected function execute(InputInterface $input, OutputInterface $output) : void { + $this->write('Running generate:grid command'); try { - $this->write("Running generate:grid command"); - - $model = $input->getArgument('model'); - $this->getDefinition()->getArgument('model')->validate($model); - - if (!$this->getApplication()->isModelExists($model)) { - throw new InputException( - "Model $model is not exist, " . - "run command bluzman generate:model $model before" - ); - } - - if ($module = $input->getArgument('module')) { - $this->getDefinition()->getArgument('module')->validate($module); - - if (!$this->getApplication()->isModuleExists($module)) { - throw new InputException( - "Module $module is not exist, " . - "run command bluzman generate:module $module before" - ); - } - } + // validate + $this->validateModelArgument(); + $this->validateModuleArgument(); // generate directories and files $this->generate($input, $output); - // verify it + // verify files $this->verify($input, $output); - - $this->write("GRID for {$model} has been successfully created."); - - if ($module = $input->getArgument('module')) { - $this->write( - "Open page /acl in your browser and set permissions for {$module}" - ); - } } catch (InputException $e) { $this->error("ERROR: {$e->getMessage()}"); } @@ -95,54 +69,35 @@ protected function execute(InputInterface $input, OutputInterface $output) * @return void * @throws InputException */ - protected function generate(InputInterface $input, OutputInterface $output) + protected function generate(InputInterface $input, OutputInterface $output) : void { $model = ucfirst($input->getArgument('model')); + $module = $input->getArgument('module'); - // generate CRUD - $crudFile = $this->getApplication()->getModelPath($model) . DS . 'Grid.php'; + // template data + $data = [ + 'model' => $model, + 'module' => $module + ]; - if (file_exists($crudFile)) { - $this->comment("Crud file $model/Grid.php already exists"); - } else { - $template = $this->getTemplate('GridTemplate'); - $template->setFilePath($crudFile); - $template->setTemplateData([ - 'model' => $model - ]); + // generate GRID class + $gridFile = $this->getApplication()->getModelPath($model) . DS . 'Grid.php'; + $this->generateFile('GridTemplate', $gridFile, $data); - $generator = new Generator\Generator($template); - $generator->make(); - } + if ($module) { + $this->write(" |> Generate Grid controller $module/controllers/grid.php"); - if ($module = $input->getArgument('module')) { - $this->write("Generate $module/controllers/grid.php"); + // controller and view generators required the `Model\Table` class + // validator is present on previous step + $data['columns'] = $this->getTableInstance($model)::getMeta(); $controllerFile = $this->getControllerPath($module, 'grid'); - if (file_exists($controllerFile)) { - $this->comment("Controller file $module/grid already exists"); - } else { - $template = new Generator\Template\GridControllerTemplate(); - $template->setFilePath($controllerFile); - $template->setTemplateData(['model' => $model, 'module' => $module]); - - $generator = new Generator\Generator($template); - $generator->make(); - } + $this->generateFile('GridControllerTemplate', $controllerFile, $data); - $this->write("Generate $module/views/grid.phtml"); + $this->write(" |> Generate Grid view $module/views/grid.phtml"); $viewFile = $this->getViewPath($module, 'grid'); - if (file_exists($viewFile)) { - $this->comment("View file $module/grid already exists"); - } else { - $template = new Generator\Template\GridViewTemplate(); - $template->setFilePath($viewFile); - $template->setTemplateData(['model' => $model, 'module' => $module]); - - $generator = new Generator\Generator($template); - $generator->make(); - } + $this->generateFile('GridViewTemplate', $viewFile, $data); } } @@ -152,9 +107,12 @@ protected function generate(InputInterface $input, OutputInterface $output) * @return void * @throws \Bluzman\Generator\GeneratorException */ - public function verify(InputInterface $input, OutputInterface $output) + public function verify(InputInterface $input, OutputInterface $output) : void { - $modelPath = $this->getApplication()->getModelPath($input->getArgument('model')); + $model = $input->getArgument('model'); + $module = $input->getArgument('module'); + + $modelPath = $this->getApplication()->getModelPath($model); $paths = [ $modelPath . DS . 'Grid.php', @@ -165,5 +123,15 @@ public function verify(InputInterface $input, OutputInterface $output) throw new Generator\GeneratorException("File `$path` is not exists"); } } + + // notifications + $this->write(" |> GRID for {$model} has been successfully created."); + + if ($module) { + $this->write( + " |> Open page /acl in your browser ". + "and set permission Management for {$module} module" + ); + } } } diff --git a/src/Command/Generate/ModelCommand.php b/src/Command/Generate/ModelCommand.php index 2346fe3..f55b790 100644 --- a/src/Command/Generate/ModelCommand.php +++ b/src/Command/Generate/ModelCommand.php @@ -6,9 +6,6 @@ namespace Bluzman\Command\Generate; -use Bluz\Proxy\Db; -use Bluz\Validator\Validator as v; -use Bluzman\Input\InputArgument; use Bluzman\Input\InputException; use Bluzman\Generator; use Symfony\Component\Console\Input\InputInterface; @@ -38,13 +35,7 @@ protected function configure() ; $this->addModelArgument(); - - $table = new InputArgument('table', InputArgument::REQUIRED, 'Table name is required'); - $table->setValidator( - v::create()->string()->alphaNumeric('_')->noWhitespace() - ); - - $this->getDefinition()->addArgument($table); + $this->addTableArgument(); } /** @@ -52,24 +43,19 @@ protected function configure() * @param OutputInterface $output * @return void */ - protected function execute(InputInterface $input, OutputInterface $output) + protected function execute(InputInterface $input, OutputInterface $output) : void { + $this->write('Running generate:model command'); try { - $this->write('Running generate:model command'); - - $model = $input->getArgument('model'); - $this->getDefinition()->getArgument('model')->validate($model); - - $table = $input->getArgument('table'); - $this->getDefinition()->getArgument('table')->validate($table); + // validate + $this->validateModelArgument(); + $this->validateTableArgument(); // generate directories and files $this->generate($input, $output); // verify it $this->verify($input, $output); - - $this->write("Model {$model} has been successfully created."); } catch (InputException $e) { $this->error("ERROR: {$e->getMessage()}"); } @@ -81,10 +67,17 @@ protected function execute(InputInterface $input, OutputInterface $output) * @return void * @throws InputException */ - protected function generate(InputInterface $input, OutputInterface $output) + protected function generate(InputInterface $input, OutputInterface $output) : void { - $modelName = ucfirst($input->getArgument('model')); - $tableName = $input->getArgument('table'); + $model = ucfirst($input->getArgument('model')); + $table = $input->getArgument('table'); + + $data = [ + 'model' => $model, + 'table' => $table, + 'primaryKey' => $this->getPrimaryKey($table), + 'columns' => $this->getColumns($table) + ]; /* if ($this->getApplication()->isModelExists($modelName)) { @@ -100,42 +93,12 @@ protected function generate(InputInterface $input, OutputInterface $output) } */ // generate table - $tableFile = $this->getApplication()->getModelPath($modelName) . DS . 'Table.php'; - - if (file_exists($tableFile)) { - $this->comment("Table file $modelName/Table.php already exists"); - } else { - $template = $this->getTemplate('TableTemplate'); - $template->setFilePath($tableFile); - $template->setTemplateData([ - 'model' => $modelName, - 'table' => $tableName, - 'primaryKey' => $this->getPrimaryKey($tableName) - ]); - - $generator = new Generator\Generator($template); - $generator->make(); - } + $tableFile = $this->getApplication()->getModelPath($model) . DS . 'Table.php'; + $this->generateFile('TableTemplate', $tableFile, $data); // generate row - $rowFile = $this->getApplication()->getModelPath($modelName) . DS . 'Row.php'; - - if (file_exists($rowFile)) { - $this->comment("Row file $modelName/Row.php already exists"); - } else { - $template = $this->getTemplate('RowTemplate'); - $template->setFilePath($rowFile); - $template->setTemplateData( - [ - 'model' => $modelName, - 'table' => $tableName, - 'columns' => $this->getColumns($tableName) - ] - ); - - $generator = new Generator\Generator($template); - $generator->make(); - } + $rowFile = $this->getApplication()->getModelPath($model) . DS . 'Row.php'; + $this->generateFile('RowTemplate', $rowFile, $data); } /** @@ -144,9 +107,10 @@ protected function generate(InputInterface $input, OutputInterface $output) * @return void * @throws \Bluzman\Generator\GeneratorException */ - public function verify(InputInterface $input, OutputInterface $output) + public function verify(InputInterface $input, OutputInterface $output) : void { - $modelPath = $this->getApplication()->getModelPath($input->getArgument('model')); + $model = $input->getArgument('model'); + $modelPath = $this->getApplication()->getModelPath($model); $paths = [ $modelPath . DS . 'Table.php', @@ -158,46 +122,7 @@ public function verify(InputInterface $input, OutputInterface $output) throw new Generator\GeneratorException("File `$path` is not exists"); } } - } - /** - * @todo move it to DB class - * @return array - */ - protected function getPrimaryKey($table) - { - $connect = Db::getOption('connect'); - - return Db::fetchColumn( - ' - SELECT COLUMN_NAME - FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE - WHERE TABLE_SCHEMA = ? - AND TABLE_NAME = ? - AND CONSTRAINT_NAME = ? - ', - [$connect['name'], $table, 'PRIMARY'] - ); - } - - /** - * @todo move it to DB class - * @return array - */ - protected function getColumns($table) - { - $connect = Db::getOption('connect'); - - return Db::fetchAll( - ' - SELECT - COLUMN_NAME as name, - COLUMN_TYPE as type - FROM INFORMATION_SCHEMA.COLUMNS - WHERE TABLE_SCHEMA = ? - AND TABLE_NAME = ? - ', - [$connect['name'], $table] - ); + $this->write(" |> Model {$model} has been successfully created."); } } diff --git a/src/Command/Generate/ModuleCommand.php b/src/Command/Generate/ModuleCommand.php index ec9c08c..9765081 100644 --- a/src/Command/Generate/ModuleCommand.php +++ b/src/Command/Generate/ModuleCommand.php @@ -35,14 +35,6 @@ protected function configure() ; $this->addModuleArgument(); - - $controller = new InputArgument( - 'controller', - InputArgument::OPTIONAL | InputArgument::IS_ARRAY, - 'Controller name(s)' - ); - - $this->getDefinition()->addArgument($controller); } /** @@ -52,36 +44,16 @@ protected function configure() */ protected function execute(InputInterface $input, OutputInterface $output) { + $this->write('Running generate:module command'); try { - $this->write("Running generate:module command"); - - $module = $input->getArgument('module'); - - $argument = $this->getDefinition()->getArgument('module'); - $argument->validate($module); + // validate + $this->validateModuleArgument(); // create main folder and subfolders $this->generate($input, $output); // verify it $this->verify($input, $output); - - $this->write("Module $module has been successfully created."); - - // create controllers - $controllers = $input->getArgument('controller') ?? []; - - $command = $this->getApplication()->find('generate:controller'); - - foreach ($controllers as $controller) { - $arguments = [ - 'command' => 'generate:controller', - 'module' => $module, - 'controller' => $controller - ]; - $greetInput = new ArrayInput($arguments); - $command->run($greetInput, $output); - } } catch (\Exception $e) { $this->error("ERROR: {$e->getMessage()}"); } @@ -94,8 +66,9 @@ protected function execute(InputInterface $input, OutputInterface $output) */ protected function generate(InputInterface $input, OutputInterface $output) { - $this->addSubFolders( - $this->getApplication()->getModulePath($input->getArgument('module')), + $path = $this->getApplication()->getModulePath($input->getArgument('module')); + $this->createSubFolders( + $path, [ 'controllers', 'views' @@ -107,7 +80,7 @@ protected function generate(InputInterface $input, OutputInterface $output) * @param string $path * @param string[] $subFolders */ - protected function addSubFolders($path, array $subFolders = []) + protected function createSubFolders($path, array $subFolders = []) { if (!$this->getFs()->exists($path)) { $this->getFs()->mkdir($path); @@ -116,7 +89,7 @@ protected function addSubFolders($path, array $subFolders = []) foreach ($subFolders as $subFolderName) { $subFolderPath = $path . DIRECTORY_SEPARATOR . $subFolderName; if ($this->getFs()->exists($subFolderPath)) { - $this->comment("Directory $subFolderPath already exists"); + $this->comment(" |> Directory $subFolderPath already exists"); } else { $this->getFs()->mkdir($subFolderPath, 0755); $this->getFs()->touch([$subFolderPath . DIRECTORY_SEPARATOR . '.keep']); @@ -130,9 +103,10 @@ protected function addSubFolders($path, array $subFolders = []) * @return void * @throws GeneratorException */ - public function verify(InputInterface $input, OutputInterface $output) + public function verify(InputInterface $input, OutputInterface $output) : void { - $modulePath = $this->getApplication()->getModulePath($input->getArgument('module')); + $module = $input->getArgument('module'); + $modulePath = $this->getApplication()->getModulePath($module); $paths = [ $modulePath, @@ -145,5 +119,7 @@ public function verify(InputInterface $input, OutputInterface $output) throw new GeneratorException("Directory `$path` is not exists"); } } + + $this->write(" |> Module $module has been successfully created."); } } diff --git a/src/Command/Generate/RestCommand.php b/src/Command/Generate/RestCommand.php index cea0da5..6c9cc9c 100644 --- a/src/Command/Generate/RestCommand.php +++ b/src/Command/Generate/RestCommand.php @@ -38,51 +38,21 @@ protected function configure() } /** - * @param InputInterface $input - * @param OutputInterface $output - * @return void + * {@inheritdoc} */ - protected function execute(InputInterface $input, OutputInterface $output) + protected function execute(InputInterface $input, OutputInterface $output) : void { + $this->write('Running generate:rest command'); try { - $this->write('Running generate:rest command'); - - $model = $input->getArgument('model'); - $this->getDefinition()->getArgument('model')->validate($model); - - if (!$this->getApplication()->isModelExists($model)) { - throw new InputException( - "Model $model is not exist, " . - "run command bluzman generate:model $model before" - ); - } - - $crudPath = $this->getApplication()->getModelPath($model) . DS . 'Crud.php'; - if (!is_file($crudPath)) { - throw new InputException( - "CRUD for $model is not exist, " . - "run command bluzman generate:crud $model before" - ); - } - - $module = $input->getArgument('module'); - $this->getDefinition()->getArgument('module')->validate($module); - - if (!$this->getApplication()->isModuleExists($module)) { - throw new InputException( - "Module $module is not exist, " . - "run command bluzman generate:module $module before" - ); - } + // validate + $this->validateModelArgument(); + $this->validateModuleArgument(); // generate directories and files $this->generate($input, $output); // verify it $this->verify($input, $output); - - $this->write("REST for {$model} has been successfully created."); - $this->write("Open page /acl in your browser and set permissions for {$module}"); } catch (InputException $e) { $this->error("ERROR: {$e->getMessage()}"); } @@ -94,24 +64,27 @@ protected function execute(InputInterface $input, OutputInterface $output) * @return void * @throws InputException */ - protected function generate(InputInterface $input, OutputInterface $output) + protected function generate(InputInterface $input, OutputInterface $output) : void { $model = ucfirst($input->getArgument('model')); $module = $input->getArgument('module'); + $data = [ + 'model' => $model, + 'module' => $module + ]; + + $crudPath = $this->getApplication()->getModelPath($model) . DS . 'Crud.php'; + if (!is_file($crudPath)) { + throw new InputException( + "CRUD for $model is not exist, " . + "run command bluzman generate:crud $model before" + ); + } - $this->write("Generate $module/controllers/rest.php"); + $this->write(" |> Generate $module/controllers/rest.php"); $controllerFile = $this->getControllerPath($module, 'rest'); - if (file_exists($controllerFile)) { - $this->comment("Controller file $module/crud already exists"); - } else { - $template = $this->getTemplate('RestTemplate'); - $template->setFilePath($controllerFile); - $template->setTemplateData(['model' => $model]); - - $generator = new Generator\Generator($template); - $generator->make(); - } + $this->generateFile('RestTemplate', $controllerFile, $data); } /** @@ -120,9 +93,12 @@ protected function generate(InputInterface $input, OutputInterface $output) * @return void * @throws \Bluzman\Generator\GeneratorException */ - public function verify(InputInterface $input, OutputInterface $output) + public function verify(InputInterface $input, OutputInterface $output) : void { - $modulePath = $this->getApplication()->getModulePath($input->getArgument('module')); + $model = $input->getArgument('model'); + $module = $input->getArgument('module'); + + $modulePath = $this->getApplication()->getModulePath($module); $paths = [ $modulePath . DS . 'controllers' . DS . 'rest.php', @@ -133,5 +109,11 @@ public function verify(InputInterface $input, OutputInterface $output) throw new Generator\GeneratorException("File `$path` is not exists"); } } + + $this->write(" |> REST for {$model} has been successfully created."); + $this->write( + " |> Open page /acl in your browser ". + "and set permissions Management for {$module} module" + ); } } diff --git a/src/Command/Generate/ScaffoldCommand.php b/src/Command/Generate/ScaffoldCommand.php new file mode 100644 index 0000000..abcbb9b --- /dev/null +++ b/src/Command/Generate/ScaffoldCommand.php @@ -0,0 +1,173 @@ +setName('generate:scaffold') + // the short description shown while running "php bin/bluzman list" + ->setDescription('Generate a new model and module with crud and grid') + // the full command description shown when running the command with + // the "--help" option + ->setHelp('This command allows you to generate a scaffolding') + ; + + $this->addModelArgument(); + $this->addTableArgument(); + $this->addModuleArgument(); + } + + /** + * {@inheritdoc} + */ + protected function execute(InputInterface $input, OutputInterface $output) : void + { + $this->write('Running generate:scaffold command'); + try { + // generate + $this->runGenerateModel(); + $this->runGenerateModule(); + $this->runGenerateCrud(); + $this->runGenerateGrid(); + + // verify it + $this->verify($input, $output); + + } catch (\Exception $e) { + $this->error("ERROR: {$e->getMessage()}"); + } + } + + /** + * Generate Model + * + * @return void + */ + protected function runGenerateModel() : void + { + $command = $this->getApplication()->find('generate:model'); + + $arguments = [ + 'command' => 'generate:model', + 'model' => $this->getInput()->getArgument('model'), + 'table' => $this->getInput()->getArgument('table') + ]; + $command->run( + new ArrayInput($arguments), + $this->getOutput() + ); + } + + /** + * Generate Module + * + * @return void + */ + protected function runGenerateModule() : void + { + $command = $this->getApplication()->find('generate:module'); + + $arguments = [ + 'command' => 'generate:module', + 'module' => $this->getInput()->getArgument('module') + ]; + $command->run( + new ArrayInput($arguments), + $this->getOutput() + ); + } + + /** + * Generate Crud + * + * @return void + */ + protected function runGenerateCrud() : void + { + $command = $this->getApplication()->find('generate:crud'); + + $arguments = [ + 'command' => 'generate:crud', + 'model' => $this->getInput()->getArgument('model'), + 'module' => $this->getInput()->getArgument('module') + ]; + $command->run( + new ArrayInput($arguments), + $this->getOutput() + ); + } + + /** + * Generate Grid + * + * @return void + */ + protected function runGenerateGrid() : void + { + $command = $this->getApplication()->find('generate:grid'); + + $arguments = [ + 'command' => 'generate:grid', + 'model' => $this->getInput()->getArgument('model'), + 'module' => $this->getInput()->getArgument('module') + ]; + $command->run( + new ArrayInput($arguments), + $this->getOutput() + ); + } + + /** + * {@inheritdoc} + * + * @throws GeneratorException + */ + public function verify(InputInterface $input, OutputInterface $output) : void + { + $model = $input->getArgument('model'); + $module = $input->getArgument('module'); + + $modelPath = $this->getApplication()->getModelPath($model); + $modulePath = $this->getApplication()->getModulePath($module); + + $paths = [ + $modelPath . DS . 'Crud.php', + $modelPath . DS . 'Grid.php', + $modelPath . DS . 'Row.php', + $modelPath . DS . 'Table.php', + $modulePath . DS . 'controllers' . DS . 'crud.php', + $modulePath . DS . 'controllers' . DS . 'grid.php', + $modulePath . DS . 'views' . DS . 'crud.phtml', + $modulePath . DS . 'views' . DS . 'grid.phtml', + ]; + + foreach ($paths as $path) { + if (!$this->getFs()->exists($path)) { + throw new GeneratorException("File `$path` is not exists"); + } + } + + $this->write("Scaffolding for {$model} has been successfully created."); + } +} diff --git a/src/Generator/Generator.php b/src/Generator/Generator.php index 34a2c66..6135920 100644 --- a/src/Generator/Generator.php +++ b/src/Generator/Generator.php @@ -88,6 +88,7 @@ public function __construct(AbstractTemplate $template) $this->setTemplate($template); $this->setAbsolutePath(__DIR__); $this->setFs(new Filesystem()); + } public function getCompiledTemplate() @@ -105,6 +106,7 @@ public function getCompiledTemplate() */ public function make() { +// var_dump($this->getTemplate()->getTemplateData()); $this->getFs()->dumpFile( $this->getTemplate()->getFilePath(), $this->getCompiledTemplate() diff --git a/src/Generator/Template/AbstractTemplate.php b/src/Generator/Template/AbstractTemplate.php index f6ffbe4..0c37eee 100644 --- a/src/Generator/Template/AbstractTemplate.php +++ b/src/Generator/Template/AbstractTemplate.php @@ -11,6 +11,7 @@ * * @package Bluzman\Generator\Template * + * @todo Migrate to Bluz\View\View * @author Pavel Machekhin * @created 2013-03-28 16:36 */ diff --git a/src/Generator/views/models/grid.phtml b/src/Generator/views/models/grid.phtml index a32b3d1..caee1e6 100644 --- a/src/Generator/views/models/grid.phtml +++ b/src/Generator/views/models/grid.phtml @@ -33,6 +33,17 @@ class Grid extends \Bluz\Grid\Grid $this->setAdapter($adapter); $this->setDefaultLimit(25); - $this->setAllowOrders([]); + + $this->setAllowFilters([ + $meta) : ?> + '', + + ]); + $this->setAllowOrders([ + $meta) : ?> + '', + + ]); + } } diff --git a/src/Generator/views/views/crud.phtml b/src/Generator/views/views/crud.phtml index 038176e..1fb0dd6 100644 --- a/src/Generator/views/views/crud.phtml +++ b/src/Generator/views/views/crud.phtml @@ -5,8 +5,38 @@ $url = $this->url('', 'crud'); \n"?>
- - + + +
// need for plain request diff --git a/src/Generator/views/views/grid.phtml b/src/Generator/views/views/grid.phtml index 60ae79e..3ef2fac 100644 --- a/src/Generator/views/views/grid.phtml +++ b/src/Generator/views/views/grid.phtml @@ -30,8 +30,9 @@ $uid = $grid->getUid(); - - + $meta) : $columnTitle = ucfirst($column); ?> + + @@ -39,15 +40,17 @@ $uid = $grid->getUid(); getData() as $row) : ?>'?> - - + $meta) : ?> + + +
order('id') ?: '#' ?>"?>">"?>Header exampleorder('{$column}') ?: '#' ?>"?>">"?>
"?>Content example"?> - url('$module', 'crud', ['id' => \$row['id']]) ?>"?>" class="btn btn-primary" + url('$module', 'crud', ['id' => \$row['$first']]) ?>"?>" class="btn btn-primary" data-ajax-dialog data-ajax-method="get" data-modal-style="modal-lg"> - url('$module', 'crud', ['id' => \$row['id']]) ?>"?>" class="btn btn-danger" + url('$module', 'crud', ['id' => \$row['$first']]) ?>"?>" class="btn btn-danger" data-confirm data-ajax data-ajax-method="delete"> diff --git a/tests/Command/Generate/GridCommandTest.php b/tests/Command/Generate/GridCommandTest.php index d63dea9..ad1030c 100644 --- a/tests/Command/Generate/GridCommandTest.php +++ b/tests/Command/Generate/GridCommandTest.php @@ -24,7 +24,14 @@ class GridCommandTest extends AbstractCommandTest */ protected $modelPath; - protected $dataForTemplate = ['author' => 'test', 'date' => '00-00-00 00:00:00']; + protected $dataForTemplate = [ + 'author' => 'test', + 'date' => '00-00-00 00:00:00', + 'columns' => [ + 'id' => ['type' => 'int'], + 'name' => ['type' => 'varchar'], + ] + ]; public function setUp() { @@ -63,6 +70,7 @@ public function testCorrectWorkflow() ->andReturn($template) ->getMock(); + $this->getApplication()->addCommands([$command]); $commandTester = new CommandTester($command); diff --git a/tests/Generator/samples/grid.html b/tests/Generator/samples/grid.html index 5650938..c255a53 100644 --- a/tests/Generator/samples/grid.html +++ b/tests/Generator/samples/grid.html @@ -32,6 +32,13 @@ $this->setAdapter($adapter); $this->setDefaultLimit(25); - $this->setAllowOrders([]); + $this->setAllowFilters([ + 'id', + 'name', + ]); + $this->setAllowOrders([ + 'id', + 'name', + ]); } } From b5d5b5d462215bce07fbcb7aa30988bd72ff3b0c Mon Sep 17 00:00:00 2001 From: Anton Shevchuk Date: Tue, 21 Nov 2017 14:42:10 +0200 Subject: [PATCH 06/11] Removed unused code --- src/Command/Generate/ControllerCommand.php | 1 - src/Command/Generate/ModelCommand.php | 1 - src/Command/Generate/ModuleCommand.php | 2 - src/Input/InputArgument.php | 1 - src/Input/InputOption.php | 1 - src/Input/InputValidationTrait.php | 54 ---------------------- 6 files changed, 60 deletions(-) delete mode 100644 src/Input/InputValidationTrait.php diff --git a/src/Command/Generate/ControllerCommand.php b/src/Command/Generate/ControllerCommand.php index a16d786..1063013 100644 --- a/src/Command/Generate/ControllerCommand.php +++ b/src/Command/Generate/ControllerCommand.php @@ -7,7 +7,6 @@ namespace Bluzman\Command\Generate; use Bluzman\Generator; -use Symfony\Component\Console\Input\ArrayInput; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; diff --git a/src/Command/Generate/ModelCommand.php b/src/Command/Generate/ModelCommand.php index f55b790..3350c90 100644 --- a/src/Command/Generate/ModelCommand.php +++ b/src/Command/Generate/ModelCommand.php @@ -10,7 +10,6 @@ use Bluzman\Generator; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; -use Symfony\Component\Console\Question\ConfirmationQuestion; /** * ModelCommand diff --git a/src/Command/Generate/ModuleCommand.php b/src/Command/Generate/ModuleCommand.php index 9765081..255170d 100644 --- a/src/Command/Generate/ModuleCommand.php +++ b/src/Command/Generate/ModuleCommand.php @@ -7,8 +7,6 @@ namespace Bluzman\Command\Generate; use Bluzman\Generator\GeneratorException; -use Bluzman\Input\InputArgument; -use Symfony\Component\Console\Input\ArrayInput; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; diff --git a/src/Input/InputArgument.php b/src/Input/InputArgument.php index 5ee8617..dbcddc2 100644 --- a/src/Input/InputArgument.php +++ b/src/Input/InputArgument.php @@ -11,5 +11,4 @@ */ class InputArgument extends \Symfony\Component\Console\Input\InputArgument { - use InputValidationTrait; } diff --git a/src/Input/InputOption.php b/src/Input/InputOption.php index dd80f18..6c00870 100644 --- a/src/Input/InputOption.php +++ b/src/Input/InputOption.php @@ -11,5 +11,4 @@ */ class InputOption extends \Symfony\Component\Console\Input\InputOption { - use InputValidationTrait; } diff --git a/src/Input/InputValidationTrait.php b/src/Input/InputValidationTrait.php deleted file mode 100644 index ac0aa6e..0000000 --- a/src/Input/InputValidationTrait.php +++ /dev/null @@ -1,54 +0,0 @@ -validator = $validator; - } - - /** - * @return ValidatorChain - */ - public function getValidator() - { - return $this->validator; - } - - /** - * @param $value - * @return bool - * @throws \Exception - */ - public function validate($value) - { - if (!$this->getValidator()) { - return true; - } - - if ($this->getValidator()->validate($value)) { - return true; - } - throw new InputException($this->getValidator()->getError()); - } -} From a879961e499645e9d98498e63b3d4932c30ba840 Mon Sep 17 00:00:00 2001 From: Anton Shevchuk Date: Tue, 21 Nov 2017 15:46:20 +0200 Subject: [PATCH 07/11] Fixed codestyle --- src/Command/Db/AbstractDbCommand.php | 4 +- .../Generate/AbstractGenerateCommand.php | 3 +- src/Command/Generate/CrudCommand.php | 2 +- src/Command/Generate/GridCommand.php | 2 +- src/Command/Generate/RestCommand.php | 2 +- src/Command/Generate/ScaffoldCommand.php | 1 - src/Generator/Generator.php | 40 +++++++++---------- 7 files changed, 25 insertions(+), 29 deletions(-) diff --git a/src/Command/Db/AbstractDbCommand.php b/src/Command/Db/AbstractDbCommand.php index f701cc9..31688ca 100644 --- a/src/Command/Db/AbstractDbCommand.php +++ b/src/Command/Db/AbstractDbCommand.php @@ -47,8 +47,8 @@ protected function execute(InputInterface $input, OutputInterface $output) // write database name to console // to avoid any mistakes $config = include $phinxArguments['--configuration']; - $this->write('host '. $config['environments']['default']['host']); - $this->write('name '. $config['environments']['default']['name']); + $this->write('host ' . $config['environments']['default']['host']); + $this->write('name ' . $config['environments']['default']['name']); if ($command->getDefinition()->hasOption('environment')) { $phinxArguments['--environment'] = 'default'; diff --git a/src/Command/Generate/AbstractGenerateCommand.php b/src/Command/Generate/AbstractGenerateCommand.php index dd63329..a15a662 100644 --- a/src/Command/Generate/AbstractGenerateCommand.php +++ b/src/Command/Generate/AbstractGenerateCommand.php @@ -112,12 +112,11 @@ protected function getTableInstance($model) : Table ); } include_once $file; - $class = '\\Application\\'. ucfirst($model) . '\\Table'; + $class = '\\Application\\' . ucfirst($model) . '\\Table'; if (!class_exists($class)) { throw new GeneratorException("Bluzman can't found `Table` class for model `$model`"); } return $class::getInstance(); - } /** diff --git a/src/Command/Generate/CrudCommand.php b/src/Command/Generate/CrudCommand.php index 4dc344c..815d72b 100644 --- a/src/Command/Generate/CrudCommand.php +++ b/src/Command/Generate/CrudCommand.php @@ -132,7 +132,7 @@ public function verify(InputInterface $input, OutputInterface $output) : void $this->write(" |> CRUD for {$model} has been successfully created."); if ($module) { $this->write( - " |> Open page /acl in your browser ". + " |> Open page /acl in your browser " . "and set permission Management for {$module} module" ); } diff --git a/src/Command/Generate/GridCommand.php b/src/Command/Generate/GridCommand.php index c2b6f2e..cc3596f 100644 --- a/src/Command/Generate/GridCommand.php +++ b/src/Command/Generate/GridCommand.php @@ -129,7 +129,7 @@ public function verify(InputInterface $input, OutputInterface $output) : void if ($module) { $this->write( - " |> Open page /acl in your browser ". + " |> Open page /acl in your browser " . "and set permission Management for {$module} module" ); } diff --git a/src/Command/Generate/RestCommand.php b/src/Command/Generate/RestCommand.php index 6c9cc9c..4a374d4 100644 --- a/src/Command/Generate/RestCommand.php +++ b/src/Command/Generate/RestCommand.php @@ -112,7 +112,7 @@ public function verify(InputInterface $input, OutputInterface $output) : void $this->write(" |> REST for {$model} has been successfully created."); $this->write( - " |> Open page /acl in your browser ". + " |> Open page /acl in your browser " . "and set permissions Management for {$module} module" ); } diff --git a/src/Command/Generate/ScaffoldCommand.php b/src/Command/Generate/ScaffoldCommand.php index abcbb9b..1eb07f4 100644 --- a/src/Command/Generate/ScaffoldCommand.php +++ b/src/Command/Generate/ScaffoldCommand.php @@ -53,7 +53,6 @@ protected function execute(InputInterface $input, OutputInterface $output) : voi // verify it $this->verify($input, $output); - } catch (\Exception $e) { $this->error("ERROR: {$e->getMessage()}"); } diff --git a/src/Generator/Generator.php b/src/Generator/Generator.php index 6135920..05439b6 100644 --- a/src/Generator/Generator.php +++ b/src/Generator/Generator.php @@ -35,6 +35,13 @@ class Generator */ protected $fs; + public function __construct(AbstractTemplate $template) + { + $this->setTemplate($template); + $this->setAbsolutePath(__DIR__); + $this->setFs(new Filesystem()); + } + /** * @return AbstractTemplate */ @@ -46,7 +53,7 @@ public function getTemplate() /** * @param AbstractTemplate $template */ - public function setTemplate($template) + public function setTemplate($template) : void { $this->template = $template; } @@ -62,7 +69,7 @@ public function getAbsolutePath() /** * @param string $absolutePath */ - public function setAbsolutePath($absolutePath) + public function setAbsolutePath($absolutePath) : void { $this->absolutePath = $absolutePath; } @@ -78,20 +85,23 @@ public function getFs() /** * @param Filesystem $fs */ - public function setFs($fs) + public function setFs($fs) : void { $this->fs = $fs; } - public function __construct(AbstractTemplate $template) + /** + * @internal param $path + */ + public function make() : void { - $this->setTemplate($template); - $this->setAbsolutePath(__DIR__); - $this->setFs(new Filesystem()); - + $this->getFs()->dumpFile( + $this->getTemplate()->getFilePath(), + $this->getCompiledTemplate() + ); } - public function getCompiledTemplate() + protected function getCompiledTemplate() { $view = new View(); $view->setPath($this->getAbsolutePath()); @@ -100,16 +110,4 @@ public function getCompiledTemplate() return $view->render(); } - - /** - * @internal param $path - */ - public function make() - { -// var_dump($this->getTemplate()->getTemplateData()); - $this->getFs()->dumpFile( - $this->getTemplate()->getFilePath(), - $this->getCompiledTemplate() - ); - } } From 2486efbd73c438c687f8fb4f0d0c2c1fda4f5be8 Mon Sep 17 00:00:00 2001 From: Anton Shevchuk Date: Tue, 21 Nov 2017 15:54:27 +0200 Subject: [PATCH 08/11] Fixed protection level --- src/Generator/Generator.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Generator/Generator.php b/src/Generator/Generator.php index 05439b6..9b6d6bd 100644 --- a/src/Generator/Generator.php +++ b/src/Generator/Generator.php @@ -101,7 +101,7 @@ public function make() : void ); } - protected function getCompiledTemplate() + public function getCompiledTemplate() { $view = new View(); $view->setPath($this->getAbsolutePath()); From ebf6e3464b988140eda27653fb1bcaaa4ac46ec8 Mon Sep 17 00:00:00 2001 From: Anton Shevchuk Date: Tue, 21 Nov 2017 18:59:59 +0200 Subject: [PATCH 09/11] Small codestyle fixes --- src/Command/Generate/CrudCommand.php | 3 ++- src/Command/Generate/GridCommand.php | 12 ++++++++---- src/Generator/views/models/table.phtml | 7 ++++--- src/Generator/views/views/crud.phtml | 8 ++++---- src/Generator/views/views/grid.phtml | 14 +++++++------- tests/Generator/samples/table.html | 5 +++-- 6 files changed, 28 insertions(+), 21 deletions(-) diff --git a/src/Command/Generate/CrudCommand.php b/src/Command/Generate/CrudCommand.php index 815d72b..1cda8dd 100644 --- a/src/Command/Generate/CrudCommand.php +++ b/src/Command/Generate/CrudCommand.php @@ -79,7 +79,8 @@ protected function generate(InputInterface $input, OutputInterface $output) : vo 'module' => $module ]; - // generate CRUD + // generate CRUD class + $this->write(" |> Generate CRUD class $model\\Crud"); $crudFile = $this->getApplication()->getModelPath($model) . DS . 'Crud.php'; $this->generateFile('CrudTemplate', $crudFile, $data); diff --git a/src/Command/Generate/GridCommand.php b/src/Command/Generate/GridCommand.php index cc3596f..c992145 100644 --- a/src/Command/Generate/GridCommand.php +++ b/src/Command/Generate/GridCommand.php @@ -80,17 +80,21 @@ protected function generate(InputInterface $input, OutputInterface $output) : vo 'module' => $module ]; + if ($module) { + // controller and view generators required the `Model\Table` class + // validator is present on previous step + $data['columns'] = $this->getTableInstance($model)::getMeta(); + } + // generate GRID class + $this->write(" |> Generate Grid class $model\\Grid"); + $gridFile = $this->getApplication()->getModelPath($model) . DS . 'Grid.php'; $this->generateFile('GridTemplate', $gridFile, $data); if ($module) { $this->write(" |> Generate Grid controller $module/controllers/grid.php"); - // controller and view generators required the `Model\Table` class - // validator is present on previous step - $data['columns'] = $this->getTableInstance($model)::getMeta(); - $controllerFile = $this->getControllerPath($module, 'grid'); $this->generateFile('GridControllerTemplate', $controllerFile, $data); diff --git a/src/Generator/views/models/table.phtml b/src/Generator/views/models/table.phtml index 7ae59c5..20f4d98 100644 --- a/src/Generator/views/models/table.phtml +++ b/src/Generator/views/models/table.phtml @@ -1,12 +1,10 @@ - + /** * @namespace */ namespace Application\; -use \Application\\Row; - /** * Class Table for `` * @@ -22,6 +20,9 @@ class Table extends \Bluz\Db\Table */ protected $name = ''; + /** + * @var string + */ protected $rowClass = Row::class; /** diff --git a/src/Generator/views/views/crud.phtml b/src/Generator/views/views/crud.phtml index 1fb0dd6..e0b7a31 100644 --- a/src/Generator/views/views/crud.phtml +++ b/src/Generator/views/views/crud.phtml @@ -14,19 +14,19 @@ $url = $this->url('', 'crud');