diff --git a/config/modules.yml b/config/modules.yml new file mode 100644 index 00000000..fc6c8d70 --- /dev/null +++ b/config/modules.yml @@ -0,0 +1,77 @@ +parameters: + templates_module_generator_base_path: /fop_console/src/Resources/templates/generate_module_command/module + +services: + fop.console.module_generator.file_generator: + abstract: true + class: FOP\Console\Generator\FileGenerator + arguments: + - '@twig' + +# configuration generation + fop.console.module_generator.configuration.yaml_file: + public: true + parent: fop.console.module_generator.file_generator + class: FOP\Console\Generator\YamlFileGenerator + calls: + - [ setTemplateName, [ 'configuration.yml.twig' ] ] + - [ setTemplatesBaseFolder,[ '%templates_module_generator_base_path%/configuration' ] ] + - [ setFileNameModule, ['/admin/configuration.yml']] + tags: + - { name: fop.console.module_generator.configuration_generator } + + fop.console.module_generator.configuration.form_class_file: + public: true + parent: fop.console.module_generator.file_generator + class: FOP\Console\Generator\ClassFileGenerator + calls: + - [ setTemplateName, [ 'configuration_form.php.twig' ] ] + - [ setTemplatesBaseFolder,[ '%templates_module_generator_base_path%/configuration' ] ] + - [ setClassFolderName,[ 'src/Form/' ] ] + - [ setFileNameModule,[ 'ConfigurationForm.php' ] ] + tags: + - { name: fop.console.module_generator.configuration_generator } + + fop.console.module_generator.configuration.text_data_class_file: + public: true + parent: fop.console.module_generator.file_generator + class: FOP\Console\Generator\ClassFileGenerator + calls: + - [ setTemplateName, [ 'text_data_configuration.php.twig' ] ] + - [ setTemplatesBaseFolder,[ '%templates_module_generator_base_path%/configuration' ] ] + - [ setClassFolderName,[ 'src/Configuration/' ] ] + - [ setFileNameModule,[ 'TextDataConfiguration.php' ] ] + tags: + - { name: fop.console.module_generator.configuration_generator } + + fop.console.module_generator.configuration.text_data_provider_class_file: + public: true + parent: fop.console.module_generator.file_generator + class: FOP\Console\Generator\ClassFileGenerator + calls: + - [ setTemplateName, [ 'text_data_provider.php.twig' ] ] + - [ setTemplatesBaseFolder,[ '%templates_module_generator_base_path%/configuration' ] ] + - [ setClassFolderName,[ 'src/Provider/' ] ] + - [ setFileNameModule,[ 'ConfigurationTextFormDataProvider.php' ] ] + + fop.console.module_generator.configuration.controller_twig_file: + public: true + parent: fop.console.module_generator.file_generator + class: FOP\Console\Generator\TwigFileGenerator + calls: + - [ setTemplateName, [ 'template_configuration.twig' ] ] + - [ setTemplatesBaseFolder,[ '%templates_module_generator_base_path%/configuration' ] ] + - [ setFileNameModule,[ 'configuration_view.html.twig' ] ] + - [ setModuleFolder,[ 'views/templates/admin/controller' ] ] + tags: + - { name: fop.console.module_generator.configuration_generator } + + fop.console.module_generator.configuration.display_code_controller: + public: true + parent: fop.console.module_generator.file_generator + class: FOP\Console\Generator\CodeDisplayGenerator + calls: + - [ setTemplateName, [ 'code_controller.php.twig' ] ] + - [ setTemplatesBaseFolder,[ '%templates_module_generator_base_path%/configuration' ] ] + tags: + - { name: fop.console.module_generator.configuration_generator } diff --git a/config/services.yml b/config/services.yml index 403e9fc1..1ca9f5b9 100644 --- a/config/services.yml +++ b/config/services.yml @@ -115,6 +115,8 @@ services: fop.console.module.generate.command: class: FOP\Console\Commands\Module\ModuleGenerate + calls: + - [ setGeneratorConfigurationServices, [ !tagged_iterator fop.console.module_generator.configuration_generator ] ] tags: [ console.command ] fop.console.override.make.command: @@ -144,3 +146,4 @@ services: imports: - { resource: overriders.yml } + - { resource: modules.yml } diff --git a/src/Commands/Module/ModuleGenerate.php b/src/Commands/Module/ModuleGenerate.php index 1f179634..775da98a 100644 --- a/src/Commands/Module/ModuleGenerate.php +++ b/src/Commands/Module/ModuleGenerate.php @@ -21,15 +21,18 @@ namespace FOP\Console\Commands\Module; use FOP\Console\Command; +use FOP\Console\Generator\TwigVariablesDTO; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Question\Question; use Symfony\Component\Filesystem\Filesystem; +use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter; class ModuleGenerate extends Command { protected $filesystem; protected $baseControllerFolder; + protected $baseConfigFolder; protected $baseTestFolder; protected $baseFolder; protected $moduleName; @@ -40,15 +43,27 @@ class ModuleGenerate extends Command protected $testGeneration = false; private $twig; + /** + * @var TwigVariablesDTO|null + */ + private $configGeneration = null; + private array $generatorConfigurationServices = []; + public function __construct() { parent::__construct(); $this->filesystem = new Filesystem(); $this->baseFolder = '/fop_console/src/Resources/templates/generate_module_command/module'; - $this->baseControllerFolder = $this->baseFolder . '/controller'; - $this->baseViewFolder = $this->baseFolder . '/views'; - $this->baseTestFolder = $this->baseFolder . '/test'; + $this->baseControllerFolder = $this->baseFolder.'/controller'; + $this->baseViewFolder = $this->baseFolder.'/views'; + $this->baseTestFolder = $this->baseFolder.'/test'; + $this->baseConfigFolder = $this->baseFolder.'/configuration'; + } + + public function setGeneratorConfigurationServices(iterable $services) + { + $this->generatorConfigurationServices = iterator_to_array($services); } /** @@ -57,14 +72,115 @@ public function __construct() protected function configure(): void { $this->setName('fop:module:generate') - ->setDescription('Scaffold new PrestaShop module') - ; + ->setDescription('Scaffold new PrestaShop module'); + } + + /** + * @param InputInterface $input + * @param OutputInterface $output + * + * @return int|void|null + */ + protected function execute(InputInterface $input, OutputInterface $output) + { + $this->twig = $this->getContainer() + ->get('twig'); + + if ($this->isNewModule === true) { + $output->writeln('create module folder'); + $this->createModule($this->moduleName); + + $output->writeln('create main file'); + $this->createMain($this->moduleName); + + $output->writeln('create composer.json'); + $this->createComposerJson($this->moduleName, $this->moduleNamespace); + + $output->writeln('create config'); + $this->createConfig($this->moduleName); + + $output->writeln('create routes'); + $this->createRoute($this->moduleName, $this->moduleNamespace); + + $output->writeln('create configuration controller'); + $this->createController($this->moduleName, $this->moduleNamespace); + + $output->writeln('create form '); + $this->createControllerForm($this->moduleName, $this->moduleNamespace); + + $output->writeln('create configuration controller template'); + $this->createControllerTemplate( + $this->moduleName, + $this->moduleNamespace + ); + + if ($this->testGeneration === true) { + $output->writeln('create test folder'); + $this->createTest($this->moduleName); + } + + $output->writeln('....'); + + $output->writeln( + 'OK! Now you can edit composer.json and run "composer install" inside your new module.' + ); + $output->writeln(''); + } else { + if ($this->frontControllerName) { + $output->writeln('create front controller file'); + $this->createFrontController( + $this->moduleName, + $this->frontControllerName + ); + + $output->writeln('create front javascript file'); + $this->createFrontControllerJavascript( + $this->moduleName, + $this->frontControllerName + ); + } + if ($this->configGeneration) { + $output->writeln('create configuration file'); + $this->createConfiguration(); + } + } + } + + protected function createModule($modulename) + { + $this->filesystem->mkdir($this->getModuleDirectory($modulename)); + } + + /** + * @param string $modulename + * + * @return string + */ + protected function getModuleDirectory($modulename): string + { + return _PS_MODULE_DIR_.$modulename; + } + + protected function createMain($modulename) + { + $controller_code = $this->twig->render( + '@Modules'.$this->baseFolder.DIRECTORY_SEPARATOR.'main.php.twig', + [ + 'module_name' => $modulename, + ] + ); + + $this->filesystem->dumpFile( + $this->getModuleDirectory($modulename).DIRECTORY_SEPARATOR.$modulename. + '.php', + $controller_code + ); } protected function createComposerJson($modulename, $namespace) { $composer_code = $this->twig->render( - '@Modules' . $this->baseFolder . DIRECTORY_SEPARATOR . 'composer.json.twig', + '@Modules'.$this->baseFolder.DIRECTORY_SEPARATOR.'composer.json.twig', [ 'module_name' => $modulename, 'test' => $this->testGeneration, @@ -72,7 +188,7 @@ protected function createComposerJson($modulename, $namespace) ] ); $this->filesystem->dumpFile( - $this->getModuleDirectory($modulename) . DIRECTORY_SEPARATOR . + $this->getModuleDirectory($modulename).DIRECTORY_SEPARATOR. 'composer.json', $composer_code ); @@ -81,21 +197,41 @@ protected function createComposerJson($modulename, $namespace) protected function createConfig($modulename) { $service_code = $this->twig->render( - '@Modules' . $this->baseControllerFolder . DIRECTORY_SEPARATOR . 'services.yml.twig' + '@Modules'.$this->baseControllerFolder.DIRECTORY_SEPARATOR.'services.yml.twig' ); $module_config_path = - $this->getModuleDirectory($modulename) . DIRECTORY_SEPARATOR . 'config' . - DIRECTORY_SEPARATOR . 'admin'; + $this->getModuleDirectory($modulename).DIRECTORY_SEPARATOR.'config'. + DIRECTORY_SEPARATOR.'admin'; $this->filesystem->dumpFile( - $module_config_path . DIRECTORY_SEPARATOR . 'services.yml', + $module_config_path.DIRECTORY_SEPARATOR.'services.yml', $service_code ); } + protected function createRoute($modulename, $namespace) + { + $module_route_path = + $this->getModuleDirectory($modulename).DIRECTORY_SEPARATOR.'config'; + if ($this->filesystem->exists($module_route_path) === false) { + $this->filesystem->mkdir($module_route_path); + } + $route_code = $this->twig->render( + '@Modules'.$this->baseControllerFolder.DIRECTORY_SEPARATOR.'routes.yml.twig', + [ + 'module_name' => $modulename, + 'name_space' => $namespace, + ] + ); + $this->filesystem->dumpFile( + $module_route_path.DIRECTORY_SEPARATOR.'routes.yml', + $route_code + ); + } + protected function createController($modulename, $namespace) { $controller_code = $this->twig->render( - '@Modules' . $this->baseControllerFolder . DIRECTORY_SEPARATOR . 'configuration.php.twig', + '@Modules'.$this->baseControllerFolder.DIRECTORY_SEPARATOR.'configuration.php.twig', [ 'class_name' => 'ConfigurationController', 'module_name' => $modulename, @@ -104,8 +240,8 @@ protected function createController($modulename, $namespace) ); $this->filesystem->dumpFile( - $this->getModuleDirectory($modulename) . DIRECTORY_SEPARATOR . 'src' . - DIRECTORY_SEPARATOR . 'Controller' . DIRECTORY_SEPARATOR . + $this->getModuleDirectory($modulename).DIRECTORY_SEPARATOR.'src'. + DIRECTORY_SEPARATOR.'Controller'.DIRECTORY_SEPARATOR. 'ConfigurationController.php', $controller_code ); @@ -114,7 +250,7 @@ protected function createController($modulename, $namespace) protected function createControllerForm($modulename, $namespace) { $controller_code = $this->twig->render( - '@Modules' . $this->baseControllerFolder . DIRECTORY_SEPARATOR . 'form.php.twig', + '@Modules'.$this->baseControllerFolder.DIRECTORY_SEPARATOR.'form.php.twig', [ 'class_name' => 'ConfigurationType', 'module_name' => $modulename, @@ -123,9 +259,9 @@ protected function createControllerForm($modulename, $namespace) ); $this->filesystem->dumpFile( - $this->getModuleDirectory($modulename) . DIRECTORY_SEPARATOR . 'src' . - DIRECTORY_SEPARATOR . 'Form' . DIRECTORY_SEPARATOR . 'Type' . - DIRECTORY_SEPARATOR . 'ConfigurationType.php', + $this->getModuleDirectory($modulename).DIRECTORY_SEPARATOR.'src'. + DIRECTORY_SEPARATOR.'Form'.DIRECTORY_SEPARATOR.'Type'. + DIRECTORY_SEPARATOR.'ConfigurationType.php', $controller_code ); } @@ -133,34 +269,49 @@ protected function createControllerForm($modulename, $namespace) protected function createControllerTemplate($modulename, $templatename) { $module_view_path = - $this->getModuleDirectory($modulename) . DIRECTORY_SEPARATOR . 'views' . - DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR . 'admin' . - DIRECTORY_SEPARATOR . 'controller'; + $this->getModuleDirectory($modulename).DIRECTORY_SEPARATOR.'views'. + DIRECTORY_SEPARATOR.'templates'.DIRECTORY_SEPARATOR.'admin'. + DIRECTORY_SEPARATOR.'controller'; $this->filesystem->mkdir($module_view_path); $this->filesystem->copy( - _PS_MODULE_DIR_ . $this->baseControllerFolder . DIRECTORY_SEPARATOR . + _PS_MODULE_DIR_.$this->baseControllerFolder.DIRECTORY_SEPARATOR. 'template_controller.twig', - $module_view_path . DIRECTORY_SEPARATOR . 'admin_configuration.html.twig' + $module_view_path.DIRECTORY_SEPARATOR.'admin_configuration.html.twig' + ); + } + + protected function createTest($modulename) + { + $module_dir = $this->getModuleDirectory($modulename); + $test_dir = $module_dir.DIRECTORY_SEPARATOR.'test'; + $this->filesystem->mkdir($test_dir); + $this->filesystem->copy( + _PS_MODULE_DIR_.$this->baseTestFolder.DIRECTORY_SEPARATOR.'bootstrap.php.twig', + $test_dir.DIRECTORY_SEPARATOR.'bootstrap.php' + ); + $this->filesystem->copy( + _PS_MODULE_DIR_.$this->baseTestFolder.DIRECTORY_SEPARATOR.'phpunit.xml.twig', + $module_dir.DIRECTORY_SEPARATOR.'phpunit.xml' ); } protected function createFrontController($module_name, $front_controller_name) { $front_controller_folder = - $this->getModuleDirectory($this->moduleName) . DIRECTORY_SEPARATOR . - 'controllers' . DIRECTORY_SEPARATOR . 'front'; + $this->getModuleDirectory($this->moduleName).DIRECTORY_SEPARATOR. + 'controllers'.DIRECTORY_SEPARATOR.'front'; $this->filesystem->mkdir($front_controller_folder); - $model_front_file_name = $this->baseControllerFolder . DIRECTORY_SEPARATOR . + $model_front_file_name = $this->baseControllerFolder.DIRECTORY_SEPARATOR. 'front_controller.php.twig'; - $front_controller_code = $this->twig->render('@Modules' . $model_front_file_name, [ + $front_controller_code = $this->twig->render('@Modules'.$model_front_file_name, [ 'module_name' => $module_name, 'front_controller_name' => $front_controller_name, ]); $front_filename = - $front_controller_folder . DIRECTORY_SEPARATOR . $front_controller_name . + $front_controller_folder.DIRECTORY_SEPARATOR.$front_controller_name. '.php'; $this->filesystem->dumpFile($front_filename, $front_controller_code); } @@ -170,13 +321,13 @@ protected function createFrontControllerJavascript( $front_controller_name ) { $js_folder = - $this->getModuleDirectory($this->moduleName) . DIRECTORY_SEPARATOR . 'views' . - DIRECTORY_SEPARATOR . 'js'; + $this->getModuleDirectory($this->moduleName).DIRECTORY_SEPARATOR.'views'. + DIRECTORY_SEPARATOR.'js'; $this->filesystem->mkdir($js_folder); $js_front_controller_code = $this->twig->render( - '@Modules' . $this->baseViewFolder . DIRECTORY_SEPARATOR . 'js' - . DIRECTORY_SEPARATOR . + '@Modules'.$this->baseViewFolder.DIRECTORY_SEPARATOR.'js' + .DIRECTORY_SEPARATOR. 'front_controller.js.twig', [ 'module_name' => $module_name, @@ -184,143 +335,21 @@ protected function createFrontControllerJavascript( ] ); $this->filesystem->dumpFile( - $js_folder . DIRECTORY_SEPARATOR . $front_controller_name . '.js', + $js_folder.DIRECTORY_SEPARATOR.$front_controller_name.'.js', $js_front_controller_code ); } - protected function createMain($modulename) + private function createConfiguration() { - $controller_code = $this->twig->render( - '@Modules' . $this->baseFolder . DIRECTORY_SEPARATOR . 'main.php.twig', - [ - 'module_name' => $modulename, - ] - ); - - $this->filesystem->dumpFile( - $this->getModuleDirectory($modulename) . DIRECTORY_SEPARATOR . $modulename . - '.php', - $controller_code - ); - } - - protected function createModule($modulename) - { - $this->filesystem->mkdir($this->getModuleDirectory($modulename)); - } - protected function createRoute($modulename, $namespace) - { - $module_route_path = - $this->getModuleDirectory($modulename) . DIRECTORY_SEPARATOR . 'config'; - if ($this->filesystem->exists($module_route_path) === false) { - $this->filesystem->mkdir($module_route_path); + foreach ($this->generatorConfigurationServices as $generator) { + $generator->setModuleName($this->moduleName); + $generator->setTwigValues($this->configGeneration); + $generator->generate(); } - $route_code = $this->twig->render( - '@Modules' . $this->baseControllerFolder . DIRECTORY_SEPARATOR . 'routes.yml.twig', - [ - 'module_name' => $modulename, - 'name_space' => $namespace, - ] - ); - $this->filesystem->dumpFile( - $module_route_path . DIRECTORY_SEPARATOR . 'routes.yml', - $route_code - ); - } - - protected function createTest($modulename) - { - $module_dir = $this->getModuleDirectory($modulename); - $test_dir = $module_dir . DIRECTORY_SEPARATOR . 'test'; - $this->filesystem->mkdir($test_dir); - $this->filesystem->copy( - _PS_MODULE_DIR_ . $this->baseTestFolder . DIRECTORY_SEPARATOR . 'bootstrap.php.twig', - $test_dir . DIRECTORY_SEPARATOR . 'bootstrap.php' - ); - $this->filesystem->copy( - _PS_MODULE_DIR_ . $this->baseTestFolder . DIRECTORY_SEPARATOR . 'phpunit.xml.twig', - $module_dir . DIRECTORY_SEPARATOR . 'phpunit.xml' - ); - } - - /** - * @param InputInterface $input - * @param OutputInterface $output - * - * @return int|void|null - */ - protected function execute(InputInterface $input, OutputInterface $output) - { - $this->twig = $this->getContainer() - ->get('twig') - ; - - if ($this->isNewModule === true) { - $output->writeln('create module folder'); - $this->createModule($this->moduleName); - - $output->writeln('create main file'); - $this->createMain($this->moduleName); - - $output->writeln('create composer.json'); - $this->createComposerJson($this->moduleName, $this->moduleNamespace); - - $output->writeln('create config'); - $this->createConfig($this->moduleName); - $output->writeln('create routes'); - $this->createRoute($this->moduleName, $this->moduleNamespace); - - $output->writeln('create configuration controller'); - $this->createController($this->moduleName, $this->moduleNamespace); - - $output->writeln('create form '); - $this->createControllerForm($this->moduleName, $this->moduleNamespace); - - $output->writeln('create configuration controller template'); - $this->createControllerTemplate( - $this->moduleName, - $this->moduleNamespace - ); - - if ($this->testGeneration === true) { - $output->writeln('create test folder'); - $this->createTest($this->moduleName); - } - - $output->writeln('....'); - - $output->writeln( - 'OK! Now you can edit composer.json and run "composer install" inside your new module.' - ); - $output->writeln(''); - } else { - if ($this->frontControllerName) { - $output->writeln('create front controller file'); - $this->createFrontController( - $this->moduleName, - $this->frontControllerName - ); - - $output->writeln('create front javascript file'); - $this->createFrontControllerJavascript( - $this->moduleName, - $this->frontControllerName - ); - } - } - } - /** - * @param string $modulename - * - * @return string - */ - protected function getModuleDirectory($modulename): string - { - return _PS_MODULE_DIR_ . $modulename; } protected function interact(InputInterface $input, OutputInterface $output) @@ -331,31 +360,51 @@ protected function interact(InputInterface $input, OutputInterface $output) 'Please enter the name of the module (ex. testmodule): ', 'testmodule' ); - $ask_namespace = new Question( - 'Please enter the name space (ex Test\Module): ', - 'Test\Module' - ); - $ask_front_controller = - new Question('You need add a front controller? [yes/no]: ', 'no'); - $ask_front_controller_name = new Question( - 'What\'s the name of the front contoller? [yes/no]: ', 'no' - ); - $ask_phpunit_generation = - new Question('You want to add tests? [yes/no]: ', 'no'); $this->moduleName = $helper->ask($input, $output, $ask_module_name); + $this->isNewModule = !file_exists($this->getModuleDirectory($this->moduleName)); if ($this->isNewModule === true) { + + $ask_namespace = new Question( + 'Please enter the name space (ex Test\Module): ', + 'Test\Module' + ); $this->moduleNamespace = $helper->ask($input, $output, $ask_namespace); + + $ask_phpunit_generation = + new Question('You want to add tests? [yes/no]: ', 'no'); $this->testGeneration = $helper->ask($input, $output, $ask_phpunit_generation) === 'yes'; } + $ask_front_controller = + new Question('You need add a front controller? [yes/no]: ', 'no'); if ($helper->ask($input, $output, $ask_front_controller) === 'yes') { + + $ask_front_controller_name = new Question( + 'What\'s the name of the front contoller? [yes/no]: ', 'no' + ); $this->frontControllerName = $helper->ask($input, $output, $ask_front_controller_name); + + } + + $ask_configuration_generation = + new Question('You want add configuration? [yes/no]: ', 'no'); + if ($helper->ask($input, $output, $ask_configuration_generation) === 'yes') { + + $this->configGeneration = new TwigVariablesDTO(); + $this->configGeneration->nameSpace = $helper->ask($input, $output, new Question('Name space? : ')); + $this->configGeneration->serviceNameSpace = + $helper->ask($input, $output, new Question('Service name space? (example mymodule.service...): ')); + $this->configGeneration->className = + $helper->ask($input, $output, new Question('Class name? (example BasicData): ', "BasicData")); + $converter = new CamelCaseToSnakeCaseNameConverter(); + $this->configGeneration->serviceName = $converter->normalize($this->configGeneration->className); + $this->configGeneration->moduleName = $this->moduleName; } } } diff --git a/src/Generator/ClassFileGenerator.php b/src/Generator/ClassFileGenerator.php new file mode 100644 index 00000000..d456db7f --- /dev/null +++ b/src/Generator/ClassFileGenerator.php @@ -0,0 +1,43 @@ + + * @copyright since 2020 Friends of Presta + * @license https://opensource.org/licenses/AFL-3.0 Academic Free License ("AFL") v. 3.0 + * + */ + +namespace FOP\Console\Generator; + +class ClassFileGenerator extends FileGenerator +{ + private string $classFolderName; + + public function setClassFolderName(string $classFolderName): void + { + $this->classFolderName = $classFolderName; + } + + public function getFileNameModule(): string + { + return $this->twigValues->className . parent::getFileNameModule(); + } + + protected function getModuleFolder(): string + { + return $this->getModuleDirectory() + . DIRECTORY_SEPARATOR + . $this->classFolderName; + } +} diff --git a/src/Generator/CodeDisplayGenerator.php b/src/Generator/CodeDisplayGenerator.php new file mode 100644 index 00000000..2cb71ee7 --- /dev/null +++ b/src/Generator/CodeDisplayGenerator.php @@ -0,0 +1,51 @@ + + * @copyright since 2020 Friends of Presta + * @license https://opensource.org/licenses/AFL-3.0 Academic Free License ("AFL") v. 3.0 + * + */ + +namespace FOP\Console\Generator; + +class CodeDisplayGenerator extends FileGenerator +{ + private string $moduleFolder; + + public function generate(): void + { + $code = $this->twig + ->render( + '@Modules' + . $this->templatesBaseFolder + . DIRECTORY_SEPARATOR + . $this->templateName, + (array) $this->twigValues + ); + echo $code . PHP_EOL; + } + + protected function getModuleFolder(): string + { + return $this->getModuleDirectory() + . DIRECTORY_SEPARATOR + . $this->moduleFolder; + } + + public function setModuleFolder(string $moduleFolder): void + { + $this->moduleFolder = $moduleFolder; + } +} diff --git a/src/Generator/FileGenerator.php b/src/Generator/FileGenerator.php new file mode 100644 index 00000000..f6d199c8 --- /dev/null +++ b/src/Generator/FileGenerator.php @@ -0,0 +1,102 @@ + + * @copyright since 2020 Friends of Presta + * @license https://opensource.org/licenses/AFL-3.0 Academic Free License ("AFL") v. 3.0 + * + */ + +namespace FOP\Console\Generator; + +use Symfony\Component\Filesystem\Filesystem; +use Twig\Environment; + +abstract class FileGenerator +{ + protected string $moduleName; + protected string $templatesBaseFolder; + protected Environment $twig; + protected TwigVariablesDTO $twigValues; + protected string $templateName; + protected Filesystem $filesystem; + protected string $fileNameModule; + + public function __construct(Environment $twig) + { + $this->twig = $twig; + $this->filesystem = new Filesystem(); + } + + public function generate(): void + { + $code = $this->twig + ->render( + '@Modules' + . $this->templatesBaseFolder + . DIRECTORY_SEPARATOR + . $this->templateName, + (array) $this->twigValues + ); + + $this->filesystem->dumpFile( + $this->getModuleFolder() . $this->getFileNameModule(), + $code + ); + } + + abstract protected function getModuleFolder(): string; + + public function getFileNameModule(): string + { + return $this->fileNameModule; + } + + public function setFileNameModule(string $fileNameModule): void + { + $this->fileNameModule = $fileNameModule; + } + + /** + * @param mixed $moduleName + */ + public function setModuleName($moduleName): self + { + $this->moduleName = $moduleName; + + return $this; + } + + public function setTemplateName(string $templateName): self + { + $this->templateName = $templateName; + + return $this; + } + + public function setTemplatesBaseFolder(string $templatesBaseFolder): void + { + $this->templatesBaseFolder = $templatesBaseFolder; + } + + public function setTwigValues(TwigVariablesDTO $twigValues): void + { + $this->twigValues = $twigValues; + } + + protected function getModuleDirectory(): string + { + return _PS_MODULE_DIR_ . $this->moduleName; + } +} diff --git a/src/Generator/TwigFileGenerator.php b/src/Generator/TwigFileGenerator.php new file mode 100644 index 00000000..4f49be57 --- /dev/null +++ b/src/Generator/TwigFileGenerator.php @@ -0,0 +1,57 @@ + + * @copyright since 2020 Friends of Presta + * @license https://opensource.org/licenses/AFL-3.0 Academic Free License ("AFL") v. 3.0 + * + */ + +namespace FOP\Console\Generator; + +class TwigFileGenerator extends FileGenerator +{ + private string $moduleFolder; + + public function generate(): void + { + $from = _PS_MODULE_DIR_ + . DIRECTORY_SEPARATOR + . $this->templatesBaseFolder + . DIRECTORY_SEPARATOR + . $this->templateName; + + $this->filesystem->copy( + $from, + $this->getModuleFolder() . $this->getFileNameModule() + ); + } + + public function getFileNameModule(): string + { + return DIRECTORY_SEPARATOR . $this->twigValues->serviceName . '_' . parent::getFileNameModule(); + } + + protected function getModuleFolder(): string + { + return $this->getModuleDirectory() + . DIRECTORY_SEPARATOR + . $this->moduleFolder; + } + + public function setModuleFolder(string $moduleFolder): void + { + $this->moduleFolder = $moduleFolder; + } +} diff --git a/src/Generator/TwigVariablesDTO.php b/src/Generator/TwigVariablesDTO.php new file mode 100644 index 00000000..4dc2ad4e --- /dev/null +++ b/src/Generator/TwigVariablesDTO.php @@ -0,0 +1,30 @@ + + * @copyright since 2020 Friends of Presta + * @license https://opensource.org/licenses/AFL-3.0 Academic Free License ("AFL") v. 3.0 + * + */ + +namespace FOP\Console\Generator; + +class TwigVariablesDTO +{ + public string $moduleName; + public string $className; + public string $nameSpace; + public string $serviceNameSpace; + public string $serviceName; +} diff --git a/src/Generator/YamlFileGenerator.php b/src/Generator/YamlFileGenerator.php new file mode 100644 index 00000000..ba73ba87 --- /dev/null +++ b/src/Generator/YamlFileGenerator.php @@ -0,0 +1,31 @@ + + * @copyright since 2020 Friends of Presta + * @license https://opensource.org/licenses/AFL-3.0 Academic Free License ("AFL") v. 3.0 + * + */ + +namespace FOP\Console\Generator; + +class YamlFileGenerator extends FileGenerator +{ + protected function getModuleFolder(): string + { + return $this->getModuleDirectory() + . DIRECTORY_SEPARATOR + . 'config'; + } +} diff --git a/src/Resources/templates/generate_module_command/module/configuration/code_controller.php.twig b/src/Resources/templates/generate_module_command/module/configuration/code_controller.php.twig new file mode 100644 index 00000000..5c79d3c9 --- /dev/null +++ b/src/Resources/templates/generate_module_command/module/configuration/code_controller.php.twig @@ -0,0 +1,34 @@ + + +>>>>> please, copy this code to your controller file <<<<< + + + public function configAction(Request $request) + { + $textFormDataHandler = $this->get('{{ serviceNameSpace }}.handler.{{ serviceName }}_configuration_text_form_data_handler'); + + $textForm = $textFormDataHandler->getForm(); + $textForm->handleRequest($request); + + if ($textForm->isSubmitted() && $textForm->isValid()) { + /** You can return array of errors in form handler and they can be displayed to user with flashErrors */ + $errors = $textFormDataHandler->save($textForm->getData()); + + + empty($errors) ? + $this->addFlash('success', $this->trans('Successful update.', 'Admin.Notifications.Success')) : + $this->flashErrors($errors); + + } + + $form => $textForm->createView(); + + return $this->render( + '@Modules/{{moduleName}}/views/templates/admin/controller/{{serviceName}}_configuration_view.html.twig', + [ + 'form' => $form, + ] + ); + } + + diff --git a/src/Resources/templates/generate_module_command/module/configuration/configuration.yml.twig b/src/Resources/templates/generate_module_command/module/configuration/configuration.yml.twig new file mode 100644 index 00000000..26d75450 --- /dev/null +++ b/src/Resources/templates/generate_module_command/module/configuration/configuration.yml.twig @@ -0,0 +1,28 @@ +services: + {{ serviceNameSpace }}.form.type.{{ serviceName }}_text: + class: {{ nameSpace }}\Form\{{className}}ConfigurationFormType + parent: 'form.type.translatable.aware' + public: true + calls: + - [ setOrderStatusChoicesProvider,[ '@prestashop.core.form.choice_provider.order_state_by_id' ] ] + tags: + - { name: form.type } + + {{ serviceNameSpace }}.data.configuration.{{ serviceName }}_text_data_configuration: + class: {{ nameSpace }}\DataConfiguration\{{className}}TextDataConfiguration + arguments: [ '@prestashop.adapter.legacy.configuration' ] + + {{ serviceNameSpace }}.provider.{{serviceName}}_configuration_text_form_data_provider: + class: {{ nameSpace }}\Provider\{{className}}ConfigurationTextFormDataProvider + arguments: + - '@{{ serviceNameSpace }}.data.configuration.{{serviceName}}_text_data_configuration' + - + {{ serviceNameSpace }}.handler.{{ serviceName }}_configuration_text_form_data_handler: + class: 'PrestaShop\PrestaShop\Core\Form\Handler' + public: true + arguments: + - '@form.factory' + - '@prestashop.core.hook.dispatcher' + - '@{{serviceNameSpace}}.provider.{{ serviceName }}_configuration_text_form_data_provider' + - '{{nameSpace}}\Form\{{className}}FormType' + - '{{ className }}' \ No newline at end of file diff --git a/src/Resources/templates/generate_module_command/module/configuration/configuration_form.php.twig b/src/Resources/templates/generate_module_command/module/configuration/configuration_form.php.twig new file mode 100644 index 00000000..92ca8c14 --- /dev/null +++ b/src/Resources/templates/generate_module_command/module/configuration/configuration_form.php.twig @@ -0,0 +1,24 @@ +add({{className}}TextDataConfiguration::CONFIGNAME, TextType::class, [ + 'label' => $this->trans('xxxx',[], 'Modules.{{ moduleName|capitalize }}.Admin'), + 'help' => $this->trans('xxxxx',[],'Modules.{{ moduleName|capitalize }}.Admin'), + ]); + } + +} \ No newline at end of file diff --git a/src/Resources/templates/generate_module_command/module/configuration/template_configuration.twig b/src/Resources/templates/generate_module_command/module/configuration/template_configuration.twig new file mode 100644 index 00000000..3ffcfabc --- /dev/null +++ b/src/Resources/templates/generate_module_command/module/configuration/template_configuration.twig @@ -0,0 +1,25 @@ +{% extends '@PrestaShop/Admin/layout.html.twig' %} +{# TODO: Edit #} +{% set layoutTitle = 'xxxxxx'|trans({}, 'Modules.xxxx.Admin') %} + +{% block content %} + {{ form_start(form) }} +
+

+ settings {{ 'Text form types'|trans({}, 'Modules.xxxx.Admin') }} +

+
+
+ {{ form_widget(form) }} +
+
+ +
+ {{ form_end(form) }} +{% endblock %} diff --git a/src/Resources/templates/generate_module_command/module/configuration/text_data_configuration.php.twig b/src/Resources/templates/generate_module_command/module/configuration/text_data_configuration.php.twig new file mode 100644 index 00000000..c75aca40 --- /dev/null +++ b/src/Resources/templates/generate_module_command/module/configuration/text_data_configuration.php.twig @@ -0,0 +1,48 @@ +configuration = $configuration; + } + + public function getConfiguration(): array + { + $return = []; + + $return[self::CONFIGNAME] = json_decode($this->configuration->get(static::CONFIGNAME), true); + + return $return; + } + + public function updateConfiguration(array $configuration): array + { + $this->configuration->set(self::CONFIGNAME, json_encode($configuration[self::CONFIGNAME])); + return []; + } + + /** + * Ensure the parameters passed are valid. + * + * @return bool Returns true if no exception are thrown + */ + public function validateConfiguration(array $configuration): bool + { + return isset($configuration[self::CONFIGNAME]); + } +} \ No newline at end of file diff --git a/src/Resources/templates/generate_module_command/module/configuration/text_data_provider.php.twig b/src/Resources/templates/generate_module_command/module/configuration/text_data_provider.php.twig new file mode 100644 index 00000000..f1218180 --- /dev/null +++ b/src/Resources/templates/generate_module_command/module/configuration/text_data_provider.php.twig @@ -0,0 +1,31 @@ +configurationTextDataConfiguration = $configurationTextDataConfiguration; + } + + public function getData(): array + { + return $this->configurationTextDataConfiguration->getConfiguration(); + } + + public function setData(array $data): array + { + return $this->configurationTextDataConfiguration->updateConfiguration($data); + } + + +} \ No newline at end of file