add make:repository-query and make:entity-factory

This commit is contained in:
Simon Vieille 2021-06-03 23:21:16 +02:00
parent 6446a4a5d4
commit ae652b5059
7 changed files with 232 additions and 16 deletions

View File

@ -31,27 +31,27 @@ class MakeCrudController extends AbstractMaker
->addArgument(
'controller-class',
InputArgument::OPTIONAL,
'Choose a name for your CRUD controller class (e.g. <fg=yellow>FooAdminController</>)'
'Choose a name for your CRUD controller class (e.g. <fg=yellow>MyEntityAdminController</>)'
)
->addArgument(
'entity-class',
InputArgument::OPTIONAL,
'Define the entity (e.g. <fg=yellow>App\Entity\Foo</>)'
'Define the entity (e.g. <fg=yellow>MyEntity</>)'
)
->addArgument(
'repository-query-class',
InputArgument::OPTIONAL,
'Define the repository query (e.g. <fg=yellow>App\Repository\FooRepositoryQuery</>)'
'Define the repository query (e.g. <fg=yellow>MyEntityRepositoryQuery</>)'
)
->addArgument(
'factory-class',
InputArgument::OPTIONAL,
'Define the factory (e.g. <fg=yellow>App\Factory\FooFactory</>)'
'Define the factory (e.g. <fg=yellow>MyEntityFactory</>)'
)
->addArgument(
'form-class',
InputArgument::OPTIONAL,
'Define the form (e.g. <fg=yellow>App\Form\FooType</>)'
'Define the form (e.g. <fg=yellow>MyEntityType</>)'
)
->setHelp('')
;
@ -65,21 +65,41 @@ class MakeCrudController extends AbstractMaker
'Controller'
);
$entity = u($input->getArgument('entity-class'));
$lastBackSlashIndex = $entity->indexOfLast('\\');
$route = u($entity->slice($lastBackSlashIndex))->snake();
$entityDetails = $generator->createClassNameDetails(
$input->getArgument('entity-class'),
'Entity\\',
''
);
$repoDetails = $generator->createClassNameDetails(
$input->getArgument('repository-query-class'),
'Repository\\',
''
);
$formDetails = $generator->createClassNameDetails(
$input->getArgument('form-class'),
'Type\\',
''
);
$factoryDetails = $generator->createClassNameDetails(
$input->getArgument('factory-class'),
'Factory\\',
''
);
$options = [
'entity' => (string) $entity,
'route' => (string) $route,
'repository_query' => $input->getArgument('repository-query-class'),
'form' => $input->getArgument('form-class'),
'factory' => $input->getArgument('factory-class'),
'entity' => $entityDetails->getFullName(),
'route' => (string) u($entityDetails->getShortName())->snake(),
'repository_query' => $repoDetails->getFullName(),
'form' => $formDetails->getFullName(),
'factory' => $factoryDetails->getFullName(),
];
$controllerPath = $generator->generateController(
$controllerClassNameDetails->getFullName(),
__DIR__.'/../Resources/maker/crud-controller/CrudController.tpl.php',
__DIR__.'/../Resources/maker/controller/CrudController.tpl.php',
$options
);

View File

@ -0,0 +1,82 @@
<?php
namespace App\Core\Maker;
use Doctrine\Common\Annotations\Annotation;
use Symfony\Bundle\MakerBundle\ConsoleStyle;
use Symfony\Bundle\MakerBundle\DependencyBuilder;
use Symfony\Bundle\MakerBundle\Generator;
use Symfony\Bundle\MakerBundle\InputConfiguration;
use Symfony\Bundle\MakerBundle\Maker\AbstractMaker;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use function Symfony\Component\String\u;
class MakeEntityFactory extends AbstractMaker
{
public static function getCommandName(): string
{
return 'make:entity-factory';
}
public static function getCommandDescription(): string
{
return 'Creates a factory';
}
public function configureCommand(Command $command, InputConfiguration $inputConf)
{
$command
->addArgument(
'entity-class',
InputArgument::OPTIONAL,
'Define the entity (e.g. <fg=yellow>MyEntity</>)'
)
->addArgument(
'factory-class',
InputArgument::OPTIONAL,
'Choose a name for your factory (e.g. <fg=yellow>MyEntityFactory</>)'
)
->setHelp('')
;
}
public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator)
{
$entityDetails = $generator->createClassNameDetails(
$input->getArgument('entity-class'),
'Entity\\',
''
);
$factoryDetails = $generator->createClassNameDetails(
$input->getArgument('factory-class'),
'Factory\\',
''
);
$options = [
'entity' => $entityDetails->getFullName(),
];
$factoryPath = $generator->generateController(
$factoryDetails->getFullName(),
__DIR__.'/../Resources/maker/entity/EntityFactory.tpl.php',
$options
);
$generator->writeChanges();
$this->writeSuccessMessage($io);
$io->text('Next: Open your new factory class and configure it!');
}
public function configureDependencies(DependencyBuilder $dependencies)
{
$dependencies->addClassDependency(
Annotation::class,
'doctrine/annotations'
);
}
}

View File

@ -0,0 +1,85 @@
<?php
namespace App\Core\Maker;
use Doctrine\Common\Annotations\Annotation;
use Symfony\Bundle\MakerBundle\ConsoleStyle;
use Symfony\Bundle\MakerBundle\DependencyBuilder;
use Symfony\Bundle\MakerBundle\Generator;
use Symfony\Bundle\MakerBundle\InputConfiguration;
use Symfony\Bundle\MakerBundle\Maker\AbstractMaker;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use function Symfony\Component\String\u;
class MakeRepositoryQuery extends AbstractMaker
{
public static function getCommandName(): string
{
return 'make:repository-query';
}
public static function getCommandDescription(): string
{
return 'Creates a repository query';
}
public function configureCommand(Command $command, InputConfiguration $inputConf)
{
$command
->addArgument(
'repository-class',
InputArgument::OPTIONAL,
'Define the repository (e.g. <fg=yellow>MyEntityRepository</>)'
)
->setHelp('')
;
}
public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator)
{
$repositoryClass = $input->getArgument('repository-class');
$repositoryDetails = $generator->createClassNameDetails(
$repositoryClass,
'Repository\\',
''
);
$queryDetails = $generator->createClassNameDetails(
$repositoryClass,
'Repository\\',
'Query'
);
$id = u($queryDetails->getShortName())
->truncate(1)
->lower()
;
$options = [
'repository' => $repositoryDetails->getFullName(),
'id' => $id,
];
$factoryPath = $generator->generateController(
$queryDetails->getFullName(),
__DIR__.'/../Resources/maker/repository/RepositoryQuery.tpl.php',
$options
);
$generator->writeChanges();
$this->writeSuccessMessage($io);
$io->text('Next: Open your new repository query class and configure it!');
}
public function configureDependencies(DependencyBuilder $dependencies)
{
$dependencies->addClassDependency(
Annotation::class,
'doctrine/annotations'
);
}
}

View File

@ -0,0 +1,14 @@
<?= "<?php\n" ?>
namespace <?= $namespace; ?>;
use App\Core\Factory\FactoryInterface;
use <?= $entity ?> as Entity;
class <?= $class_name; ?> implements FactoryInterface
{
public function create(): Entity
{
return new Entity();
}
}

View File

@ -0,0 +1,15 @@
<?= "<?php\n" ?>
namespace <?= $namespace; ?>;
use App\Core\Repository\RepositoryQuery;
use Knp\Component\Pager\PaginatorInterface;
use <?= $repository; ?> as Repository;
class <?= $class_name; ?> extends RepositoryQuery
{
public function __construct(Repository $repository, PaginatorInterface $paginator)
{
parent::__construct($repository, '<?= $id; ?>', $paginator);
}
}

View File

@ -248,14 +248,14 @@
{% block list_footer_before %}{% endblock %}
{% if isSortable %}
<div class="d-block mb-2">
<div class="mb-2">
<span class="fa fa-hand-pointer"></span>
{{ 'You can sort items with drag & drop'|trans }}
</div>
{% endif %}
{% if configuration.hasBatchAction(context) %}
<div class="d-block">
<div class="mb-2">
<form class="form-inline" action="{{ path(configuration.pageRoute('batch'), {page: pager.currentPageNumber}) }}" id="form-batch" method="POST">
<select class="form-control my-1 mr-sm-2" name="batch[target]">
<option value="selection">{{ 'For selection'|trans }}</option>