|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace BEAR\Package; |
|
6
|
|
|
|
|
7
|
|
|
use BEAR\AppMeta\AbstractAppMeta; |
|
8
|
|
|
use BEAR\Package\Exception\InvalidContextException; |
|
9
|
|
|
use BEAR\Package\Module\AppMetaModule; |
|
10
|
|
|
use BEAR\Package\Module\CacheNamespaceModule; |
|
11
|
|
|
use Ray\Di\AbstractModule; |
|
12
|
|
|
use Ray\Di\AssistedModule; |
|
13
|
|
|
|
|
14
|
|
|
use function array_reverse; |
|
15
|
|
|
use function class_exists; |
|
16
|
|
|
use function explode; |
|
17
|
|
|
use function is_a; |
|
18
|
|
|
use function is_subclass_of; |
|
19
|
|
|
use function ucwords; |
|
20
|
|
|
|
|
21
|
|
|
class Module |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* Return module from $appMeta and $context |
|
25
|
|
|
*/ |
|
26
|
|
|
public function __invoke(AbstractAppMeta $appMeta, string $context, string $cacheNamespace = ''): AbstractModule |
|
27
|
|
|
{ |
|
28
|
|
|
$contextsArray = array_reverse(explode('-', $context)); |
|
29
|
|
|
$module = new AssistedModule(); |
|
30
|
|
|
foreach ($contextsArray as $contextItem) { |
|
31
|
|
|
$module = $this->installContextModule($appMeta, $contextItem, $module); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
$module->override(new AppMetaModule($appMeta)); |
|
|
|
|
|
|
35
|
|
|
$module->override(new CacheNamespaceModule($cacheNamespace)); |
|
|
|
|
|
|
36
|
|
|
|
|
37
|
|
|
return $module; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
private function installContextModule(AbstractAppMeta $appMeta, string $contextItem, AbstractModule $module): AbstractModule |
|
41
|
|
|
{ |
|
42
|
|
|
$class = $appMeta->name . '\Module\\' . ucwords($contextItem) . 'Module'; |
|
43
|
|
|
if (! class_exists($class)) { |
|
44
|
|
|
$class = 'BEAR\Package\Context\\' . ucwords($contextItem) . 'Module'; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
if (! is_a($class, AbstractModule::class, true)) { |
|
48
|
|
|
throw new InvalidContextException($contextItem); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** @psalm-suppress UnsafeInstantiation */ |
|
52
|
|
|
$module = is_subclass_of($class, AbstractAppModule::class) ? new $class($appMeta, $module) : new $class($module); |
|
|
|
|
|
|
53
|
|
|
|
|
54
|
|
|
return $module; |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
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: