1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Tadcka package. |
5
|
|
|
* |
6
|
|
|
* (c) Tadas Gliaubicas <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Tadcka\Bundle\RoutingBundle\Doctrine\EntityManager; |
13
|
|
|
|
14
|
|
|
use Doctrine\ORM\AbstractQuery; |
15
|
|
|
use Doctrine\ORM\EntityManager; |
16
|
|
|
use Doctrine\ORM\EntityRepository; |
17
|
|
|
use Tadcka\Component\Routing\Model\Manager\RouteManager as BaseRouteManager; |
18
|
|
|
use Tadcka\Component\Routing\Model\RouteInterface; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @author Tadas Gliaubicas <[email protected]> |
22
|
|
|
* |
23
|
|
|
* @since 7/1/14 11:10 PM |
24
|
|
|
*/ |
25
|
|
|
class RouteManager extends BaseRouteManager |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var EntityManager |
29
|
|
|
*/ |
30
|
|
|
protected $em; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var EntityRepository |
34
|
|
|
*/ |
35
|
|
|
protected $repository; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
protected $class; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Constructor. |
44
|
|
|
* |
45
|
|
|
* @param EntityManager $em |
46
|
|
|
* @param string $class |
47
|
|
|
*/ |
48
|
|
|
public function __construct(EntityManager $em, $class) |
49
|
|
|
{ |
50
|
|
|
$this->em = $em; |
51
|
|
|
$this->repository = $em->getRepository($class); |
52
|
|
|
$this->class = $em->getClassMetadata($class)->name; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* {@inheritdoc} |
57
|
|
|
*/ |
58
|
|
|
public function findAllNameAndPattern() |
59
|
|
|
{ |
60
|
|
|
$qb = $this->repository->createQueryBuilder('r'); |
61
|
|
|
|
62
|
|
|
$qb->select('r.name, r.routePattern AS route_pattern'); |
63
|
|
|
|
64
|
|
|
return $qb->getQuery()->getResult(AbstractQuery::HYDRATE_ARRAY); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* {@inheritdoc} |
69
|
|
|
*/ |
70
|
|
|
public function findByName($name) |
71
|
|
|
{ |
72
|
|
|
return $this->repository->findOneBy(array('name' => $name)); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* {@inheritdoc} |
77
|
|
|
*/ |
78
|
|
|
public function findVisibleByName($name) |
79
|
|
|
{ |
80
|
|
|
return $this->repository->findOneBy(array('name' => $name, 'visible' => true)); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* {@inheritdoc} |
85
|
|
|
*/ |
86
|
|
|
public function findByNames(array $names) |
87
|
|
|
{ |
88
|
|
|
return $this->repository->findBy(array('name' => $names)); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* {@inheritdoc} |
93
|
|
|
*/ |
94
|
|
|
public function findVisibleByNames(array $names) |
95
|
|
|
{ |
96
|
|
|
return $this->repository->findBy(array('name' => $names, 'visible' => true)); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* {@inheritdoc} |
101
|
|
|
*/ |
102
|
|
|
public function findByRoutePattern($routePattern) |
103
|
|
|
{ |
104
|
|
|
return $this->repository->findOneBy(array('routePattern' => $routePattern)); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* {@inheritdoc} |
109
|
|
|
*/ |
110
|
|
|
public function findByRoutePatterns(array $routePatterns) |
111
|
|
|
{ |
112
|
|
|
return $this->repository->findBy(array('routePattern' => $routePatterns)); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* {@inheritdoc} |
117
|
|
|
*/ |
118
|
|
|
public function findVisibleByRoutePatterns(array $routePatterns) |
119
|
|
|
{ |
120
|
|
|
return $this->repository->findBy(array('routePattern' => $routePatterns, 'visible' => true)); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* {@inheritdoc} |
125
|
|
|
*/ |
126
|
|
|
public function all() |
127
|
|
|
{ |
128
|
|
|
return $this->repository->findAll(); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* {@inheritdoc} |
133
|
|
|
*/ |
134
|
|
|
public function add(RouteInterface $route, $save = false) |
135
|
|
|
{ |
136
|
|
|
$this->em->persist($route); |
137
|
|
|
if (true === $save) { |
138
|
|
|
$this->save(); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* {@inheritdoc} |
144
|
|
|
*/ |
145
|
|
|
public function remove(RouteInterface $route, $save = false) |
146
|
|
|
{ |
147
|
|
|
$this->em->remove($route); |
148
|
|
|
if (true === $save) { |
149
|
|
|
$this->save(); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* {@inheritdoc} |
155
|
|
|
*/ |
156
|
|
|
public function save() |
157
|
|
|
{ |
158
|
|
|
$this->em->flush(); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* {@inheritdoc} |
163
|
|
|
*/ |
164
|
|
|
public function clear() |
165
|
|
|
{ |
166
|
|
|
$this->em->clear($this->getClass()); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* {@inheritdoc} |
171
|
|
|
*/ |
172
|
|
|
public function getClass() |
173
|
|
|
{ |
174
|
|
|
return $this->class; |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|