forked from steverhoades/ZFModuleLoadingExample
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModules.php
More file actions
161 lines (145 loc) · 5.06 KB
/
Copy pathModules.php
File metadata and controls
161 lines (145 loc) · 5.06 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<?php
/**
*
* @author Steve Rhoades
* @see http://www.stephenrhoades.com
*/
class Application_Resource_Modules extends Zend_Application_Resource_Modules
{
/**
* @var Zend_Controller_Front
*/
protected $_front;
/**
* @var Zend_Cache_Core
*/
protected $_cache;
/**
* @var Array
*/
protected $_modules;
public function init()
{
$bootstrap = $this->getBootstrap();
$bootstrap->bootstrap('frontcontroller');
$this->_front = $bootstrap->getResource('frontcontroller');
/*
* Uncommend the lines below to enable caching
*/
//$bootstrap->bootstrap('cachemanager');
//$this->_cache = $bootstrap->getResource('cachemanager')->getCache('default');
/*
* Get the list of modules
*/
$this->_modules = $this->_front->getControllerDirectory();
/*
* Load the module configurations
*/
$this->_loadModuleConfigs();
/*
* Bootstrap the Default module on every request
*/
$this->bootstrapModule('default');
/*
* Register instance of this class with the application module service. We
* need to retain this class due to the fact that the Zend_Application_Resource_ResourceAbstract class
* will create a new instance everytime a Zend_Application_Bootstrap_BootstrapAbstract::getPluginResource($type)
* instead of returning the first instantiated class.
*/
Application_Service_Module::setModuleLoader($this);
return $this->_bootstraps;
}
/**
* Bootstrap Modules on demand.
*
* @param String $module Name of the module to bootstrap
* @throws Zend_Application_Resource_Exception
*/
public function bootstrapModule($module)
{
if(false === array_key_exists($module,$this->_bootstraps)) {
$moduleDirectory = $this->_modules[strtolower($module)];
$bootstrapClass = $this->_formatModuleName($module) . '_Bootstrap';
if (!class_exists($bootstrapClass, false)) {
$bootstrapPath = dirname($moduleDirectory) . '/Bootstrap.php';
if (file_exists($bootstrapPath)) {
$eMsgTpl = 'Bootstrap file found for module "%s" but bootstrap class "%s" not found';
include_once $bootstrapPath;
if (('Default' != $module)
&& !class_exists($bootstrapClass, false)
) {
throw new Zend_Application_Resource_Exception(sprintf(
$eMsgTpl, $module, $bootstrapClass
));
} elseif ('Default' == $module) {
if (!class_exists($bootstrapClass, false)) {
$bootstrapClass = 'Bootstrap';
if (!class_exists($bootstrapClass, false)) {
throw new Zend_Application_Resource_Exception(sprintf(
$eMsgTpl, $module, $bootstrapClass
));
}
}
}
}
}
$moduleBootstrap = new $bootstrapClass($this->getBootstrap());
$moduleBootstrap->bootstrap();
$this->_bootstraps[$module] = $moduleBootstrap;
}
}
/**
* Load all module.ini files in the module's configs directory
*
* @return void
*/
final private function _loadModuleConfigs()
{
$ds = DIRECTORY_SEPARATOR;
$modules = array_keys($this->_modules);
/*
* Uncomment the lines at 106, 145, 146 to enable caching
*
*/
//if(false === ($appOptions = $this->_cache->load('module_configurations'))) {
$appOptions = $this->getBootstrap()->getOptions();
foreach ($modules as $module) {
$filename = $this->_front->getModuleDirectory($module) . $ds . 'configs'. $ds . 'module.ini';
if(file_exists($filename)) {
$cfg = new Zend_Config_Ini($filename, $this->getBootstrap()
->getEnvironment());
$options = $cfg->toArray();
if (empty($options)) {
continue;
}
if (array_key_exists($module, $appOptions) && is_array($appOptions[$module])) {
foreach ($options as $key => $value) {
if (array_key_exists($key, $appOptions[$module]) && is_array($appOptions[$module][$key])) {
$appOptions[$module][$key] = array_merge($appOptions[$module][$key], $value);
} else {
$appOptions[$module][$key] = $value;
}
}
} else {
$appOptions[$module] = $options;
}
}
}
//$this->_cache->save($appOptions, 'module_configurations', array(), 9200);
//}
/*
* This block checks the appOptions for routes defined in the module.ini and will
* create a Zend_Config object for each to configure routing properly for Rest and
* other custom routes per module.
*/
$router = $this->_front->getRouter();
foreach($appOptions as $module => $options) {
if(in_array($module, $modules) && isset($options['routes'])) {
$routeName = sprintf("routes_%s", $module);
$config = new Zend_Config(array($routeName => $options['routes']));
$router->addConfig($config, $routeName);
}
}
$this->getBootstrap()->setOptions($appOptions);
}
}