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
|
|
|
if (!$this->fs->isDirectory(modules_dir())) { |
40
|
|
|
$this->fs->makeDirectory(modules_dir()); |
41
|
|
|
} |
42
|
|
|
$this->copyDirectoryWithTemplates($this->templatePath, $this->modulePath); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function addModuleConfig() |
46
|
|
|
{ |
47
|
|
|
$modulesConfigPath = base_dir() . DS . 'shared' . DS . 'config' . DS . 'modules.php'; |
48
|
|
|
$modules = require $modulesConfigPath; |
49
|
|
|
|
50
|
|
|
foreach ($modules['modules'] as $module => $options) { |
51
|
|
|
if ($module == $this->moduleName || $options['prefix'] == strtolower($this->moduleName)) { |
52
|
|
|
throw new \Exception("A module or prefix named '$this->moduleName' already exists"); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$this->fs->put( |
57
|
|
|
$modulesConfigPath, |
58
|
|
|
str_replace( |
59
|
|
|
"'modules' => [", |
60
|
|
|
$this->writeModuleConfig($this->moduleName), |
61
|
|
|
$this->fs->get($modulesConfigPath) |
62
|
|
|
) |
63
|
|
|
); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
private function copyDirectoryWithTemplates($src, $dst) { |
67
|
|
|
if (!$this->fs->isDirectory($src)) { |
68
|
|
|
throw new \Exception("Directory '$src' does not exist"); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
if (!$this->fs->isDirectory($dst)) { |
72
|
|
|
$this->fs->makeDirectory($dst); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$dir = $this->fs->listDirectory($src); |
76
|
|
|
|
77
|
|
|
foreach ($dir as $file) { |
78
|
|
|
$srcPath = $file; |
79
|
|
|
$dstPath = str_replace($src, $dst, $file); |
80
|
|
|
|
81
|
|
|
if ($this->fs->isDirectory($srcPath)) { |
82
|
|
|
$this->copyDirectoryWithTemplates($srcPath, $dstPath); |
83
|
|
|
} else { |
84
|
|
|
$processedContent = require_once $srcPath; |
85
|
|
|
$this->fs->put($dstPath, $processedContent); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Add module to config |
92
|
|
|
* @param string $module |
93
|
|
|
* @return string |
94
|
|
|
*/ |
95
|
|
|
private function writeModuleConfig(string $module): string |
96
|
|
|
{ |
97
|
|
|
$enabled = $this->optionEnabled ? "true" : "false"; |
98
|
|
|
|
99
|
|
|
$prefix = $this->template == "web" && $this->demo == "yes" ? "" : strtolower($module); |
100
|
|
|
|
101
|
|
|
return "'modules' => [ |
102
|
|
|
'" . $module . "' => [ |
103
|
|
|
'prefix' => '" . $prefix . "', |
104
|
|
|
'enabled' => " . $enabled . ", |
105
|
|
|
],"; |
106
|
|
|
} |
107
|
|
|
} |