Skip to content

Configuration for custom namespaces #378

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: 1.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions config/makers.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

<service id="maker.maker.make_controller" class="Symfony\Bundle\MakerBundle\Maker\MakeController">
<tag name="maker.command" />
<argument type="service" id="maker.namespaces_helper" />
</service>

<service id="maker.maker.make_crud" class="Symfony\Bundle\MakerBundle\Maker\MakeCrud">
Expand Down Expand Up @@ -82,6 +83,7 @@
<argument type="service" id="maker.file_manager" />
<argument type="service" id="maker.renderer.form_type_renderer" />
<argument type="service" id="maker.doctrine_helper" />
<argument type="service" id="maker.namespaces_helper" />
<argument type="service" id="router" on-invalid="ignore" />
<tag name="maker.command" />
</service>
Expand All @@ -90,6 +92,7 @@
<argument type="service" id="maker.file_manager" />
<argument type="service" id="maker.doctrine_helper" />
<argument type="service" id="maker.entity_class_generator" />
<argument type="service" id="maker.namespaces_helper" />
<argument type="service" id="router" on-invalid="ignore" />
<tag name="maker.command" />
</service>
Expand Down Expand Up @@ -118,6 +121,7 @@
</service>

<service id="maker.maker.make_test" class="Symfony\Bundle\MakerBundle\Maker\MakeTest">
<argument type="service" id="maker.namespaces_helper" />
<tag name="maker.command" />
</service>

Expand Down Expand Up @@ -157,6 +161,7 @@
<argument type="service" id="maker.file_manager" />
<argument type="service" id="maker.security_config_updater" />
<argument type="service" id="maker.security_controller_builder" />
<argument type="service" id="maker.namespaces_helper" />
<tag name="maker.command" />
</service>

Expand Down
12 changes: 8 additions & 4 deletions config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
</service>

<service id="maker.autoloader_finder" class="Symfony\Bundle\MakerBundle\Util\ComposerAutoloaderFinder" >
<argument /> <!-- root namespace -->
<argument type="service" id="maker.namespaces_helper" />
</service>

<service id="maker.autoloader_util" class="Symfony\Bundle\MakerBundle\Util\AutoloaderUtil">
Expand All @@ -36,10 +36,14 @@
</service>

<service id="maker.doctrine_helper" class="Symfony\Bundle\MakerBundle\Doctrine\DoctrineHelper">
<argument /> <!-- entity namespace -->
<argument type="service" id="maker.namespaces_helper" />
<argument type="service" id="doctrine" on-invalid="ignore" />
</service>

<service id="maker.namespaces_helper" class="Symfony\Bundle\MakerBundle\Util\NamespacesHelper">
<argument type="collection" /> <!-- namespaces -->
</service>

<service id="maker.template_linter" class="Symfony\Bundle\MakerBundle\Util\TemplateLinter">
<argument>%env(default::string:MAKER_PHP_CS_FIXER_BINARY_PATH)%</argument>
<argument>%env(default::string:MAKER_PHP_CS_FIXER_CONFIG_PATH)%</argument>
Expand All @@ -54,7 +58,7 @@

<service id="maker.generator" class="Symfony\Bundle\MakerBundle\Generator">
<argument type="service" id="maker.file_manager" />
<argument /> <!-- root namespace -->
<argument type="service" id="maker.namespaces_helper" />
<argument>null</argument> <!-- PhpCompatUtil -->
<argument type="service" id="maker.template_component_generator" />
</service>
Expand Down Expand Up @@ -82,7 +86,7 @@
<service id="maker.template_component_generator" class="Symfony\Bundle\MakerBundle\Util\TemplateComponentGenerator">
<argument /> <!-- generate_final_classes -->
<argument /> <!-- generate_final_entities -->
<argument /> <!-- root_namespace -->
<argument type="service" id="maker.namespaces_helper" />
</service>
</services>
</container>
12 changes: 8 additions & 4 deletions src/Doctrine/DoctrineHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use Doctrine\Persistence\Mapping\MappingException as PersistenceMappingException;
use Doctrine\Persistence\Mapping\StaticReflectionService;
use Symfony\Bundle\MakerBundle\Util\ClassNameDetails;
use Symfony\Bundle\MakerBundle\Util\NamespacesHelper;
use Symfony\Component\Uid\Ulid;
use Symfony\Component\Uid\Uuid;

Expand All @@ -39,11 +40,10 @@
final class DoctrineHelper
{
public function __construct(
private string $entityNamespace,
private NamespacesHelper $namespacesHelper,
private ?ManagerRegistry $registry = null,
private ?array $mappingDriversByPrefix = null,
) {
$this->entityNamespace = trim($entityNamespace, '\\');
}

public function getRegistry(): ManagerRegistry
Expand All @@ -64,7 +64,11 @@ private function isDoctrineInstalled(): bool

public function getEntityNamespace(): string
{
return $this->entityNamespace;
return \sprintf(
'%s\\%s',
$this->namespacesHelper->getRootNamespace(),
$this->namespacesHelper->getEntityNamespace()
);
}

public function doesClassUseDriver(string $className, string $driverClass): bool
Expand Down Expand Up @@ -138,7 +142,7 @@ public function getEntitiesForAutocomplete(): array
$allMetadata = $this->getMetadata();

foreach (array_keys($allMetadata) as $classname) {
$entityClassDetails = new ClassNameDetails($classname, $this->entityNamespace);
$entityClassDetails = new ClassNameDetails($classname, $this->getEntityNamespace());
$entities[] = $entityClassDetails->getRelativeName();
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Doctrine/EntityClassGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function generateEntityClass(ClassNameDetails $entityClassDetails, bool $
{
$repoClassDetails = $this->generator->createClassNameDetails(
$entityClassDetails->getRelativeName(),
'Repository\\',
$this->generator->getNamespacesHelper()->getRepositoryNamespace(),
'Repository'
);

Expand Down
13 changes: 9 additions & 4 deletions src/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Symfony\Bundle\MakerBundle\Exception\RuntimeCommandException;
use Symfony\Bundle\MakerBundle\Util\ClassNameDetails;
use Symfony\Bundle\MakerBundle\Util\ClassSource\Model\ClassData;
use Symfony\Bundle\MakerBundle\Util\NamespacesHelper;
use Symfony\Bundle\MakerBundle\Util\PhpCompatUtil;
use Symfony\Bundle\MakerBundle\Util\TemplateComponentGenerator;

Expand All @@ -30,12 +31,11 @@ class Generator

public function __construct(
private FileManager $fileManager,
private string $namespacePrefix,
private NamespacesHelper $namespacesHelper,
?PhpCompatUtil $phpCompatUtil = null,
private ?TemplateComponentGenerator $templateComponentGenerator = null,
) {
$this->twigHelper = new GeneratorTwigHelper($fileManager);
$this->namespacePrefix = trim($namespacePrefix, '\\');

if (null !== $phpCompatUtil) {
trigger_deprecation('symfony/maker-bundle', 'v1.44.0', 'Initializing Generator while providing an instance of PhpCompatUtil is deprecated.');
Expand Down Expand Up @@ -180,7 +180,7 @@ public function getFileContentsForPendingOperation(string $targetPath): string
*/
public function createClassNameDetails(string $name, string $namespacePrefix, string $suffix = '', string $validationErrorMessage = ''): ClassNameDetails
{
$fullNamespacePrefix = $this->namespacePrefix.'\\'.$namespacePrefix;
$fullNamespacePrefix = $this->namespacesHelper->getRootNamespace().'\\'.$namespacePrefix;
if ('\\' === $name[0]) {
// class is already "absolute" - leave it alone (but strip opening \)
$className = substr($name, 1);
Expand Down Expand Up @@ -240,9 +240,14 @@ public function writeChanges()
$this->pendingOperations = [];
}

public function getNamespacesHelper(): NamespacesHelper
{
return $this->namespacesHelper;
}

public function getRootNamespace(): string
{
return $this->namespacePrefix;
return $this->namespacesHelper->getRootNamespace();
}

public function generateController(string $controllerClassName, string $controllerTemplatePath, array $parameters = []): string
Expand Down
2 changes: 1 addition & 1 deletion src/Maker/MakeAuthenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ function ($answer) {
);
$input->setArgument('authenticator-class', $io->askQuestion($questionAuthenticatorClass));

$interactiveSecurityHelper = new InteractiveSecurityHelper();
$interactiveSecurityHelper = new InteractiveSecurityHelper($this->generator->getNamespacesHelper());
$command->addOption('firewall-name', null, InputOption::VALUE_OPTIONAL);
$input->setOption('firewall-name', $interactiveSecurityHelper->guessFirewallName($io, $securityData));

Expand Down
2 changes: 1 addition & 1 deletion src/Maker/MakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen

$commandClassNameDetails = $generator->createClassNameDetails(
$commandNameHasAppPrefix ? substr($commandName, 4) : $commandName,
'Command\\',
$generator->getNamespacesHelper()->getCommandNamespace(),
'Command',
\sprintf('The "%s" command name is not valid because it would be implemented by "%s" class, which is not valid as a PHP class name (it must start with a letter or underscore, followed by any number of letters, numbers, or underscores).', $commandName, Str::asClassName($commandName, 'Command'))
);
Expand Down
19 changes: 15 additions & 4 deletions src/Maker/MakeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Symfony\Bundle\MakerBundle\Maker\Common\CanGenerateTestsTrait;
use Symfony\Bundle\MakerBundle\Str;
use Symfony\Bundle\MakerBundle\Util\ClassSource\Model\ClassData;
use Symfony\Bundle\MakerBundle\Util\NamespacesHelper;
use Symfony\Bundle\MakerBundle\Util\PhpCompatUtil;
use Symfony\Bundle\TwigBundle\TwigBundle;
use Symfony\Component\Console\Command\Command;
Expand All @@ -43,8 +44,10 @@ final class MakeController extends AbstractMaker
private bool $usesTwigTemplate;
private string $twigTemplatePath;

public function __construct(private ?PhpCompatUtil $phpCompatUtil = null)
{
public function __construct(
private NamespacesHelper $namespacesHelper,
private ?PhpCompatUtil $phpCompatUtil = null,
) {
if (null !== $phpCompatUtil) {
@trigger_deprecation(
'symfony/maker-bundle',
Expand Down Expand Up @@ -82,7 +85,7 @@ public function interact(InputInterface $input, ConsoleStyle $io, Command $comma
$this->isInvokable = (bool) $input->getOption('invokable');

$controllerClass = $input->getArgument('controller-class');
$controllerClassName = \sprintf('Controller\%s', $controllerClass);
$controllerClassName = \sprintf('%s\%s', $this->namespacesHelper->getControllerNamespace(), $controllerClass);

// If the class name provided is absolute, we do not assume it will live in src/Controller
// e.g. src/Custom/Location/For/MyController instead of src/Controller/MyController
Expand All @@ -92,6 +95,7 @@ public function interact(InputInterface $input, ConsoleStyle $io, Command $comma

$this->controllerClassData = ClassData::create(
class: $controllerClassName,
rootNamespace: $this->namespacesHelper->getRootNamespace(),
suffix: 'Controller',
extendsClass: AbstractController::class,
useStatements: [
Expand Down Expand Up @@ -138,8 +142,15 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
}

if ($this->shouldGenerateTests()) {
$testClassName = \sprintf(
'%s\%s\%s',
$this->namespacesHelper->getTestNamespace(),
$this->namespacesHelper->getControllerNamespace(),
$this->controllerClassData->getClassName(relative: true, withoutSuffix: true)
);
$testClassData = ClassData::create(
class: \sprintf('Tests\Controller\%s', $this->controllerClassData->getClassName(relative: true, withoutSuffix: true)),
class: $testClassName,
rootNamespace: $this->namespacesHelper->getRootNamespace(),
suffix: 'ControllerTest',
extendsClass: WebTestCase::class,
);
Expand Down
20 changes: 14 additions & 6 deletions src/Maker/MakeCrud.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
{
$entityClassDetails = $generator->createClassNameDetails(
Validator::entityExists($input->getArgument('entity-class'), $this->doctrineHelper->getEntitiesForAutocomplete()),
'Entity\\'
$generator->getNamespacesHelper()->getEntityNamespace()
);

$entityDoctrineDetails = $this->doctrineHelper->createDoctrineDetails($entityClassDetails->getFullName());
Expand All @@ -118,7 +118,7 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
if (null !== $entityDoctrineDetails->getRepositoryClass()) {
$repositoryClassDetails = $generator->createClassNameDetails(
'\\'.$entityDoctrineDetails->getRepositoryClass(),
'Repository\\',
$generator->getNamespacesHelper()->getRepositoryNamespace(),
'Repository'
);

Expand All @@ -133,22 +133,23 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen

$controllerClassDetails = $generator->createClassNameDetails(
$this->controllerClassName,
'Controller\\',
$generator->getNamespacesHelper()->getControllerNamespace(),
'Controller'
);

$iter = 0;
do {
$formClassDetails = $generator->createClassNameDetails(
$entityClassDetails->getRelativeNameWithoutSuffix().($iter ?: '').'Type',
'Form\\',
$generator->getNamespacesHelper()->getFormNamespace(),
'Type'
);
++$iter;
} while (class_exists($formClassDetails->getFullName()));

$controllerClassData = ClassData::create(
class: \sprintf('Controller\%s', $this->controllerClassName),
class: \sprintf('%s\%s', $generator->getNamespacesHelper()->getControllerNamespace(), $this->controllerClassName),
rootNamespace: $generator->getRootNamespace(),
suffix: 'Controller',
extendsClass: AbstractController::class,
useStatements: [
Expand Down Expand Up @@ -247,8 +248,15 @@ class: \sprintf('Controller\%s', $this->controllerClassName),
}

if ($this->shouldGenerateTests()) {
$testClassName = \sprintf(
'%s\%s\%s',
$generator->getNamespacesHelper()->getTestNamespace(),
$generator->getNamespacesHelper()->getControllerNamespace(),
$entityClassDetails->getRelativeNameWithoutSuffix()
);
$testClassData = ClassData::create(
class: \sprintf('Tests\Controller\%s', $entityClassDetails->getRelativeNameWithoutSuffix()),
class: $testClassName,
rootNamespace: $generator->getRootNamespace(),
suffix: 'ControllerTest',
extendsClass: WebTestCase::class,
useStatements: [
Expand Down
9 changes: 5 additions & 4 deletions src/Maker/MakeEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
use Symfony\Bundle\MakerBundle\Util\ClassSource\Model\ClassProperty;
use Symfony\Bundle\MakerBundle\Util\ClassSourceManipulator;
use Symfony\Bundle\MakerBundle\Util\CliOutputHelper;
use Symfony\Bundle\MakerBundle\Util\NamespacesHelper;
use Symfony\Bundle\MakerBundle\Validator;
use Symfony\Bundle\MercureBundle\DependencyInjection\MercureExtension;
use Symfony\Component\Console\Command\Command;
Expand Down Expand Up @@ -66,7 +67,7 @@ public function __construct(

if (null === $generator) {
@trigger_error(\sprintf('Passing a "%s" instance as 4th argument is mandatory since version 1.5.', Generator::class), \E_USER_DEPRECATED);
$this->generator = new Generator($fileManager, 'App\\');
$this->generator = new Generator($fileManager, new NamespacesHelper());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to also pass root_namespace set to App\\?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If not given the root_namespace will default to App\\, we can probably explicitly set it to avoid confusion.

Any thoughts?

} else {
$this->generator = $generator;
}
Expand Down Expand Up @@ -142,7 +143,7 @@ public function interact(InputInterface $input, ConsoleStyle $io, Command $comma
if (
!$input->getOption('api-resource')
&& class_exists(ApiResource::class)
&& !class_exists($this->generator->createClassNameDetails($entityClassName, 'Entity\\')->getFullName())
&& !class_exists($this->generator->createClassNameDetails($entityClassName, $this->generator->getNamespacesHelper()->getEntityNamespace())->getFullName())
) {
$description = $command->getDefinition()->getOption('api-resource')->getDescription();
$question = new ConfirmationQuestion($description, false);
Expand All @@ -154,7 +155,7 @@ public function interact(InputInterface $input, ConsoleStyle $io, Command $comma
if (
!$input->getOption('broadcast')
&& class_exists(Broadcast::class)
&& !class_exists($this->generator->createClassNameDetails($entityClassName, 'Entity\\')->getFullName())
&& !class_exists($this->generator->createClassNameDetails($entityClassName, $this->generator->getNamespacesHelper()->getEntityNamespace())->getFullName())
) {
$description = $command->getDefinition()->getOption('broadcast')->getDescription();
$question = new ConfirmationQuestion($description, false);
Expand Down Expand Up @@ -183,7 +184,7 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen

$entityClassDetails = $generator->createClassNameDetails(
$input->getArgument('name'),
'Entity\\'
$generator->getNamespacesHelper()->getEntityNamespace(),
);

$classExists = class_exists($entityClassDetails->getFullName());
Expand Down
2 changes: 1 addition & 1 deletion src/Maker/MakeFixtures.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
{
$fixturesClassNameDetails = $generator->createClassNameDetails(
$input->getArgument('fixtures-class'),
'DataFixtures\\'
$generator->getNamespacesHelper()->getFixturesNamespace()
);

$useStatements = new UseStatementGenerator([
Expand Down
4 changes: 2 additions & 2 deletions src/Maker/MakeForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
{
$formClassNameDetails = $generator->createClassNameDetails(
$input->getArgument('name'),
'Form\\',
$generator->getNamespacesHelper()->getFormNamespace(),
'Type'
);

Expand All @@ -91,7 +91,7 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
if (null !== $boundClass) {
$boundClassDetails = $generator->createClassNameDetails(
$boundClass,
'Entity\\'
$generator->getNamespacesHelper()->getEntityNamespace()
);

$doctrineEntityDetails = $this->entityHelper->createDoctrineDetails($boundClassDetails->getFullName());
Expand Down
2 changes: 1 addition & 1 deletion src/Maker/MakeFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
{
$testClassNameDetails = $generator->createClassNameDetails(
$input->getArgument('name'),
'Tests\\',
$generator->getNamespacesHelper()->getTestNamespace(),
'Test'
);

Expand Down
Loading
Loading