Skip to content
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

Refactoring the code to be more MakerBundle-like #15

Open
wants to merge 2 commits into
base: master
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
11 changes: 10 additions & 1 deletion src/Maker/AdminConfigurationMaker/AdminGeneratorSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

namespace FriendsOfSulu\MakerBundle\Maker\AdminConfigurationMaker;

/** @internal */
class AdminGeneratorSettings
{
public bool $shouldAddMenuItem = false;
public bool $shouldAddMenuItem = true;

public bool $shouldHaveEditForm = true;

Expand All @@ -18,6 +19,14 @@ class AdminGeneratorSettings

public string $listKey;

public function __construct(
public string $resourceKey
) {
$this->slug = '/' . $resourceKey;
$this->formKey = $resourceKey;
$this->listKey = $resourceKey;
}

/** @var array<string> */
public array $listToolbarActions = [
'add', 'delete', 'export',
Expand Down
109 changes: 66 additions & 43 deletions src/Maker/AdminConfigurationMaker/MakeAdminConfigurationCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

use FriendsOfSulu\MakerBundle\Utils\ConsoleHelperTrait;
use FriendsOfSulu\MakerBundle\Utils\NameGenerators\ResourceKeyExtractor;
use FriendsOfSulu\MakerBundle\Utils\NameGenerators\UniqueNameGenerator;
use Symfony\Bundle\MakerBundle\ConsoleStyle;
use Symfony\Bundle\MakerBundle\DependencyBuilder;
use Symfony\Bundle\MakerBundle\Doctrine\DoctrineHelper;
use Symfony\Bundle\MakerBundle\Generator;
use Symfony\Bundle\MakerBundle\InputConfiguration;
use Symfony\Bundle\MakerBundle\Maker\AbstractMaker;
Expand All @@ -17,29 +17,31 @@
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Question\ChoiceQuestion;
use Webmozart\Assert\Assert;

class MakeAdminConfigurationCommand extends AbstractMaker
/** @internal */
final class MakeAdminConfigurationCommand extends AbstractMaker
{
use ConsoleHelperTrait;

public const ARG_RESOURCE_CLASS = 'resourceClass';
public const OPT_FORCE = 'force';
public const OPT_PERMISSIONS = 'permissions';
public const OPT_ADVANCED = 'advanced';
public const OPT_ASSUME_DEFAULTS = 'assume-defaults';

private AdminGeneratorSettings $settings;

public const ADMIN_DEPENDENCIES = [
"Sulu\Bundle\AdminBundle\Admin\Admin",
"Sulu\Bundle\AdminBundle\Admin\Navigation\NavigationItem",
"Sulu\Bundle\AdminBundle\Admin\Navigation\NavigationItemCollection",
"Sulu\Bundle\AdminBundle\Admin\View\ToolbarAction",
'Sulu\Bundle\AdminBundle\Admin\Admin',
'Sulu\Bundle\AdminBundle\Admin\Navigation\NavigationItem',
'Sulu\Bundle\AdminBundle\Admin\Navigation\NavigationItemCollection',
'Sulu\Bundle\AdminBundle\Admin\View\ToolbarAction',
'Sulu\Bundle\AdminBundle\Admin\View\ViewBuilderFactoryInterface',
"Sulu\Bundle\AdminBundle\Admin\View\ViewCollection",
'Sulu\Bundle\AdminBundle\Admin\View\ViewCollection',
];

public function __construct(
private ResourceKeyExtractor $resourceKeyExtractor,
private UniqueNameGenerator $nameGenerator
private DoctrineHelper $doctrineHelper,
) {
}

Expand All @@ -64,47 +66,56 @@ public function configureCommand(Command $command, InputConfiguration $inputConf
->addArgument(self::ARG_RESOURCE_CLASS, InputArgument::OPTIONAL, \sprintf('Class that you want to generate the list view for (eg. <fg=yellow>%s</>)', Str::asClassName(Str::getRandomTerm())))
->addOption(self::OPT_PERMISSIONS, null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'List of permissions that should be configurable')
->addOption(self::OPT_FORCE, '-f', InputOption::VALUE_NONE, 'Force the creation of a new file even if the old one is already there')
->addOption(self::OPT_ADVANCED, '-a', InputOption::VALUE_NONE, 'Show all the options. This only works for interactive prompts though')
->addOption(self::OPT_ASSUME_DEFAULTS, '-d', InputOption::VALUE_NONE, 'Assume default values and ask less questions')
;
}

public function interact(InputInterface $input, ConsoleStyle $io, Command $command): void
{
$this->interactiveEntityArgument($input, self::ARG_RESOURCE_CLASS, $this->doctrineHelper);

/** @var class-string $resourceClassName */
$resourceClassName = $input->getArgument(self::ARG_RESOURCE_CLASS);
$resourceKey = $this->resourceKeyExtractor->getUniqueName($resourceClassName);

$this->settings = $this->askMethodsToBeGenerated(
$io,
assumeDefaults: true === $input->getOption(self::OPT_ASSUME_DEFAULTS),
resourceKey: $resourceKey
);
}

public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator): void
{
/** @var string $className */
$className = $input->getArgument(self::ARG_RESOURCE_CLASS);
Assert::classExists($className);
$resourceKey = $this->resourceKeyExtractor->getUniqueName($className);
$generatedClassName = 'App\\Admin\\' . $this->nameGenerator->getUniqueName($className) . 'Admin';
/** @var class-string $resourceClassName */
$resourceClassName = $input->getArgument(self::ARG_RESOURCE_CLASS);

$className = $generator->createClassNameDetails(
Str::getShortClassName($resourceClassName),
namespacePrefix: 'Admin\\',
suffix: 'Admin'
);

$useStatements = new UseStatementGenerator(
\array_merge(
self::ADMIN_DEPENDENCIES,
[
"Sulu\Component\Security\Authorization\PermissionTypes",
"Sulu\Component\Security\Authorization\SecurityCheckerInterface",
'Sulu\Component\Security\Authorization\PermissionTypes',
'Sulu\Component\Security\Authorization\SecurityCheckerInterface',
]
)
);

$settings = new AdminGeneratorSettings();

$settings->shouldAddMenuItem = $io->confirm('Do you want to have a menu entry?');
$settings->shouldHaveEditForm = $io->confirm('Do you want to have an edit form?');
$settings->shouldHaveReferences = $io->confirm('Do you want to have an a references tab?');

if ($settings->shouldHaveReferences) {
if ($this->settings->shouldHaveReferences) {
$useStatements->addUseStatement('Sulu\Bundle\ReferenceBundle\Infrastructure\Sulu\Admin\View\ReferenceViewBuilderFactoryInterface');
}

$slug = $this->askString($io, 'Enter the API slug', '/' . $resourceKey);
$settings->slug = '/' . \ltrim($slug, '/');

if (\str_contains($settings->slug, '_')) {
if (\str_contains($this->settings->slug, '_')) {
$io->warning('Your slug contains an _ this could cause problems when generating a controller for this class. It is recommended to not use underscores in the slug.');
}

/** @var class-string $permissionTypeClass */
$permissionTypeClass = "Sulu\Component\Security\Authorization\PermissionTypes";
$permissionTypeClass = 'Sulu\Component\Security\Authorization\PermissionTypes';

/** @var array<string> $availablePermissions */
$availablePermissions = \array_keys((new \ReflectionClass($permissionTypeClass))->getConstants());
Expand All @@ -122,26 +133,18 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
/** @var array<string> $answer */
$answer = $io->askQuestion($choiceQuestion);

$settings->permissionTypes = $answer;
$this->settings->permissionTypes = $answer;
} else {
$settings->permissionTypes = $currentOptionvalue ?: $availablePermissions;
}

if ($input->hasOption(self::OPT_ADVANCED) && $input->isInteractive()) {
$settings->formKey = $this->askString($io, 'Form Key', $resourceKey);
$settings->listKey = $this->askString($io, 'List Key', $resourceKey);
} else {
$settings->formKey = $resourceKey;
$settings->listKey = $resourceKey;
$this->settings->permissionTypes = $currentOptionvalue ?: $availablePermissions;
}

$generator->generateClass(
$generatedClassName,
$className->getFullName(),
__DIR__ . '/configurationTemplate.tpl.php',
[
'use_statements' => $useStatements,
'resourceKey' => $resourceKey,
'settings' => $settings,
'resourceKey' => $this->settings->resourceKey,
'settings' => $this->settings,
]
);

Expand All @@ -154,4 +157,24 @@ public function configureDependencies(DependencyBuilder $dependencies): void
$dependencies->addClassDependency($class, 'sulu/sulu-admin');
}
}

private function askMethodsToBeGenerated(ConsoleStyle $io, bool $assumeDefaults, string $resourceKey): AdminGeneratorSettings
{
$settings = new AdminGeneratorSettings($resourceKey);
if ($assumeDefaults) {
return $settings;
}

$settings->shouldAddMenuItem = $io->confirm('Do you want to have a menu entry?');
$settings->shouldHaveEditForm = $io->confirm('Do you want to have an edit form?');
$settings->shouldHaveReferences = $io->confirm('Do you want to have an a references tab?');

$slug = $this->askString($io, 'Enter the API slug', '/' . $settings->resourceKey);
$this->settings->slug = '/' . \ltrim($slug, '/');

$settings->formKey = $this->askString($io, 'Form Key', $resourceKey);
$settings->listKey = $this->askString($io, 'List Key', $resourceKey);

return $settings;
}
}
1 change: 1 addition & 0 deletions src/Maker/ControllerMaker/ControllerGeneratorSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace FriendsOfSulu\MakerBundle\Maker\ControllerMaker;

/** @internal */
class ControllerGeneratorSettings
{
public bool $shouldHaveGetListAction = true;
Expand Down
Loading