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\Mvc; |
13
|
|
|
|
14
|
|
|
use Phalcon\Di\DiInterface; |
15
|
|
|
use Phalcon\Loader; |
16
|
|
|
use Phalcon\Mvc\View; |
|
|
|
|
17
|
|
|
use Phalcon\Mvc\ModuleDefinitionInterface; |
18
|
|
|
use Zemit\Bootstrap\Config; |
19
|
|
|
use Zemit\Di\Injectable; |
20
|
|
|
use Zemit\Utils; |
21
|
|
|
use Zemit\Url; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* {@inheritDoc} |
25
|
|
|
*/ |
26
|
|
|
class Module extends Injectable implements ModuleDefinitionInterface |
27
|
|
|
{ |
28
|
|
|
public const NAME_FRONTEND = 'frontend'; |
29
|
|
|
public const NAME_ADMIN = 'admin'; |
30
|
|
|
public const NAME_API = 'api'; |
31
|
|
|
public const NAME_OAUTH2 = 'oauth2'; |
32
|
|
|
|
33
|
|
|
public string $name; |
34
|
|
|
|
35
|
|
|
public Config $config; |
36
|
|
|
|
37
|
|
|
public Dispatcher $dispatcher; |
38
|
|
|
|
39
|
|
|
public Loader $loader; |
40
|
|
|
|
41
|
|
|
public Router $router; |
42
|
|
|
|
43
|
|
|
public View $view; |
44
|
|
|
|
45
|
|
|
public Url $url; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Registers an autoloader related to the frontend module |
49
|
|
|
*/ |
50
|
|
|
public function registerAutoloaders(DiInterface $container = null): void |
51
|
|
|
{ |
52
|
|
|
$this->getServices(); |
|
|
|
|
53
|
|
|
$this->loader->registerNamespaces($this->getNamespaces(), true); |
54
|
|
|
$this->loader->register(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Registers services related to the module |
59
|
|
|
*/ |
60
|
|
|
public function registerServices(DiInterface $container): void |
61
|
|
|
{ |
62
|
|
|
$this->getServices($container); |
63
|
|
|
|
64
|
|
|
$defaultNamespace = $this->getDefaultNamespace(); |
65
|
|
|
$this->dispatcher->setDefaultNamespace($defaultNamespace); |
66
|
|
|
$this->dispatcher->setNamespaceName($defaultNamespace); |
67
|
|
|
$this->view->setViewsDir($this->getViewsDir()); |
68
|
|
|
|
69
|
|
|
// url settings |
70
|
|
|
$this->url->setBasePath('/' . $this->name . '/'); |
71
|
|
|
$this->url->setStaticBaseUri('/' . $this->name . '/'); |
72
|
|
|
$this->router->setDefaults([ |
73
|
|
|
'namespace' => $defaultNamespace, |
74
|
|
|
'module' => $this->name, |
75
|
|
|
'controller' => 'index', |
76
|
|
|
'action' => 'index', |
77
|
|
|
]); |
78
|
|
|
|
79
|
|
|
// router settings |
80
|
|
|
$this->router->notFound([ |
81
|
|
|
'controller' => 'error', |
82
|
|
|
'action' => 'notFound', |
83
|
|
|
]); |
84
|
|
|
$this->router->removeExtraSlashes(true); |
85
|
|
|
|
86
|
|
|
$this->setServices($container); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function getServices(DiInterface $container): void |
90
|
|
|
{ |
91
|
|
|
$this->config ??= $container['config'] ??= new Config(); |
92
|
|
|
$this->loader ??= $container['loader'] ??= new Loader(); |
93
|
|
|
$this->router ??= $container['router'] ??= new Router(); |
94
|
|
|
$this->dispatcher ??= $container['dispatcher'] ??= new Dispatcher(); |
95
|
|
|
$this->view ??= $container['view'] ??= new View(); |
96
|
|
|
$this->url ??= $container['url'] ??= new Url(); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function setServices(DiInterface $container): void |
100
|
|
|
{ |
101
|
|
|
$container->set('config', $this->config); |
102
|
|
|
$container->set('dispatcher', $this->dispatcher); |
103
|
|
|
$container->set('loader', $this->loader); |
104
|
|
|
$container->set('router', $this->router); |
105
|
|
|
$container->set('view', $this->view); |
106
|
|
|
$container->set('url', $this->url); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function getNamespaces(): array |
110
|
|
|
{ |
111
|
|
|
// Caller namespace |
112
|
|
|
$namespace = $this->getNamespace(); |
113
|
|
|
$dirname = $this->getDirname(); |
114
|
|
|
|
115
|
|
|
// register the vendor module controllers |
116
|
|
|
$namespaces = []; |
117
|
|
|
$namespaces[$namespace . '\\Controllers'] = $dirname . '/Controllers/'; |
118
|
|
|
$namespaces[$namespace . '\\Models'] = $dirname . '/Models/'; |
119
|
|
|
|
120
|
|
|
// add zemit core models |
121
|
|
|
$corePath = dirname(__DIR__); |
122
|
|
|
$namespaces['Zemit\\Models'] = $corePath . '/Models/'; |
123
|
|
|
|
124
|
|
|
return $namespaces; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function getDefaultNamespace(): string |
128
|
|
|
{ |
129
|
|
|
return $this->getNamespace() . '\\Controllers'; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public function getViewsDir(): array |
133
|
|
|
{ |
134
|
|
|
return [$this->getDirname() . '/Views/']; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public function getDirname(): string |
138
|
|
|
{ |
139
|
|
|
return Utils::getDirname($this); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
public function getNamespace(): string |
143
|
|
|
{ |
144
|
|
|
return Utils::getNamespace($this); |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: