Skip to content
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
16 changes: 16 additions & 0 deletions src/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,37 @@ class Generator
private GeneratorTwigHelper $twigHelper;
private array $pendingOperations = [];
private array $generatedFiles = [];
private array $configuredNamespaces;

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

$defaults = [];
foreach (NamespaceType::cases() as $case) {
$defaults[$case->value] = $case->defaultNamespace();
}
$this->configuredNamespaces = array_merge($defaults, $configuredNamespaces);

if (null !== $phpCompatUtil) {
trigger_deprecation('symfony/maker-bundle', 'v1.44.0', 'Initializing Generator while providing an instance of PhpCompatUtil is deprecated.');
}
}

/**
* Returns the configured namespace prefix for a given type.
*/
public function getNamespace(NamespaceType $type): string
{
return $this->configuredNamespaces[$type->value];
}

/**
* Generate a new file for a class from a template.
*
Expand Down
5 changes: 3 additions & 2 deletions src/Maker/MakeAuthenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Bundle\MakerBundle\ConsoleStyle;
use Symfony\Bundle\MakerBundle\NamespaceType;
use Symfony\Bundle\MakerBundle\DependencyBuilder;
use Symfony\Bundle\MakerBundle\Doctrine\DoctrineHelper;
use Symfony\Bundle\MakerBundle\Exception\RuntimeCommandException;
Expand Down Expand Up @@ -330,7 +331,7 @@ private function generateAuthenticatorClass(array $securityData, string $authent

$userClassNameDetails = $this->generator->createClassNameDetails(
'\\'.$userClass,
'Entity\\'
$this->generator->getNamespace(NamespaceType::Entity).'\\'
);

$this->generator->generateClass(
Expand All @@ -354,7 +355,7 @@ private function generateFormLoginFiles(string $controllerClass, string $userNam
{
$controllerClassNameDetails = $this->generator->createClassNameDetails(
$controllerClass,
'Controller\\',
$this->generator->getNamespace(NamespaceType::Controller).'\\',
'Controller'
);

Expand Down
3 changes: 2 additions & 1 deletion src/Maker/MakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Bundle\MakerBundle\Maker;

use Symfony\Bundle\MakerBundle\ConsoleStyle;
use Symfony\Bundle\MakerBundle\NamespaceType;
use Symfony\Bundle\MakerBundle\DependencyBuilder;
use Symfony\Bundle\MakerBundle\Generator;
use Symfony\Bundle\MakerBundle\InputConfiguration;
Expand Down Expand Up @@ -69,7 +70,7 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen

$commandClassNameDetails = $generator->createClassNameDetails(
$commandNameHasAppPrefix ? substr($commandName, 4) : $commandName,
'Command\\',
$generator->getNamespace(NamespaceType::Command).'\\',
'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
47 changes: 24 additions & 23 deletions src/Maker/MakeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Bundle\MakerBundle\ConsoleStyle;
use Symfony\Bundle\MakerBundle\NamespaceType;
use Symfony\Bundle\MakerBundle\DependencyBuilder;
use Symfony\Bundle\MakerBundle\Generator;
use Symfony\Bundle\MakerBundle\InputConfiguration;
Expand All @@ -39,9 +40,8 @@ final class MakeController extends AbstractMaker
use CanGenerateTestsTrait;

private bool $isInvokable;
private ClassData $controllerClassData;
private bool $usesTwigTemplate;
private string $twigTemplatePath;
private string $controllerClass;

public function __construct(private ?PhpCompatUtil $phpCompatUtil = null)
{
Expand Down Expand Up @@ -80,17 +80,23 @@ public function interact(InputInterface $input, ConsoleStyle $io, Command $comma
{
$this->usesTwigTemplate = $this->isTwigInstalled() && !$input->getOption('no-template');
$this->isInvokable = (bool) $input->getOption('invokable');
$this->controllerClass = $input->getArgument('controller-class');

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

public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator): void
{
$controllerNamespace = $generator->getNamespace(NamespaceType::Controller);
$controllerClassName = \sprintf('%s\%s', $controllerNamespace, $this->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
if ($isAbsoluteNamespace = '\\' === $controllerClass[0]) {
$controllerClassName = substr($controllerClass, 1);
if ($isAbsoluteNamespace = '\\' === $this->controllerClass[0]) {
$controllerClassName = substr($this->controllerClass, 1);
}

$this->controllerClassData = ClassData::create(
$controllerClassData = ClassData::create(
class: $controllerClassName,
suffix: 'Controller',
extendsClass: AbstractController::class,
Expand All @@ -105,47 +111,42 @@ class: $controllerClassName,
// templates/my/controller.html.twig. We do however remove the root_namespace prefix in either case
// so we don't end up with templates/app/my/controller.html.twig
$templateName = $isAbsoluteNamespace ?
$this->controllerClassData->getFullClassName(withoutRootNamespace: true, withoutSuffix: true) :
$this->controllerClassData->getClassName(relative: true, withoutSuffix: true)
$controllerClassData->getFullClassName(withoutRootNamespace: true, withoutSuffix: true) :
$controllerClassData->getClassName(relative: true, withoutSuffix: true)
;

// Convert the Twig template name into a file path where it will be generated.
$this->twigTemplatePath = \sprintf('%s%s', Str::asFilePath($templateName), $this->isInvokable ? '.html.twig' : '/index.html.twig');
$twigTemplatePath = \sprintf('%s%s', Str::asFilePath($templateName), $this->isInvokable ? '.html.twig' : '/index.html.twig');

$this->interactSetGenerateTests($input, $io);
}

public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator): void
{
$controllerPath = $generator->generateClassFromClassData($this->controllerClassData, 'controller/Controller.tpl.php', [
'route_path' => Str::asRoutePath($this->controllerClassData->getClassName(relative: true, withoutSuffix: true)),
'route_name' => Str::AsRouteName($this->controllerClassData->getClassName(relative: true, withoutSuffix: true)),
$controllerPath = $generator->generateClassFromClassData($controllerClassData, 'controller/Controller.tpl.php', [
'route_path' => Str::asRoutePath($controllerClassData->getClassName(relative: true, withoutSuffix: true)),
'route_name' => Str::AsRouteName($controllerClassData->getClassName(relative: true, withoutSuffix: true)),
'method_name' => $this->isInvokable ? '__invoke' : 'index',
'with_template' => $this->usesTwigTemplate,
'template_name' => $this->twigTemplatePath,
'template_name' => $twigTemplatePath,
], true);

if ($this->usesTwigTemplate) {
$generator->generateTemplate(
$this->twigTemplatePath,
$twigTemplatePath,
'controller/twig_template.tpl.php',
[
'controller_path' => $controllerPath,
'root_directory' => $generator->getRootDirectory(),
'class_name' => $this->controllerClassData->getClassName(),
'class_name' => $controllerClassData->getClassName(),
]
);
}

if ($this->shouldGenerateTests()) {
$testClassData = ClassData::create(
class: \sprintf('Tests\Controller\%s', $this->controllerClassData->getClassName(relative: true, withoutSuffix: true)),
class: \sprintf('Tests\%s\%s', $controllerNamespace, $controllerClassData->getClassName(relative: true, withoutSuffix: true)),
suffix: 'ControllerTest',
extendsClass: WebTestCase::class,
);

$generator->generateClassFromClassData($testClassData, 'controller/test/Test.tpl.php', [
'route_path' => Str::asRoutePath($this->controllerClassData->getClassName(relative: true, withoutSuffix: true)),
'route_path' => Str::asRoutePath($controllerClassData->getClassName(relative: true, withoutSuffix: true)),
]);

if (!class_exists(WebTestCase::class)) {
Expand Down
11 changes: 6 additions & 5 deletions src/Maker/MakeCrud.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Bundle\MakerBundle\ConsoleStyle;
use Symfony\Bundle\MakerBundle\NamespaceType;
use Symfony\Bundle\MakerBundle\DependencyBuilder;
use Symfony\Bundle\MakerBundle\Doctrine\DoctrineHelper;
use Symfony\Bundle\MakerBundle\Generator;
Expand Down Expand Up @@ -107,7 +108,7 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
{
$entityClassDetails = $generator->createClassNameDetails(
Validator::entityExists($input->getArgument('entity-class'), $this->doctrineHelper->getEntitiesForAutocomplete()),
'Entity\\'
$generator->getNamespace(NamespaceType::Entity).'\\'
);

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

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

$controllerClassDetails = $generator->createClassNameDetails(
$this->controllerClassName,
'Controller\\',
$generator->getNamespace(NamespaceType::Controller).'\\',
'Controller'
);

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

$controllerClassData = ClassData::create(
class: \sprintf('Controller\%s', $this->controllerClassName),
class: \sprintf('%s\%s', $generator->getNamespace(NamespaceType::Controller), $this->controllerClassName),
suffix: 'Controller',
extendsClass: AbstractController::class,
useStatements: [
Expand Down
7 changes: 4 additions & 3 deletions src/Maker/MakeEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use ApiPlatform\Metadata\ApiResource;
use Doctrine\DBAL\Types\Type;
use Symfony\Bundle\MakerBundle\ConsoleStyle;
use Symfony\Bundle\MakerBundle\NamespaceType;
use Symfony\Bundle\MakerBundle\DependencyBuilder;
use Symfony\Bundle\MakerBundle\Doctrine\DoctrineHelper;
use Symfony\Bundle\MakerBundle\Doctrine\EntityClassGenerator;
Expand Down Expand Up @@ -143,7 +144,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->getNamespace(NamespaceType::Entity).'\\')->getFullName())
) {
$description = $command->getDefinition()->getOption('api-resource')->getDescription();
$question = new ConfirmationQuestion($description, false);
Expand All @@ -155,7 +156,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->getNamespace(NamespaceType::Entity).'\\')->getFullName())
) {
$description = $command->getDefinition()->getOption('broadcast')->getDescription();
$question = new ConfirmationQuestion($description, false);
Expand Down Expand Up @@ -184,7 +185,7 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen

$entityClassDetails = $generator->createClassNameDetails(
$input->getArgument('name'),
'Entity\\'
$generator->getNamespace(NamespaceType::Entity).'\\'
);

$classExists = class_exists($entityClassDetails->getFullName());
Expand Down
5 changes: 3 additions & 2 deletions src/Maker/MakeForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Doctrine\Bundle\DoctrineBundle\DoctrineBundle;
use Symfony\Bundle\MakerBundle\ConsoleStyle;
use Symfony\Bundle\MakerBundle\NamespaceType;
use Symfony\Bundle\MakerBundle\DependencyBuilder;
use Symfony\Bundle\MakerBundle\Doctrine\DoctrineHelper;
use Symfony\Bundle\MakerBundle\Generator;
Expand Down Expand Up @@ -79,7 +80,7 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
{
$formClassNameDetails = $generator->createClassNameDetails(
$input->getArgument('name'),
'Form\\',
$generator->getNamespace(NamespaceType::Form).'\\',
'Type'
);

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

$doctrineEntityDetails = $this->entityHelper->createDoctrineDetails($boundClassDetails->getFullName());
Expand Down
10 changes: 5 additions & 5 deletions src/Maker/MakeRegistrationForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Bundle\MakerBundle\ConsoleStyle;
use Symfony\Bundle\MakerBundle\NamespaceType;
use Symfony\Bundle\MakerBundle\DependencyBuilder;
use Symfony\Bundle\MakerBundle\Doctrine\DoctrineHelper;
use Symfony\Bundle\MakerBundle\Exception\RuntimeCommandException;
Expand Down Expand Up @@ -214,8 +215,7 @@ private function interactAuthenticatorQuestions(ConsoleStyle $io, InteractiveSec
public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator): void
{
$userClassNameDetails = $generator->createClassNameDetails(
'\\'.$this->userClass,
'Entity\\'
'\\'.$this->userClass, $generator->getNamespace(NamespaceType::Entity).'\\'
);

$userDoctrineDetails = $this->doctrineHelper->createDoctrineDetails($userClassNameDetails->getFullName());
Expand All @@ -229,7 +229,7 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
$userRepository = $userDoctrineDetails->getRepositoryClass();

if (null !== $userRepository) {
$userRepoClassDetails = $generator->createClassNameDetails('\\'.$userRepository, 'Repository\\', 'Repository');
$userRepoClassDetails = $generator->createClassNameDetails('\\'.$userRepository, $generator->getNamespace(NamespaceType::Repository).'\\', 'Repository');

$userRepoVars = [
'repository_full_class_name' => $userRepoClassDetails->getFullName(),
Expand Down Expand Up @@ -297,7 +297,7 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
// 2) Generate the controller
$controllerClassNameDetails = $generator->createClassNameDetails(
'RegistrationController',
'Controller\\'
$generator->getNamespace(NamespaceType::Controller).'\\'
);

$useStatements = new UseStatementGenerator([
Expand Down Expand Up @@ -549,7 +549,7 @@ private function generateFormClass(ClassNameDetails $userClassDetails, Generator
{
$formClassDetails = $generator->createClassNameDetails(
'RegistrationFormType',
'Form\\'
$generator->getNamespace(NamespaceType::Form).'\\'
);

$formFields = [
Expand Down
Loading
Loading