1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Zemit Framework. |
5
|
|
|
* |
6
|
|
|
* (c) Zemit Team <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE.txt |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Zemit\Cli; |
13
|
|
|
|
14
|
|
|
use Phalcon\Autoload\Loader; |
15
|
|
|
use Phalcon\Cli\RouterInterface; |
16
|
|
|
use Phalcon\Di\DiInterface; |
17
|
|
|
use Phalcon\Mvc\ModuleDefinitionInterface; |
18
|
|
|
use Zemit\Bootstrap\Config; |
19
|
|
|
use Zemit\Support\Utils; |
20
|
|
|
|
21
|
|
|
class Module implements ModuleDefinitionInterface |
22
|
|
|
{ |
23
|
|
|
public const string NAME_CLI = 'cli'; |
|
|
|
|
24
|
|
|
|
25
|
|
|
public string $name = self::NAME_CLI; |
26
|
|
|
|
27
|
|
|
public ?Config $config = null; |
28
|
|
|
|
29
|
|
|
public ?Dispatcher $dispatcher = null; |
30
|
|
|
|
31
|
|
|
public ?Loader $loader = null; |
32
|
|
|
|
33
|
|
|
public ?Router $router = null; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Registers an autoloader related to the frontend module |
37
|
|
|
*/ |
38
|
1 |
|
public function registerAutoloaders(DiInterface $container = null): void |
39
|
|
|
{ |
40
|
1 |
|
$this->loader = $container['loader'] ?? new Loader(); |
41
|
1 |
|
assert($this->loader instanceof Loader); |
42
|
|
|
|
43
|
1 |
|
$this->loader->setNamespaces($this->getNamespaces(), true); |
44
|
1 |
|
$this->loader->register(); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Registers services related to the module |
49
|
|
|
*/ |
50
|
1 |
|
public function registerServices(DiInterface $container): void |
51
|
|
|
{ |
52
|
1 |
|
$this->getServices($container); |
53
|
|
|
|
54
|
1 |
|
assert($this->dispatcher instanceof DispatcherInterface); |
55
|
|
|
// assert($this->router instanceof RouterInterface); // @todo add this when phalcon pushed the fix |
56
|
1 |
|
assert($this->router instanceof \Zemit\Router\RouterInterface); |
57
|
|
|
|
58
|
|
|
// dispatcher settings |
59
|
1 |
|
$defaultNamespace = $this->getDefaultNamespace(); |
60
|
1 |
|
$this->dispatcher->setDefaultNamespace($defaultNamespace); |
61
|
1 |
|
$this->dispatcher->setNamespaceName($defaultNamespace); |
62
|
|
|
|
63
|
|
|
// router settings |
64
|
1 |
|
$this->router->setDefaults([ |
65
|
1 |
|
'namespace' => $defaultNamespace, |
66
|
1 |
|
'module' => $this->name, |
67
|
1 |
|
'controller' => 'help', |
68
|
1 |
|
'action' => 'main', |
69
|
1 |
|
]); |
70
|
|
|
|
71
|
1 |
|
$this->setServices($container); |
72
|
|
|
} |
73
|
|
|
|
74
|
2 |
|
public function getNamespaces(): array |
75
|
|
|
{ |
76
|
2 |
|
$namespaces = []; |
77
|
|
|
|
78
|
|
|
// Caller namespace |
79
|
2 |
|
$namespace = $this->getNamespace(); |
80
|
2 |
|
$dirname = $this->getDirname(); |
81
|
|
|
|
82
|
|
|
// register the vendor module controllers |
83
|
2 |
|
$namespaces[$namespace . '\\Tasks'] = $dirname . '/Tasks/'; |
84
|
2 |
|
$namespaces[$namespace . '\\Models'] = $dirname . '/Models/'; |
85
|
|
|
|
86
|
|
|
// add zemit core models |
87
|
2 |
|
$corePath = dirname(__DIR__); |
88
|
2 |
|
$namespaces['Zemit\\Models'] = $corePath . '/Models/'; |
89
|
|
|
|
90
|
2 |
|
return $namespaces; |
91
|
|
|
} |
92
|
|
|
|
93
|
3 |
|
public function getServices(DiInterface $container = null): void |
94
|
|
|
{ |
95
|
3 |
|
$this->loader = $container['loader'] ?? new Loader(); |
96
|
3 |
|
$this->config ??= $container['config'] ?? new Config(); |
97
|
3 |
|
$this->router ??= $container['router'] ?? new Router(); |
98
|
3 |
|
$this->dispatcher ??= $container['dispatcher'] ?? new Dispatcher(); |
99
|
|
|
} |
100
|
|
|
|
101
|
2 |
|
public function setServices(DiInterface $container): void |
102
|
|
|
{ |
103
|
2 |
|
$container->set('config', $this->config); |
104
|
2 |
|
$container->set('dispatcher', $this->dispatcher); |
105
|
2 |
|
$container->set('loader', $this->loader); |
106
|
2 |
|
$container->set('router', $this->router); |
107
|
|
|
} |
108
|
|
|
|
109
|
2 |
|
public function getDefaultNamespace(): string |
110
|
|
|
{ |
111
|
2 |
|
return $this->getNamespace() . '\\Tasks'; |
112
|
|
|
} |
113
|
|
|
|
114
|
3 |
|
public function getDirname(): string |
115
|
|
|
{ |
116
|
3 |
|
return Utils::getDirname($this); |
117
|
|
|
} |
118
|
|
|
|
119
|
5 |
|
public function getNamespace(): string |
120
|
|
|
{ |
121
|
5 |
|
return Utils::getNamespace($this); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|