Skip to content

Commit e6c940f

Browse files
dunglasweaverryan
authored andcommitted
Remove some unused props
1 parent 319a73d commit e6c940f

File tree

13 files changed

+116
-24
lines changed

13 files changed

+116
-24
lines changed

src/Command/MakerCommand.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,14 @@ final class MakerCommand extends Command
3838
/** @var ConsoleStyle */
3939
private $io;
4040
private $checkDependencies = true;
41+
private $generator;
4142

42-
public function __construct(MakerInterface $maker, FileManager $fileManager)
43+
public function __construct(MakerInterface $maker, FileManager $fileManager, Generator $generator)
4344
{
4445
$this->maker = $maker;
4546
$this->fileManager = $fileManager;
4647
$this->inputConfig = new InputConfiguration();
48+
$this->generator = $generator;
4749

4850
parent::__construct();
4951
}
@@ -60,7 +62,7 @@ protected function initialize(InputInterface $input, OutputInterface $output)
6062

6163
if ($this->checkDependencies) {
6264
$dependencies = new DependencyBuilder();
63-
$this->maker->configureDependencies($dependencies);
65+
$this->maker->configureDependencies($dependencies, $input);
6466

6567
if ($missingPackagesMessage = $dependencies->getMissingPackagesMessage($this->getName())) {
6668
throw new RuntimeCommandException($missingPackagesMessage);
@@ -88,12 +90,10 @@ protected function interact(InputInterface $input, OutputInterface $output)
8890

8991
protected function execute(InputInterface $input, OutputInterface $output)
9092
{
91-
$generator = new Generator($this->fileManager, 'App\\');
92-
93-
$this->maker->generate($input, $this->io, $generator);
93+
$this->maker->generate($input, $this->io, $this->generator);
9494

9595
// sanity check for custom makers
96-
if ($generator->hasPendingOperations()) {
96+
if ($this->generator->hasPendingOperations()) {
9797
throw new \LogicException('Make sure to call the writeChanges() method on the generator.');
9898
}
9999
}

src/DependencyInjection/CompilerPass/MakeCommandRegistrationPass.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public function process(ContainerBuilder $container)
3838
)->setArguments([
3939
new Reference($id),
4040
new Reference('maker.file_manager'),
41+
new Reference('maker.generator'),
4142
])->addTag('console.command', ['command' => $class::getCommandName()]);
4243
}
4344
}

src/Doctrine/EntityRegenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ final class EntityRegenerator
3030
private $generator;
3131
private $overwrite;
3232

33-
public function __construct(DoctrineHelper $doctrineHelper, FileManager $fileManager, Generator $generator, string $projectDirectory, bool $overwrite)
33+
public function __construct(DoctrineHelper $doctrineHelper, FileManager $fileManager, Generator $generator, bool $overwrite)
3434
{
3535
$this->doctrineHelper = $doctrineHelper;
3636
$this->fileManager = $fileManager;

src/InputAwareMakerInterface.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony MakerBundle package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\MakerBundle;
13+
14+
use Symfony\Component\Console\Input\InputInterface;
15+
16+
/**
17+
* Lets the configureDependencies method access to the command's input.
18+
*
19+
* @author Kévin Dunglas <[email protected]>
20+
*/
21+
interface InputAwareMakerInterface extends MakerInterface
22+
{
23+
/**
24+
* {@inheritdoc}
25+
*/
26+
public function configureDependencies(DependencyBuilder $dependencies, InputInterface $input = null);
27+
}

src/Maker/MakeController.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use Doctrine\Common\Annotations\Annotation;
1515
use Symfony\Bundle\MakerBundle\ConsoleStyle;
1616
use Symfony\Bundle\MakerBundle\DependencyBuilder;
17-
use Symfony\Bundle\MakerBundle\FileManager;
1817
use Symfony\Bundle\MakerBundle\Generator;
1918
use Symfony\Bundle\MakerBundle\InputConfiguration;
2019
use Symfony\Bundle\MakerBundle\Str;
@@ -29,13 +28,6 @@
2928
*/
3029
final class MakeController extends AbstractMaker
3130
{
32-
private $fileManager;
33-
34-
public function __construct(FileManager $fileManager)
35-
{
36-
$this->fileManager = $fileManager;
37-
}
38-
3931
public static function getCommandName(): string
4032
{
4133
return 'make:controller';

src/Maker/MakeEntity.php

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Bundle\MakerBundle\Maker;
1313

14+
use ApiPlatform\Core\Annotation\ApiResource;
1415
use Doctrine\Bundle\DoctrineBundle\DoctrineBundle;
1516
use Doctrine\DBAL\Types\Type;
1617
use Doctrine\ORM\Mapping\Column;
@@ -20,6 +21,7 @@
2021
use Symfony\Bundle\MakerBundle\Doctrine\DoctrineHelper;
2122
use Symfony\Bundle\MakerBundle\Exception\RuntimeCommandException;
2223
use Symfony\Bundle\MakerBundle\Generator;
24+
use Symfony\Bundle\MakerBundle\InputAwareMakerInterface;
2325
use Symfony\Bundle\MakerBundle\InputConfiguration;
2426
use Symfony\Bundle\MakerBundle\Str;
2527
use Symfony\Bundle\MakerBundle\Doctrine\EntityRegenerator;
@@ -31,24 +33,33 @@
3133
use Symfony\Component\Console\Input\InputArgument;
3234
use Symfony\Component\Console\Input\InputInterface;
3335
use Symfony\Component\Console\Input\InputOption;
36+
use Symfony\Component\Console\Question\ConfirmationQuestion;
3437
use Symfony\Component\Console\Question\Question;
3538
use Symfony\Component\Finder\SplFileInfo;
3639

3740
/**
3841
* @author Javier Eguiluz <[email protected]>
3942
* @author Ryan Weaver <[email protected]>
43+
* @author Kévin Dunglas <[email protected]>
4044
*/
41-
final class MakeEntity extends AbstractMaker
45+
final class MakeEntity extends AbstractMaker implements InputAwareMakerInterface
4246
{
4347
private $fileManager;
4448
private $doctrineHelper;
45-
private $projectDirectory;
49+
private $generator;
4650

47-
public function __construct(FileManager $fileManager, string $projectDirectory, DoctrineHelper $doctrineHelper)
51+
public function __construct(FileManager $fileManager, string $projectDirectory, DoctrineHelper $doctrineHelper, Generator $generator = null)
4852
{
4953
$this->fileManager = $fileManager;
50-
$this->projectDirectory = $projectDirectory;
5154
$this->doctrineHelper = $doctrineHelper;
55+
// $projectDirectory is unused, argument kept for BC
56+
57+
if (null === $generator) {
58+
@trigger_error(sprintf('Passing a "%s" instance as 4th argument is mandatory since version 1.5.', Generator::class), E_USER_DEPRECATED);
59+
$this->generator = new Generator($fileManager, 'App\\');
60+
} else {
61+
$this->generator = $generator;
62+
}
5263
}
5364

5465
public static function getCommandName(): string
@@ -59,8 +70,9 @@ public static function getCommandName(): string
5970
public function configureCommand(Command $command, InputConfiguration $inputConf)
6071
{
6172
$command
62-
->setDescription('Creates or updates a Doctrine entity class')
73+
->setDescription('Creates or updates a Doctrine entity class, and optionally an API Platform resource')
6374
->addArgument('name', InputArgument::OPTIONAL, sprintf('Class name of the entity to create or update (e.g. <fg=yellow>%s</>)', Str::asClassName(Str::getRandomTerm())))
75+
->addOption('api-resource', 'a', InputOption::VALUE_NONE, 'Mark this class as an API Platform resource (expose a CRUD API for it)')
6476
->addOption('regenerate', null, InputOption::VALUE_NONE, 'Instead of adding new fields, simply generate the methods (e.g. getter/setter) for existing fields')
6577
->addOption('overwrite', null, InputOption::VALUE_NONE, 'Overwrite any existing getter/setter methods')
6678
->setHelp(file_get_contents(__DIR__.'/../Resources/help/MakeEntity.txt'))
@@ -106,6 +118,18 @@ public function interact(InputInterface $input, ConsoleStyle $io, Command $comma
106118
$value = $io->askQuestion($question);
107119

108120
$input->setArgument('name', $value);
121+
122+
if (
123+
!$input->getOption('api-resource') &&
124+
class_exists(ApiResource::class) &&
125+
!class_exists($this->generator->createClassNameDetails($value, 'Entity\\')->getFullName())
126+
) {
127+
$description = $command->getDefinition()->getOption('api-resource')->getDescription();
128+
$question = new ConfirmationQuestion($description, false);
129+
$value = $io->askQuestion($question);
130+
131+
$input->setOption('api-resource', $value);
132+
}
109133
}
110134

111135
public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator)
@@ -142,6 +166,7 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
142166
'doctrine/Entity.tpl.php',
143167
[
144168
'repository_full_class_name' => $repositoryClassDetails->getFullName(),
169+
'api_resource' => $input->getOption('api-resource'),
145170
]
146171
);
147172

@@ -269,8 +294,15 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
269294
]);
270295
}
271296

272-
public function configureDependencies(DependencyBuilder $dependencies)
297+
public function configureDependencies(DependencyBuilder $dependencies, InputInterface $input = null)
273298
{
299+
if (null !== $input && $input->getOption('api-resource')) {
300+
$dependencies->addClassDependency(
301+
ApiResource::class,
302+
'api'
303+
);
304+
}
305+
274306
// guarantee DoctrineBundle
275307
$dependencies->addClassDependency(
276308
DoctrineBundle::class,
@@ -775,7 +807,7 @@ private function isClassInVendor(string $class): bool
775807

776808
private function regenerateEntities(string $classOrNamespace, bool $overwrite, Generator $generator)
777809
{
778-
$regenerator = new EntityRegenerator($this->doctrineHelper, $this->fileManager, $generator, $this->projectDirectory, $overwrite);
810+
$regenerator = new EntityRegenerator($this->doctrineHelper, $this->fileManager, $generator, $overwrite);
779811
$regenerator->regenerateEntities($classOrNamespace);
780812
}
781813

src/Maker/MakeForm.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public function configureCommand(Command $command, InputConfiguration $inputConf
4949
$command
5050
->setDescription('Creates a new form class')
5151
->addArgument('name', InputArgument::OPTIONAL, sprintf('The name of the form class (e.g. <fg=yellow>%sType</>)', Str::asClassName(Str::getRandomTerm())))
52-
->addArgument('bound-class', InputArgument::OPTIONAL, 'The name of Entity or custom model class that the new form will be bound to (empty for none)')
52+
->addArgument('bound-class', InputArgument::OPTIONAL, 'The name of Entity or fully qualified model class name that the new form will be bound to (empty for none)')
5353
->setHelp(file_get_contents(__DIR__.'/../Resources/help/MakeForm.txt'))
5454
;
5555

src/Resources/config/makers.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
<argument type="service" id="maker.file_manager" />
3030
<argument>%kernel.project_dir%</argument>
3131
<argument type="service" id="maker.doctrine_helper" />
32+
<argument type="service" id="maker.generator" />
3233
<tag name="maker.command" />
3334
</service>
3435

src/Resources/config/services.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,10 @@
2626
<service id="maker.doctrine_helper" class="Symfony\Bundle\MakerBundle\Doctrine\DoctrineHelper">
2727
<argument type="service" id="doctrine" on-invalid="ignore"/>
2828
</service>
29+
30+
<service id="maker.generator" class="Symfony\Bundle\MakerBundle\Generator">
31+
<argument type="service" id="maker.file_manager" />
32+
<argument>App\</argument>
33+
</service>
2934
</services>
3035
</container>

src/Resources/help/MakeEntity.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ The <info>%command.name%</info> command creates or updates an entity and reposit
44

55
If the argument is missing, the command will ask for the entity class name interactively.
66

7+
You can also mark this class as an API Platform resource. A hypermedia CRUD API will
8+
automatically be available for this entity class:
9+
10+
<info>php %command.full_name% --api-resource</info>
11+
712
You can also generate all the getter/setter/adder/remover methods
813
for the properties of existing entities:
914

0 commit comments

Comments
 (0)