|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author Gerard van Helden <[email protected]> |
|
4
|
|
|
* @copyright Zicht Online <http://zicht.nl> |
|
5
|
|
|
*/ |
|
6
|
|
|
namespace Zicht\Bundle\FrameworkExtraBundle\Command; |
|
7
|
|
|
|
|
8
|
|
|
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
|
9
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
10
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
11
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
12
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
13
|
|
|
/* @codingStandardsIgnoreStart*/ |
|
14
|
|
|
use function Zicht\Itertools\filter; |
|
15
|
|
|
/* @codingStandardsIgnoreEnd */ |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Class ValidateEntityCommand |
|
19
|
|
|
* |
|
20
|
|
|
* @package Zicht\Bundle\FrameworkExtraBundle\Command |
|
21
|
|
|
*/ |
|
22
|
|
|
class ValidateEntityCommand extends ContainerAwareCommand |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @{inheritDoc} |
|
26
|
|
|
*/ |
|
27
|
|
|
protected function configure() |
|
28
|
|
|
{ |
|
29
|
|
|
$this |
|
30
|
|
|
->setName('zicht:entity:validate') |
|
31
|
|
|
->addArgument('entity', InputArgument::IS_ARRAY|InputArgument::OPTIONAL) |
|
32
|
|
|
->setHelp( |
|
33
|
|
|
'This command validates all entities in a repository, |
|
34
|
|
|
useful to test the database for irregularities' |
|
35
|
|
|
) |
|
36
|
|
|
->addOption( |
|
37
|
|
|
'group', |
|
38
|
|
|
'g', |
|
39
|
|
|
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, |
|
40
|
|
|
"Optional validation group(s)" |
|
41
|
|
|
); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @{inheritDoc} |
|
46
|
|
|
*/ |
|
47
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
48
|
|
|
{ |
|
49
|
|
|
$entities = $input->getArgument('entity') ?: $this->getAllEntities(); |
|
50
|
|
|
|
|
51
|
|
|
foreach ($entities as $entity) { |
|
52
|
|
|
$groups = $input->getOption('group'); |
|
53
|
|
|
$repo = $this->getContainer()->get('doctrine')->getRepository($entity); |
|
54
|
|
|
|
|
55
|
|
|
foreach ($repo->findAll() as $entity) { |
|
|
|
|
|
|
56
|
|
|
$violations = $this->getContainer()->get('validator')->validate($entity, $groups ? $groups : null); |
|
57
|
|
|
|
|
58
|
|
|
if (count($violations)) { |
|
59
|
|
|
$output->writeln(get_class($entity) . "::" . $entity->getId()); |
|
60
|
|
|
foreach ($violations as $error) { |
|
61
|
|
|
$output->writeln(" -> {$error}"); |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* get all entities |
|
70
|
|
|
* |
|
71
|
|
|
* @return \Zicht\Itertools\lib\FilterIterator |
|
72
|
|
|
*/ |
|
73
|
|
|
protected function getAllEntities() |
|
74
|
|
|
{ |
|
75
|
|
|
$allMeta = $this |
|
76
|
|
|
->getContainer() |
|
77
|
|
|
->get('doctrine') |
|
78
|
|
|
->getManager() |
|
79
|
|
|
->getMetadataFactory() |
|
80
|
|
|
->getAllMetadata(); |
|
81
|
|
|
|
|
82
|
|
|
$isAcceptable = function ($meta) { |
|
83
|
|
|
return !$meta->isMappedSuperclass && empty($meta->subClasses); |
|
84
|
|
|
}; |
|
85
|
|
|
|
|
86
|
|
|
return filter($isAcceptable, $allMeta); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|