1 | <?php |
---|
2 | |
---|
3 | /** |
---|
4 | * This file is part of the OpenPNE package. |
---|
5 | * (c) OpenPNE Project (http://www.openpne.jp/) |
---|
6 | * |
---|
7 | * For the full copyright and license information, please view the LICENSE |
---|
8 | * file and the NOTICE file that were distributed with this source code. |
---|
9 | */ |
---|
10 | |
---|
11 | /** |
---|
12 | * sfOpenPNEApplicationConfiguration represents a configuration for OpenPNE application. |
---|
13 | * |
---|
14 | * @package OpenPNE |
---|
15 | * @subpackage config |
---|
16 | * @author Kousuke Ebihara <ebihara@tejimaya.com> |
---|
17 | */ |
---|
18 | abstract class sfOpenPNEApplicationConfiguration extends sfApplicationConfiguration |
---|
19 | { |
---|
20 | static protected $zendLoaded = false; |
---|
21 | |
---|
22 | public function initialize() |
---|
23 | { |
---|
24 | mb_internal_encoding('UTF-8'); |
---|
25 | |
---|
26 | require sfConfig::get('sf_data_dir').'/version.php'; |
---|
27 | |
---|
28 | $this->dispatcher->connect('task.cache.clear', array($this, 'clearPluginCache')); |
---|
29 | $this->dispatcher->connect('template.filter_parameters', array($this, 'filterTemplateParameters')); |
---|
30 | |
---|
31 | $this->setConfigHandlers(); |
---|
32 | } |
---|
33 | |
---|
34 | public function setup() |
---|
35 | { |
---|
36 | $DS = DIRECTORY_SEPARATOR; |
---|
37 | $OpenPNE2Path = sfConfig::get('sf_lib_dir').$DS.'vendor'.$DS; // ##PROJECT_LIB_DIR##/vendor/ |
---|
38 | set_include_path($OpenPNE2Path.PATH_SEPARATOR.get_include_path()); |
---|
39 | |
---|
40 | $result = parent::setup(); |
---|
41 | $configCache = $this->getConfigCache(); |
---|
42 | $file = $configCache->checkConfig('data/config/plugin.yml', true); |
---|
43 | if ($file) |
---|
44 | { |
---|
45 | include($file); |
---|
46 | } |
---|
47 | |
---|
48 | if (sfConfig::get('op_plugin_activation')) |
---|
49 | { |
---|
50 | $pluginActivations = array_merge(array_fill_keys($this->getPlugins(), true), sfConfig::get('op_plugin_activation')); |
---|
51 | foreach ($pluginActivations as $key => $value) |
---|
52 | { |
---|
53 | if (!in_array($key, $this->getPlugins())) |
---|
54 | { |
---|
55 | unset($pluginActivations[$key]); |
---|
56 | } |
---|
57 | } |
---|
58 | $this->enablePlugins(array_keys($pluginActivations, true)); |
---|
59 | $this->disablePlugins(array_keys($pluginActivations, false)); |
---|
60 | } |
---|
61 | |
---|
62 | // gadget |
---|
63 | include($this->getConfigCache()->checkConfig('config/gadget_layout_config.yml')); |
---|
64 | include($this->getConfigCache()->checkConfig('config/gadget_config.yml')); |
---|
65 | |
---|
66 | require_once sfConfig::get('sf_lib_dir').'/config/opGadgetConfigHandler.class.php'; |
---|
67 | $gadgetConfigs = sfConfig::get('op_gadget_config', array()); |
---|
68 | foreach ($gadgetConfigs as $key => $config) |
---|
69 | { |
---|
70 | $filename = 'config/'.sfInflector::underscore($key); |
---|
71 | $params = array(); |
---|
72 | if ($key != 'gadget') |
---|
73 | { |
---|
74 | $filename .= '_gadget'; |
---|
75 | $params['prefix'] = sfInflector::underscore($key).'_'; |
---|
76 | } |
---|
77 | $filename .= '.yml'; |
---|
78 | $this->getConfigCache()->registerConfigHandler($filename, 'opGadgetConfigHandler', $params); |
---|
79 | include($this->getConfigCache()->checkConfig($filename)); |
---|
80 | } |
---|
81 | |
---|
82 | return $result; |
---|
83 | } |
---|
84 | |
---|
85 | public function getAllPlugins() |
---|
86 | { |
---|
87 | return array_keys($this->getAllPluginPaths()); |
---|
88 | } |
---|
89 | |
---|
90 | public function getAllOpenPNEPlugins() |
---|
91 | { |
---|
92 | $list = $this->getAllPlugins(); |
---|
93 | $result = array(); |
---|
94 | |
---|
95 | foreach ($list as $value) |
---|
96 | { |
---|
97 | if (!strncmp($value, 'op', 2)) |
---|
98 | { |
---|
99 | $result[] = $value; |
---|
100 | } |
---|
101 | } |
---|
102 | |
---|
103 | return $result; |
---|
104 | } |
---|
105 | |
---|
106 | public function getEnabledAuthPlugin() |
---|
107 | { |
---|
108 | $list = $this->getPlugins(); |
---|
109 | $result = array(); |
---|
110 | |
---|
111 | foreach ($list as $value) |
---|
112 | { |
---|
113 | if (!strncmp($value, 'opAuth', 6)) |
---|
114 | { |
---|
115 | $result[] = $value; |
---|
116 | } |
---|
117 | } |
---|
118 | |
---|
119 | return $result; |
---|
120 | } |
---|
121 | |
---|
122 | public function isPluginExists($pluginName) |
---|
123 | { |
---|
124 | return in_array($pluginName, $this->getAllPlugins()); |
---|
125 | } |
---|
126 | |
---|
127 | public function isEnabledPlugin($pluginName) |
---|
128 | { |
---|
129 | return in_array($pluginName, $this->getPlugins()); |
---|
130 | } |
---|
131 | |
---|
132 | public function isDisabledPlugin($pluginName) |
---|
133 | { |
---|
134 | return (bool)(!$this->isEnabledPlugin($pluginName) && in_array($pluginName, $this->getAllPlugins())); |
---|
135 | } |
---|
136 | |
---|
137 | public function clearPluginCache($params = array()) |
---|
138 | { |
---|
139 | $appConfiguration = $params['app']; |
---|
140 | $environment = $params['env']; |
---|
141 | $subDir = sfConfig::get('sf_cache_dir').'/'.$appConfiguration->getApplication().'/'.$environment.'/plugins'; |
---|
142 | |
---|
143 | if (is_dir($subDir)) |
---|
144 | { |
---|
145 | $filesystem = new sfFilesystem(); |
---|
146 | $filesystem->remove(sfFinder::type('any')->discard('.sf')->in($subDir)); |
---|
147 | } |
---|
148 | } |
---|
149 | |
---|
150 | /** |
---|
151 | * @see sfApplicationConfiguration |
---|
152 | */ |
---|
153 | public function getConfigCache() |
---|
154 | { |
---|
155 | if (is_null($this->configCache)) |
---|
156 | { |
---|
157 | require_once sfConfig::get('sf_lib_dir').'/config/opConfigCache.class.php'; |
---|
158 | $this->configCache = new opConfigCache($this); |
---|
159 | } |
---|
160 | |
---|
161 | return $this->configCache; |
---|
162 | } |
---|
163 | |
---|
164 | /** |
---|
165 | * Listens to the template.filter_parameters event. |
---|
166 | * |
---|
167 | * @param sfEvent $event An sfEvent instance |
---|
168 | * @param array $parameters An array of template parameters to filter |
---|
169 | * |
---|
170 | * @return array The filtered parameters array |
---|
171 | */ |
---|
172 | public function filterTemplateParameters(sfEvent $event, $parameters) |
---|
173 | { |
---|
174 | $parameters['op_config'] = new opConfig(); |
---|
175 | sfOutputEscaper::markClassAsSafe('opConfig'); |
---|
176 | return $parameters; |
---|
177 | } |
---|
178 | |
---|
179 | /** |
---|
180 | * Gets directories where controller classes are stored for a given module. |
---|
181 | * |
---|
182 | * @param string $moduleName The module name |
---|
183 | * |
---|
184 | * @return array An array of directories |
---|
185 | */ |
---|
186 | public function getControllerDirs($moduleName) |
---|
187 | { |
---|
188 | $dirs = array(); |
---|
189 | |
---|
190 | $dirs = array_merge($dirs, $this->globEnablePlugin('/apps/'.sfConfig::get('sf_app').'/modules/'.$moduleName.'/actions', true)); |
---|
191 | $dirs = array_merge($dirs, parent::getControllerDirs($moduleName)); |
---|
192 | |
---|
193 | return $dirs; |
---|
194 | } |
---|
195 | |
---|
196 | /** |
---|
197 | * Gets directories where template files are stored for a given module. |
---|
198 | * |
---|
199 | * @param string $moduleName The module name |
---|
200 | * |
---|
201 | * @return array An array of directories |
---|
202 | */ |
---|
203 | public function getTemplateDirs($moduleName) |
---|
204 | { |
---|
205 | $dirs = array(); |
---|
206 | |
---|
207 | $dirs = array_merge($dirs, $this->globEnablePlugin('/apps/'.sfConfig::get('sf_app').'/templates')); |
---|
208 | $dirs = array_merge($dirs, $this->globEnablePlugin('/apps/'.sfConfig::get('sf_app').'/modules/'.$moduleName.'/templates')); |
---|
209 | $dirs = array_merge($dirs, parent::getTemplateDirs($moduleName)); |
---|
210 | |
---|
211 | return $dirs; |
---|
212 | } |
---|
213 | |
---|
214 | /** |
---|
215 | * Gets the decorator directories. |
---|
216 | * |
---|
217 | * @return array An array of the decorator directories |
---|
218 | */ |
---|
219 | public function getDecoratorDirs() |
---|
220 | { |
---|
221 | $dirs = array(); |
---|
222 | |
---|
223 | $dirs = array_merge($dirs, $this->globEnablePlugin('/apps/'.sfConfig::get('sf_app').'/templates')); |
---|
224 | $dirs = array_merge($dirs, parent::getDecoratorDirs()); |
---|
225 | |
---|
226 | return $dirs; |
---|
227 | } |
---|
228 | |
---|
229 | /** |
---|
230 | * Gets the i18n directories to use for a given module. |
---|
231 | * |
---|
232 | * @param string $moduleName The module name |
---|
233 | * |
---|
234 | * @return array An array of i18n directories |
---|
235 | */ |
---|
236 | public function getI18NDirs($moduleName) |
---|
237 | { |
---|
238 | $dirs = array(); |
---|
239 | |
---|
240 | $dirs = array_merge($dirs, $this->globEnablePlugin('/apps/'.sfConfig::get('sf_app').'/i18n')); |
---|
241 | $dirs = array_merge($dirs, $this->globEnablePlugin('/apps/'.sfConfig::get('sf_app').'/modules/'.$moduleName.'/i18n')); |
---|
242 | $dirs = array_merge($dirs, parent::getI18NDirs($moduleName)); |
---|
243 | |
---|
244 | return $dirs; |
---|
245 | } |
---|
246 | |
---|
247 | /** |
---|
248 | * Gets the configuration file paths for a given relative configuration path. |
---|
249 | * |
---|
250 | * @param string $configPath The configuration path |
---|
251 | * |
---|
252 | * @return array An array of paths |
---|
253 | */ |
---|
254 | public function getConfigPaths($configPath) |
---|
255 | { |
---|
256 | $files = array(); |
---|
257 | |
---|
258 | if ($libDirs = glob(sfConfig::get('sf_lib_dir').'/config/'.$configPath)) { |
---|
259 | $files = array_merge($files, $libDirs); // library configurations |
---|
260 | } |
---|
261 | |
---|
262 | $files = array_merge($files, $this->globEnablePlugin($configPath)); |
---|
263 | $files = array_merge($files, $this->globEnablePlugin('/apps/'.sfConfig::get('sf_app').'/'.$configPath)); |
---|
264 | |
---|
265 | $configs = array(); |
---|
266 | foreach (array_unique($files) as $file) |
---|
267 | { |
---|
268 | if (is_readable($file)) |
---|
269 | { |
---|
270 | $configs[] = $file; |
---|
271 | } |
---|
272 | } |
---|
273 | |
---|
274 | $configs = array_merge(parent::getConfigPaths($configPath), $configs); |
---|
275 | return $configs; |
---|
276 | } |
---|
277 | |
---|
278 | public function globEnablePlugin($pattern, $isControllerPath = false) |
---|
279 | { |
---|
280 | $dirs = array(); |
---|
281 | $pluginPaths = $this->getPluginPaths(); |
---|
282 | |
---|
283 | foreach ($pluginPaths as $pluginPath) |
---|
284 | { |
---|
285 | if ($pluginDirs = glob($pluginPath.$pattern)) |
---|
286 | { |
---|
287 | if ($isControllerPath) |
---|
288 | { |
---|
289 | $dirs = array_merge($dirs, array_combine($pluginDirs, array_fill(0, count($pluginDirs), false))); |
---|
290 | } |
---|
291 | else |
---|
292 | { |
---|
293 | $dirs = array_merge($dirs, $pluginDirs); |
---|
294 | } |
---|
295 | } |
---|
296 | } |
---|
297 | |
---|
298 | return $dirs; |
---|
299 | } |
---|
300 | |
---|
301 | public function getGlobalTemplateDir($templateFile) |
---|
302 | { |
---|
303 | foreach ($this->getGlobalTemplateDirs() as $dir) |
---|
304 | { |
---|
305 | if (is_readable($dir.'/'.$templateFile)) |
---|
306 | { |
---|
307 | return $dir; |
---|
308 | } |
---|
309 | } |
---|
310 | |
---|
311 | return null; |
---|
312 | } |
---|
313 | |
---|
314 | public function getGlobalTemplateDirs() |
---|
315 | { |
---|
316 | $dirs = array(); |
---|
317 | $dirs[] = sfConfig::get('sf_root_dir').'/templates'; |
---|
318 | $dirs = array_merge($dirs, $this->getPluginSubPaths('/templates')); |
---|
319 | return $dirs; |
---|
320 | } |
---|
321 | |
---|
322 | protected function setConfigHandlers() |
---|
323 | { |
---|
324 | $this->getConfigCache()->registerConfigHandler('config/sns_config.yml', 'opConfigConfigHandler', array('prefix' => 'openpne_sns_')); |
---|
325 | include($this->getConfigCache()->checkConfig('config/sns_config.yml')); |
---|
326 | |
---|
327 | $this->getConfigCache()->registerConfigHandler('config/member_config.yml', 'opConfigConfigHandler', array('prefix' => 'openpne_member_')); |
---|
328 | include($this->getConfigCache()->checkConfig('config/member_config.yml')); |
---|
329 | |
---|
330 | $this->getConfigCache()->registerConfigHandler('config/community_config.yml', 'opConfigConfigHandler', array('prefix' => 'openpne_community_')); |
---|
331 | include($this->getConfigCache()->checkConfig('config/community_config.yml')); |
---|
332 | } |
---|
333 | |
---|
334 | static public function registerZend() |
---|
335 | { |
---|
336 | if (self::$zendLoaded) |
---|
337 | { |
---|
338 | return true; |
---|
339 | } |
---|
340 | |
---|
341 | $DS = DIRECTORY_SEPARATOR; // Alias |
---|
342 | $zendPath = sfConfig::get('sf_lib_dir').$DS.'vendor'.$DS; // ##PROJECT_LIB_DIR##/vendor/ |
---|
343 | |
---|
344 | set_include_path($zendPath.PATH_SEPARATOR.get_include_path()); |
---|
345 | require_once 'Zend/Loader.php'; |
---|
346 | Zend_Loader::registerAutoLoad(); |
---|
347 | self::$zendLoaded = true; |
---|
348 | } |
---|
349 | |
---|
350 | static public function registerJanRainOpenID() |
---|
351 | { |
---|
352 | $DS = DIRECTORY_SEPARATOR; |
---|
353 | $openidPath = sfConfig::get('sf_lib_dir').$DS.'vendor'.$DS.'php-openid'.$DS; // ##PROJECT_LIB_DIR##/vendor/php-openid/ |
---|
354 | set_include_path($openidPath.PATH_SEPARATOR.get_include_path()); |
---|
355 | |
---|
356 | require_once 'Auth/OpenID/Consumer.php'; |
---|
357 | require_once 'Auth/OpenID/FileStore.php'; |
---|
358 | require_once 'Auth/OpenID/SReg.php'; |
---|
359 | } |
---|
360 | } |
---|