1 | <?php |
||
2 | |||
3 | namespace Aoe\Restler\System\RestApi; |
||
4 | |||
5 | /*************************************************************** |
||
6 | * Copyright notice |
||
7 | * |
||
8 | * (c) 2015 AOE GmbH <[email protected]> |
||
9 | * |
||
10 | * All rights reserved |
||
11 | * |
||
12 | * This script is part of the TYPO3 project. The TYPO3 project is |
||
13 | * free software; you can redistribute it and/or modify |
||
14 | * it under the terms of the GNU General Public License as published by |
||
15 | * the Free Software Foundation; either version 3 of the License, or |
||
16 | * (at your option) any later version. |
||
17 | * |
||
18 | * The GNU General Public License can be found at |
||
19 | * http://www.gnu.org/copyleft/gpl.html. |
||
20 | * |
||
21 | * This script is distributed in the hope that it will be useful, |
||
22 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
24 | * GNU General Public License for more details. |
||
25 | * |
||
26 | * This copyright notice MUST APPEAR in all copies of the script! |
||
27 | ***************************************************************/ |
||
28 | |||
29 | use Luracast\Restler\Restler; |
||
30 | use Luracast\Restler\Scope; |
||
31 | use TYPO3\CMS\Core\SingletonInterface; |
||
32 | |||
33 | /** |
||
34 | * We must override the Scope-class from restler. Otherwise we can't override the 'original' Restler-object (which represents |
||
35 | * the REST-API-Request) when we want to execute (multiple) REST-API-requests via the class Aoe\Restler\System\Restler\RestApiRequest |
||
36 | */ |
||
37 | class RestApiRequestScope extends Scope implements SingletonInterface |
||
38 | { |
||
39 | /** |
||
40 | * @var Scope |
||
41 | */ |
||
42 | private $originalRestApiRequest; |
||
43 | |||
44 | /** |
||
45 | * This array contains all instanciated REST-API-authentication-objects, which |
||
46 | * where already initialized before we call the REST-API-request via PHP |
||
47 | */ |
||
48 | private array $originalRestApiAuthenticationObjects = []; |
||
49 | |||
50 | /** |
||
51 | * @return Restler |
||
52 | */ |
||
53 | public function getOriginalRestApiRequest() |
||
54 | { |
||
55 | return $this->originalRestApiRequest ?? static::get('Restler'); |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
56 | } |
||
57 | |||
58 | /** |
||
59 | * Override (the stored) restler-object, because this restler-object 'defines' the REST-API-request, which we want to call |
||
60 | */ |
||
61 | public function overrideOriginalRestApiRequest(RestApiRequest $restApiRequest): void |
||
62 | { |
||
63 | static::set('Restler', $restApiRequest); |
||
64 | } |
||
65 | |||
66 | /** |
||
67 | * Remove all initialized REST-API-authentication-objects, which are currently instanciated. We must do this, because the |
||
68 | * REST-API-authentication-objects can depend on the called REST-API-endpoint, because they can be instanciated with different |
||
69 | * 'configurations', which depends on the called REST-API-endpoint. The REST-API-endpoint can contain 'configurations' for the |
||
70 | * REST-API-authentication-object like this: |
||
71 | * @class [authentication-class] {@[property-of-authentication-class] [property-value]} |
||
72 | * @class Aoe\MyRestApiExtension\Controller\MyAuthenticationController {@checkAuthentication true} |
||
73 | */ |
||
74 | public function removeRestApiAuthenticationObjects(): void |
||
75 | { |
||
76 | foreach ($this->getOriginalRestApiRequest()->_authClasses as $className) { |
||
77 | if (array_key_exists($className, static::$instances)) { |
||
78 | unset(static::$instances[$className]); |
||
79 | } |
||
80 | } |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * Restore (the overridden) restler-object |
||
85 | */ |
||
86 | public function restoreOriginalRestApiRequest(): void |
||
87 | { |
||
88 | static::set('Restler', $this->originalRestApiRequest); |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * Restore all initialized REST-API-authentication-objects, which |
||
93 | * were already initialized before we call the REST-API-request via PHP |
||
94 | */ |
||
95 | public function restoreOriginalRestApiAuthenticationObjects(): void |
||
96 | { |
||
97 | $this->removeRestApiAuthenticationObjects(); |
||
98 | |||
99 | foreach ($this->originalRestApiAuthenticationObjects as $className => $object) { |
||
100 | static::$instances[$className] = $object; |
||
101 | } |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * store (the original) restler-object |
||
106 | * |
||
107 | * @param Restler $originalRestApiRequest optional, default is null (normally, the object already exists in the 'Scope-Repository') |
||
108 | */ |
||
109 | public function storeOriginalRestApiRequest(Restler $originalRestApiRequest = null): void |
||
110 | { |
||
111 | if ($originalRestApiRequest instanceof Restler) { |
||
112 | static::set('Restler', $originalRestApiRequest); |
||
113 | } |
||
114 | |||
115 | $this->originalRestApiRequest = static::get('Restler'); |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * store all REST-API-authentication-objects, which where already initialized before we call the REST-API-request via PHP |
||
120 | */ |
||
121 | public function storeOriginalRestApiAuthenticationObjects(): void |
||
122 | { |
||
123 | foreach ($this->getOriginalRestApiRequest()->_authClasses as $className) { |
||
124 | if (array_key_exists($className, static::$instances)) { |
||
125 | $this->originalRestApiAuthenticationObjects[$className] = static::$instances[$className]; |
||
126 | } |
||
127 | } |
||
128 | } |
||
129 | } |
||
130 |