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

initial commit

parents
<?php
/*
* This file is part of CWD Generator Bundle
*
* (c)2016 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 Cwd\GeneratorBundle\Command;
use Cwd\GeneratorBundle\Service\ClassGenerator;
use Sensio\Bundle\GeneratorBundle\Command\GeneratorCommand;
use Sensio\Bundle\GeneratorBundle\Command\Validators;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
/**
* Default Command.
*
* @author Ludwig Ruderstaller <lr@cwd.at>
*/
class GenerateFilesCommand extends GeneratorCommand
{
/**
* @see Command
*/
public function configure()
{
$this
->setDefinition(array(
new InputOption(
'controller',
'',
InputOption::VALUE_REQUIRED,
'The name of the controller to create'
),
new InputOption(
'route-format',
'',
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'
),
))
->setDescription('Generates default classes for entity')
->setHelp('')
->setName('cwd:generate:classes');
}
/**
* @param InputInterface $input
* @param OutputInterface $output
*/
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);
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')
);
$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'));
}
}
<?php
/*
* This file is part of CWD Generator Bundle
*
* (c)2016 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 Cwd\GeneratorBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
/**
* Class cwdGeneratorBundle
* @package Cwd\GeneratorBundle
* @author Ludwig Ruderstaler <lr@cwd.at>
*/
class CwdGeneratorBundle extends Bundle
{
}
<?php
/*
* This file is part of CWD Generator Bundle
*
* (c)2016 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 Cwd\GeneratorBundle\DependencyInjection;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;
/**
* This is the class that validates and merges configuration from your app/config files.
*
* To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/configuration.html}
*/
class Configuration implements ConfigurationInterface
{
/**
* {@inheritdoc}
*/
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('cwd_generator');
// 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;
}
}
<?php
/*
* This file is part of CWD Generator Bundle
*
* (c)2016 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 Cwd\GeneratorBundle\DependencyInjection;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader;
/**
* This is the class that loads and manages your bundle configuration.
*
* @link http://symfony.com/doc/current/cookbook/bundles/extension.html
*/
class cwdGeneratorExtension extends Extension
{
/**
* {@inheritdoc}
*/
public function load(array $configs, ContainerBuilder $container)
{
$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 }}
*
* (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;
{% block use_statements %}
use {{ namespace }}\Form\Type\{{ type }}Type;
use {{ namespace }}\Grid\{{ type }}Grid;
use {{ namespace|split('\\')|first }}\Model\Entity\{{ type }};
use Symfony\Component\HttpFoundation\JsonResponse;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
{% endblock %}
{% block class_definition %}
/**
* Class {{ type }}Controller
*
* @package {{ namespace }}\Controller
* @author Ludwig Ruderstaller <lr@cwd.at>
*
* @Route("/{{ type|lower }}")
*/
class {{ type }}Controller extends AbstractCrudController
{% endblock class_definition %}
{
{% block class_body %}
/**
* Set raw option values right before validation. This can be used to chain
* options in inheritance setups.
*
* @return array
*/
protected function setOptions()
{
$options = array(
'entityService' => '{{ namespace|split('\\')|first|lower }}.service.{{ type|lower }}',
'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',
'icon' => 'fa fa-users',
'title' => '{{ type }}',
);
return array_merge(parent::setOptions(), $options);
}
/**
* @param Request $request
*
* @Route("/list/data")
* @Method({"GET", "POST"})
* @return JsonResponse
*/
public function ajaxDataAction(Request $request)
{
return parent::ajaxDataAction($request);
}
/**
* @Route("/list")
* @Route("/")
* @Method({"GET"})
* @Template("{{ bundle }}:Grid:list.html.twig")
* @Security("has_role('ROLE_ADMIN')")
*
* @return array
*/
public function listAction()
{
return parent::listAction();
}
/**
* @Route("/detail/{id}")
* @Method({"GET"})
* @Template()
* @ParamConverter("crudObject", class="Model:{{ type }}")
*
* @param {{ type }} $crudObject
*
* @return array<string,{{ type }}>
*/
public function detailAction({{ type }} $crudObject)
{
return array('crudObject' => $crudObject);
}
/**
* @Route("/create")
* @Method({"GET", "POST"})
*
* @param Request $request
*
* @return RedirectResponse|Response
*/
public function createAction(Request $request)
{
$object = new {{ type }}();
return $this->formHandler($object, $request, true);
}
/**
* Edit action
*
* @ParamConverter("crudObject", class="Model:{{ type }}")
* @Route("/edit/{id}")
* @Method({"GET", "POST", "PUT"})
*
* @param {{ type }} $crudObject
* @param Request $request
*
* @return RedirectResponse|Response
*/
public function editAction({{ type }} $crudObject, Request $request)
{
return $this->formHandler($crudObject, $request, false);
}
/**
* @Route("/delete/{id}")
* @ParamConverter("crudObject", class="Model:{{ type }}")
* @Method({"GET", "DELETE"})
*
* @param {{ type }} $crudObject
* @param Request $request
*
* @return RedirectResponse|null
*/
public function deleteAction({{ type }} $crudObject, Request $request)
{
return $this->deleteHandler($crudObject, $request);
}
{% endblock class_body %}
}
<?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 }}
*
* (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;
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 }}
*
* (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;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Security\Core\Authorization\AuthorizationChecker;
/**
* Class {{ type }} Form
*
* @package {{ namespace }}\Form\Type
* @author Ludwig Ruderstaller <lr@cwd.at>
*
*/
class {{ type }}Type extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*
* @return FormBuilderInterface
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', TextType::class, array('label' => 'Name'))
->add('save', SubmitType::class, array('label' => 'Save'));
return $builder;
}
/**
* @param OptionsResolver $resolver
* @return void
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(
array(
'data_class' => '{{ namespace|split('\\')|first }}\Model\Entity\{{ type }}',
)
);
}
}
<?php
/*
* This file is part of {{ namespace|split('\\')|first }}
*
* (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;
use Cwd\BootgridBundle\Column\ActionType;
use Cwd\BootgridBundle\Column\DateType;
use Cwd\BootgridBundle\Column\NumberType;
use Cwd\BootgridBundle\Column\TextType;
use Cwd\BootgridBundle\Grid\AbstractGrid;
use Cwd\BootgridBundle\Grid\GridBuilderInterface;
use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\ORM\QueryBuilder;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
/**
* Class {{ type }}Type
*
* @package {{ namespace }}\Grid
* @author Ludwig Ruderstaller <lr@cwd.at>
*/
class {{ type }}Grid extends AbstractGrid
{
/**
* @param GridBuilderInterface $builder
* @param array $options
*/
public function buildGrid(GridBuilderInterface $builder, array $options)
{
$builder->add(new NumberType('id', 'u.id', ['label' => 'ID', 'identifier' => true]))
->add(new TextType('title', 'u.title', ['label' => 'Title', 'translatable' => true]))
->add(new DateType(
'createdAt',
'u.createdAt',
[
'label' => 'Created',
'format' => 'd.m.Y H:i:s',
]
))
->add(new DateType(
'updatedAt',
'u.updatedAt',
[
'label' => 'Updated',
'visible' => false,
'format' => 'd.m.Y H:i:s',
]
))
->add(new ActionType(
'actions',
'u.id',
[
'label' => '',
'actions' => [
[
'route' => '{{ namespace|split('\\')|first|lower }}_{{ templatePath }}_{{ type }}_edit',
'class' => 'btn-primary',
'icon' => 'fa-edit',
'title' => 'Edit',
],
[
'route' => '{{ namespace|split('\\')|first|lower }}_{{ templatePath }}_{{ type }}_delete',
'class' => 'btn-danger deleterow',
'icon' => 'fa-trash-o',
'title' => 'Delete',
],
]
]