|
5 | 5 | namespace Baraja\StructuredApi; |
6 | 6 |
|
7 | 7 |
|
| 8 | +use Baraja\Localization\Localization; |
8 | 9 | use Baraja\RuntimeInvokeException; |
9 | 10 | use Baraja\Serializer\Serializer; |
10 | 11 | use Baraja\ServiceMethodInvoker; |
@@ -61,26 +62,26 @@ public function __construct( |
61 | 62 | public function run(?string $path = null, ?array $params = [], ?string $method = null, bool $throw = false): void |
62 | 63 | { |
63 | 64 | $path ??= Url::get()->getRelativeUrl(); |
64 | | - $this->checkFirewall(); |
65 | 65 | $method = $method === null || $method === '' ? Helpers::httpMethod() : $method; |
66 | 66 | $params = array_merge($this->safeGetParams($path), $this->getBodyParams($method), $params ?? []); |
67 | 67 | $panel = new Panel($path, $params, $method); |
68 | | - Debugger::getBar()->addPanel($panel); |
| 68 | + $isDebugger = class_exists(Debugger::class); |
| 69 | + if ($isDebugger) { |
| 70 | + Debugger::getBar()->addPanel($panel); |
| 71 | + } |
69 | 72 |
|
70 | 73 | if (preg_match('/^api\/v(?<v>\d{1,3}(?:\.\d{1,3})?)\/(?<path>.*?)$/', $path, $pathParser) === 1) { |
71 | 74 | try { |
72 | 75 | $route = $this->route((string) preg_replace('/^(.*?)(\?.*|)$/', '$1', $pathParser['path']), $pathParser['v'], $params); |
73 | | - $response = null; |
74 | 76 | try { |
75 | | - $endpoint = $this->getEndpointService($route['class'], $params); |
| 77 | + $endpoint = $this->getEndpointService($route['class']); |
76 | 78 | $panel->setEndpoint($endpoint); |
77 | 79 | $response = $this->process($endpoint, $params, $route['action'], $method, $panel); |
78 | 80 | $panel->setResponse($response); |
79 | 81 | } catch (StructuredApiException $e) { |
80 | 82 | throw $e; |
81 | 83 | } catch (\Throwable $e) { |
82 | | - $isDebugger = class_exists(Debugger::class); |
83 | | - if ($isDebugger === true) { |
| 84 | + if ($isDebugger) { |
84 | 85 | Debugger::log($e, ILogger::EXCEPTION); |
85 | 86 | } |
86 | 87 |
|
@@ -149,39 +150,14 @@ public function getEndpoints(): array |
149 | 150 | * Create new API endpoint instance with all injected dependencies. |
150 | 151 | * |
151 | 152 | * @param class-string $className |
152 | | - * @param array<string|int, mixed> $params |
153 | 153 | * @internal |
154 | 154 | */ |
155 | | - public function getEndpointService(string $className, array $params): Endpoint |
| 155 | + public function getEndpointService(string $className): Endpoint |
156 | 156 | { |
157 | 157 | $endpoint = $this->container->getEndpoint($className); |
158 | | - $endpoint->setConvention($this->convention); |
159 | | - |
160 | | - $createReflection = static function (object $class, string $propertyName): ?\ReflectionProperty { |
161 | | - try { |
162 | | - $ref = new \ReflectionProperty($class, $propertyName); |
163 | | - $ref->setAccessible(true); |
164 | | - |
165 | | - return $ref; |
166 | | - } catch (\ReflectionException) { |
167 | | - $refClass = new \ReflectionClass($class); |
168 | | - $parentClass = $refClass->getParentClass(); |
169 | | - while ($parentClass !== false) { |
170 | | - try { |
171 | | - $ref = $parentClass->getProperty($propertyName); |
172 | | - $ref->setAccessible(true); |
173 | | - |
174 | | - return $ref; |
175 | | - } catch (\ReflectionException) { |
176 | | - $parentClass = $refClass->getParentClass(); |
177 | | - } |
178 | | - } |
179 | | - } |
180 | | - |
181 | | - return null; |
182 | | - }; |
183 | | - |
184 | | - $createReflection($endpoint, 'data')?->setValue($endpoint, $params); |
| 158 | + if ($endpoint instanceof BaseEndpoint) { |
| 159 | + $endpoint->convention = $this->convention; |
| 160 | + } |
185 | 161 |
|
186 | 162 | return $endpoint; |
187 | 163 | } |
@@ -336,8 +312,17 @@ private function invokeActionMethod( |
336 | 312 | array $params, |
337 | 313 | Panel $panel, |
338 | 314 | ): ?Response { |
339 | | - $endpoint->startup(); |
340 | | - $endpoint->startupCheck(); |
| 315 | + if (PHP_SAPI !== 'cli') { |
| 316 | + $httpRequest = class_exists(Request::class) |
| 317 | + ? $this->container->getByType(Request::class) |
| 318 | + : null; |
| 319 | + $localization = class_exists(Localization::class) |
| 320 | + ? $this->container->getByType(Localization::class) |
| 321 | + : null; |
| 322 | + if ($httpRequest !== null && $localization !== null) { |
| 323 | + $localization->processHttpRequest($httpRequest); |
| 324 | + } |
| 325 | + } |
341 | 326 |
|
342 | 327 | try { |
343 | 328 | $invoker = new ServiceMethodInvoker($this->projectEntityRepository); |
@@ -381,8 +366,9 @@ private function invokeActionMethod( |
381 | 366 | if ($method !== 'GET' && $response === null) { |
382 | 367 | $response = new JsonResponse($this->convention, ['state' => 'ok']); |
383 | 368 | } |
384 | | - |
385 | | - $endpoint->saveState(); |
| 369 | + if ($endpoint instanceof BaseEndpoint) { |
| 370 | + $endpoint->saveState(); |
| 371 | + } |
386 | 372 |
|
387 | 373 | return $response; |
388 | 374 | } |
@@ -432,19 +418,6 @@ private function getBodyParams(string $method): array |
432 | 418 | } |
433 | 419 |
|
434 | 420 |
|
435 | | - private function checkFirewall(): void |
436 | | - { |
437 | | - if (str_contains($_SERVER['HTTP_USER_AGENT'] ?? '', 'CloudFlare-AlwaysOnline') === true) { |
438 | | - header('HTTP/1.0 403 Forbidden'); |
439 | | - echo '<title>Access denied | API endpoint</title>'; |
440 | | - echo '<h1>Access denied</h1>'; |
441 | | - echo '<p>API endpoint crawling is disabled for robots.</p>'; |
442 | | - echo '<p><b>Information for developers:</b> Endpoint API indexing is disabled for privacy reasons. At the same time, robots can crawl a disproportionate amount of data, copying your valuable data.'; |
443 | | - die; |
444 | | - } |
445 | | - } |
446 | | - |
447 | | - |
448 | 421 | private function rewriteInvalidArgumentException(\InvalidArgumentException $e): ?Response |
449 | 422 | { |
450 | 423 | $message = null; |
|
0 commit comments