forked from Block8/Octo
/
Module.php
112 lines (86 loc) · 2.84 KB
/
Module.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php
namespace Octo;
use b8\Config;
use Octo\ModuleManager;
abstract class Module
{
/**
* @var ModuleManager
*/
protected $manager;
/**
* @var \b8\Config
*/
protected $config;
/**
* @var string
*/
protected $namespace;
public function __construct(ModuleManager &$manager, Config &$config, $namespace)
{
$this->manager = $manager;
$this->config = $config;
$this->namespace = $namespace;
if (method_exists($this, 'requires')) {
$this->requires();
}
}
public function init()
{
$base = $this->getPath();
$templatePath = $base . 'Template/';
$adminTemplatePath = $base . 'Admin/Template/';
$octoConfig = $this->config->get('Octo', []);
if (is_dir($templatePath)) {
$octoConfig['paths']['templates'][$this->getName()] = $templatePath;
}
if (is_dir($adminTemplatePath)) {
$octoConfig['paths']['admin_templates'][$this->getName()] = $adminTemplatePath;
}
$octoConfig['paths']['modules'][$this->getName()] = $base;
$octoConfig['paths']['namespaces'][$this->namespace . '\\' . $this->getName()] = $base;
if (!isset($octoConfig['namespaces']['blocks'])) {
$octoConfig['namespaces']['blocks'] = [];
}
$blocks = $this->getBlocks($this->namespace);
$octoConfig['namespaces']['blocks'] = array_merge($octoConfig['namespaces']['blocks'], $blocks);
$app = $this->config->get('app', []);
$app['namespaces'] = array_merge($app['namespaces'], $this->getModels($this->namespace));
$this->config->set('app', $app);
$this->config->set('Octo', $octoConfig);
}
abstract protected function getName();
abstract protected function getPath();
protected function getModels($namespace)
{
$modelPath = $this->getPath() . 'Model/';
if (!is_dir($modelPath)) {
return [];
}
$dir = new \DirectoryIterator($modelPath);
$rtn = [];
foreach ($dir as $item) {
if ($item->isFile() && $item->getExtension() == 'php') {
$modelName = $item->getBasename('.php');
$rtn[$modelName] = $namespace . '\\' . $this->getName();
}
}
return $rtn;
}
protected function getBlocks($namespace)
{
$blockPath = $this->getPath() . 'Block/';
if (!is_dir($blockPath)) {
return [];
}
$dir = new \DirectoryIterator($blockPath);
$rtn = [];
foreach ($dir as $item) {
if ($item->isFile() && $item->getExtension() == 'php') {
$modelName = $item->getBasename('.php');
$rtn[$modelName] = $namespace . '\\' . $this->getName();
}
}
return $rtn;
}
}