From c84e14a23cd00ff033e57c54632bbe6e7361a4a9 Mon Sep 17 00:00:00 2001 From: Bertrand Dunogier Date: Fri, 7 Oct 2016 00:29:29 +0200 Subject: [PATCH 1/5] Proof of concept of bundle generator --- ...eneratePlatformUISkeletonBundleCommand.php | 38 ++++++++++ Generator/PlatformUIPluginGenerator.php | 71 +++++++++++++++++++ .../skeleton/bundle/Configuration.php.twig | 37 ++++++++++ .../bundle/PlatformUIPluginExtension.php.twig | 50 +++++++++++++ .../skeleton/bundle/css.yml.twig | 4 ++ .../skeleton/bundle/yui.yml.twig | 4 ++ 6 files changed, 204 insertions(+) create mode 100644 Command/GeneratePlatformUISkeletonBundleCommand.php create mode 100644 Generator/PlatformUIPluginGenerator.php create mode 100644 Resources/SensioGeneratorBundle/skeleton/bundle/Configuration.php.twig create mode 100644 Resources/SensioGeneratorBundle/skeleton/bundle/PlatformUIPluginExtension.php.twig create mode 100644 Resources/SensioGeneratorBundle/skeleton/bundle/css.yml.twig create mode 100644 Resources/SensioGeneratorBundle/skeleton/bundle/yui.yml.twig diff --git a/Command/GeneratePlatformUISkeletonBundleCommand.php b/Command/GeneratePlatformUISkeletonBundleCommand.php new file mode 100644 index 000000000..386702dd5 --- /dev/null +++ b/Command/GeneratePlatformUISkeletonBundleCommand.php @@ -0,0 +1,38 @@ +setName('generate:ez:platform-ui-plugin'); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + $this->getGenerator( + $this->getContainer()->get('kernel')->getBundle('eZPlatformUIBundle') + ); + parent::execute($input, $output); + } + + protected function createGenerator() + { + return new PlatformUIPluginGenerator($this->getContainer()->get('filesystem')); + + // $thisBundle = $this->getContainer()->get('kernel')->getBundle('BDEzPlatformUIExtensionSkeletonGeneratorBundle'); + // $skeletonDirs = $this->getSkeletonDirs($thisBundle); + // $generator->setSkeletonDirs($skeletonDirs); + // return $generator; + } +} diff --git a/Generator/PlatformUIPluginGenerator.php b/Generator/PlatformUIPluginGenerator.php new file mode 100644 index 000000000..abeb89e11 --- /dev/null +++ b/Generator/PlatformUIPluginGenerator.php @@ -0,0 +1,71 @@ +filesystem = $filesystem; + } + + public function generate($namespace, $bundle, $dir, $format, $structure) + { + $dir .= '/'.strtr($namespace, '\\', '/'); + if (file_exists($dir)) { + if (!is_dir($dir)) { + throw new \RuntimeException(sprintf('Unable to generate the bundle as the target directory "%s" exists but is a file.', realpath($dir))); + } + $files = scandir($dir); + if ($files != array('.', '..')) { + throw new \RuntimeException(sprintf('Unable to generate the bundle as the target directory "%s" is not empty.', realpath($dir))); + } + if (!is_writable($dir)) { + throw new \RuntimeException(sprintf('Unable to generate the bundle as the target directory "%s" is not writable.', realpath($dir))); + } + } + + $basename = substr($bundle, 0, -6); + $parameters = array( + 'namespace' => $namespace, + 'bundle' => $bundle, + 'format' => $format, + 'bundle_basename' => $basename, + 'extension_alias' => Container::underscore($basename), + ); + + if ('xml' === $format || 'annotation' === $format) { + $this->renderFile('bundle/yui.xml.twig', $dir.'/Resources/config/yui.xml', $parameters); + $this->renderFile('bundle/css.xml.twig', $dir.'/Resources/config/css.xml', $parameters); + } else { + $this->renderFile('bundle/yui.'.$format.'.twig', $dir.'/Resources/config/yui.'.$format, $parameters); + $this->renderFile('bundle/css.'.$format.'.twig', $dir.'/Resources/config/css.'.$format, $parameters); + } + + $this->renderFile('bundle/Bundle.php.twig', $dir.'/'.$bundle.'.php', $parameters); + $this->renderFile('bundle/PlatformUIPluginExtension.php.twig', $dir.'/DependencyInjection/'.$basename.'Extension.php', $parameters); + $this->renderFile('bundle/Configuration.php.twig', $dir.'/DependencyInjection/Configuration.php', $parameters); + + if ($structure) { + $this->renderFile('bundle/messages.fr.xlf', $dir.'/Resources/translations/messages.fr.xlf', $parameters); + + $this->filesystem->mkdir($dir.'/Resources/doc'); + $this->filesystem->touch($dir.'/Resources/doc/index.rst'); + $this->filesystem->mkdir($dir.'/Resources/translations'); + $this->filesystem->mkdir($dir.'/Resources/public/css'); + $this->filesystem->mkdir($dir.'/Resources/public/images'); + $this->filesystem->mkdir($dir.'/Resources/public/js'); + } + } +} diff --git a/Resources/SensioGeneratorBundle/skeleton/bundle/Configuration.php.twig b/Resources/SensioGeneratorBundle/skeleton/bundle/Configuration.php.twig new file mode 100644 index 000000000..d77aa2b2f --- /dev/null +++ b/Resources/SensioGeneratorBundle/skeleton/bundle/Configuration.php.twig @@ -0,0 +1,37 @@ +root('{{ extension_alias }}'); + + // Here you should define the parameters that are allowed to + // configure your bundle. See the documentation linked above for + // more information on that topic. + + return $treeBuilder; + } +{% endblock class_body %} +} diff --git a/Resources/SensioGeneratorBundle/skeleton/bundle/PlatformUIPluginExtension.php.twig b/Resources/SensioGeneratorBundle/skeleton/bundle/PlatformUIPluginExtension.php.twig new file mode 100644 index 000000000..63b059a87 --- /dev/null +++ b/Resources/SensioGeneratorBundle/skeleton/bundle/PlatformUIPluginExtension.php.twig @@ -0,0 +1,50 @@ +{% extends 'skeleton/bundle/Extension.php.twig' %} + +{% block use_statements %} +{{ parent() }} +use Symfony\Component\Yaml\Yaml; +use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface; +use Symfony\Component\Config\Resource\FileResource; +{% endblock use_statements %} + +{% block class_definition %} +class {{ bundle_basename }}Extension extends Extension implements PrependExtensionInterface +{% endblock class_definition %} + +{% block class_body %} +{{ parent() }} + + public function prepend(ContainerBuilder $container) + { + $container->prependExtensionConfig( + 'assetic', array('bundles' => array('{{ bundle }}')) + ); + + $this->prependYui($container); + $this->prependCss($container); + } + + private function prependYui(ContainerBuilder $container) + { + $container->setParameter( + 'extension_skeleton.js_dir', + 'bundles/{{ extension_alias }}/js' + ); + $yuiConfigFile = __DIR__ . '/../Resources/config/yui.yml'; + $config = Yaml::parse(file_get_contents($yuiConfigFile)); + $container->prependExtensionConfig('ez_platformui', $config); + $container->addResource(new FileResource($yuiConfigFile)); + } + + private function prependCss(ContainerBuilder $container) + { + $container->setParameter( + 'extension_skeleton.css_dir', + 'bundles/{{ extension_alias }}/css' + ); + $cssConfigFile = __DIR__ . '/../Resources/config/css.yml'; + $config = Yaml::parse(file_get_contents($cssConfigFile)); + $container->prependExtensionConfig('ez_platformui', $config); + $container->addResource(new FileResource($cssConfigFile)); + } +{% endblock %} diff --git a/Resources/SensioGeneratorBundle/skeleton/bundle/css.yml.twig b/Resources/SensioGeneratorBundle/skeleton/bundle/css.yml.twig new file mode 100644 index 000000000..8cad4efa8 --- /dev/null +++ b/Resources/SensioGeneratorBundle/skeleton/bundle/css.yml.twig @@ -0,0 +1,4 @@ +system: + default: + css: + files: diff --git a/Resources/SensioGeneratorBundle/skeleton/bundle/yui.yml.twig b/Resources/SensioGeneratorBundle/skeleton/bundle/yui.yml.twig new file mode 100644 index 000000000..21d9cb0ed --- /dev/null +++ b/Resources/SensioGeneratorBundle/skeleton/bundle/yui.yml.twig @@ -0,0 +1,4 @@ +system: + default: + yui: + modules: From e56aa10c6c965375f59ab9164e58aa885ab09231 Mon Sep 17 00:00:00 2001 From: Bertrand Dunogier Date: Fri, 7 Oct 2016 10:08:44 +0200 Subject: [PATCH 2/5] [php-cs] only check files ending with .php (not .php.twig) --- .php_cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.php_cs b/.php_cs index dbbd3ad00..cc1acabbf 100644 --- a/.php_cs +++ b/.php_cs @@ -25,6 +25,6 @@ return Symfony\CS\Config\Config::create() 'Resources/public', 'Tests/js', ]) - ->files()->name('*.php') + ->files()->name('/\.php$/') ) ; From 471b276e6ddb28b93b7671354bb95a64f1c64620 Mon Sep 17 00:00:00 2001 From: Bertrand Dunogier Date: Fri, 7 Oct 2016 10:09:04 +0200 Subject: [PATCH 3/5] fixup! Proof of concept of bundle generator --- Generator/PlatformUIPluginGenerator.php | 34 ++++++++++++------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/Generator/PlatformUIPluginGenerator.php b/Generator/PlatformUIPluginGenerator.php index abeb89e11..580242675 100644 --- a/Generator/PlatformUIPluginGenerator.php +++ b/Generator/PlatformUIPluginGenerator.php @@ -22,7 +22,7 @@ public function __construct(Filesystem $filesystem) public function generate($namespace, $bundle, $dir, $format, $structure) { - $dir .= '/'.strtr($namespace, '\\', '/'); + $dir .= '/' . strtr($namespace, '\\', '/'); if (file_exists($dir)) { if (!is_dir($dir)) { throw new \RuntimeException(sprintf('Unable to generate the bundle as the target directory "%s" exists but is a file.', realpath($dir))); @@ -39,33 +39,33 @@ public function generate($namespace, $bundle, $dir, $format, $structure) $basename = substr($bundle, 0, -6); $parameters = array( 'namespace' => $namespace, - 'bundle' => $bundle, - 'format' => $format, + 'bundle' => $bundle, + 'format' => $format, 'bundle_basename' => $basename, 'extension_alias' => Container::underscore($basename), ); if ('xml' === $format || 'annotation' === $format) { - $this->renderFile('bundle/yui.xml.twig', $dir.'/Resources/config/yui.xml', $parameters); - $this->renderFile('bundle/css.xml.twig', $dir.'/Resources/config/css.xml', $parameters); + $this->renderFile('bundle/yui.xml.twig', $dir . '/Resources/config/yui.xml', $parameters); + $this->renderFile('bundle/css.xml.twig', $dir . '/Resources/config/css.xml', $parameters); } else { - $this->renderFile('bundle/yui.'.$format.'.twig', $dir.'/Resources/config/yui.'.$format, $parameters); - $this->renderFile('bundle/css.'.$format.'.twig', $dir.'/Resources/config/css.'.$format, $parameters); + $this->renderFile('bundle/yui.' . $format . '.twig', $dir . '/Resources/config/yui.' . $format, $parameters); + $this->renderFile('bundle/css.' . $format . '.twig', $dir . '/Resources/config/css.' . $format, $parameters); } - $this->renderFile('bundle/Bundle.php.twig', $dir.'/'.$bundle.'.php', $parameters); - $this->renderFile('bundle/PlatformUIPluginExtension.php.twig', $dir.'/DependencyInjection/'.$basename.'Extension.php', $parameters); - $this->renderFile('bundle/Configuration.php.twig', $dir.'/DependencyInjection/Configuration.php', $parameters); + $this->renderFile('bundle/Bundle.php.twig', $dir . '/' . $bundle . '.php', $parameters); + $this->renderFile('bundle/PlatformUIPluginExtension.php.twig', $dir . '/DependencyInjection/' . $basename . 'Extension.php', $parameters); + $this->renderFile('bundle/Configuration.php.twig', $dir . '/DependencyInjection/Configuration.php', $parameters); if ($structure) { - $this->renderFile('bundle/messages.fr.xlf', $dir.'/Resources/translations/messages.fr.xlf', $parameters); + $this->renderFile('bundle/messages.fr.xlf', $dir . '/Resources/translations/messages.fr.xlf', $parameters); - $this->filesystem->mkdir($dir.'/Resources/doc'); - $this->filesystem->touch($dir.'/Resources/doc/index.rst'); - $this->filesystem->mkdir($dir.'/Resources/translations'); - $this->filesystem->mkdir($dir.'/Resources/public/css'); - $this->filesystem->mkdir($dir.'/Resources/public/images'); - $this->filesystem->mkdir($dir.'/Resources/public/js'); + $this->filesystem->mkdir($dir . '/Resources/doc'); + $this->filesystem->touch($dir . '/Resources/doc/index.rst'); + $this->filesystem->mkdir($dir . '/Resources/translations'); + $this->filesystem->mkdir($dir . '/Resources/public/css'); + $this->filesystem->mkdir($dir . '/Resources/public/images'); + $this->filesystem->mkdir($dir . '/Resources/public/js'); } } } From b36b3e0379da87cd411288a1e682e264898b60e0 Mon Sep 17 00:00:00 2001 From: Bertrand Dunogier Date: Fri, 7 Oct 2016 10:34:23 +0200 Subject: [PATCH 4/5] Removed services.yml loading from Extension --- .../skeleton/bundle/PlatformUIPluginExtension.php.twig | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Resources/SensioGeneratorBundle/skeleton/bundle/PlatformUIPluginExtension.php.twig b/Resources/SensioGeneratorBundle/skeleton/bundle/PlatformUIPluginExtension.php.twig index 63b059a87..5d2085b7b 100644 --- a/Resources/SensioGeneratorBundle/skeleton/bundle/PlatformUIPluginExtension.php.twig +++ b/Resources/SensioGeneratorBundle/skeleton/bundle/PlatformUIPluginExtension.php.twig @@ -13,6 +13,14 @@ class {{ bundle_basename }}Extension extends Extension implements PrependExtensi {% block class_body %} {{ parent() }} + /** + * {@inheritdoc} + */ + public function load(array $configs, ContainerBuilder $container) + { + $configuration = new Configuration(); + $config = $this->processConfiguration($configuration, $configs); + } public function prepend(ContainerBuilder $container) { From 2d2385276ff94e036a3de8662d85cae822cecde8 Mon Sep 17 00:00:00 2001 From: Bertrand Dunogier Date: Tue, 11 Oct 2016 12:20:16 +0200 Subject: [PATCH 5/5] Added controller generation, as it is required in other parts --- Generator/PlatformUIPluginGenerator.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Generator/PlatformUIPluginGenerator.php b/Generator/PlatformUIPluginGenerator.php index 580242675..ec7a9cf14 100644 --- a/Generator/PlatformUIPluginGenerator.php +++ b/Generator/PlatformUIPluginGenerator.php @@ -56,6 +56,12 @@ public function generate($namespace, $bundle, $dir, $format, $structure) $this->renderFile('bundle/Bundle.php.twig', $dir . '/' . $bundle . '.php', $parameters); $this->renderFile('bundle/PlatformUIPluginExtension.php.twig', $dir . '/DependencyInjection/' . $basename . 'Extension.php', $parameters); $this->renderFile('bundle/Configuration.php.twig', $dir . '/DependencyInjection/Configuration.php', $parameters); + $this->renderFile('bundle/DefaultController.php.twig', $dir.'/Controller/DefaultController.php', $parameters); + $this->renderFile('bundle/DefaultControllerTest.php.twig', $dir.'/Tests/Controller/DefaultControllerTest.php', $parameters); + + if ('annotation' != $format) { + $this->renderFile('bundle/routing.'.$format.'.twig', $dir.'/Resources/config/routing.'.$format, $parameters); + } if ($structure) { $this->renderFile('bundle/messages.fr.xlf', $dir . '/Resources/translations/messages.fr.xlf', $parameters);