This repository was archived by the owner on Sep 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathAutoRouteListener.php
123 lines (104 loc) · 3.77 KB
/
AutoRouteListener.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
/*
* This file is part of the Symfony CMF package.
*
* (c) 2011-2015 Symfony CMF
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Cmf\Bundle\RoutingAutoBundle\Doctrine\Phpcr;
use Doctrine\Common\Persistence\Event\ManagerEventArgs;
use Doctrine\ODM\PHPCR\DocumentManager;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Cmf\Bundle\RoutingAutoBundle\Model\AutoRoute;
use Symfony\Cmf\Component\RoutingAuto\UriContextCollection;
use Symfony\Cmf\Component\RoutingAuto\Mapping\Exception\ClassNotMappedException;
/**
* Doctrine PHPCR ODM listener for maintaining automatic routes.
*
* @author Daniel Leech <[email protected]>
*/
class AutoRouteListener
{
protected $postFlushDone = false;
public function __construct(ContainerInterface $container)
{
$this->container = $container;
}
/**
* @return AutoRouteManager
*/
protected function getAutoRouteManager()
{
// lazy load the auto_route_manager service to prevent a cirular-reference
// to the document manager.
return $this->container->get('cmf_routing_auto.auto_route_manager');
}
protected function getMetadataFactory()
{
return $this->container->get('cmf_routing_auto.metadata.factory');
}
public function onFlush(ManagerEventArgs $args)
{
/** @var $dm DocumentManager */
$dm = $args->getObjectManager();
$uow = $dm->getUnitOfWork();
$arm = $this->getAutoRouteManager();
$scheduledInserts = $uow->getScheduledInserts();
$scheduledUpdates = $uow->getScheduledUpdates();
$updates = array_merge($scheduledInserts, $scheduledUpdates);
$autoRoute = null;
foreach ($updates as $document) {
if ($this->isAutoRouteable($document)) {
$locale = $uow->getCurrentLocale($document);
$uriContextCollection = new UriContextCollection($document);
$arm->buildUriContextCollection($uriContextCollection);
// refactor this.
foreach ($uriContextCollection->getUriContexts() as $uriContext) {
$autoRoute = $uriContext->getAutoRoute();
$dm->persist($autoRoute);
$uow->computeChangeSets();
}
// reset locale to the original locale
if (null !== $locale) {
$dm->findTranslation(get_class($document), $uow->getDocumentId($document), $locale);
}
}
}
$removes = $uow->getScheduledRemovals();
foreach ($removes as $document) {
if ($this->isAutoRouteable($document)) {
$referrers = $dm->getReferrers($document);
$referrers = $referrers->filter(function ($referrer) {
if ($referrer instanceof AutoRoute) {
return true;
}
return false;
});
foreach ($referrers as $autoRoute) {
$uow->scheduleRemove($autoRoute);
}
}
}
}
public function endFlush(ManagerEventArgs $args)
{
$dm = $args->getObjectManager();
$arm = $this->getAutoRouteManager();
$arm->handleDefunctRoutes();
if (!$this->postFlushDone) {
$this->postFlushDone = true;
$dm->flush();
}
$this->postFlushDone = false;
}
private function isAutoRouteable($document)
{
try {
return (bool) $this->getMetadataFactory()->getMetadataForClass(get_class($document));
} catch (ClassNotMappedException $e) {
return false;
}
}
}