Skip to content

Commit

Permalink
[make:controller] generate final controller class
Browse files Browse the repository at this point in the history
  • Loading branch information
jrushlow committed Aug 30, 2024
1 parent a3b7f14 commit e9192e4
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 20 deletions.
50 changes: 38 additions & 12 deletions src/Maker/MakeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
use Symfony\Bundle\MakerBundle\Generator;
use Symfony\Bundle\MakerBundle\InputConfiguration;
use Symfony\Bundle\MakerBundle\Str;
use Symfony\Bundle\MakerBundle\Util\ClassSource\Model\ClassData;
use Symfony\Bundle\MakerBundle\Util\PhpCompatUtil;
use Symfony\Bundle\MakerBundle\Util\UseStatementGenerator;
use Symfony\Bundle\TwigBundle\TwigBundle;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
Expand Down Expand Up @@ -76,22 +76,48 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
$withTemplate = $this->isTwigInstalled() && !$input->getOption('no-template');
$isInvokable = (bool) $input->getOption('invokable');

$useStatements = new UseStatementGenerator([
AbstractController::class,
$withTemplate ? Response::class : JsonResponse::class,
Route::class,
]);
$controllerClass = $input->getArgument('controller-class');

$templateName = Str::asFilePath($controllerClassNameDetails->getRelativeNameWithoutSuffix())
$controllerClassData = ClassData::create(
class: '\\' === $controllerClass[0] ? substr($controllerClass, 1) : \sprintf('Controller\%s', $input->getArgument('controller-class')),
suffix: 'Controller',
extendsClass: AbstractController::class,
useStatements: [
$withTemplate ? Response::class : JsonResponse::class,
Route::class,
]
);

// dd([
// $controllerClassNameDetails,
// $controllerClassNameDetails->getRelativeName(),
// $controllerClassNameDetails->getShortName(),
// $controllerClassNameDetails->getFullName(),
// $controllerClassNameDetails->getRelativeNameWithoutSuffix(),
// ],
// [
// $controllerClassData,
// $controllerClassData->getClassName(relative: true),
// $controllerClassData->getClassName(),
// $controllerClassData->getFullClassName(),
// $controllerClassData->getClassName(relative: true, withoutSuffix: true),
// ]
// );

// $templateName = Str::asFilePath($controllerClassNameDetails->getRelativeNameWithoutSuffix())
$templateName = Str::asFilePath($controllerClassData->getClassName(relative: true, withoutSuffix: true))
.($isInvokable ? '.html.twig' : '/index.html.twig');

$controllerPath = $generator->generateController(
$controllerClassNameDetails->getFullName(),
$controllerClassData->getFullClassName(),
'controller/Controller.tpl.php',
[
'use_statements' => $useStatements,
'route_path' => Str::asRoutePath($controllerClassNameDetails->getRelativeNameWithoutSuffix()),
'route_name' => Str::asRouteName($controllerClassNameDetails->getRelativeNameWithoutSuffix()),
'class_data' => $controllerClassData,
// 'use_statements' => $useStatements,
// 'route_path' => Str::asRoutePath($controllerClassNameDetails->getRelativeNameWithoutSuffix()),
'route_path' => Str::asRoutePath($controllerClassData->getClassName(relative: true, withoutSuffix: true)),
'route_name' => Str::AsRouteName($controllerClassData->getClassName(relative: true, withoutSuffix: true)),
// 'route_name' => Str::asRouteName($controllerClassNameDetails->getRelativeNameWithoutSuffix()),
'method_name' => $isInvokable ? '__invoke' : 'index',
'with_template' => $withTemplate,
'template_name' => $templateName,
Expand All @@ -105,7 +131,7 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
[
'controller_path' => $controllerPath,
'root_directory' => $generator->getRootDirectory(),
'class_name' => $controllerClassNameDetails->getShortName(),
'class_name' => $controllerClassData->getClassName(),
]
);
}
Expand Down
6 changes: 3 additions & 3 deletions src/Resources/skeleton/controller/Controller.tpl.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?= "<?php\n" ?>

namespace <?= $namespace; ?>;
namespace <?= $class_data->getNamespace(); ?>;

<?= $use_statements; ?>
<?= $class_data->getUseStatements(); ?>

class <?= $class_name; ?> extends AbstractController
<?= $class_data->getClassDeclaration(); ?>
{
<?= $generator->generateRouteForControllerMethod($route_path, $route_name); ?>
public function <?= $method_name ?>(): <?php if ($with_template) { ?>Response<?php } else { ?>JsonResponse<?php } ?>
Expand Down
26 changes: 24 additions & 2 deletions src/Util/ClassSource/Model/ClassData.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ private function __construct(
private UseStatementGenerator $useStatementGenerator,
private bool $isFinal = true,
private string $rootNamespace = 'App',
private ?string $classSuffix = null,
) {
if (str_starts_with(haystack: $this->namespace, needle: $this->rootNamespace)) {
$this->namespace = substr_replace(string: $this->namespace, replace: '', offset: 0, length: \strlen($this->rootNamespace) + 1);
}
}

public static function create(string $class, ?string $suffix = null, ?string $extendsClass = null, bool $isEntity = false, array $useStatements = []): self
Expand All @@ -52,12 +56,30 @@ className: Str::asClassName($className),
extends: null === $extendsClass ? null : Str::getShortClassName($extendsClass),
isEntity: $isEntity,
useStatementGenerator: $useStatements,
classSuffix: $suffix,
);
}

public function getClassName(): string
public function getClassName(bool $relative = false, bool $withoutSuffix = false): string
{
return $this->className;
if (!$withoutSuffix && !$relative) {
return $this->className;
}

if ($relative) {
$class = \sprintf('%s\%s', $this->namespace, $this->className);

$firstNsSeparatorPosition = stripos($class, '\\');
$class = substr_replace(string: $class, replace: '', offset: 0, length: $firstNsSeparatorPosition + 1);

if ($withoutSuffix) {
$class = Str::removeSuffix($class, $this->classSuffix);
}

return $class;
}

return Str::removeSuffix($this->className, $this->classSuffix);
}

public function getNamespace(): string
Expand Down
45 changes: 45 additions & 0 deletions tests/Util/ClassSource/ClassDataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use PHPUnit\Framework\TestCase;
use Symfony\Bundle\MakerBundle\MakerBundle;
use Symfony\Bundle\MakerBundle\Test\MakerTestKernel;
use Symfony\Bundle\MakerBundle\Util\ClassNameDetails;
use Symfony\Bundle\MakerBundle\Util\ClassSource\Model\ClassData;

class ClassDataTest extends TestCase
Expand Down Expand Up @@ -91,4 +92,48 @@ public function namespaceDataProvider(): \Generator
yield ['MyController', 'Maker', 'Maker', 'Maker\MyController'];
yield ['Controller\MyController', 'Maker', 'Maker\Controller', 'Maker\Controller\MyController'];
}

public function testGetClassName(): void
{
$class = ClassData::create(class: 'Controller\\Foo', suffix: 'Controller');
self::assertSame('FooController', $class->getClassName());
self::assertSame('Foo', $class->getClassName(relative: false, withoutSuffix: true));
self::assertSame('FooController', $class->getClassName(relative: true, withoutSuffix: false));
self::assertSame('Foo', $class->getClassName(relative: true, withoutSuffix: true));
self::assertSame('App\Controller\FooController', $class->getFullClassName());
}

public function testGetClassNameRelativeNamespace(): void
{
$class = ClassData::create(class: 'Controller\\Admin\\Foo', suffix: 'Controller');
self::assertSame('FooController', $class->getClassName());
self::assertSame('Foo', $class->getClassName(relative: false, withoutSuffix: true));
self::assertSame('Admin\FooController', $class->getClassName(relative: true, withoutSuffix: false));
self::assertSame('Admin\Foo', $class->getClassName(relative: true, withoutSuffix: true));
self::assertSame('App\Controller\Admin\FooController', $class->getFullClassName());
}

public function testGetClassNameWithAbsoluteNamespace(): void
{
$class = ClassData::create(class: '\\Foo\\Bar\\Admin\\Baz', suffix: 'Controller');
self::assertSame('BazController', $class->getClassName());
self::assertSame('Baz', $class->getClassName(relative: false, withoutSuffix: true));
// self::assertSame('Admin\FooController', $class->getClassName(relative: true, withoutSuffix: false));
// self::assertSame('Admin\Baz', $class->getClassName(relative: true, withoutSuffix: true));
self::assertSame('Foo\Bar\Admin\BazController', $class->getFullClassName());

Check failure on line 123 in tests/Util/ClassSource/ClassDataTest.php

View workflow job for this annotation

GitHub Actions / PHP 8.3 + @6.4.x-dev highest deps

Failed asserting that two strings are identical.

Check failure on line 123 in tests/Util/ClassSource/ClassDataTest.php

View workflow job for this annotation

GitHub Actions / PHP 8.3 + @7.0.x-dev highest deps

Failed asserting that two strings are identical.

Check failure on line 123 in tests/Util/ClassSource/ClassDataTest.php

View workflow job for this annotation

GitHub Actions / PHP 8.3 + @7.1.x-dev highest deps

Failed asserting that two strings are identical.

Check failure on line 123 in tests/Util/ClassSource/ClassDataTest.php

View workflow job for this annotation

GitHub Actions / PHP 8.1 + @6.4.* lowest deps

Failed asserting that two strings are identical.

Check failure on line 123 in tests/Util/ClassSource/ClassDataTest.php

View workflow job for this annotation

GitHub Actions / PHP 8.1 + @6.4.* highest deps

Failed asserting that two strings are identical.
}

// public function testClassNameDetails(): void
// {
// $class = new ClassNameDetails(
// fullClassName: 'Foo',
// namespacePrefix: 'Controller\\',
// suffix: 'Controller',
// );
//
// self::assertSame('FooController', $class->getFullName());
// self::assertSame('MyController', $class->getShortName());
// self::assertSame('My', $class->getRelativeNameWithoutSuffix());
// self::assertSame('MyController', $class->getRelativeName());
// }
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class GeneratedControllerTest extends WebTestCase
final class GeneratedControllerTest extends WebTestCase
{
public function testController()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class GeneratedControllerTest extends WebTestCase
final class GeneratedControllerTest extends WebTestCase
{
public function testController()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class GeneratedControllerTest extends WebTestCase
final class GeneratedControllerTest extends WebTestCase
{
public function testControllerValidity()
{
Expand Down

0 comments on commit e9192e4

Please sign in to comment.