1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ae\FeatureBundle\Command; |
4
|
|
|
|
5
|
|
|
use Ae\FeatureBundle\Entity\FeatureManager; |
6
|
|
|
use Doctrine\ORM\EntityManager; |
7
|
|
|
use Symfony\Component\Console\Command\Command; |
8
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
9
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
10
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Command used to disable an existing feature. |
14
|
|
|
* |
15
|
|
|
* @author Emanuele Minotto <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
class DisableFeatureCommand extends Command |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var FeatureManager |
21
|
|
|
*/ |
22
|
|
|
private $featureManager; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var EntityManager |
26
|
|
|
*/ |
27
|
|
|
private $entityManager; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param FeatureManager $featureManager |
31
|
|
|
* @param EntityManager $entityManager |
32
|
|
|
*/ |
33
|
1 |
|
public function __construct( |
34
|
|
|
FeatureManager $featureManager, |
35
|
|
|
EntityManager $entityManager |
|
|
|
|
36
|
|
|
) { |
37
|
1 |
|
parent::__construct(); |
38
|
|
|
|
39
|
1 |
|
$this->featureManager = $featureManager; |
40
|
1 |
|
$this->entityManager = $entityManager; |
41
|
1 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* {@inheritdoc} |
45
|
|
|
*/ |
46
|
1 |
|
protected function configure() |
47
|
|
|
{ |
48
|
1 |
|
$this |
49
|
1 |
|
->setName('features:disable') |
50
|
1 |
|
->setDescription('Disable an existing feature') |
51
|
1 |
|
->addArgument('parent', InputArgument::REQUIRED, 'Parent feature') |
52
|
1 |
|
->addArgument('name', InputArgument::REQUIRED, 'Feature name'); |
53
|
1 |
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* {@inheritdoc} |
57
|
|
|
*/ |
58
|
1 |
|
public function execute(InputInterface $input, OutputInterface $output) |
59
|
|
|
{ |
60
|
1 |
|
$name = $input->getArgument('name'); |
61
|
1 |
|
$parent = $input->getArgument('parent'); |
62
|
|
|
|
63
|
1 |
|
$output->write(sprintf( |
64
|
1 |
|
'Disabling <info>%s</info>.<info>%s</info>... ', |
65
|
1 |
|
$parent, |
66
|
|
|
$name |
67
|
1 |
|
)); |
68
|
|
|
|
69
|
1 |
|
$feature = $this->featureManager->find($name, $parent); |
70
|
|
|
|
71
|
1 |
|
$feature->setEnabled(false); |
72
|
|
|
|
73
|
1 |
|
$this->entityManager->persist($feature); |
74
|
1 |
|
$this->entityManager->flush(); |
75
|
|
|
|
76
|
1 |
|
$this->featureManager->emptyCache($name, $parent); |
77
|
|
|
|
78
|
1 |
|
$output->writeln('OK'); |
79
|
1 |
|
} |
80
|
|
|
} |
81
|
|
|
|
The
EntityManager
might become unusable for example if a transaction is rolled back and it gets closed. Let’s assume that somewhere in your application, or in a third-party library, there is code such as the following:If that code throws an exception and the
EntityManager
is closed. Any other code which depends on the same instance of theEntityManager
during this request will fail.On the other hand, if you instead inject the
ManagerRegistry
, thegetManager()
method guarantees that you will always get a usable manager instance.