Skip to content

Commit

Permalink
Improve lazyness of commands
Browse files Browse the repository at this point in the history
  • Loading branch information
jordisala1991 committed Jun 25, 2022
1 parent f07d94f commit f79270e
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 13 deletions.
12 changes: 10 additions & 2 deletions src/Command/ExplainAdminCommand.php
Expand Up @@ -14,6 +14,7 @@
namespace Sonata\AdminBundle\Command;

use Sonata\AdminBundle\Admin\Pool;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
Expand All @@ -22,8 +23,13 @@
/**
* @author Thomas Rabaix <thomas.rabaix@sonata-project.org>
*/
#[AsCommand(name: 'sonata:admin:explain', description: 'Explain an admin service')]
final class ExplainAdminCommand extends Command
{
// TODO: Remove static properties when support for Symfony < 5.4 is dropped.
protected static $defaultName = 'sonata:admin:explain';
protected static $defaultDescription = 'Explain an admin service';

private Pool $pool;

/**
Expand All @@ -38,9 +44,11 @@ public function __construct(Pool $pool)

public function configure(): void
{
\assert(null !== static::$defaultDescription);

$this
->setName('sonata:admin:explain')
->setDescription('Explain an admin service')
// TODO: Remove setDescription when support for Symfony < 5.4 is dropped.
->setDescription(static::$defaultDescription)
->addArgument('admin', InputArgument::REQUIRED, 'The admin service id');
}

Expand Down
12 changes: 10 additions & 2 deletions src/Command/GenerateObjectAclCommand.php
Expand Up @@ -15,6 +15,7 @@

use Sonata\AdminBundle\Admin\Pool;
use Sonata\AdminBundle\Util\ObjectAclManipulatorInterface;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
Expand All @@ -24,8 +25,13 @@
/**
* @author Thomas Rabaix <thomas.rabaix@sonata-project.org>
*/
#[AsCommand(name: 'sonata:admin:generate-object-acl', description: 'Install ACL for the objects of the Admin Classes.')]
final class GenerateObjectAclCommand extends QuestionableCommand
{
// TODO: Remove static properties when support for Symfony < 5.4 is dropped.
protected static $defaultName = 'sonata:admin:generate-object-acl';
protected static $defaultDescription = 'Install ACL for the objects of the Admin Classes.';

private string $userModelClass = '';

private Pool $pool;
Expand All @@ -52,9 +58,11 @@ public function __construct(Pool $pool, array $aclObjectManipulators)

public function configure(): void
{
\assert(null !== static::$defaultDescription);

$this
->setName('sonata:admin:generate-object-acl')
->setDescription('Install ACL for the objects of the Admin Classes.')
// TODO: Remove setDescription when support for Symfony < 5.4 is dropped.
->setDescription(static::$defaultDescription)
->addOption('object_owner', null, InputOption::VALUE_OPTIONAL, 'If set, the task will set the object owner for each admin.')
->addOption('user_model', null, InputOption::VALUE_OPTIONAL, 'Fully qualified class name <comment>App\Model\User</comment>. If not set, it will be asked the first time an object owner is set.')
->addOption('step', null, InputOption::VALUE_NONE, 'If set, the task will ask for each admin if the ACLs need to be generated and what object owner to set, if any.');
Expand Down
12 changes: 10 additions & 2 deletions src/Command/ListAdminCommand.php
Expand Up @@ -14,15 +14,21 @@
namespace Sonata\AdminBundle\Command;

use Sonata\AdminBundle\Admin\Pool;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

/**
* @author Thomas Rabaix <thomas.rabaix@sonata-project.org>
*/
#[AsCommand(name: 'sonata:admin:list', description: 'List all admin services available')]
final class ListAdminCommand extends Command
{
// TODO: Remove static properties when support for Symfony < 5.4 is dropped.
protected static $defaultName = 'sonata:admin:list';
protected static $defaultDescription = 'List all admin services available';

private Pool $pool;

/**
Expand All @@ -37,9 +43,11 @@ public function __construct(Pool $pool)

public function configure(): void
{
\assert(null !== static::$defaultDescription);

$this
->setName('sonata:admin:list')
->setDescription('List all admin services available');
// TODO: Remove setDescription when support for Symfony < 5.4 is dropped.
->setDescription(static::$defaultDescription);
}

public function execute(InputInterface $input, OutputInterface $output): int
Expand Down
12 changes: 10 additions & 2 deletions src/Command/SetupAclCommand.php
Expand Up @@ -15,15 +15,21 @@

use Sonata\AdminBundle\Admin\Pool;
use Sonata\AdminBundle\Util\AdminAclManipulatorInterface;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

/**
* @author Thomas Rabaix <thomas.rabaix@sonata-project.org>
*/
#[AsCommand(name: 'sonata:admin:setup-acl', description: 'Install ACL for Admin Classes')]
final class SetupAclCommand extends Command
{
// TODO: Remove static properties when support for Symfony < 5.4 is dropped.
protected static $defaultName = 'sonata:admin:setup-acl';
protected static $defaultDescription = 'Install ACL for Admin Classes';

private Pool $pool;

private AdminAclManipulatorInterface $aclManipulator;
Expand All @@ -41,9 +47,11 @@ public function __construct(Pool $pool, AdminAclManipulatorInterface $aclManipul

public function configure(): void
{
\assert(null !== static::$defaultDescription);

$this
->setName('sonata:admin:setup-acl')
->setDescription('Install ACL for Admin Classes');
// TODO: Remove setDescription when support for Symfony < 5.4 is dropped.
->setDescription(static::$defaultDescription);
}

public function execute(InputInterface $input, OutputInterface $output): int
Expand Down
9 changes: 4 additions & 5 deletions src/Resources/config/commands.php
Expand Up @@ -20,30 +20,29 @@

return static function (ContainerConfigurator $containerConfigurator): void {
// Use "service" function for creating references to services when dropping support for Symfony 4.4
// Use "param" function for creating references to parameters when dropping support for Symfony 5.1
$containerConfigurator->services()

->set('sonata.admin.command.explain', ExplainAdminCommand::class)
->tag('console.command', ['command' => 'sonata:admin:explain'])
->tag('console.command')
->args([
new ReferenceConfigurator('sonata.admin.pool'),
])

->set('sonata.admin.command.generate_object_acl', GenerateObjectAclCommand::class)
->tag('console.command', ['command' => 'sonata:admin:generate-object-acl'])
->tag('console.command')
->args([
new ReferenceConfigurator('sonata.admin.pool'),
[],
])

->set('sonata.admin.command.list', ListAdminCommand::class)
->tag('console.command', ['command' => 'sonata:admin:list'])
->tag('console.command')
->args([
new ReferenceConfigurator('sonata.admin.pool'),
])

->set('sonata.admin.command.setup_acl', SetupAclCommand::class)
->tag('console.command', ['command' => 'sonata:admin:setup-acl'])
->tag('console.command')
->args([
new ReferenceConfigurator('sonata.admin.pool'),
new ReferenceConfigurator('sonata.admin.manipulator.acl.admin'),
Expand Down

0 comments on commit f79270e

Please sign in to comment.