Skip to content

Commit

Permalink
Add configuration of root and entity namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergey Rabochiy committed May 4, 2018
1 parent a76bc31 commit 51d7138
Show file tree
Hide file tree
Showing 11 changed files with 88 additions and 16 deletions.
6 changes: 4 additions & 2 deletions src/Command/MakerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,17 @@ final class MakerCommand extends Command
{
private $maker;
private $fileManager;
private $rootNamespace;
private $inputConfig;
/** @var ConsoleStyle */
private $io;
private $checkDependencies = true;

public function __construct(MakerInterface $maker, FileManager $fileManager)
public function __construct(MakerInterface $maker, FileManager $fileManager, string $rootNamespace)
{
$this->maker = $maker;
$this->fileManager = $fileManager;
$this->rootNamespace = trim($rootNamespace, '\\');
$this->inputConfig = new InputConfiguration();

parent::__construct();
Expand Down Expand Up @@ -88,7 +90,7 @@ protected function interact(InputInterface $input, OutputInterface $output)

protected function execute(InputInterface $input, OutputInterface $output)
{
$generator = new Generator($this->fileManager, 'App\\');
$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 @@ -17,6 +17,7 @@
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Parameter;
use Symfony\Component\DependencyInjection\Reference;

class MakeCommandRegistrationPass implements CompilerPassInterface
Expand All @@ -38,6 +39,7 @@ public function process(ContainerBuilder $container)
)->setArguments([
new Reference($id),
new Reference('maker.file_manager'),
new Parameter('maker.root_namespace'),
])->addTag('console.command', ['command' => $class::getCommandName()]);
}
}
Expand Down
36 changes: 36 additions & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

/*
* This file is part of the Symfony MakerBundle package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\MakerBundle\DependencyInjection;

use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

class Configuration implements ConfigurationInterface
{
/**
* {@inheritdoc}
*/
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('maker');

$rootNode
->children()
->scalarNode('root_namespace')->defaultValue('App')->end()
->scalarNode('entity_namespace')->defaultNull()->end()
->end()
;

return $treeBuilder;
}
}
22 changes: 22 additions & 0 deletions src/DependencyInjection/MakerExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,29 @@ public function load(array $configs, ContainerBuilder $container)
$loader->load('services.xml');
$loader->load('makers.xml');

$configuration = $this->getConfiguration($configs, $container);
$config = $this->processConfiguration($configuration, $configs);

$container->setParameter('maker.root_namespace', $config['root_namespace']);
$container->setParameter('maker.entity_namespace', $config['entity_namespace'] ?: '%maker.root_namespace%\\Entity');

$container->registerForAutoconfiguration(MakerInterface::class)
->addTag(MakeCommandRegistrationPass::MAKER_TAG);
}

/**
* {@inheritdoc}
*/
public function getAlias()
{
return 'maker';
}

/**
* {@inheritdoc}
*/
public function getConfiguration(array $config, ContainerBuilder $container)
{
return new Configuration();
}
}
10 changes: 8 additions & 2 deletions src/Doctrine/DoctrineHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,19 @@
*/
final class DoctrineHelper
{
/**
* @var string
*/
private $entityNamespace;

/**
* @var ManagerRegistry
*/
private $registry;

public function __construct(ManagerRegistry $registry = null)
public function __construct(string $entityNamespace, ManagerRegistry $registry = null)
{
$this->entityNamespace = trim($entityNamespace, '\\');
$this->registry = $registry;
}

Expand Down Expand Up @@ -94,7 +100,7 @@ public function getEntitiesForAutocomplete(): array

/* @var ClassMetadata $metadata */
foreach (array_keys($allMetadata) as $classname) {
$entityClassDetails = new ClassNameDetails($classname, 'App\\Entity');
$entityClassDetails = new ClassNameDetails($classname, $this->entityNamespace);
$entities[] = $entityClassDetails->getRelativeName();
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ class Generator
private $pendingOperations = [];
private $namespacePrefix;

public function __construct(FileManager $fileManager, $namespacePrefix)
public function __construct(FileManager $fileManager, string $namespacePrefix)
{
$this->fileManager = $fileManager;
$this->twigHelper = new GeneratorTwigHelper($fileManager);
$this->namespacePrefix = rtrim($namespacePrefix, '\\');
$this->namespacePrefix = trim($namespacePrefix, '\\');
}

/**
Expand Down
12 changes: 7 additions & 5 deletions src/Maker/MakeEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,14 @@ final class MakeEntity extends AbstractMaker
private $fileManager;
private $doctrineHelper;
private $projectDirectory;
private $entityNamespace;

public function __construct(FileManager $fileManager, string $projectDirectory, DoctrineHelper $doctrineHelper)
public function __construct(FileManager $fileManager, DoctrineHelper $doctrineHelper, string $projectDirectory, string $entityNamespace)
{
$this->fileManager = $fileManager;
$this->projectDirectory = $projectDirectory;
$this->doctrineHelper = $doctrineHelper;
$this->projectDirectory = $projectDirectory;
$this->entityNamespace = $entityNamespace;
}

public static function getCommandName(): string
Expand Down Expand Up @@ -80,7 +82,7 @@ public function interact(InputInterface $input, ConsoleStyle $io, Command $comma
'This command will generate any missing methods (e.g. getters & setters) for a class or all classes in a namespace.',
'To overwrite any existing methods, re-run this command with the --overwrite flag',
], null, 'fg=yellow');
$classOrNamespace = $io->ask('Enter a class or namespace to regenerate', 'App\\Entity', [Validator::class, 'notBlank']);
$classOrNamespace = $io->ask('Enter a class or namespace to regenerate', $this->entityNamespace, [Validator::class, 'notBlank']);

$input->setArgument('name', $classOrNamespace);

Expand Down Expand Up @@ -485,14 +487,14 @@ private function askRelationDetails(ConsoleStyle $io, string $generatedEntityCla
$targetEntityClass = $io->askQuestion($question);

if (!class_exists($targetEntityClass)) {
if (!class_exists('App\\Entity\\'.$targetEntityClass)) {
if (!class_exists($this->entityNamespace.'\\'.$targetEntityClass)) {
$io->error(sprintf('Unknown class "%s"', $targetEntityClass));
$targetEntityClass = null;

continue;
}

$targetEntityClass = 'App\\Entity\\'.$targetEntityClass;
$targetEntityClass = $this->entityNamespace.'\\'.$targetEntityClass;
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/Resources/config/makers.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@

<service id="maker.maker.make_entity" class="Symfony\Bundle\MakerBundle\Maker\MakeEntity">
<argument type="service" id="maker.file_manager" />
<argument>%kernel.project_dir%</argument>
<argument type="service" id="maker.doctrine_helper" />
<argument>%kernel.project_dir%</argument>
<argument>%maker.entity_namespace%</argument>
<tag name="maker.command" />
</service>

Expand Down
1 change: 1 addition & 0 deletions src/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
</service>

<service id="maker.doctrine_helper" class="Symfony\Bundle\MakerBundle\Doctrine\DoctrineHelper">
<argument>%maker.entity_namespace%</argument>
<argument type="service" id="doctrine" on-invalid="ignore"/>
</service>
</services>
Expand Down
4 changes: 2 additions & 2 deletions tests/Command/MakerCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ public function testExceptionOnMissingDependencies()

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

$command = new MakerCommand($maker, $fileManager);
$command = new MakerCommand($maker, $fileManager, 'App\\');
// needed because it's normally set by the Application
$command->setName('make:foo');
$tester = new CommandTester($command);
$tester->execute(array());
}
}
}
4 changes: 2 additions & 2 deletions tests/Doctrine/EntityRegeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ private function doTestRegeneration(string $sourceDir, Kernel $kernel, string $n
});

$fileManager = new FileManager($fs, $autoloaderUtil, $tmpDir);
$doctrineHelper = new DoctrineHelper($container->get('doctrine'));
$doctrineHelper = new DoctrineHelper('App\\Entity', $container->get('doctrine'));
$regenerator = new EntityRegenerator(
$doctrineHelper,
$fileManager,
Expand Down Expand Up @@ -203,4 +203,4 @@ public function getRootDir()
{
return __DIR__.'/../tmp/current_project';
}
}
}

0 comments on commit 51d7138

Please sign in to comment.