1 | <?php |
||
2 | |||
3 | |||
4 | namespace W2w\Laravel\Apie\Services; |
||
5 | |||
6 | use Illuminate\Container\Container; |
||
7 | use Illuminate\Http\Request; |
||
8 | use W2w\Laravel\Apie\Exceptions\ApieContextMissingException; |
||
9 | use W2w\Laravel\Apie\Plugins\Illuminate\IlluminatePlugin; |
||
10 | use W2w\Lib\Apie\Apie; |
||
11 | use W2w\Lib\Apie\Exceptions\BadConfigurationException; |
||
12 | use W2w\Lib\Apie\Plugins\FakeAnnotations\FakeAnnotationsPlugin; |
||
13 | |||
14 | final class ApieContext |
||
15 | { |
||
16 | private $contextKey = []; |
||
17 | |||
18 | private $container; |
||
19 | |||
20 | private $apie; |
||
21 | |||
22 | private $config; |
||
23 | |||
24 | private $contexts; |
||
25 | |||
26 | private $instantiatedContexts; |
||
27 | |||
28 | public function __construct(Container $container, Apie $apie, array $config, array& $contexts) |
||
29 | { |
||
30 | $this->container = $container; |
||
31 | $this->apie = $apie; |
||
32 | $this->config = $config; |
||
33 | $this->contexts = &$contexts; |
||
34 | } |
||
35 | |||
36 | public function getApie(): Apie |
||
37 | { |
||
38 | return $this->apie; |
||
39 | } |
||
40 | |||
41 | public function getActiveContext(): ApieContext |
||
42 | { |
||
43 | if ($this->container->bound(Request::class)) { |
||
44 | /** @var Request $request */ |
||
45 | $request = $this->container->get(Request::class); |
||
46 | $contexts = $request->route('context'); |
||
47 | if (is_string($contexts) && $contexts) { |
||
48 | $contexts = explode('.', $contexts); |
||
49 | } |
||
50 | if (is_array($contexts)) { |
||
51 | $context = $this; |
||
52 | while ($contexts && $context) { |
||
0 ignored issues
–
show
|
|||
53 | $key = array_shift($contexts); |
||
54 | $context = $context->getContext($key); |
||
55 | } |
||
56 | if ($context) { |
||
57 | return $context; |
||
58 | } |
||
59 | } |
||
60 | } |
||
61 | return $this; |
||
62 | } |
||
63 | |||
64 | public function getContext(string $context): ApieContext |
||
65 | { |
||
66 | // done for potential file read exploits |
||
67 | if (!preg_match('/^[a-z0-9-]+$/i', $context)) { |
||
68 | throw new BadConfigurationException('Context "' . $context . '" is not a valid context name!'); |
||
69 | } |
||
70 | if (!isset($this->instantiatedContexts[$context])) { |
||
71 | $this->instantiatedContexts[$context] = $this->createContext($context); |
||
72 | } |
||
73 | return $this->instantiatedContexts[$context]; |
||
74 | } |
||
75 | |||
76 | private function createContext(string $context): ApieContext |
||
77 | { |
||
78 | if (!isset($this->contexts[$context])) { |
||
79 | throw new ApieContextMissingException($context); |
||
80 | } |
||
81 | $plugins = []; |
||
82 | foreach ($this->contexts[$context]['plugins'] as $plugin) { |
||
83 | $plugins[] = $this->container->make($plugin); |
||
84 | } |
||
85 | if (!empty($this->contexts[$context]['resource-config'])) { |
||
86 | $plugins[] = new FakeAnnotationsPlugin($this->contexts[$context]['resource-config']); |
||
87 | } |
||
88 | $plugins[] = new IlluminatePlugin($this->container, $this->contexts[$context]); |
||
89 | $apie = $this->apie->createContext($plugins); |
||
90 | $apieContext = new ApieContext( |
||
91 | $this->container, |
||
92 | $apie, |
||
93 | $this->contexts[$context], |
||
94 | $this->contexts[$context]['contexts'] |
||
95 | ); |
||
96 | $key = $this->contextKey; |
||
97 | $key[] = $context; |
||
98 | $apieContext->contextKey = $key; |
||
99 | return $apieContext; |
||
100 | } |
||
101 | |||
102 | public function getConfig(string $key) |
||
103 | { |
||
104 | return $this->config[$key]; |
||
105 | } |
||
106 | |||
107 | public function getContextKey(): array |
||
108 | { |
||
109 | return $this->contextKey; |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * @return ApieContext[] |
||
114 | */ |
||
115 | public function allContexts(): array |
||
116 | { |
||
117 | $res = []; |
||
118 | foreach (array_keys($this->contexts) as $context) { |
||
119 | $res[$context] = $this->getContext($context); |
||
120 | } |
||
121 | return $res; |
||
122 | } |
||
123 | } |
||
124 |
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
empty(..)
or! empty(...)
instead.