1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Doctrine\ORM\Tools\Console\Command\ClearCache; |
6
|
|
|
|
7
|
|
|
use Doctrine\ORM\Cache; |
8
|
|
|
use Doctrine\ORM\Cache\Region\DefaultRegion; |
9
|
|
|
use InvalidArgumentException; |
10
|
|
|
use Symfony\Component\Console\Command\Command; |
11
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
12
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
13
|
|
|
use Symfony\Component\Console\Input\InputOption; |
14
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
15
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
16
|
|
|
use function get_class; |
17
|
|
|
use function gettype; |
18
|
|
|
use function is_object; |
19
|
|
|
use function sprintf; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Command to clear a entity cache region. |
23
|
|
|
*/ |
24
|
|
|
class EntityRegionCommand extends Command |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* {@inheritdoc} |
28
|
|
|
*/ |
29
|
4 |
|
protected function configure() |
30
|
|
|
{ |
31
|
4 |
|
$this->setName('orm:clear-cache:region:entity') |
32
|
4 |
|
->setDescription('Clear a second-level cache entity region') |
33
|
4 |
|
->addArgument('entity-class', InputArgument::OPTIONAL, 'The entity name.') |
34
|
4 |
|
->addArgument('entity-id', InputArgument::OPTIONAL, 'The entity identifier.') |
35
|
4 |
|
->addOption('all', null, InputOption::VALUE_NONE, 'If defined, all entity regions will be deleted/invalidated.') |
36
|
4 |
|
->addOption('flush', null, InputOption::VALUE_NONE, 'If defined, all cache entries will be flushed.') |
37
|
4 |
|
->setHelp(<<<'EOT' |
38
|
4 |
|
The <info>%command.name%</info> command is meant to clear a second-level cache entity region for an associated Entity Manager. |
39
|
|
|
It is possible to delete/invalidate all entity region, a specific entity region or flushes the cache provider. |
40
|
|
|
|
41
|
|
|
The execution type differ on how you execute the command. |
42
|
|
|
If you want to invalidate all entries for an entity region this command would do the work: |
43
|
|
|
|
44
|
|
|
<info>%command.name% 'Entities\MyEntity'</info> |
45
|
|
|
|
46
|
|
|
To invalidate a specific entry you should use : |
47
|
|
|
|
48
|
|
|
<info>%command.name% 'Entities\MyEntity' 1</info> |
49
|
|
|
|
50
|
|
|
If you want to invalidate all entries for the all entity regions: |
51
|
|
|
|
52
|
|
|
<info>%command.name% --all</info> |
53
|
|
|
|
54
|
|
|
Alternatively, if you want to flush the configured cache provider for an entity region use this command: |
55
|
|
|
|
56
|
|
|
<info>%command.name% 'Entities\MyEntity' --flush</info> |
57
|
|
|
|
58
|
|
|
Finally, be aware that if <info>--flush</info> option is passed, |
59
|
|
|
not all cache providers are able to flush entries, because of a limitation of its execution nature. |
60
|
|
|
EOT |
61
|
|
|
); |
62
|
4 |
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* {@inheritdoc} |
66
|
|
|
*/ |
67
|
4 |
|
protected function execute(InputInterface $input, OutputInterface $output) |
68
|
|
|
{ |
69
|
4 |
|
$ui = new SymfonyStyle($input, $output); |
70
|
|
|
|
71
|
4 |
|
$em = $this->getHelper('em')->getEntityManager(); |
72
|
4 |
|
$entityClass = $input->getArgument('entity-class'); |
73
|
4 |
|
$entityId = $input->getArgument('entity-id'); |
74
|
4 |
|
$cache = $em->getCache(); |
75
|
|
|
|
76
|
4 |
|
if (! $cache instanceof Cache) { |
77
|
|
|
throw new InvalidArgumentException('No second-level cache is configured on the given EntityManager.'); |
78
|
|
|
} |
79
|
|
|
|
80
|
4 |
|
if (! $entityClass && ! $input->getOption('all')) { |
81
|
|
|
throw new InvalidArgumentException('Invalid argument "--entity-class"'); |
82
|
|
|
} |
83
|
|
|
|
84
|
4 |
|
if ($input->getOption('flush')) { |
85
|
1 |
|
$entityRegion = $cache->getEntityCacheRegion($entityClass); |
86
|
|
|
|
87
|
1 |
|
if (! $entityRegion instanceof DefaultRegion) { |
88
|
|
|
throw new InvalidArgumentException(sprintf( |
89
|
|
|
'The option "--flush" expects a "Doctrine\ORM\Cache\Region\DefaultRegion", but got "%s".', |
90
|
|
|
is_object($entityRegion) ? get_class($entityRegion) : gettype($entityRegion) |
91
|
|
|
)); |
92
|
|
|
} |
93
|
|
|
|
94
|
1 |
|
$entityRegion->getCache()->flushAll(); |
95
|
|
|
|
96
|
1 |
|
$ui->comment(sprintf('Flushing cache provider configured for entity named <info>"%s"</info>', $entityClass)); |
97
|
|
|
|
98
|
1 |
|
return; |
99
|
|
|
} |
100
|
|
|
|
101
|
3 |
|
if ($input->getOption('all')) { |
102
|
1 |
|
$ui->comment('Clearing <info>all</info> second-level cache entity regions'); |
103
|
|
|
|
104
|
1 |
|
$cache->evictEntityRegions(); |
105
|
|
|
|
106
|
1 |
|
return; |
107
|
|
|
} |
108
|
|
|
|
109
|
2 |
|
if ($entityId) { |
110
|
1 |
|
$ui->comment( |
111
|
1 |
|
sprintf( |
112
|
1 |
|
'Clearing second-level cache entry for entity <info>"%s"</info> identified by <info>"%s"</info>', |
113
|
1 |
|
$entityClass, |
114
|
1 |
|
$entityId |
115
|
|
|
) |
116
|
|
|
); |
117
|
1 |
|
$cache->evictEntity($entityClass, $entityId); |
118
|
|
|
|
119
|
1 |
|
return; |
120
|
|
|
} |
121
|
|
|
|
122
|
1 |
|
$ui->comment(sprintf('Clearing second-level cache for entity <info>"%s"</info>', $entityClass)); |
123
|
1 |
|
$cache->evictEntityRegion($entityClass); |
124
|
1 |
|
} |
125
|
|
|
} |
126
|
|
|
|