1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Philip Bergman <[email protected]> |
4
|
|
|
* @copyright Zicht Online <http://www.zicht.nl> |
5
|
|
|
*/ |
6
|
|
|
namespace Zicht\Bundle\PageBundle\Command; |
7
|
|
|
|
8
|
|
|
use Psr\Log\LogLevel; |
9
|
|
|
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
10
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
11
|
|
|
use Symfony\Component\Console\Input\InputOption; |
12
|
|
|
use Symfony\Component\Console\Logger\ConsoleLogger; |
13
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
14
|
|
|
use Zicht\Bundle\PageBundle\Entity\ContentItem; |
15
|
|
|
use Zicht\Bundle\PageBundle\Entity\Page; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class CheckContentItemsCommand |
19
|
|
|
* |
20
|
|
|
* @package Zicht\Bundle\PageBundle\Command |
21
|
|
|
*/ |
22
|
|
|
class CheckContentItemsCommand extends ContainerAwareCommand |
23
|
|
|
{ |
24
|
|
|
/** @var bool */ |
25
|
|
|
protected $isVeryVerbose; |
26
|
|
|
/** @var bool */ |
27
|
|
|
protected $force; |
28
|
|
|
/** @var ConsoleLogger */ |
29
|
|
|
protected $logger; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @{inheritDoc} |
33
|
|
|
*/ |
34
|
|
|
protected function configure() |
35
|
|
|
{ |
36
|
|
|
$this |
37
|
|
|
->setName('zicht:page:contentitems:check') |
38
|
|
|
->addOption('force', 'f', InputOption::VALUE_NONE, 'Set this flag te remove/invalid broken content items') |
39
|
|
|
->setHelp('Check/validate the page content items'); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @{inheritDoc} |
44
|
|
|
*/ |
45
|
|
|
protected function initialize(InputInterface $input, OutputInterface $output) |
46
|
|
|
{ |
47
|
|
|
$this->force = $input->getOption('force'); |
48
|
|
|
$this->logger = $this->getLogger($output); |
49
|
|
|
$this->isVeryVerbose = $output->getVerbosity() >= OutputInterface::VERBOSITY_VERY_VERBOSE; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @{inheritDoc} |
54
|
|
|
*/ |
55
|
|
|
public function execute(InputInterface $input, OutputInterface $output) |
56
|
|
|
{ |
57
|
|
|
$em = $this->getContainer()->get('doctrine')->getManager(); |
58
|
|
|
|
59
|
|
|
foreach ($this->getBasePageMeta() as $meta) { |
60
|
|
|
$this->logger->info(sprintf('Checking all sub classes of "%s"', $meta->getName())); |
61
|
|
|
|
62
|
|
|
foreach ($meta->subClasses as $class) { |
63
|
|
|
$entities = $em->getRepository($class)->findAll(); |
64
|
|
|
|
65
|
|
|
/** @var Page $entity */ |
66
|
|
|
foreach ($entities as $entity) { |
67
|
|
|
$debugString = sprintf( |
68
|
|
|
'[%04d] [%s] %s', |
69
|
|
|
$entity->getId(), |
70
|
|
|
$this->getShortName($class, $meta->getName()), |
71
|
|
|
$entity->getTitle() |
72
|
|
|
); |
73
|
|
|
$this->logger->debug($debugString); |
74
|
|
|
$matrix = $entity->getContentItemMatrix(); |
75
|
|
|
|
76
|
|
|
/** @var ContentItem $contentitem */ |
77
|
|
|
foreach ($entity->getContentItems() as $contentitem) { |
78
|
|
|
$failed = false; |
79
|
|
|
|
80
|
|
|
if (in_array($contentitem->getRegion(), $matrix->getRegions())) { |
81
|
|
|
if (!in_array(get_class($contentitem), $matrix->getTypes($contentitem->getRegion()))) { |
82
|
|
|
|
83
|
|
|
$warningString = sprintf( |
84
|
|
|
'[%04d] [%s] [%s] "%s" not allowed for defined types "%s"', |
85
|
|
|
$entity->getId(), |
86
|
|
|
$this->getShortName($class, $meta->getName()), |
87
|
|
|
$contentitem->getRegion(), |
88
|
|
|
$this->getShortName(get_class($contentitem), $matrix->getNamespacePrefix()), |
89
|
|
|
implode( |
90
|
|
|
'", "', |
91
|
|
|
array_map( |
92
|
|
|
function ($name) use ($matrix) { |
93
|
|
|
return $this->getShortName($name, $matrix->getNamespacePrefix()); |
94
|
|
|
}, |
95
|
|
|
$matrix->getTypes($contentitem->getRegion()) |
96
|
|
|
) |
97
|
|
|
) |
98
|
|
|
); |
99
|
|
|
|
100
|
|
|
$this->logger->warning($warningString); |
101
|
|
|
$failed = true; |
102
|
|
|
} |
103
|
|
|
} else { |
104
|
|
|
$this->logger->warning( |
105
|
|
|
sprintf( |
106
|
|
|
'[%04d] [%s] Region "%s" not allowed for defined regions "%s"', |
107
|
|
|
$entity->getId(), |
108
|
|
|
$this->getShortName($class, $meta->getName()), |
109
|
|
|
$contentitem->getRegion(), |
110
|
|
|
implode('", "', $matrix->getRegions()) |
111
|
|
|
) |
112
|
|
|
); |
113
|
|
|
$failed = true; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
if ($failed && $this->force) { |
117
|
|
|
$entity->removeContentItem($contentitem); |
118
|
|
|
$em->flush(); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @param string $class |
128
|
|
|
* @param string $baseClass |
129
|
|
|
* @return mixed |
130
|
|
|
*/ |
131
|
|
|
protected function getShortName($class, $baseClass) |
132
|
|
|
{ |
133
|
|
|
preg_match(sprintf('#%s\\\?(?P<name>[^$]+)#', preg_quote($baseClass, '#')), $class, $m); |
134
|
|
|
|
135
|
|
|
return (isset($m['name']) && !$this->isVeryVerbose) ? $m['name'] : $class; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param OutputInterface $output |
140
|
|
|
* @return ConsoleLogger |
141
|
|
|
*/ |
142
|
|
|
protected function getLogger(OutputInterface $output) |
143
|
|
|
{ |
144
|
|
|
return new ConsoleLogger( |
145
|
|
|
$output, |
146
|
|
|
[ |
147
|
|
|
LogLevel::WARNING => OutputInterface::VERBOSITY_NORMAL, |
148
|
|
|
LogLevel::INFO => OutputInterface::VERBOSITY_NORMAL, |
149
|
|
|
LogLevel::DEBUG => OutputInterface::VERBOSITY_VERBOSE, |
150
|
|
|
], |
151
|
|
|
[ |
152
|
|
|
LogLevel::WARNING => 'comment', |
153
|
|
|
LogLevel::DEBUG => 'fg=cyan', |
154
|
|
|
] |
155
|
|
|
); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @return \Generator|\Doctrine\ORM\Mapping\ClassMetadata[] |
160
|
|
|
*/ |
161
|
|
|
protected function getBasePageMeta() |
162
|
|
|
{ |
163
|
|
|
$allMeta = $this |
164
|
|
|
->getContainer() |
165
|
|
|
->get('doctrine') |
166
|
|
|
->getManager() |
167
|
|
|
->getMetadataFactory() |
168
|
|
|
->getAllMetadata(); |
169
|
|
|
|
170
|
|
|
$done = []; |
171
|
|
|
|
172
|
|
|
/** @var \Doctrine\ORM\Mapping\ClassMetadata $meta */ |
173
|
|
|
foreach ($allMeta as $meta) { |
174
|
|
|
if (!empty($meta->subClasses) && is_a($meta->getName(), Page::class, true)) { |
175
|
|
|
foreach ($meta->parentClasses as $parent) { |
176
|
|
|
if (in_array($parent, $done)) { |
177
|
|
|
continue 2; |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
if (in_array($meta->getName(), $done)) { |
181
|
|
|
continue; |
182
|
|
|
} |
183
|
|
|
$done[] = $meta->getName(); |
184
|
|
|
yield $meta; |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|