|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the BEAR.Middleware package. |
|
4
|
|
|
* |
|
5
|
|
|
* @license http://opensource.org/licenses/MIT MIT |
|
6
|
|
|
*/ |
|
7
|
|
|
namespace BEAR\Middleware; |
|
8
|
|
|
|
|
9
|
|
|
use BEAR\AppMeta\AbstractAppMeta; |
|
10
|
|
|
use BEAR\Middleware\Exception\InvalidContextException; |
|
11
|
|
|
use BEAR\Middleware\Module\AppMetaModule; |
|
12
|
|
|
use BEAR\Middleware\Module\MiddlewareModule; |
|
13
|
|
|
use BEAR\Middleware\Module\StreamModule; |
|
14
|
|
|
use Ray\Compiler\DiCompiler; |
|
15
|
|
|
use Ray\Compiler\Exception\NotCompiled; |
|
16
|
|
|
use Ray\Compiler\ScriptInjector; |
|
17
|
|
|
use Ray\Di\AbstractModule; |
|
18
|
|
|
use Ray\Di\InjectorInterface; |
|
19
|
|
|
|
|
20
|
|
|
class Boot |
|
21
|
|
|
{ |
|
22
|
1 |
|
public function getInjector(AbstractAppMeta $appMeta, $contexts) |
|
23
|
|
|
{ |
|
24
|
|
|
try { |
|
25
|
1 |
|
$injector = (new ScriptInjector($appMeta->tmpDir))->getInstance(InjectorInterface::class); |
|
26
|
1 |
|
} catch (NotCompiled $e) { |
|
27
|
1 |
|
$module = $this->getContxtualModule($appMeta, $contexts); |
|
28
|
|
|
$module->override(new MiddlewareModule(new AppMetaModule($appMeta))); |
|
|
|
|
|
|
29
|
|
|
$compiler = new DiCompiler(new StreamModule($module), $appMeta->tmpDir); |
|
|
|
|
|
|
30
|
|
|
$compiler->compile(); |
|
31
|
|
|
$injector = (new ScriptInjector($appMeta->tmpDir))->getInstance(InjectorInterface::class); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
return $injector; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Return configured module |
|
39
|
|
|
* |
|
40
|
|
|
* @param AbstractAppMeta $appMeta |
|
41
|
|
|
* @param string $contexts |
|
42
|
|
|
* |
|
43
|
|
|
* @return AbstractModule |
|
44
|
|
|
*/ |
|
45
|
1 |
|
private function getContxtualModule(AbstractAppMeta $appMeta, $contexts) |
|
46
|
|
|
{ |
|
47
|
1 |
|
$contextsArray = array_reverse(explode('-', $contexts)); |
|
48
|
1 |
|
$module = null; |
|
49
|
1 |
|
foreach ($contextsArray as $context) { |
|
50
|
1 |
|
$class = $appMeta->name . '\Module\\' . ucwords($context) . 'Module'; |
|
51
|
1 |
|
if (! class_exists($class)) { |
|
52
|
1 |
|
$class = 'BEAR\Package\Context\\' . ucwords($context) . 'Module'; |
|
53
|
|
|
} |
|
54
|
1 |
|
if (! is_a($class, AbstractModule::class, true)) { |
|
55
|
1 |
|
throw new InvalidContextException($context); |
|
56
|
|
|
} |
|
57
|
|
|
$module = new $class($module); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
return $module; |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: