diff --git a/src/Command/MakerCommand.php b/src/Command/MakerCommand.php index 4bf01cb5c..b81b8dbea 100644 --- a/src/Command/MakerCommand.php +++ b/src/Command/MakerCommand.php @@ -32,19 +32,19 @@ */ final class MakerCommand extends Command { - private $fileManager; private $maker; private $rootNamespace; + private $fileManager; private $inputConfig; /** @var ConsoleStyle */ private $io; private $checkDependencies = true; - public function __construct(FileManager $fileManager, MakerInterface $maker, string $rootNamespace) + public function __construct(MakerInterface $maker, string $rootNamespace, FileManager $fileManager) { - $this->fileManager = $fileManager; $this->maker = $maker; $this->rootNamespace = trim($rootNamespace, '\\'); + $this->fileManager = $fileManager; $this->inputConfig = new InputConfiguration(); parent::__construct(); @@ -90,6 +90,16 @@ protected function interact(InputInterface $input, OutputInterface $output) protected function execute(InputInterface $input, OutputInterface $output) { + if (!$this->fileManager->isNamespaceConfiguredToAutoload($this->rootNamespace)) { + throw new \LogicException( + sprintf( + 'It looks like your app may be using a namespace other than "%s".'. + ' See https://symfony.com/doc/current/bundles/SymfonyMakerBundle/index.html for how to configure this.', + $this->rootNamespace + ) + ); + } + $generator = new Generator($this->fileManager, $this->rootNamespace); $this->maker->generate($input, $this->io, $generator); diff --git a/src/DependencyInjection/CompilerPass/MakeCommandRegistrationPass.php b/src/DependencyInjection/CompilerPass/MakeCommandRegistrationPass.php index bc0024fb2..93e936e74 100644 --- a/src/DependencyInjection/CompilerPass/MakeCommandRegistrationPass.php +++ b/src/DependencyInjection/CompilerPass/MakeCommandRegistrationPass.php @@ -35,7 +35,7 @@ public function process(ContainerBuilder $container) $commandDefinition = new ChildDefinition('maker.auto_command.abstract'); $commandDefinition->setClass(MakerCommand::class); - $commandDefinition->replaceArgument(1, new Reference($id)); + $commandDefinition->replaceArgument(0, new Reference($id)); $commandDefinition->addTag('console.command', ['command' => $class::getCommandName()]); $container->setDefinition(sprintf('maker.auto_command.%s', Str::asTwigVariable($class::getCommandName())), $commandDefinition); diff --git a/src/DependencyInjection/MakerExtension.php b/src/DependencyInjection/MakerExtension.php index 906e8ee7c..d9fbc73cf 100644 --- a/src/DependencyInjection/MakerExtension.php +++ b/src/DependencyInjection/MakerExtension.php @@ -40,7 +40,7 @@ public function load(array $configs, ContainerBuilder $container) $rootNamespace = trim($config['root_namespace'], '\\'); $makeCommandDefinition = $container->getDefinition('maker.auto_command.abstract'); - $makeCommandDefinition->replaceArgument(2, $rootNamespace); + $makeCommandDefinition->replaceArgument(1, $rootNamespace); $doctrineHelperDefinition = $container->getDefinition('maker.doctrine_helper'); $doctrineHelperDefinition->replaceArgument(0, $rootNamespace.'\\Entity'); diff --git a/src/FileManager.php b/src/FileManager.php index 60a30ed0a..386abe1b3 100644 --- a/src/FileManager.php +++ b/src/FileManager.php @@ -163,6 +163,11 @@ public function getNamespacePrefixForClass(string $className): string return $this->autoloaderUtil->getNamespacePrefixForClass($className); } + public function isNamespaceConfiguredToAutoload(string $namespace): string + { + return $this->autoloaderUtil->isNamespaceConfiguredToAutoload($namespace); + } + /** * Resolve '../' in paths (like real_path), but for non-existent files. * diff --git a/src/Resources/config/services.xml b/src/Resources/config/services.xml index e60dbe9c8..5f29ef56b 100644 --- a/src/Resources/config/services.xml +++ b/src/Resources/config/services.xml @@ -33,9 +33,9 @@ - + diff --git a/tests/Command/MakerCommandTest.php b/tests/Command/MakerCommandTest.php index 09bac5e34..f1a078ea4 100644 --- a/tests/Command/MakerCommandTest.php +++ b/tests/Command/MakerCommandTest.php @@ -26,7 +26,24 @@ public function testExceptionOnMissingDependencies() $fileManager = $this->createMock(FileManager::class); - $command = new MakerCommand($fileManager, $maker, 'App\\'); + $command = new MakerCommand($maker, 'App', $fileManager); + // needed because it's normally set by the Application + $command->setName('make:foo'); + $tester = new CommandTester($command); + $tester->execute(array()); + } + + /** + * @expectedException \LogicException + * @expectedExceptionMessage using a namespace other than "Unknown". + */ + public function testExceptionOnUnknownRootNamespace() + { + $maker = $this->createMock(MakerInterface::class); + + $fileManager = $this->createMock(FileManager::class); + + $command = new MakerCommand($maker, 'Unknown', $fileManager); // needed because it's normally set by the Application $command->setName('make:foo'); $tester = new CommandTester($command);