|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author Gerard van Helden <[email protected]> |
|
4
|
|
|
* @copyright Zicht Online <http://zicht.nl> |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
namespace Zicht\Bundle\FrameworkExtraBundle\Doctrine; |
|
8
|
|
|
|
|
9
|
|
|
use Doctrine\Common\EventSubscriber; |
|
10
|
|
|
use Doctrine\ORM\Event\PostFlushEventArgs; |
|
11
|
|
|
use Zicht\Bundle\FrameworkExtraBundle\Command\RepairNestedTreeCommand; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* This is a utility observer which validates the given repository's tree after every flush. |
|
15
|
|
|
* |
|
16
|
|
|
* You should create and enable this this subscriber only in development and/or testing environments to find out |
|
17
|
|
|
* whatever causes the nested set to get corrupted. |
|
18
|
|
|
* |
|
19
|
|
|
* Easiest way to use this is adding the following lines to your config_development.yml: |
|
20
|
|
|
* |
|
21
|
|
|
* <pre> |
|
22
|
|
|
* services: |
|
23
|
|
|
* tree_validation_subscriber: |
|
24
|
|
|
* class: Zicht\Bundle\FrameworkExtraBundle\Doctrine\NestedTreeValidationSubscriber |
|
25
|
|
|
* arguments: ['ZichtMenuBundle:MenuItem'] |
|
26
|
|
|
* tags: |
|
27
|
|
|
* - { name: doctrine.event_subscriber } |
|
28
|
|
|
* </pre> |
|
29
|
|
|
*/ |
|
30
|
|
|
class NestedTreeValidationSubscriber implements EventSubscriber |
|
31
|
|
|
{ |
|
32
|
|
|
/** |
|
33
|
|
|
* @var string |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $entityName; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @{inheritDoc} |
|
39
|
|
|
*/ |
|
40
|
|
|
public function getSubscribedEvents() |
|
41
|
|
|
{ |
|
42
|
|
|
return array( |
|
43
|
|
|
'postFlush' |
|
44
|
|
|
); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Setup the listener to check the specified entity name after any flush |
|
49
|
|
|
* |
|
50
|
|
|
* @param string $entityName |
|
51
|
|
|
*/ |
|
52
|
|
|
public function __construct($entityName) |
|
53
|
|
|
{ |
|
54
|
|
|
$this->entityName = $entityName; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Throw an exception if the validation fails after any save |
|
59
|
|
|
* |
|
60
|
|
|
* @param PostFlushEventArgs $e |
|
61
|
|
|
* @return void |
|
62
|
|
|
* @throws \UnexpectedValueException |
|
63
|
|
|
*/ |
|
64
|
|
|
public function postFlush(PostFlushEventArgs $e) |
|
65
|
|
|
{ |
|
66
|
|
|
$repo = $e->getEntityManager()->getRepository($this->entityName); |
|
67
|
|
|
if (true !== ($issues = $repo->verify())) { |
|
|
|
|
|
|
68
|
|
|
throw new \UnexpectedValueException( |
|
69
|
|
|
sprintf( |
|
70
|
|
|
"The repository '%s' did not validate. Run the '%s' console command to find out what's going on", |
|
71
|
|
|
$this->entityName, |
|
72
|
|
|
RepairNestedTreeCommand::COMMAND_NAME |
|
73
|
|
|
) |
|
74
|
|
|
); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|