1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace kalanis\kw_modules\Loaders\Di; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use kalanis\kw_modules\Interfaces\ILoader; |
7
|
|
|
use kalanis\kw_modules\Interfaces\IMdTranslations; |
8
|
|
|
use kalanis\kw_modules\Interfaces\IModule; |
9
|
|
|
use kalanis\kw_modules\ModuleException; |
10
|
|
|
use kalanis\kw_modules\Traits\TMdLang; |
11
|
|
|
use Psr\Container\ContainerInterface; |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class ALoader |
16
|
|
|
* @package kalanis\kw_modules\Loaders\Di |
17
|
|
|
* Use Dependency Injection |
18
|
|
|
* @codeCoverageIgnore contains external autoloader |
19
|
|
|
* |
20
|
|
|
* Name is passed as first big and the rest little ( ucfirst(strtolower($x)) ) |
21
|
|
|
* - lookup by curly braces |
22
|
|
|
*/ |
23
|
|
|
abstract class ALoader implements ILoader |
24
|
|
|
{ |
25
|
|
|
use TMdLang; |
26
|
|
|
|
27
|
|
|
/** @var ContainerInterface */ |
28
|
|
|
protected $container = null; |
29
|
|
|
|
30
|
|
|
public function __construct(ContainerInterface $container, ?IMdTranslations $lang = null) |
31
|
|
|
{ |
32
|
|
|
$this->setMdLang($lang); |
33
|
|
|
$this->container = $container; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function load(array $module, array $constructParams = []): ?IModule |
37
|
|
|
{ |
38
|
|
|
$classPath = $this->getClassName($module); |
39
|
|
|
if ($this->container->has($classPath)) { |
40
|
|
|
$module = $this->container->get($classPath); |
41
|
|
|
if (!$module instanceof IModule) { |
42
|
|
|
throw new ModuleException($this->getMdLang()->mdNotInstanceOfIModule($classPath)); |
43
|
|
|
} |
44
|
|
|
return $module; |
45
|
|
|
} |
46
|
|
|
return null; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param string[] $module |
51
|
|
|
* @throws ModuleException |
52
|
|
|
* @return string |
53
|
|
|
*/ |
54
|
|
|
abstract protected function getClassName(array $module): string; |
55
|
|
|
} |
56
|
|
|
|