Completed
Push — master ( 14f560...15f305 )
by
unknown
08:01
created

ComposerModuleManager::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 9
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 15
rs 9.4285
1
<?php declare(strict_types=1);
2
3
namespace samsonphp\core\loader\module;
4
5
use samsonframework\resource\ResourceMap;
6
use samsonphp\event\Event;
7
8
/**
9
 * Class ComposerModuleManager implementation
10
 *
11
 * @package samsonphp\core\loader\module
12
 */
13
class ComposerModuleManager implements ModuleManagerInterface
14
{
15
    /** Composer precontainer id */
16
    const PRE_CONTAINER_ID = 'samsonframework_precontainer';
17
18
    /** Composer compressable module id */
19
    const COMPRESSABLE_MODULE_ID = 'samsonphp_package_compressable';
20
21
    /** @var array List of modules */
22
    protected $composerModules = [];
23
24
    /** @var string Application path */
25
    protected $appPath;
26
27
    /** @var string Vendor dir */
28
    protected $vendorDir;
29
30
    /** @var array Collection of interface => [classes]*/
31
    public $implements = [];
32
    /** @var array Collection of class => class */
33
    protected $extends = [];
34
35
    /**
36
     * ComposerModuleManager constructor.
37
     *
38
     * @param string $path
39
     * @param string $vendorDir
40
     */
41
    public function __construct(string $path, string $vendorDir)
42
    {
43
        $this->appPath = $path;
44
        $this->vendorDir = $vendorDir;
45
46
        // TODO Move this somewhere
47
        Event::fire('core.composer.create', [
48
            &$this->composerModules,
49
            $this->appPath, [
50
                'vendorsList' => ['samsonphp/', 'samsonos/', 'samsoncms/', 'samsonjavascript/'],
51
                'ignoreKey' => 'samson_module_ignore',
52
                'includeKey' => 'samson_module_include'
53
            ]
54
        ]);
55
    }
56
57
    /** {@inheritdoc} */
58
    public function getRegisteredModules() : array
59
    {
60
        $list = [];
61
        // Get all modules by their names
62
        foreach (array_keys($this->composerModules) as $moduleName) {
63
            $list[$moduleName] = $this->getModule($moduleName);
64
        }
65
        return $list;
66
    }
67
68
    /** {@inheritdoc} */
69
    public function getModule(string $moduleName) : Module
70
    {
71
        // Check if module exists
72
        if (!array_key_exists($moduleName, $this->composerModules)) {
73
            throw new \Exception(sprintf('Module with name "%s" not found', $moduleName));
74
        }
75
76
        $composerModule = $this->composerModules[$moduleName];
77
        $modulePath = $this->appPath . $this->vendorDir . $moduleName;
78
79
        // Convert resource map to module type
80
        $resourceMap = ResourceMap::get($modulePath);
81
82
        // Get all module classes
83
        $classes = [];
84
        foreach ($resourceMap->classData as $classData) {
85
            $classes[$classData['path']] = $classData['className'];
86
            if (array_key_exists('implements', $classData)) {
87
                foreach ($classData['implements'] as $interfaceName) {
88
                    $this->implements[$interfaceName] = $classData['className'];
89
                }
90
            }
91
            if (array_key_exists('extends', $classData) && isset($classData['extends']{0})) {
92
                $this->extends[$classData['extends']] = $classData['className'];
93
            }
94
        }
95
96
        foreach ($this->implements as $interface => $class) {
97
            if (array_key_exists($class, $this->extends)) {
98
                $this->implements[$interface] = $this->extends[$class];
99
            }
100
        }
101
102
        // Create new module
103
        $module = new Module($moduleName, $modulePath, $classes);
104
        $module->composerParameters = $composerModule;
105
        if (isset($resourceMap->module[0])) {
106
            $module->className = $resourceMap->module[0];
107
            $module->pathName = $resourceMap->module[1];
108
        }
109
        $module->isVirtualModule = !array_key_exists(0, $resourceMap->module) &&
0 ignored issues
show
Bug introduced by
The property isVirtualModule does not seem to exist in samsonphp\core\loader\module\Module.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
110
            array_key_exists(self::COMPRESSABLE_MODULE_ID, $composerModule) &&
111
            (bool)$composerModule[self::COMPRESSABLE_MODULE_ID] === true;
112
113
        return $module;
114
    }
115
}
116