|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Silverback API Components Bundle Project |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Daniel West <[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
|
|
|
declare(strict_types=1); |
|
13
|
|
|
|
|
14
|
|
|
namespace Silverback\ApiComponentsBundle\Entity\Core; |
|
15
|
|
|
|
|
16
|
|
|
use ApiPlatform\Core\Annotation\ApiResource; |
|
17
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
|
18
|
|
|
use Doctrine\Common\Collections\Collection; |
|
19
|
|
|
use Silverback\ApiComponentsBundle\Annotation as Silverback; |
|
20
|
|
|
use Silverback\ApiComponentsBundle\Entity\Utility\IdTrait; |
|
21
|
|
|
use Silverback\ApiComponentsBundle\Entity\Utility\TimestampedTrait; |
|
22
|
|
|
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity; |
|
23
|
|
|
use Symfony\Component\Validator\Constraints as Assert; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Although a user will be able to get the routes and the tree of data down to getting the ID for a component |
|
27
|
|
|
* fetching a component will be restricted based on the route it is within. |
|
28
|
|
|
* |
|
29
|
|
|
* @author Daniel West <[email protected]> |
|
30
|
|
|
* |
|
31
|
|
|
* @Silverback\Timestamped |
|
32
|
|
|
* @ApiResource( |
|
33
|
|
|
* mercure=true, |
|
34
|
|
|
* collectionOperations={ |
|
35
|
|
|
* "get", |
|
36
|
|
|
* "post", |
|
37
|
|
|
* "generate"={ "method"="POST", "path"="/routes/generate" } |
|
38
|
|
|
* }, |
|
39
|
|
|
* itemOperations={ |
|
40
|
|
|
* "get"={ "requirements"={"id"="(.+)"}, "security"="is_granted('read_route', object)" }, |
|
41
|
|
|
* "delete"={ "requirements"={"id"="(.+)"}, "security"="is_granted('read_route', object)" }, |
|
42
|
|
|
* "put"={ "requirements"={"id"="(.+)"}, "security"="is_granted('read_route', object)" }, |
|
43
|
|
|
* "patch"={ "requirements"={"id"="(.+)"}, "security"="is_granted('read_route', object)" } |
|
44
|
|
|
* } |
|
45
|
|
|
* ) |
|
46
|
|
|
* @Assert\Expression( |
|
47
|
|
|
* "!(this.getPage() == null & this.getPageData() == null)", |
|
48
|
|
|
* message="Please specify either page or pageData." |
|
49
|
|
|
* ) |
|
50
|
|
|
* @Assert\Expression( |
|
51
|
|
|
* "!(this.getPage() != null & this.getPageData() != null)", |
|
52
|
|
|
* message="Please specify either page or pageData, not both." |
|
53
|
|
|
* ) |
|
54
|
|
|
* @UniqueEntity("name", message="The route name must be unique.") |
|
55
|
|
|
* @UniqueEntity("path", message="The route path must be unique.") |
|
56
|
|
|
*/ |
|
57
|
|
|
class Route |
|
58
|
|
|
{ |
|
59
|
|
|
use IdTrait; |
|
60
|
|
|
use TimestampedTrait; |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @Assert\NotBlank |
|
64
|
|
|
*/ |
|
65
|
|
|
private string $path = ''; |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @Assert\NotNull |
|
69
|
|
|
*/ |
|
70
|
|
|
private string $name; |
|
71
|
|
|
|
|
72
|
|
|
private ?Route $redirect = null; |
|
73
|
|
|
|
|
74
|
|
|
private Collection $redirectedFrom; |
|
75
|
|
|
|
|
76
|
|
|
private ?Page $page = null; |
|
77
|
|
|
|
|
78
|
|
|
private ?AbstractPageData $pageData = null; |
|
79
|
|
|
|
|
80
|
3 |
|
public function __construct() |
|
81
|
|
|
{ |
|
82
|
3 |
|
$this->redirectedFrom = new ArrayCollection(); |
|
83
|
3 |
|
} |
|
84
|
|
|
|
|
85
|
|
|
public function getPath(): string |
|
86
|
|
|
{ |
|
87
|
|
|
return $this->path; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
1 |
|
public function setPath(string $path): self |
|
91
|
|
|
{ |
|
92
|
1 |
|
$this->path = $path; |
|
93
|
|
|
|
|
94
|
1 |
|
return $this; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
public function getName(): string |
|
98
|
|
|
{ |
|
99
|
|
|
return $this->name; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
1 |
|
public function setName(string $name): self |
|
103
|
|
|
{ |
|
104
|
1 |
|
$this->name = $name; |
|
105
|
|
|
|
|
106
|
1 |
|
return $this; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
public function getRedirect(): ?self |
|
110
|
|
|
{ |
|
111
|
|
|
return $this->redirect; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
public function setRedirect(?self $redirect): self |
|
115
|
|
|
{ |
|
116
|
|
|
$this->redirect = $redirect; |
|
117
|
|
|
if ($redirect) { |
|
118
|
|
|
$redirect->addRedirectedFrom($this); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
return $this; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* @return Collection|Route[] |
|
126
|
|
|
*/ |
|
127
|
|
|
public function getRedirectedFrom() |
|
128
|
|
|
{ |
|
129
|
|
|
return $this->redirectedFrom; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* @param array|Collection $redirectedFrom |
|
134
|
|
|
*/ |
|
135
|
|
|
public function setRedirectedFrom($redirectedFrom): self |
|
136
|
|
|
{ |
|
137
|
|
|
$isArray = \is_array($redirectedFrom); |
|
138
|
|
|
if (!$isArray && !$redirectedFrom instanceof Collection) { |
|
|
|
|
|
|
139
|
|
|
throw new \InvalidArgumentException('setRedirectedFrom requires an array or Collection'); |
|
140
|
|
|
} |
|
141
|
|
|
$this->redirectedFrom = $isArray ? new ArrayCollection($redirectedFrom) : $redirectedFrom; |
|
|
|
|
|
|
142
|
|
|
|
|
143
|
|
|
return $this; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
public function addRedirectedFrom(self $redirectedFrom): self |
|
147
|
|
|
{ |
|
148
|
|
|
if (!$this->redirectedFrom->contains($redirectedFrom)) { |
|
149
|
|
|
$this->redirectedFrom->add($redirectedFrom); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
return $this; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
public function getPage(): ?Page |
|
156
|
|
|
{ |
|
157
|
|
|
return $this->page; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
public function setPage(?Page $page): self |
|
161
|
|
|
{ |
|
162
|
|
|
$this->page = $page; |
|
163
|
|
|
if ($this->page) { |
|
164
|
|
|
$this->page->setRoute($this); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
return $this; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
public function getPageData(): ?AbstractPageData |
|
171
|
|
|
{ |
|
172
|
|
|
return $this->pageData; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
public function setPageData(?AbstractPageData $pageData): self |
|
176
|
|
|
{ |
|
177
|
|
|
$this->pageData = $pageData; |
|
178
|
|
|
if ($this->pageData) { |
|
179
|
|
|
$this->pageData->setRoute($this); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
return $this; |
|
183
|
|
|
} |
|
184
|
|
|
} |
|
185
|
|
|
|