These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | /** |
||
3 | * This file is part of the BEAR.Package package. |
||
4 | * |
||
5 | * @license http://opensource.org/licenses/MIT MIT |
||
6 | */ |
||
7 | namespace BEAR\Package; |
||
8 | |||
9 | use BEAR\AppMeta\AbstractAppMeta; |
||
10 | use BEAR\AppMeta\AppMeta; |
||
11 | use BEAR\Package\Exception\InvalidContextException; |
||
12 | use BEAR\Package\Provide\Resource\ResourceObjectModule; |
||
13 | use Ray\Compiler\DiCompiler; |
||
14 | use Ray\Compiler\Exception\NotCompiled; |
||
15 | use Ray\Compiler\ScriptInjector; |
||
16 | use Ray\Di\AbstractModule; |
||
17 | use Ray\Di\Injector; |
||
18 | use Ray\Di\InjectorInterface; |
||
19 | use Ray\Di\Name; |
||
20 | |||
21 | final class AppInjector implements InjectorInterface |
||
22 | { |
||
23 | /** |
||
24 | * @var AbstractModule |
||
25 | */ |
||
26 | private $appModule; |
||
27 | |||
28 | /** |
||
29 | * @var string |
||
30 | */ |
||
31 | private $scriptDir; |
||
32 | |||
33 | /** |
||
34 | * @var string |
||
35 | */ |
||
36 | private $logDir; |
||
37 | |||
38 | 25 | public function __construct($name, $contexts) |
|
39 | { |
||
40 | 25 | $appMeta = new AppMeta($name, $contexts); |
|
41 | 25 | $this->scriptDir = $appMeta->tmpDir; |
|
42 | 25 | $this->logDir = $appMeta->logDir; |
|
43 | 25 | $this->appModule = $this->newModule($appMeta, $contexts); |
|
44 | 23 | } |
|
45 | |||
46 | /** |
||
47 | * {@inheritdoc} |
||
48 | */ |
||
49 | 22 | public function getInstance($interface, $name = Name::ANY) |
|
50 | { |
||
51 | try { |
||
52 | 22 | return $this->getInjector()->getInstance($interface, $name); |
|
53 | 1 | } catch (NotCompiled $e) { |
|
54 | 1 | file_put_contents(sprintf('%s/%s', $this->logDir, 'compile-err.log'), (string) $e); |
|
55 | |||
56 | 1 | throw $e; |
|
57 | } |
||
58 | } |
||
59 | |||
60 | 1 | public function getOverrideInstance(AbstractModule $module, $interface, $name = Name::ANY) |
|
61 | { |
||
62 | 1 | $appModule = clone $this->appModule; |
|
63 | 1 | $appModule->override($module); |
|
0 ignored issues
–
show
|
|||
64 | |||
65 | 1 | return (new Injector($appModule, $this->scriptDir))->getInstance($interface, $name); |
|
66 | } |
||
67 | |||
68 | 22 | private function getInjector() : InjectorInterface |
|
69 | { |
||
70 | 22 | $scriptInjector = new ScriptInjector($this->scriptDir); |
|
71 | try { |
||
72 | 22 | $injector = $scriptInjector->getInstance(InjectorInterface::class); |
|
73 | 5 | } catch (NotCompiled $e) { |
|
74 | 5 | $this->compile($this->appModule, $this->scriptDir); |
|
75 | 5 | $injector = $scriptInjector->getInstance(InjectorInterface::class); |
|
76 | } |
||
77 | |||
78 | 22 | return $injector; |
|
79 | } |
||
80 | |||
81 | 5 | private function compile(AbstractModule $module, string $scriptDir) |
|
82 | { |
||
83 | 5 | $compiler = new DiCompiler($module, $scriptDir); |
|
84 | 5 | $compiler->compile(); |
|
85 | 5 | } |
|
86 | |||
87 | /** |
||
88 | * Return configured module |
||
89 | */ |
||
90 | 25 | private function newModule(AbstractAppMeta $appMeta, string $contexts) : AbstractModule |
|
91 | { |
||
92 | 25 | $contextsArray = array_reverse(explode('-', $contexts)); |
|
93 | 25 | $module = null; |
|
94 | 25 | foreach ($contextsArray as $context) { |
|
95 | 25 | $class = $appMeta->name . '\Module\\' . ucwords($context) . 'Module'; |
|
96 | 25 | if (! class_exists($class)) { |
|
97 | 23 | $class = 'BEAR\Package\Context\\' . ucwords($context) . 'Module'; |
|
98 | } |
||
99 | 25 | if (! is_a($class, AbstractModule::class, true)) { |
|
100 | 2 | throw new InvalidContextException($class); |
|
101 | } |
||
102 | /* @var $module AbstractModule */ |
||
103 | 23 | $module = new $class($module); |
|
104 | } |
||
105 | 23 | $module->install(new ResourceObjectModule($appMeta)); |
|
0 ignored issues
–
show
new \BEAR\Package\Provid...eObjectModule($appMeta) is of type object<BEAR\Package\Prov...e\ResourceObjectModule> , but the function expects a object<self> .
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: function acceptsInteger($int) { }
$x = '123'; // string "123"
// Instead of
acceptsInteger($x);
// we recommend to use
acceptsInteger((integer) $x);
![]() |
|||
106 | 23 | $module->override(new AppMetaModule($appMeta)); |
|
0 ignored issues
–
show
new \BEAR\Package\AppMetaModule($appMeta) is of type object<BEAR\Package\AppMetaModule> , but the function expects a object<self> .
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: function acceptsInteger($int) { }
$x = '123'; // string "123"
// Instead of
acceptsInteger($x);
// we recommend to use
acceptsInteger((integer) $x);
![]() |
|||
107 | |||
108 | 23 | return $module; |
|
109 | } |
||
110 | } |
||
111 |
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: