|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace TreeHouse\IoBundle\Command; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\Console\Command\Command; |
|
6
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
7
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
8
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
9
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
10
|
|
|
use TreeHouse\IoBundle\Export\FeedExporter; |
|
11
|
|
|
use TreeHouse\IoBundle\Export\FeedType\FeedTypeInterface; |
|
12
|
|
|
|
|
13
|
|
|
class ExportCacheClearCommand extends Command |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var FeedExporter |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $exporter; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @param FeedExporter $exporter |
|
22
|
|
|
*/ |
|
23
|
|
|
public function __construct(FeedExporter $exporter) |
|
24
|
|
|
{ |
|
25
|
|
|
parent::__construct(); |
|
26
|
|
|
|
|
27
|
|
|
$this->exporter = $exporter; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @inheritdoc |
|
32
|
|
|
*/ |
|
33
|
|
|
protected function configure() |
|
34
|
|
|
{ |
|
35
|
|
|
$this->setName('io:export:cache:clear'); |
|
36
|
|
|
$this->addArgument('type', InputArgument::OPTIONAL | InputArgument::IS_ARRAY, 'The type(s) to export feeds for. If left empty, feeds for all known types are exported.'); |
|
37
|
|
|
$this->addOption('where', null, InputOption::VALUE_OPTIONAL, 'Limit the cache to a specific set of the query, use <comment>x</comment> as root alias'); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @inheritdoc |
|
42
|
|
|
*/ |
|
43
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
44
|
|
|
{ |
|
45
|
|
|
$types = $this->getTypes($input->getArgument('type')); |
|
|
|
|
|
|
46
|
|
|
|
|
47
|
|
|
$output->writeln(sprintf('Clearing cache for types: <info>%s</info>', implode(', ', $input->getArgument('type')))); |
|
48
|
|
|
|
|
49
|
|
|
foreach ($types as $type) { |
|
50
|
|
|
$builder = $type->getQueryBuilder('x'); |
|
51
|
|
|
if ($input->getOption('where')) { |
|
52
|
|
|
$builder->andWhere($input->getOption('where')); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
foreach ($builder->getQuery()->iterate() as list($item)) { |
|
56
|
|
|
$output->writeln(sprintf('Clearing cache for <comment>%s</comment>', $this->itemToString($item))); |
|
57
|
|
|
$this->exporter->clearCache($item, [$type]); |
|
58
|
|
|
|
|
59
|
|
|
$builder->getEntityManager()->detach($item); |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @param array $types |
|
66
|
|
|
* |
|
67
|
|
|
* @return FeedTypeInterface[] |
|
68
|
|
|
*/ |
|
69
|
|
View Code Duplication |
protected function getTypes(array $types) |
|
|
|
|
|
|
70
|
|
|
{ |
|
71
|
|
|
if (empty($types)) { |
|
72
|
|
|
return $this->exporter->getTypes(); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
$result = []; |
|
76
|
|
|
foreach ($types as &$type) { |
|
77
|
|
|
$result[] = $this->exporter->getType($type); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
return $result; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @param object $item |
|
85
|
|
|
* |
|
86
|
|
|
* @return string |
|
87
|
|
|
*/ |
|
88
|
|
|
private function itemToString($item) |
|
89
|
|
|
{ |
|
90
|
|
|
if (method_exists($item, '__toString')) { |
|
91
|
|
|
return (string) $item; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
if (method_exists($item, 'getId')) { |
|
95
|
|
|
return $item->getId(); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
return spl_object_hash($item); |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.