View Source

<ac:macro ac:name="unmigrated-inline-wiki-markup"><ac:plain-text-body><![CDATA[{zone-template-instance:ZFPROP:Proposal Zone Template}

{zone-data:component-name}
Zend_Config_Ini_Expandable
{zone-data}

{zone-data:proposer-list}
[Douglas Hammond|mailto:wizhippo@gmail.com]
{zone-data}

{zone-data:liaison}
TBD
{zone-data}

{zone-data:revision}
1.0 - 1 January 2008: Initial Draft.
{zone-data}

{zone-data:overview}
Extend Zend_Config_Ini to support variable expansion.
{zone-data}

{zone-data:references}
{zone-data}

{zone-data:requirements}
All values in ini file to be expanded from previously defined values.

* This component *will* expand variables defined previously in the ini file.
* This component *will* expand variables passed as array to constructor.
{zone-data}

{zone-data:dependencies}
* Zend_Config_Ini
{zone-data}

{zone-data:operation}
The component is instantiated with a mind-link that ...
{zone-data}

{zone-data:milestones}
* Milestone 1: Look at current Zend_Config_Ini to see how to add functionality [DONE]
* Milestone 2: Working prototype checked into the incubator supporting use cases #1
* Milestone 4: Unit tests exist, work, and are checked into SVN.
* Milestone 5: Initial documentation.
{zone-data}

{zone-data:class-list}
* Zend_Config_Ini_Expandable
{zone-data}

{zone-data:use-cases}
||UC-01||
{code}
config.ini
; variables passed in by application environment startup
; ${application.rootDir}, ${application.configDir}, ${application.libraryDir},
; ${application.dataDir}

layout.layoutPath = "${application.rootDir}/application/layouts"


test.php

$rootDir = realpath(dirname(__FILE__) . DIRECTORY_SEPARATOR . '..');
$configDir = realpath($rootDir . DIRECTORY_SEPARATOR . 'config');
$libraryDir = realpath($rootDir . DIRECTORY_SEPARATOR . 'library');
$dataDir = realpath($rootDir . DIRECTORY_SEPARATOR . 'data');

$config = new Zend_Config_Ini_Expandable(
$configDir . DIRECTORY_SEPARATOR . 'config.ini',
$configSection,
true,
array( 'application' =>
array(
'rootDir' => $rootDir,
'configDir' => $configDir,
'libraryDir' => $libraryDir,
'dataDir' => $dataDir
)
)
);

echo $config->application->layoutPath;
{code}
{zone-data}

{zone-data:skeletons}
{code}
class Zend_Config_Ini_Expandable extends Zend_Config_Ini
{

public function __construct($filename, $section = null, $options = false, array $array = array())
{
parent::__construct($filename, $section, $options);
$this->merge(new Zend_Config($array, true));
$this->_postProcessValues($this);
}

protected function _postProcessValues($config)
{
foreach ($config as $key => $value) {
if (is_a($value, "Zend_Config")) {
$this->_postProcessValues($value);
} else {
$config->__set($key, preg_replace_callback("|\\\${(.*)}|",
array($this, '_postProcessValuesCallback'), $value));
}
}
}

protected function _postProcessValuesCallback($matches)
{
if (isset($matches[1])) {
$path = explode($this->_nestSeparator, $matches[1]);

$result = $this;
foreach($path as $value) {
if(!isset($result->$value)) {
return $matches[0];
}
$result = $result->$value;
}
return $result;
}
return $matches[0];
}
}

{code}
{zone-data}

{zone-template-instance}]]></ac:plain-text-body></ac:macro>