Skip to content

Commit

Permalink
Add exception with hint message when root namespace is not found
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergey Rabochiy committed May 14, 2018
1 parent 758a2c9 commit f34fa39
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 7 deletions.
16 changes: 13 additions & 3 deletions src/Command/MakerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/DependencyInjection/MakerExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
5 changes: 5 additions & 0 deletions src/FileManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
2 changes: 1 addition & 1 deletion src/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@
</service>

<service id="maker.auto_command.abstract" class="Symfony\Bundle\MakerBundle\Command\MakerCommand" abstract="true">
<argument type="service" id="maker.file_manager" />
<argument /> <!-- maker -->
<argument /> <!-- root namespace -->
<argument type="service" id="maker.file_manager" />
</service>
</services>
</container>
19 changes: 18 additions & 1 deletion tests/Command/MakerCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit f34fa39

Please sign in to comment.