Skip to content
This repository was archived by the owner on May 3, 2022. It is now read-only.

Proof of concept of bundle generator #684

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .php_cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ return Symfony\CS\Config\Config::create()
'Resources/public',
'Tests/js',
])
->files()->name('*.php')
->files()->name('/\.php$/')
)
;
38 changes: 38 additions & 0 deletions Command/GeneratePlatformUISkeletonBundleCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
namespace EzSystems\PlatformUIBundle\Command;

use EzSystems\PlatformUIBundle\Generator\PlatformUIPluginGenerator;
use Sensio\Bundle\GeneratorBundle\Command\GenerateBundleCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class GeneratePlatformUISkeletonBundleCommand extends GenerateBundleCommand
{
/**
* @see Command
*/
protected function configure()
{
parent::configure();
$this->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;
}
}
77 changes: 77 additions & 0 deletions Generator/PlatformUIPluginGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php
namespace EzSystems\PlatformUIBundle\Generator;

use Sensio\Bundle\GeneratorBundle\Generator\Generator;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\DependencyInjection\Container;

/**
* Generates a PlatformUI plugin skeleton.
*/
class PlatformUIPluginGenerator extends Generator
{
/**
* @var \Symfony\Component\Filesystem\Filesystem
*/
private $filesystem;

public function __construct(Filesystem $filesystem)
{
$this->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);
$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);

$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');
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace {{ namespace }}\DependencyInjection;

{% block use_statements %}
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;
{% endblock use_statements %}

/**
{% block phpdoc_class_header %}
* This is the class that validates and merges configuration from your app/config files
{% endblock phpdoc_class_header %}
*
* To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html#cookbook-bundles-extension-config-class}
*/
{% block class_definition %}
class Configuration implements ConfigurationInterface
{% endblock class_definition %}
{
{% block class_body %}
/**
* {@inheritdoc}
*/
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->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 %}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{% 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() }}
/**
* {@inheritdoc}
*/
public function load(array $configs, ContainerBuilder $container)
{
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);
}

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 %}
4 changes: 4 additions & 0 deletions Resources/SensioGeneratorBundle/skeleton/bundle/css.yml.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
system:
default:
css:
files:
4 changes: 4 additions & 0 deletions Resources/SensioGeneratorBundle/skeleton/bundle/yui.yml.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
system:
default:
yui:
modules: