Commit a5c75fac authored by Ludwig Ruderstaller's avatar Ludwig Ruderstaller
Browse files

Generater for new Structure

parent 4e7bd7f7
......@@ -10,8 +10,10 @@
namespace Cwd\GeneratorBundle\Command;
use Cwd\GeneratorBundle\Service\ClassGenerator;
use Doctrine\ORM\Tools\Console\Command\SchemaTool\AbstractCommand;
use Sensio\Bundle\GeneratorBundle\Command\GeneratorCommand;
use Sensio\Bundle\GeneratorBundle\Command\Validators;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
......@@ -22,7 +24,7 @@ use Symfony\Component\HttpKernel\Bundle\BundleInterface;
*
* @author Ludwig Ruderstaller <lr@cwd.at>
*/
class GenerateFilesCommand extends GeneratorCommand
class GenerateFilesCommand extends ContainerAwareCommand
{
/**
* @see Command
......@@ -38,25 +40,11 @@ class GenerateFilesCommand extends GeneratorCommand
'The name of the controller to create'
),
new InputOption(
'route-format',
'namespace',
'',
InputOption::VALUE_REQUIRED,
'The format that is used for the routing (yml, xml, php, annotation)',
'annotation'
),
new InputOption(
'template-format',
'',
InputOption::VALUE_REQUIRED,
'The format that is used for templating (twig, php)',
'twig'
),
new InputOption(
'template-path',
'',
InputOption::VALUE_REQUIRED,
'Template path (Default: admin)',
'admin'
'The name of the controller to create',
'App'
),
))
->setDescription('Generates default classes for entity')
......@@ -70,91 +58,21 @@ class GenerateFilesCommand extends GeneratorCommand
*/
public function execute(InputInterface $input, OutputInterface $output)
{
$questionHelper = $this->getQuestionHelper();
if (null === $input->getOption('controller')) {
throw new \RuntimeException('The controller option must be provided.');
}
list($bundle, $controller) = $this->parseShortcutNotation($input->getOption('controller'));
if (is_string($bundle)) {
$bundle = Validators::validateBundleName($bundle);
$generator = $this->createGenerator();
try {
$bundle = $this->getContainer()->get('kernel')->getBundle($bundle);
} catch (\Exception $e) {
$output->writeln(sprintf('<bg=red>Bundle "%s" does not exist.</>', $bundle));
}
}
$questionHelper->writeSection($output, 'Controller generation');
$generator = $this->getGenerator($bundle);
$serviceCall = $generator->generateController(
$bundle,
$controller,
$input->getOption('route-format'),
$input->getOption('template-format'),
$input->getOption('template-path')
$generator->generateController(
$input->getOption('namespace'),
$input->getOption('controller')
);
$output->writeln('Generating the bundle code: <info>OK</info>');
$output->writeln('Add this to your service.yml:');
$output->writeln($serviceCall);
$questionHelper->writeGeneratorSummary($output, array());
}
/**
* @param BundleInterface|null $bundle
* @return array
*/
protected function getSkeletonDirs(BundleInterface $bundle = null)
{
$skeletonDirs = array();
$skeletonDirs[] = __DIR__.'/../Resources/skeleton';
$skeletonDirs[] = __DIR__.'/../Resources';
return $skeletonDirs;
}
/**
* @param string $route
* @return mixed
*/
public function getPlaceholdersFromRoute($route)
{
preg_match_all('/{(.*?)}/', $route, $placeholders);
$placeholders = $placeholders[1];
return $placeholders;
}
/**
* @param string $shortcut
* @return array
*/
public function parseShortcutNotation($shortcut)
{
$entity = str_replace('/', '\\', $shortcut);
if (false === $pos = strpos($entity, ':')) {
throw new \InvalidArgumentException(
sprintf(
'The controller name must contain a : ("%s" given, expecting something like AcmeBlogBundle:Post)',
$entity
)
);
}
return array(substr($entity, 0, $pos), substr($entity, $pos + 1));
}
/**
* @return ClassGenerator
*/
protected function createGenerator()
{
return new ClassGenerator($this->getContainer()->get('filesystem'));
......
......@@ -28,8 +28,5 @@ class CwdGeneratorExtension extends Extension
{
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yml');
}
}
services:
# cwd_generator.example:
# class: Cwd\GeneratorBundle\Example
# arguments: ["@service_id", "plain_value", %parameter%]
<?php
/*
* This file is part of {{ bundle }}
* This file is part of
*
* (c){{ "now"|date('Y') }} cwd.at GmbH <office@cwd.at>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace {{ namespace }}\Controller;
namespace {{ namespace }}\Infrastructure\Web\Controller;
{% block use_statements %}
use {{ namespace }}\Form\Type\{{ type }}Type;
use {{ namespace }}\Grid\{{ type }}Grid;
use {{ namespace|split('\\')|first }}\Model\Entity\{{ type }};
use {{ namespace }}\Infrastructure\Web\Form\{{ type }}Type;
use {{ namespace }}\Infrastructure\Web\Grid\{{ type }}Grid;
use {{ namespace }}\Domain\Model\{{ type }};
use {{ namespace }}\Domain\{{ type }}\{{ type }}Manager;
use Symfony\Component\HttpFoundation\JsonResponse;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
......@@ -28,9 +29,6 @@ use Symfony\Component\HttpFoundation\Response;
/**
* Class {{ type }}Controller
*
* @package {{ namespace }}\Controller
* @author Ludwig Ruderstaller <lr@cwd.at>
*
* @Route("/{{ type|lower }}")
*/
class {{ type }}Controller extends AbstractCrudController
......@@ -46,14 +44,14 @@ class {{ type }}Controller extends AbstractCrudController
protected function setOptions()
{
$options = array(
'entityService' => '{{ namespace|split('\\')|first|lower }}.service.{{ type|lower }}',
'entityManager' => {{ type }}Manager::class,
'entityFormType' => {{ type }}Type::class,
'gridService' => {{ type }}Grid::class,
'gridRoute' => '{{ namespace|split('\\')|first|lower }}_{{ templatePath }}_{{ type|lower }}_grid',
'createRoute' => '{{ namespace|split('\\')|first|lower }}_{{ templatePath }}_{{ type|lower }}_create',
'redirectRoute' => '{{ namespace|split('\\')|first|lower }}_{{ templatePath }}_{{ type|lower }}_list',
'createRoute' => '{{ namespace|lower }}_infrastructure_web_{{ type|lower }}_create',
'redirectRoute' => '{{ namespace|lower }}_infrastructure_web_{{ type|lower }}_list',
'icon' => 'fa fa-users',
'title' => '{{ type }}',
'title' => '{{ type|lower }}.title',
'createLabel' => '{{ type|lower }}.create',
);
return array_merge(parent::setOptions(), $options);
......@@ -75,8 +73,7 @@ class {{ type }}Controller extends AbstractCrudController
* @Route("/list")
* @Route("/")
* @Method({"GET"})
* @Template("{{ bundle }}:Grid:list.html.twig")
* @Security("has_role('ROLE_ADMIN')")
* @Template("Grid/list.html.twig")
*
* @return array
*/
......
<?php
/*
* This file is part of {{ bundle }}.
*
* (c){{ "now"|date('Y') }} cwd.at GmbH <office@cwd.at>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace {{ namespace|split('\\')|first }}\Service\Event;
use {{ namespace|split('\\')|first }}\Model\Entity\{{ type }};
use Symfony\Component\EventDispatcher\Event;
/**
* Class {{ type }}Event
*
* @package {{ namespace|split('\\')|first }}\Service\Event
* @author Ludwig Ruderstaller <lr@cwd.at>
*/
class {{ type }}Event extends Event
{
/**
* @var {{ type }}
*/
protected $entity;
/**
* @param {{ type }} $entity
*/
public function __construct({{ type }} $entity)
{
$this->entity = $entity;
}
/**
* @return {{ type }}
*/
public function get{{ type }}()
{
return $this->entity;
}
}
<?php
/*
* This file is part of {{ bundle }}
* This file is part of Project
*
* (c){{ "now"|date('Y') }} cwd.at GmbH <office@cwd.at>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace {{ namespace|split('\\')|first }}\Service\Exception;
namespace {{ namespace }}\Domain\{{ type }}\Exception;
use Cwd\CommonBundle\Exception\AbstractBaseException;
/**
* Class {{ type }}NotFoundException
*
* @package {{ namespace|split('\\')|first }}\Service\Exception
* @author Ludwig Ruderstaller <lr@cwd.at>
*/
class {{ type }}NotFoundException extends AbstractBaseException
{
......
<?php
/*
* This file is part of {{ namespace|split('\\')|first }}
* This file is part of
*
* (c){{ "now"|date('Y') }} cwd.at GmbH <office@cwd.at>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace {{ namespace }}\Form\Type;
namespace {{ namespace }}\Infrastructure\Web\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
......@@ -16,13 +16,11 @@ use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Security\Core\Authorization\AuthorizationChecker;
use {{ namespace }}\Domain\Model\{{ type }};
/**
* Class {{ type }} Form
*
* @package {{ namespace }}\Form\Type
* @author Ludwig Ruderstaller <lr@cwd.at>
*
*/
class {{ type }}Type extends AbstractType
{
......@@ -49,7 +47,7 @@ class {{ type }}Type extends AbstractType
{
$resolver->setDefaults(
array(
'data_class' => '{{ namespace|split('\\')|first }}\Model\Entity\{{ type }}',
'data_class' => {{ type }}::class,
)
);
}
......
<?php
/*
* This file is part of {{ namespace|split('\\')|first }}
* This file is part of
*
* (c){{ "now"|date('Y') }} cwd.at GmbH <office@cwd.at>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace {{ namespace }}\Grid;
namespace {{ namespace }}\Infrastructure\Web\Grid;
use Cwd\BootgridBundle\Column\ActionType;
use Cwd\BootgridBundle\Column\DateType;
......@@ -22,9 +22,6 @@ use Symfony\Component\OptionsResolver\OptionsResolverInterface;
/**
* Class {{ type }}Type
*
* @package {{ namespace }}\Grid
* @author Ludwig Ruderstaller <lr@cwd.at>
*/
class {{ type }}Grid extends AbstractGrid
{
......@@ -60,13 +57,13 @@ class {{ type }}Grid extends AbstractGrid
'label' => '',
'actions' => [
[
'route' => '{{ namespace|split('\\')|first|lower }}_{{ templatePath }}_{{ type }}_edit',
'route' => '{{ namespace|lower }}_infrastructure_web_{{ type|lower }}_edit',
'class' => 'btn-primary',
'icon' => 'fa-edit',
'title' => 'Edit',
],
[
'route' => '{{ namespace|split('\\')|first|lower }}_{{ templatePath }}_{{ type }}_delete',
'route' => '{{ namespace|lower }}_infrastructure_web_{{ type|lower }}_delete',
'class' => 'btn-danger deleterow',
'icon' => 'fa-trash-o',
'title' => 'Delete',
......@@ -86,7 +83,7 @@ class {{ type }}Grid extends AbstractGrid
$resolver->setDefaults(array(
'default_sorts' => array('u.id' => false),
'data_route' => '{{ namespace|split('\\')|first|lower }}_{{ templatePath|lower }}_{{ type|lower }}_ajaxdata',
'data_route' => '{{ namespace|lower }}_infrastructure_web_{{ type|lower }}_ajaxdata',
));
}
......
<?php
/*
* This file is part of {{ bundle }}
* This file is part of
*
* (c){{ "now"|date('Y') }} cwd.at GmbH <office@cwd.at>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace {{ namespace|split('\\')|first }}\Service;
namespace {{ namespace }}\Domain\{{ type }};
use {{ namespace|split('\\')|first }}\Model\Entity\{{ type }} as Entity;
use {{ namespace|split('\\')|first }}\Model\Repository\{{ type }}Repository as EntityRepository;
use {{ namespace }}\Domain\Model\{{ type }} as Entity;
use {{ namespace }}\Domain\{{ type }}\{{ type }}Repository as EntityRepository;
use {{ namespace }}\Domain\{{ type }}\Exception\{{ type }}NotFoundException;
use {{ namespace }}\Domain\AbstractManager;
use Doctrine\ORM\EntityManager;
use Psr\Log\LoggerInterface;
use Doctrine\Common\Persistence\ManagerRegistry;
/**
* Class {{ bundle }} Service Contract
*
* @package {{ namespace|split('\\')|first }}\Service
* @author Ludwig Ruderstaller <lr@cwd.at>
* Class {{ type }}Manager
*
* @method Entity getNew()
* @method Entity find($pid)
......@@ -27,7 +26,7 @@ use Doctrine\Common\Persistence\ManagerRegistry;
* @method NotFoundException createNotFoundException($message = null, $code = null, $previous = null)
*
*/
class {{ type }}Service extends AbstractBaseService
class {{ type }}Manager extends AbstractManager
{
/**
* @param ManagerRegistry $managerRegistry
......@@ -48,7 +47,7 @@ class {{ type }}Service extends AbstractBaseService
{
return array(
'modelName' => 'Model:{{ type }}',
'notFoundExceptionClass' => '{{ namespace|split('\\')|first }}\Service\Exception\{{ type }}NotFoundException',
'notFoundExceptionClass' => {{ type }}NotFoundException::class,
);
}
}
<?php
/*
* This file is part of
*
* (c){{ "now"|date('Y') }} cwd.at GmbH <office@cwd.at>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace {{ namespace }}\Domain\{{ type }};
use Doctrine\ORM\EntityRepository;
/**
* Class {{ type }}Repository.
*/
class {{ type }}Repository extends EntityRepository
{
}
<?php
/*
* This file is part of {{ namespace|split('\\')|first }}
* This file is part of
*
* (c){{ "now"|date('Y') }} cwd.at GmbH <office@cwd.at>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace {{ namespace|split('\\')|first }}\Tests\Service;
namespace {{ namespace }}\Tests\{{ type }};
use {{ namespace }}\Domain\{{ type }}\{{ type }}Manager;
use {{ namespace }}\Tests\AbstractManagerTestCase;
/**
* Class {{ namespace|split('\\')|first }}\{{ type }}Test
* Class {{ namespace }}\Tests\{{ type }}ManagerTest
*
* @package {{ namespace|split('\\')|first }}\Tests\Service
* @author Ludwig Ruderstaller <lr@cwd.at>
*/
class {{ type }}Test extends BaseServiceTestCase
class {{ type }}ManagerTest extends AbstractManagerTestCase
{
/**
* @var \{{ namespace|split('\\')|first }}\Service\{{ type }}
* @var \{{ namespace }}\Domain\{{ type }}\{{ type }}Manager
*/
protected $service;
protected $manager;
public function setUp()
{
parent::setUp();
$this->service = $this->container->get('{{ namespace|split('\\')|first|lower }}.service.{{ type|lower }}');
$this->service = $this->container->get({{ type }}Manager::class);
$this->primaryId = $this->getFirstId();
//$this->loginUser('admin@host.loc', $this->getUser();
}
......
......@@ -9,18 +9,21 @@
*/
namespace Cwd\GeneratorBundle\Service;
use Sensio\Bundle\GeneratorBundle\Generator\Generator;
use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
/**
* ClassGenerator.
*
* CWD Class Generator
* Based on the SensioGeneratorBundle
* @author Fabien Potencier <fabien@symfony.com>
* @author Ludwig Ruderstaller <lr@cwd.at>
*/
class ClassGenerator extends Generator
class ClassGenerator
{
private $filesystem;
private $skeletonDirs;
private static $output;
/**
* Constructor.
......@@ -30,206 +33,126 @@ class ClassGenerator extends Generator
public function __construct(Filesystem $filesystem)
{
$this->filesystem = $filesystem;
$this->setSkeletonDirs();
}
/**
* @param array $skeletonDirs An array of skeleton dirs
*/
public function setSkeletonDirs()
{
$skeletonDirs = array();
$skeletonDirs[] = __DIR__.'/../Resources/skeleton';
$skeletonDirs[] = __DIR__.'/../Resources';
$this->skeletonDirs = $skeletonDirs;
}
/**
* @param BundleInterface $bundle
* @param string $namespace
* @param string $controller
* @param string $routeFormat
* @param string $templateFormat
* @param string $templatePath
*
* @return string
*/
public function generateController(
BundleInterface $bundle,
$controller,
$routeFormat,
$templateFormat,
$templatePath
$namespace,
$controller
) {
$dir = $bundle->getPath();
$controllerFile = $dir.'/Controller/'.$controller.'Controller.php';
$serviceFile = $dir.'/../../Service/'.$controller.'Service.php';
$eventFile = $dir.'/../../Service/Event/'.$controller.'Event.php';
$testFile = $dir.'/../../Tests/Service/'.$controller.'Test.php';
$exceptionFile = $dir.'/../../Service/Exception/'.$controller.'NotFoundException.php';
$gridFile = $dir.'/Grid/'.$controller.'Grid.php';
$formFile = $dir.'/Form/Type/'.$controller.'Type.php';
if (file_exists($controllerFile)) {
throw new \RuntimeException(sprintf('Controller "%s" already exists', $controller));
}
$controllerFile = 'src/Infrastructure/Web/Controller/'.$controller.'Controller.php';
$exceptionFile = 'src/Domain/'.$controller.'/Exception/'.$controller.'NotFoundException.php';
$managerFile = 'src/Domain/'.$controller.'/'.$controller.'Manager.php';
$repositoryFile = 'src/Domain/'.$controller.'/'.$controller.'Repository.php';
$testFile = 'tests/'.$controller.'/'.$controller.'ManagerTest.php';
$gridFile = 'src/Infrastructure/Web/Grid/'.$controller.'Grid.php';
$formFile = 'src/Infrastructure/Web/Form/'.$controller.'Type.php';
$parameters = array(
'namespace' => $bundle->getNamespace(),
'bundle' => $bundle->getName(),
'namespace' => $namespace,
'type' => $controller,
'format' => array(
'routing' => $routeFormat,
'templating' => $templateFormat,
),
'templatePath' => $templatePath,
'controller' => $controller,
);
$this->renderFile('Controller.php.twig', $controllerFile, $parameters);
$this->renderFile('Service.php.twig', $serviceFile, $parameters);
$this->renderFile('Form.php.twig', $formFile, $parameters);
$this->renderFile('Exception.php.twig', $exceptionFile, $parameters);
$this->renderFile('Manager.php.twig', $managerFile, $parameters);
$this->renderFile('Repository.php.twig', $repositoryFile, $parameters);
$this->renderFile('Form.php.twig', $formFile, $parameters);
$this->renderFile('Grid.php.twig', $gridFile, $parameters);
$this->renderFile('Tests/Test.php.twig', $testFile, $parameters);