|
17 | 17 | use Symfony\Bundle\MakerBundle\Generator; |
18 | 18 | use Symfony\Bundle\MakerBundle\InputConfiguration; |
19 | 19 | use Symfony\Bundle\MakerBundle\Str; |
| 20 | +use Symfony\Bundle\MakerBundle\Util\ClassSource\Model\ClassData; |
20 | 21 | use Symfony\Bundle\MakerBundle\Util\PhpCompatUtil; |
21 | | -use Symfony\Bundle\MakerBundle\Util\UseStatementGenerator; |
22 | 22 | use Symfony\Bundle\TwigBundle\TwigBundle; |
23 | 23 | use Symfony\Component\Console\Command\Command; |
24 | 24 | use Symfony\Component\Console\Input\InputArgument; |
@@ -67,45 +67,56 @@ public function configureCommand(Command $command, InputConfiguration $inputConf |
67 | 67 |
|
68 | 68 | public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator): void |
69 | 69 | { |
70 | | - $controllerClassNameDetails = $generator->createClassNameDetails( |
71 | | - $input->getArgument('controller-class'), |
72 | | - 'Controller\\', |
73 | | - 'Controller' |
74 | | - ); |
75 | | - |
76 | 70 | $withTemplate = $this->isTwigInstalled() && !$input->getOption('no-template'); |
77 | 71 | $isInvokable = (bool) $input->getOption('invokable'); |
78 | 72 |
|
79 | | - $useStatements = new UseStatementGenerator([ |
80 | | - AbstractController::class, |
81 | | - $withTemplate ? Response::class : JsonResponse::class, |
82 | | - Route::class, |
83 | | - ]); |
84 | | - |
85 | | - $templateName = Str::asFilePath($controllerClassNameDetails->getRelativeNameWithoutSuffix()) |
86 | | - .($isInvokable ? '.html.twig' : '/index.html.twig'); |
87 | | - |
88 | | - $controllerPath = $generator->generateController( |
89 | | - $controllerClassNameDetails->getFullName(), |
90 | | - 'controller/Controller.tpl.php', |
91 | | - [ |
92 | | - 'use_statements' => $useStatements, |
93 | | - 'route_path' => Str::asRoutePath($controllerClassNameDetails->getRelativeNameWithoutSuffix()), |
94 | | - 'route_name' => Str::asRouteName($controllerClassNameDetails->getRelativeNameWithoutSuffix()), |
95 | | - 'method_name' => $isInvokable ? '__invoke' : 'index', |
96 | | - 'with_template' => $withTemplate, |
97 | | - 'template_name' => $templateName, |
| 73 | + $controllerClass = $input->getArgument('controller-class'); |
| 74 | + $controllerClassName = \sprintf('Controller\%s', $controllerClass); |
| 75 | + |
| 76 | + // If the class name provided is absolute, we do not assume it will live in src/Controller |
| 77 | + // e.g. src/Custom/Location/For/MyController instead of src/Controller/MyController |
| 78 | + if ($isAbsolute = '\\' === $controllerClass[0]) { |
| 79 | + $controllerClassName = substr($controllerClass, 1); |
| 80 | + } |
| 81 | + |
| 82 | + $controllerClassData = ClassData::create( |
| 83 | + class: $controllerClassName, |
| 84 | + suffix: 'Controller', |
| 85 | + extendsClass: AbstractController::class, |
| 86 | + useStatements: [ |
| 87 | + $withTemplate ? Response::class : JsonResponse::class, |
| 88 | + Route::class, |
98 | 89 | ] |
99 | 90 | ); |
100 | 91 |
|
| 92 | + // Again if the class name is absolute, lets not make assumptions about where the twig template |
| 93 | + // should live. E.g. templates/custom/location/for/my_controller.html.twig instead of |
| 94 | + // templates/my/controller.html.twig. We do however remove the root_namespace prefix in either case |
| 95 | + // so we don't end up with templates/app/my/controller.html.twig |
| 96 | + $templateName = $isAbsolute ? |
| 97 | + $controllerClassData->getFullClassName(withoutRootNamespace: true, withoutSuffix: true) : |
| 98 | + $controllerClassData->getClassName(relative: true, withoutSuffix: true) |
| 99 | + ; |
| 100 | + |
| 101 | + // Convert the twig template name into a file path where it will be generated. |
| 102 | + $templatePath = \sprintf('%s%s', Str::asFilePath($templateName), $isInvokable ? '.html.twig' : '/index.html.twig'); |
| 103 | + |
| 104 | + $controllerPath = $generator->generateClassFromClassData($controllerClassData, 'controller/Controller.tpl.php', [ |
| 105 | + 'route_path' => Str::asRoutePath($controllerClassData->getClassName(relative: true, withoutSuffix: true)), |
| 106 | + 'route_name' => Str::AsRouteName($controllerClassData->getClassName(relative: true, withoutSuffix: true)), |
| 107 | + 'method_name' => $isInvokable ? '__invoke' : 'index', |
| 108 | + 'with_template' => $withTemplate, |
| 109 | + 'template_name' => $templatePath, |
| 110 | + ], true); |
| 111 | + |
101 | 112 | if ($withTemplate) { |
102 | 113 | $generator->generateTemplate( |
103 | | - $templateName, |
| 114 | + $templatePath, |
104 | 115 | 'controller/twig_template.tpl.php', |
105 | 116 | [ |
106 | 117 | 'controller_path' => $controllerPath, |
107 | 118 | 'root_directory' => $generator->getRootDirectory(), |
108 | | - 'class_name' => $controllerClassNameDetails->getShortName(), |
| 119 | + 'class_name' => $controllerClassData->getClassName(), |
109 | 120 | ] |
110 | 121 | ); |
111 | 122 | } |
|
0 commit comments