1
|
|
|
<?php |
2
|
|
|
namespace Agavi\Config; |
3
|
|
|
|
4
|
|
|
// +---------------------------------------------------------------------------+ |
5
|
|
|
// | This file is part of the Agavi package. | |
6
|
|
|
// | Copyright (c) 2005-2011 the Agavi Project. | |
7
|
|
|
// | Based on the Mojavi3 MVC Framework, Copyright (c) 2003-2005 Sean Kerr. | |
8
|
|
|
// | | |
9
|
|
|
// | For the full copyright and license information, please view the LICENSE | |
10
|
|
|
// | file that was distributed with this source code. You can also view the | |
11
|
|
|
// | LICENSE file online at http://www.agavi.org/LICENSE.txt | |
12
|
|
|
// | vi: set noexpandtab: | |
13
|
|
|
// | Local Variables: | |
14
|
|
|
// | indent-tabs-mode: t | |
15
|
|
|
// | End: | |
16
|
|
|
// +---------------------------------------------------------------------------+ |
17
|
|
|
|
18
|
|
|
|
19
|
|
|
use Agavi\Config\Util\Dom\XmlConfigDomElement; |
20
|
|
|
use Agavi\Util\Toolkit; |
21
|
|
|
use Agavi\Config\Util\Dom\XmlConfigDomDocument; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* ModuleConfigHandler reads module configuration files to determine the |
25
|
|
|
* status of a module. |
26
|
|
|
* |
27
|
|
|
* @package agavi |
28
|
|
|
* @subpackage config |
29
|
|
|
* |
30
|
|
|
* @author David Zülke <[email protected]> |
31
|
|
|
* @copyright Authors |
32
|
|
|
* @copyright The Agavi Project |
33
|
|
|
* |
34
|
|
|
* @since 0.9.0 |
35
|
|
|
* |
36
|
|
|
* @version $Id$ |
37
|
|
|
*/ |
38
|
|
|
class ModuleConfigHandler extends XmlConfigHandler |
39
|
|
|
{ |
40
|
|
|
const XML_NAMESPACE = 'http://agavi.org/agavi/config/parts/module/1.1'; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Execute this configuration handler. |
44
|
|
|
* |
45
|
|
|
* @param XmlConfigDomDocument $document The document to parse. |
46
|
|
|
* |
47
|
|
|
* @return string Data to be written to a cache file. |
48
|
|
|
* |
49
|
|
|
* @throws <b>ParseException</b> If a requested configuration file is |
50
|
|
|
* improperly formatted. |
51
|
|
|
* |
52
|
|
|
* @author David Zülke <[email protected]> |
53
|
|
|
* @since 0.9.0 |
54
|
|
|
*/ |
55
|
|
|
public function execute(XmlConfigDomDocument $document) |
56
|
|
|
{ |
57
|
|
|
// set up our default namespace |
58
|
|
|
$document->setDefaultNamespace(self::XML_NAMESPACE, 'module'); |
59
|
|
|
|
60
|
|
|
// remember the config file path |
61
|
|
|
$config = $document->documentURI; |
62
|
|
|
|
63
|
|
|
$enabled = false; |
64
|
|
|
$prefix = 'modules.${moduleName}.'; |
65
|
|
|
$data = array(); |
66
|
|
|
|
67
|
|
|
// loop over <configuration> elements |
68
|
|
|
foreach ($document->getConfigurationElements() as $configuration) { |
69
|
|
|
$module = $configuration->getChild('module'); |
70
|
|
|
if (!$module) { |
71
|
|
|
continue; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
// enabled flag is treated separately |
75
|
|
|
$enabled = (bool) Toolkit::literalize($module->getAttribute('enabled')); |
76
|
|
|
|
77
|
|
|
// loop over <setting> elements; there can be many of them |
78
|
|
|
/** @var XmlConfigDomElement $setting */ |
79
|
|
View Code Duplication |
foreach ($module->get('settings') as $setting) { |
|
|
|
|
80
|
|
|
$localPrefix = $prefix; |
81
|
|
|
|
82
|
|
|
// let's see if this buddy has a <settings> parent with valuable information |
83
|
|
|
if ($setting->parentNode->localName == 'settings') { |
84
|
|
|
if ($setting->parentNode->hasAttribute('prefix')) { |
85
|
|
|
$localPrefix = $setting->parentNode->getAttribute('prefix'); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$settingName = $localPrefix . $setting->getAttribute('name'); |
90
|
|
|
if ($setting->hasAgaviParameters()) { |
91
|
|
|
$data[$settingName] = $setting->getAgaviParameters(); |
92
|
|
|
} else { |
93
|
|
|
$data[$settingName] = Toolkit::literalize($setting->getValue()); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
$code = array(); |
99
|
|
|
$code[] = '$lcModuleName = strtolower($moduleName);'; |
100
|
|
|
$code[] = 'Agavi\\Config\\Config::set(Agavi\\Util\\Toolkit::expandVariables(' . var_export($prefix . 'enabled', true) . ', array(\'moduleName\' => $lcModuleName)), ' . var_export($enabled, true) . ', true, true);'; |
101
|
|
|
if (count($data)) { |
102
|
|
|
$code[] = '$moduleConfig = ' . var_export($data, true) . ';'; |
103
|
|
|
$code[] = '$moduleConfigKeys = array_keys($moduleConfig);'; |
104
|
|
|
$code[] = 'foreach($moduleConfigKeys as &$value) $value = Agavi\\Util\\Toolkit::expandVariables($value, array(\'moduleName\' => $lcModuleName));'; |
105
|
|
|
$code[] = '$moduleConfig = array_combine($moduleConfigKeys, $moduleConfig);'; |
106
|
|
|
$code[] = 'Agavi\\Config\\Config::fromArray($moduleConfig);'; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return $this->generate($code, $config); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.