1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kunstmaan\AdminBundle\Command; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
6
|
|
|
use Kunstmaan\AdminBundle\Entity\Role; |
7
|
|
|
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
8
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
9
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
10
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Symfony CLI command to create a group using bin/console kuma:role:create <NAME_OF_THE_ROLE> |
14
|
|
|
* |
15
|
|
|
* @final since 5.1 |
16
|
|
|
* NEXT_MAJOR extend from `Command` and remove `$this->getContainer` usages |
17
|
|
|
*/ |
18
|
|
|
class CreateRoleCommand extends ContainerAwareCommand |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var EntityManagerInterface |
22
|
|
|
*/ |
23
|
|
|
private $em; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param EntityManagerInterface|null $em |
27
|
|
|
*/ |
28
|
|
|
public function __construct(/* EntityManagerInterface */ $em = null) |
29
|
|
|
{ |
30
|
|
|
parent::__construct(); |
31
|
|
|
|
32
|
|
|
if (!$em instanceof EntityManagerInterface) { |
33
|
|
|
@trigger_error(sprintf('Passing a command name as the first argument of "%s" is deprecated since version symfony 3.4 and will be removed in symfony 4.0. If the command was registered by convention, make it a service instead. ', __METHOD__), E_USER_DEPRECATED); |
|
|
|
|
34
|
|
|
|
35
|
|
|
$this->setName(null === $em ? 'kuma:role:create' : $em); |
36
|
|
|
|
37
|
|
|
return; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
$this->em = $em; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Configures the current command. |
45
|
|
|
*/ |
46
|
|
|
protected function configure() |
47
|
|
|
{ |
48
|
|
|
$this->setName('kuma:role:create') |
49
|
|
|
->setDescription('Create a role.') |
50
|
|
|
->setDefinition(array( |
51
|
|
|
new InputArgument('role', InputArgument::REQUIRED, 'The role'), |
52
|
|
|
)) |
53
|
|
|
->setHelp(<<<'EOT' |
54
|
|
|
The <info>kuma:role:create</info> command creates a role: |
55
|
|
|
|
56
|
|
|
<info>php bin/console kuma:role:create ROLE_ADMIN</info> |
57
|
|
|
|
58
|
|
|
<comment>Note:</comment> The ROLE_ prefix will be added if you don't provide it |
59
|
|
|
|
60
|
|
|
<info>php bin/console kuma:role:create ADMIN</info> |
61
|
|
|
|
62
|
|
|
will create ROLE_ADMIN. |
63
|
|
|
|
64
|
|
|
EOT |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Executes the current command. |
70
|
|
|
* |
71
|
|
|
* @param InputInterface $input The input |
72
|
|
|
* @param OutputInterface $output The output |
73
|
|
|
* |
74
|
|
|
* @return int |
75
|
|
|
*/ |
76
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
77
|
|
|
{ |
78
|
|
|
if (null === $this->em) { |
79
|
|
|
$this->em = $this->getContainer()->get('doctrine.orm.entity_manager'); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$roleName = strtoupper($input->getArgument('role')); |
83
|
|
|
if ('ROLE_' != substr($roleName, 0, 5)) { |
84
|
|
|
$roleName = 'ROLE_' . $roleName; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$role = new Role($roleName); |
88
|
|
|
$this->em->persist($role); |
89
|
|
|
$this->em->flush(); |
90
|
|
|
|
91
|
|
|
$output->writeln(sprintf('Created role <comment>%s</comment>', $roleName)); |
92
|
|
|
|
93
|
|
|
return 0; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: