Skip to content

Commit af2b38f

Browse files
committed
Refactoring all commands away from using the service locator
1 parent 9007625 commit af2b38f

8 files changed

Lines changed: 83 additions & 15 deletions

Command/ActivateUserCommand.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111

1212
namespace FOS\UserBundle\Command;
1313

14-
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
14+
use FOS\UserBundle\Util\UserManipulator;
15+
use Symfony\Component\Console\Command\Command;
1516
use Symfony\Component\Console\Input\InputArgument;
1617
use Symfony\Component\Console\Input\InputInterface;
1718
use Symfony\Component\Console\Output\OutputInterface;
@@ -20,8 +21,19 @@
2021
/**
2122
* @author Antoine Hérault <antoine.herault@gmail.com>
2223
*/
23-
class ActivateUserCommand extends ContainerAwareCommand
24+
class ActivateUserCommand extends Command
2425
{
26+
protected static $defaultName = 'fos:user:activate';
27+
28+
private $userManipulator;
29+
30+
public function __construct(UserManipulator $userManipulator)
31+
{
32+
parent::__construct();
33+
34+
$this->userManipulator = $userManipulator;
35+
}
36+
2537
/**
2638
* {@inheritdoc}
2739
*/
@@ -48,7 +60,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
4860
{
4961
$username = $input->getArgument('username');
5062

51-
$manipulator = $this->getContainer()->get('fos_user.util.user_manipulator');
63+
$manipulator = $this->userManipulator;
5264
$manipulator->activate($username);
5365

5466
$output->writeln(sprintf('User "%s" has been activated.', $username));

Command/ChangePasswordCommand.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111

1212
namespace FOS\UserBundle\Command;
1313

14-
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
14+
use FOS\UserBundle\Util\UserManipulator;
15+
use Symfony\Component\Console\Command\Command;
1516
use Symfony\Component\Console\Input\InputArgument;
1617
use Symfony\Component\Console\Input\InputInterface;
1718
use Symfony\Component\Console\Output\OutputInterface;
@@ -20,8 +21,20 @@
2021
/**
2122
* ChangePasswordCommand.
2223
*/
23-
class ChangePasswordCommand extends ContainerAwareCommand
24+
class ChangePasswordCommand extends Command
2425
{
26+
protected static $defaultName = 'fos:user:change-password';
27+
28+
private $userManipulator;
29+
30+
public function __construct(UserManipulator $userManipulator)
31+
{
32+
parent::__construct();
33+
34+
$this->userManipulator = $userManipulator;
35+
}
36+
37+
2538
/**
2639
* {@inheritdoc}
2740
*/
@@ -57,7 +70,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
5770
$username = $input->getArgument('username');
5871
$password = $input->getArgument('password');
5972

60-
$manipulator = $this->getContainer()->get('fos_user.util.user_manipulator');
73+
$manipulator = $this->userManipulator;
6174
$manipulator->changePassword($username, $password);
6275

6376
$output->writeln(sprintf('Changed password for user <comment>%s</comment>', $username));

Command/CreateUserCommand.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111

1212
namespace FOS\UserBundle\Command;
1313

14-
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
14+
use FOS\UserBundle\Util\UserManipulator;
15+
use Symfony\Component\Console\Command\Command;
1516
use Symfony\Component\Console\Input\InputArgument;
1617
use Symfony\Component\Console\Input\InputInterface;
1718
use Symfony\Component\Console\Input\InputOption;
@@ -23,8 +24,19 @@
2324
* @author Thibault Duplessis <thibault.duplessis@gmail.com>
2425
* @author Luis Cordova <cordoval@gmail.com>
2526
*/
26-
class CreateUserCommand extends ContainerAwareCommand
27+
class CreateUserCommand extends Command
2728
{
29+
protected static $defaultName = 'fos:user:create';
30+
31+
private $userManipulator;
32+
33+
public function __construct(UserManipulator $userManipulator)
34+
{
35+
parent::__construct();
36+
37+
$this->userManipulator = $userManipulator;
38+
}
39+
2840
/**
2941
* {@inheritdoc}
3042
*/
@@ -74,7 +86,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
7486
$inactive = $input->getOption('inactive');
7587
$superadmin = $input->getOption('super-admin');
7688

77-
$manipulator = $this->getContainer()->get('fos_user.util.user_manipulator');
89+
$manipulator = $this->userManipulator;
7890
$manipulator->create($username, $password, $email, !$inactive, $superadmin);
7991

8092
$output->writeln(sprintf('Created user <comment>%s</comment>', $username));

Command/DeactivateUserCommand.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111

1212
namespace FOS\UserBundle\Command;
1313

14-
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
14+
use FOS\UserBundle\Util\UserManipulator;
15+
use Symfony\Component\Console\Command\Command;
1516
use Symfony\Component\Console\Input\InputArgument;
1617
use Symfony\Component\Console\Input\InputInterface;
1718
use Symfony\Component\Console\Output\OutputInterface;
@@ -20,8 +21,19 @@
2021
/**
2122
* @author Antoine Hérault <antoine.herault@gmail.com>
2223
*/
23-
class DeactivateUserCommand extends ContainerAwareCommand
24+
class DeactivateUserCommand extends Command
2425
{
26+
protected static $defaultName = 'fos:user:deactivate';
27+
28+
private $userManipulator;
29+
30+
public function __construct(UserManipulator $userManipulator)
31+
{
32+
parent::__construct();
33+
34+
$this->userManipulator = $userManipulator;
35+
}
36+
2537
/**
2638
* {@inheritdoc}
2739
*/
@@ -48,7 +60,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
4860
{
4961
$username = $input->getArgument('username');
5062

51-
$manipulator = $this->getContainer()->get('fos_user.util.user_manipulator');
63+
$manipulator = $this->userManipulator;
5264
$manipulator->deactivate($username);
5365

5466
$output->writeln(sprintf('User "%s" has been deactivated.', $username));

Command/DemoteUserCommand.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
*/
2121
class DemoteUserCommand extends RoleCommand
2222
{
23+
protected static $defaultName = 'fos:user:demote';
24+
2325
/**
2426
* {@inheritdoc}
2527
*/

Command/PromoteUserCommand.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
*/
2323
class PromoteUserCommand extends RoleCommand
2424
{
25+
protected static $defaultName = 'fos:user:promote';
26+
2527
/**
2628
* {@inheritdoc}
2729
*/

Command/RoleCommand.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
namespace FOS\UserBundle\Command;
1313

1414
use FOS\UserBundle\Util\UserManipulator;
15-
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
15+
use Symfony\Component\Console\Command\Command;
1616
use Symfony\Component\Console\Input\InputArgument;
1717
use Symfony\Component\Console\Input\InputInterface;
1818
use Symfony\Component\Console\Input\InputOption;
@@ -22,8 +22,17 @@
2222
/**
2323
* @author Lenar Lõhmus <lenar@city.ee>
2424
*/
25-
abstract class RoleCommand extends ContainerAwareCommand
25+
abstract class RoleCommand extends Command
2626
{
27+
private $userManipulator;
28+
29+
public function __construct(UserManipulator $userManipulator)
30+
{
31+
parent::__construct();
32+
33+
$this->userManipulator = $userManipulator;
34+
}
35+
2736
/**
2837
* {@inheritdoc}
2938
*/
@@ -54,7 +63,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
5463
throw new \RuntimeException('Not enough arguments.');
5564
}
5665

57-
$manipulator = $this->getContainer()->get('fos_user.util.user_manipulator');
66+
$manipulator = $this->userManipulator;
5867
$this->executeRoleCommand($manipulator, $output, $username, $super, $role);
5968
}
6069

Resources/config/commands.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,27 @@
66

77
<services>
88
<service id="fos_user.command.activate_user" class="FOS\UserBundle\Command\ActivateUserCommand">
9+
<argument type="service" id="fos_user.util.user_manipulator" />
910
<tag name="console.command" command="fos:user:activate" />
1011
</service>
1112
<service id="fos_user.command.change_password" class="FOS\UserBundle\Command\ChangePasswordCommand">
13+
<argument type="service" id="fos_user.util.user_manipulator" />
1214
<tag name="console.command" command="fos:user:change-password" />
1315
</service>
1416
<service id="fos_user.command.create_user" class="FOS\UserBundle\Command\CreateUserCommand">
17+
<argument type="service" id="fos_user.util.user_manipulator" />
1518
<tag name="console.command" command="fos:user:create" />
1619
</service>
1720
<service id="fos_user.command.deactivate_user" class="FOS\UserBundle\Command\DeactivateUserCommand">
21+
<argument type="service" id="fos_user.util.user_manipulator" />
1822
<tag name="console.command" command="fos:user:deactivate" />
1923
</service>
2024
<service id="fos_user.command.demote_user" class="FOS\UserBundle\Command\DemoteUserCommand">
25+
<argument type="service" id="fos_user.util.user_manipulator" />
2126
<tag name="console.command" command="fos:user:demote" />
2227
</service>
2328
<service id="fos_user.command.promote_user" class="FOS\UserBundle\Command\PromoteUserCommand">
29+
<argument type="service" id="fos_user.util.user_manipulator" />
2430
<tag name="console.command" command="fos:user:promote" />
2531
</service>
2632
</services>

0 commit comments

Comments
 (0)