Passed
Pull Request — master (#142)
by
unknown
02:42
created

ModuleManager::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 8
c 2
b 0
f 0
nc 2
nop 4
dl 0
loc 16
rs 10
1
<?php
2
3
namespace Quantum\Libraries\Module;
4
5
use Quantum\Di\Di;
6
use Quantum\Libraries\Storage\FileSystem;
7
8
class ModuleManager{
9
10
    protected $fs;
11
    protected $optionEnabled;
12
13
    private $moduleName;
14
    private $template;
15
    private $demo;
16
    private $modulePath;
17
    private $templatePath;
18
19
    function __construct(string $moduleName, string $template, string $demo, $enabled){
20
        $this->moduleName = $moduleName;
21
22
        $this->template = $template;
23
        
24
        $this->demo = $demo;
25
26
        $this->optionEnabled = $enabled;
27
        
28
        $type = $this->demo == "yes" ? "Demo" : "Default";
29
30
        $this->templatePath = __DIR__ . DS . "Templates" . DS . $type . DS . ucfirst($this->template);
31
        
32
        $this->modulePath = modules_dir() . DS . $this->moduleName;
33
34
        $this->fs = Di::get(FileSystem::class);
35
    }
36
37
    public function writeContents()
38
    {
39
        $this->copyDirectoryWithTemplates($this->templatePath, $this->modulePath);
40
    }
41
42
    public function addModuleConfig()
43
    {
44
        $modulesConfigPath = base_dir() . DS . 'shared' . DS . 'config' . DS . 'modules.php';
45
        $modules = require $modulesConfigPath;
46
47
        foreach ($modules['modules'] as $module => $options) {
48
            if ($module == $this->moduleName  || $options['prefix'] == strtolower($this->moduleName)) {
49
                throw new \Exception("A module or prefix named '$this->moduleName' already exists");
50
            }
51
        }
52
53
        $this->fs->put(
54
            $modulesConfigPath,
55
            str_replace(
56
                "'modules' => [",
57
                $this->writeModuleConfig($this->moduleName),
58
                $this->fs->get($modulesConfigPath)
59
            )
60
        );
61
    }
62
63
    private function copyDirectoryWithTemplates($src, $dst) {
64
        if (!$this->fs->isDirectory($src)) {
65
            throw new \Exception("Directory '$src' does not exist");
66
        }
67
68
        if (!$this->fs->isDirectory($dst)) {
69
            $this->fs->makeDirectory($dst);
70
        }
71
72
        $dir = $this->fs->listDirectory($src);
73
74
        foreach ($dir as $file) {
75
            $srcPath = $file;
76
            $dstPath = str_replace($src, $dst, $file);
77
78
            if ($this->fs->isDirectory($srcPath)) {
79
                $this->copyDirectoryWithTemplates($srcPath, $dstPath);
80
            } else {
81
                $processedContent = require_once $srcPath;
82
                $this->fs->put($dstPath, $processedContent);
83
            }
84
        }
85
    }
86
87
    /**
88
     * Add module to config
89
     * @param string $module
90
     * @return string
91
     */
92
    private function writeModuleConfig(string $module): string
93
    {
94
        $enabled = $this->optionEnabled ? "true" : "false";
95
96
        $prefix = $this->template == "web" && $this->demo == "yes" ? "" : strtolower($module);
97
98
        return "'modules' => [
99
        '" . $module . "' => [
100
            'prefix' => '" . $prefix . "',
101
            'enabled' => " . $enabled . ",
102
        ],";
103
    }
104
}