Skip to content

Commit 1c8cf9a

Browse files
committed
Make the Generator class a service
1 parent 3d2918a commit 1c8cf9a

File tree

7 files changed

+24
-37
lines changed

7 files changed

+24
-37
lines changed

src/Command/MakerCommand.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@ final class MakerCommand extends Command
4040
private $checkDependencies = true;
4141
private $generator;
4242

43-
public function __construct(MakerInterface $maker, FileManager $fileManager)
43+
public function __construct(MakerInterface $maker, FileManager $fileManager, Generator $generator)
4444
{
4545
$this->maker = $maker;
4646
$this->fileManager = $fileManager;
4747
$this->inputConfig = new InputConfiguration();
48-
$this->generator = new Generator($this->fileManager, 'App\\');
48+
$this->generator = $generator;
4949

5050
parent::__construct();
5151
}
@@ -85,7 +85,7 @@ protected function interact(InputInterface $input, OutputInterface $output)
8585
$input->setArgument($argument->getName(), $value);
8686
}
8787

88-
$this->maker->interact($input, $this->io, $this, $this->generator);
88+
$this->maker->interact($input, $this->io, $this);
8989
}
9090

9191
protected function execute(InputInterface $input, OutputInterface $output)

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/GeneratorAwareMakerInterface.php

Lines changed: 0 additions & 28 deletions
This file was deleted.

src/Maker/MakeEntity.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
use Symfony\Bundle\MakerBundle\Doctrine\DoctrineHelper;
2222
use Symfony\Bundle\MakerBundle\Exception\RuntimeCommandException;
2323
use Symfony\Bundle\MakerBundle\Generator;
24-
use Symfony\Bundle\MakerBundle\GeneratorAwareMakerInterface;
2524
use Symfony\Bundle\MakerBundle\InputAwareMakerInterface;
2625
use Symfony\Bundle\MakerBundle\InputConfiguration;
2726
use Symfony\Bundle\MakerBundle\Str;
@@ -43,17 +42,25 @@
4342
* @author Ryan Weaver <[email protected]>
4443
* @author Kévin Dunglas <[email protected]>
4544
*/
46-
final class MakeEntity extends AbstractMaker implements InputAwareMakerInterface, GeneratorAwareMakerInterface
45+
final class MakeEntity extends AbstractMaker implements InputAwareMakerInterface
4746
{
4847
private $fileManager;
4948
private $doctrineHelper;
5049
private $projectDirectory;
50+
private $generator;
5151

52-
public function __construct(FileManager $fileManager, string $projectDirectory, DoctrineHelper $doctrineHelper)
52+
public function __construct(FileManager $fileManager, string $projectDirectory, DoctrineHelper $doctrineHelper, Generator $generator = null)
5353
{
5454
$this->fileManager = $fileManager;
5555
$this->projectDirectory = $projectDirectory;
5656
$this->doctrineHelper = $doctrineHelper;
57+
58+
if (null === $generator) {
59+
@trigger_error(sprintf('Passing a "%s" instance as 4th argument is mandatory since version 1.5.', Generator::class), E_USER_DEPRECATED);
60+
$this->generator = new Generator($fileManager, 'App\\');
61+
} else {
62+
$this->generator = $generator;
63+
}
5764
}
5865

5966
public static function getCommandName(): string
@@ -75,7 +82,7 @@ public function configureCommand(Command $command, InputConfiguration $inputConf
7582
$inputConf->setArgumentAsNonInteractive('name');
7683
}
7784

78-
public function interact(InputInterface $input, ConsoleStyle $io, Command $command, Generator $generator = null)
85+
public function interact(InputInterface $input, ConsoleStyle $io, Command $command)
7986
{
8087
if ($input->getArgument('name')) {
8188
return;
@@ -116,7 +123,7 @@ public function interact(InputInterface $input, ConsoleStyle $io, Command $comma
116123
if (
117124
!$input->getOption('api-resource') &&
118125
class_exists(ApiResource::class) &&
119-
!class_exists($generator->createClassNameDetails($value, 'Entity\\')->getFullName())
126+
!class_exists($this->generator->createClassNameDetails($value, 'Entity\\')->getFullName())
120127
) {
121128
$description = $command->getDefinition()->getOption('api-resource')->getDescription();
122129
$question = new ConfirmationQuestion($description, false);

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>

tests/Command/MakerCommandTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Symfony\Bundle\MakerBundle\Command\MakerCommand;
77
use Symfony\Bundle\MakerBundle\DependencyBuilder;
88
use Symfony\Bundle\MakerBundle\FileManager;
9+
use Symfony\Bundle\MakerBundle\Generator;
910
use Symfony\Bundle\MakerBundle\MakerInterface;
1011
use Symfony\Component\Console\Tester\CommandTester;
1112

@@ -26,7 +27,7 @@ public function testExceptionOnMissingDependencies()
2627

2728
$fileManager = $this->createMock(FileManager::class);
2829

29-
$command = new MakerCommand($maker, $fileManager);
30+
$command = new MakerCommand($maker, $fileManager, new Generator($fileManager, 'App\\'));
3031
// needed because it's normally set by the Application
3132
$command->setName('make:foo');
3233
$tester = new CommandTester($command);

0 commit comments

Comments
 (0)