|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @link https://github.com/nnx-framework/container |
|
4
|
|
|
* @author Malofeykin Andrey <[email protected]> |
|
5
|
|
|
*/ |
|
6
|
|
|
namespace Nnx\Container; |
|
7
|
|
|
|
|
8
|
|
|
use Nnx\ModuleOptions\ModuleConfigKeyProviderInterface; |
|
9
|
|
|
use Zend\ModuleManager\Feature\ConfigProviderInterface; |
|
10
|
|
|
use Zend\ModuleManager\Listener\ServiceListenerInterface; |
|
11
|
|
|
use Zend\ModuleManager\ModuleManager; |
|
12
|
|
|
use Zend\ModuleManager\ModuleManagerInterface; |
|
13
|
|
|
use Zend\Mvc\ModuleRouteListener; |
|
14
|
|
|
use Zend\Mvc\MvcEvent; |
|
15
|
|
|
use Zend\EventManager\EventInterface; |
|
16
|
|
|
use Zend\ModuleManager\Feature\AutoloaderProviderInterface; |
|
17
|
|
|
use Zend\ModuleManager\Feature\BootstrapListenerInterface; |
|
18
|
|
|
use Zend\ModuleManager\Feature\InitProviderInterface; |
|
19
|
|
|
use Zend\ServiceManager\ServiceLocatorInterface; |
|
20
|
|
|
|
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Class Module |
|
24
|
|
|
* |
|
25
|
|
|
* @package Nnx\ContainerOptions |
|
26
|
|
|
*/ |
|
27
|
|
|
class Module implements |
|
28
|
|
|
BootstrapListenerInterface, |
|
29
|
|
|
AutoloaderProviderInterface, |
|
30
|
|
|
InitProviderInterface, |
|
31
|
|
|
ConfigProviderInterface, |
|
32
|
|
|
ModuleConfigKeyProviderInterface |
|
33
|
|
|
{ |
|
34
|
|
|
/** |
|
35
|
|
|
* Имя секции в конфиги приложения отвечающей за настройки модуля |
|
36
|
|
|
* |
|
37
|
|
|
* @var string |
|
38
|
|
|
*/ |
|
39
|
|
|
const CONFIG_KEY = 'nnx_container_module_options'; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Имя модуля |
|
43
|
|
|
* |
|
44
|
|
|
* @var string |
|
45
|
|
|
*/ |
|
46
|
|
|
const MODULE_NAME = __NAMESPACE__; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @inheritdoc |
|
50
|
|
|
* |
|
51
|
|
|
* @return string |
|
52
|
|
|
*/ |
|
53
|
|
|
public function getModuleConfigKey() |
|
54
|
|
|
{ |
|
55
|
|
|
return static::CONFIG_KEY; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param ModuleManagerInterface $manager |
|
61
|
|
|
* |
|
62
|
|
|
* @throws Exception\InvalidArgumentException |
|
63
|
|
|
* @throws \Zend\ServiceManager\Exception\ServiceNotFoundException |
|
64
|
|
|
*/ |
|
65
|
|
|
public function init(ModuleManagerInterface $manager) |
|
66
|
|
|
{ |
|
67
|
|
|
if (!$manager instanceof ModuleManager) { |
|
68
|
|
|
$errMsg =sprintf('Module manager not implement %s', ModuleManager::class); |
|
69
|
|
|
throw new Exception\InvalidArgumentException($errMsg); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** @var ServiceLocatorInterface $sm */ |
|
73
|
|
|
$sm = $manager->getEvent()->getParam('ServiceManager'); |
|
74
|
|
|
|
|
75
|
|
|
if (!$sm instanceof ServiceLocatorInterface) { |
|
76
|
|
|
$errMsg = sprintf('Service locator not implement %s', ServiceLocatorInterface::class); |
|
77
|
|
|
throw new Exception\InvalidArgumentException($errMsg); |
|
78
|
|
|
} |
|
79
|
|
|
/** @var ServiceListenerInterface $serviceListener */ |
|
80
|
|
|
$serviceListener = $sm->get('ServiceListener'); |
|
81
|
|
|
if (!$serviceListener instanceof ServiceListenerInterface) { |
|
82
|
|
|
$errMsg = sprintf('ServiceListener not implement %s', ServiceListenerInterface::class); |
|
83
|
|
|
throw new Exception\InvalidArgumentException($errMsg); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
$serviceListener->addServiceManager( |
|
87
|
|
|
ContainerInterface::class, |
|
88
|
|
|
Container::CONFIG_KEY, |
|
89
|
|
|
ContainerProviderInterface::class, |
|
90
|
|
|
'getContainerConfig' |
|
91
|
|
|
); |
|
92
|
|
|
|
|
93
|
|
|
|
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @inheritdoc |
|
98
|
|
|
* |
|
99
|
|
|
* @param EventInterface $e |
|
100
|
|
|
* |
|
101
|
|
|
* @return array|void |
|
102
|
|
|
* |
|
103
|
|
|
* @throws \Zend\ServiceManager\Exception\ServiceNotFoundException |
|
104
|
|
|
*/ |
|
105
|
|
|
public function onBootstrap(EventInterface $e) |
|
106
|
|
|
{ |
|
107
|
|
|
/** @var MvcEvent $e */ |
|
108
|
|
|
$eventManager = $e->getApplication()->getEventManager(); |
|
109
|
|
|
$moduleRouteListener = new ModuleRouteListener(); |
|
110
|
|
|
$moduleRouteListener->attach($eventManager); |
|
111
|
|
|
|
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @return array |
|
117
|
|
|
*/ |
|
118
|
|
|
public function getAutoloaderConfig() |
|
119
|
|
|
{ |
|
120
|
|
|
return [ |
|
121
|
|
|
'Zend\Loader\StandardAutoloader' => [ |
|
122
|
|
|
'namespaces' => [ |
|
123
|
|
|
__NAMESPACE__ => __DIR__ . '/src/', |
|
124
|
|
|
], |
|
125
|
|
|
], |
|
126
|
|
|
]; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* @return mixed |
|
132
|
|
|
*/ |
|
133
|
|
|
public function getConfig() |
|
134
|
|
|
{ |
|
135
|
|
|
return include __DIR__ . '/config/module.config.php'; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
} |