This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | /* |
||
4 | * This file is part of the puli/manager package. |
||
5 | * |
||
6 | * (c) Bernhard Schussek <[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 Puli\Manager\Server; |
||
13 | |||
14 | use Exception; |
||
15 | use Puli\Manager\Api\Installer\InstallerManager; |
||
16 | use Puli\Manager\Api\Installer\NoSuchInstallerException; |
||
17 | use Puli\Manager\Api\Module\RootModuleFileManager; |
||
18 | use Puli\Manager\Api\Server\Server; |
||
19 | use Puli\Manager\Api\Server\ServerCollection; |
||
20 | use Puli\Manager\Api\Server\ServerManager; |
||
21 | use stdClass; |
||
22 | use Webmozart\Expression\Expr; |
||
23 | use Webmozart\Expression\Expression; |
||
24 | use Webmozart\Json\JsonValidator; |
||
25 | use Webmozart\Json\ValidationFailedException; |
||
26 | |||
27 | /** |
||
28 | * A server manager that stores the servers in the module file. |
||
29 | * |
||
30 | * @since 1.0 |
||
31 | * |
||
32 | * @author Bernhard Schussek <[email protected]> |
||
33 | */ |
||
34 | class ModuleFileServerManager implements ServerManager |
||
35 | { |
||
36 | /** |
||
37 | * The extra key that stores the server data. |
||
38 | */ |
||
39 | const SERVERS_KEY = 'servers'; |
||
40 | |||
41 | /** |
||
42 | * @var RootModuleFileManager |
||
43 | */ |
||
44 | private $rootModuleFileManager; |
||
45 | |||
46 | /** |
||
47 | * @var InstallerManager |
||
48 | */ |
||
49 | private $installerManager; |
||
50 | |||
51 | /** |
||
52 | * @var ServerCollection |
||
53 | */ |
||
54 | private $servers; |
||
55 | |||
56 | /** |
||
57 | * @var array |
||
58 | */ |
||
59 | private $serversData = array(); |
||
60 | |||
61 | 58 | public function __construct(RootModuleFileManager $rootModuleFileManager, InstallerManager $installerManager) |
|
62 | { |
||
63 | 58 | $this->rootModuleFileManager = $rootModuleFileManager; |
|
64 | 58 | $this->installerManager = $installerManager; |
|
65 | 58 | } |
|
66 | |||
67 | /** |
||
68 | * {@inheritdoc} |
||
69 | */ |
||
70 | 10 | public function addServer(Server $server) |
|
71 | { |
||
72 | 10 | $this->assertServersLoaded(); |
|
73 | |||
74 | 10 | if (!$this->installerManager->hasInstallerDescriptor($server->getInstallerName())) { |
|
75 | 2 | throw NoSuchInstallerException::forInstallerName($server->getInstallerName()); |
|
76 | } |
||
77 | |||
78 | 8 | $previousServers = $this->servers->toArray(); |
|
79 | 8 | $previousData = $this->serversData; |
|
80 | |||
81 | 8 | $this->servers->add($server); |
|
82 | 8 | $this->serversData[$server->getName()] = $this->serverToData($server); |
|
83 | |||
84 | try { |
||
85 | 8 | $this->persistServersData(); |
|
86 | 2 | } catch (Exception $e) { |
|
87 | 2 | $this->servers->replace($previousServers); |
|
88 | 2 | $this->serversData = $previousData; |
|
89 | |||
90 | 2 | throw $e; |
|
91 | } |
||
92 | 6 | } |
|
93 | |||
94 | /** |
||
95 | * {@inheritdoc} |
||
96 | */ |
||
97 | 8 | public function removeServer($serverName) |
|
98 | { |
||
99 | 8 | $this->removeServers(Expr::method('getName', Expr::same($serverName))); |
|
100 | 6 | } |
|
101 | |||
102 | /** |
||
103 | * {@inheritdoc} |
||
104 | */ |
||
105 | 14 | public function removeServers(Expression $expr) |
|
106 | { |
||
107 | 14 | $this->assertServersLoaded(); |
|
108 | |||
109 | 14 | $previousServers = $this->servers->toArray(); |
|
110 | 14 | $previousData = $this->serversData; |
|
111 | 14 | $save = false; |
|
112 | |||
113 | 14 | foreach ($this->servers as $server) { |
|
114 | 14 | if ($expr->evaluate($server)) { |
|
115 | 12 | $this->servers->remove($server->getName()); |
|
116 | 12 | unset($this->serversData[$server->getName()]); |
|
117 | 14 | $save = true; |
|
118 | } |
||
119 | } |
||
120 | |||
121 | 14 | if (!$save) { |
|
122 | 2 | return; |
|
123 | } |
||
124 | |||
125 | try { |
||
126 | 12 | $this->persistServersData(); |
|
127 | 4 | } catch (Exception $e) { |
|
128 | 4 | $this->servers->replace($previousServers); |
|
129 | 4 | $this->serversData = $previousData; |
|
130 | |||
131 | 4 | throw $e; |
|
132 | } |
||
133 | 8 | } |
|
134 | |||
135 | /** |
||
136 | * {@inheritdoc} |
||
137 | */ |
||
138 | 2 | public function clearServers() |
|
139 | { |
||
140 | 2 | $this->removeServers(Expr::true()); |
|
141 | 2 | } |
|
142 | |||
143 | /** |
||
144 | * {@inheritdoc} |
||
145 | */ |
||
146 | 16 | public function getServer($serverName) |
|
147 | { |
||
148 | 16 | $this->assertServersLoaded(); |
|
149 | |||
150 | 14 | return $this->servers->get($serverName); |
|
151 | } |
||
152 | |||
153 | /** |
||
154 | * {@inheritdoc} |
||
155 | */ |
||
156 | 27 | public function getServers() |
|
157 | { |
||
158 | 27 | $this->assertServersLoaded(); |
|
159 | |||
160 | 27 | return $this->servers; |
|
161 | } |
||
162 | |||
163 | /** |
||
164 | * {@inheritdoc} |
||
165 | */ |
||
166 | 2 | public function findServers(Expression $expr) |
|
167 | { |
||
168 | 2 | $this->assertServersLoaded(); |
|
169 | |||
170 | 2 | $servers = array(); |
|
171 | |||
172 | 2 | foreach ($this->servers as $server) { |
|
173 | 2 | if ($expr->evaluate($server)) { |
|
174 | 2 | $servers[] = $server; |
|
175 | } |
||
176 | } |
||
177 | |||
178 | 2 | return new ServerCollection($servers); |
|
179 | } |
||
180 | |||
181 | /** |
||
182 | * {@inheritdoc} |
||
183 | */ |
||
184 | 18 | public function hasServer($serverName) |
|
185 | { |
||
186 | 18 | $this->assertServersLoaded(); |
|
187 | |||
188 | 18 | return $this->servers->contains($serverName); |
|
189 | } |
||
190 | |||
191 | /** |
||
192 | * {@inheritdoc} |
||
193 | */ |
||
194 | 4 | View Code Duplication | public function hasServers(Expression $expr = null) |
0 ignored issues
–
show
|
|||
195 | { |
||
196 | 4 | $this->assertServersLoaded(); |
|
197 | |||
198 | 4 | if (!$expr) { |
|
199 | 4 | return !$this->servers->isEmpty(); |
|
200 | } |
||
201 | |||
202 | 2 | foreach ($this->servers as $server) { |
|
203 | 2 | if ($expr->evaluate($server)) { |
|
204 | 2 | return true; |
|
205 | } |
||
206 | } |
||
207 | |||
208 | 2 | return false; |
|
209 | } |
||
210 | |||
211 | 58 | private function assertServersLoaded() |
|
212 | { |
||
213 | 58 | if (null !== $this->servers) { |
|
214 | 32 | return; |
|
215 | } |
||
216 | |||
217 | 58 | $serversData = $this->rootModuleFileManager->getExtraKey(self::SERVERS_KEY); |
|
218 | |||
219 | 58 | if ($serversData) { |
|
220 | 42 | $jsonValidator = new JsonValidator(); |
|
221 | 42 | $errors = $jsonValidator->validate($serversData, __DIR__.'/../../res/schema/servers-schema-1.0.json'); |
|
222 | |||
223 | 42 | View Code Duplication | if (count($errors) > 0) { |
0 ignored issues
–
show
This code seems to be duplicated across your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. ![]() |
|||
224 | 2 | throw new ValidationFailedException(sprintf( |
|
225 | 2 | "The extra key \"%s\" is invalid:\n%s", |
|
226 | 2 | self::SERVERS_KEY, |
|
227 | 2 | implode("\n", $errors) |
|
228 | )); |
||
229 | } |
||
230 | } |
||
231 | |||
232 | 56 | $this->servers = new ServerCollection(); |
|
233 | 56 | $this->serversData = (array) $serversData; |
|
234 | |||
235 | 56 | foreach ($this->serversData as $serverName => $serverData) { |
|
236 | 40 | $this->servers->add($this->dataToServer($serverName, $serverData)); |
|
237 | } |
||
238 | 56 | } |
|
239 | |||
240 | 20 | private function persistServersData() |
|
241 | { |
||
242 | 20 | if ($this->serversData) { |
|
0 ignored issues
–
show
The expression
$this->serversData of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent. Consider making the comparison explicit by using ![]() |
|||
243 | 16 | $this->rootModuleFileManager->setExtraKey(self::SERVERS_KEY, (object) $this->serversData); |
|
244 | } else { |
||
245 | 4 | $this->rootModuleFileManager->removeExtraKey(self::SERVERS_KEY); |
|
246 | } |
||
247 | 14 | } |
|
248 | |||
249 | 40 | private function dataToServer($serverName, stdClass $data) |
|
250 | { |
||
251 | 40 | return new Server( |
|
252 | $serverName, |
||
253 | 40 | $data->installer, |
|
254 | 40 | $data->{'document-root'}, |
|
255 | 40 | isset($data->{'url-format'}) ? $data->{'url-format'} : Server::DEFAULT_URL_FORMAT, |
|
256 | 40 | isset($data->parameters) ? (array) $data->parameters : array() |
|
257 | ); |
||
258 | } |
||
259 | |||
260 | 8 | private function serverToData(Server $server) |
|
261 | { |
||
262 | 8 | $data = new stdClass(); |
|
263 | 8 | $data->installer = $server->getInstallerName(); |
|
264 | 8 | $data->{'document-root'} = $server->getDocumentRoot(); |
|
265 | |||
266 | 8 | if (Server::DEFAULT_URL_FORMAT !== ($urlFormat = $server->getUrlFormat())) { |
|
267 | 2 | $data->{'url-format'} = $urlFormat; |
|
268 | } |
||
269 | |||
270 | 8 | if ($parameters = $server->getParameterValues()) { |
|
271 | 2 | $data->parameters = (object) $parameters; |
|
272 | } |
||
273 | |||
274 | 8 | return $data; |
|
275 | } |
||
276 | } |
||
277 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.