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

ModuleManager::copyDirectoryWithTemplates()   A

Complexity

Conditions 5
Paths 7

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

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