2

I've got a common set of functionality I'd like to embed into a Zend_Application instance using the configs parameter inside of that app's Zend_Config instance. However, the slaved configuration file would like to be able to refer to things in a path relative to itself. For instance:

$/application/configs/application.ini:

[base]
config[] = APPLICATION_PATH "../CasCommon/Configs/common.ini

$/CasCommon/Configs/common.ini

[base]
resources.frontController.controllerDirectory[] = PATH_TO_THIS_IN_DIR "../Controllers"
resources.frontController.actionHelperPaths.Cas_Common_Helper = PATH_TO_THIS_IN_DIR "../ControllerHelpers"
;...

How might one accomplish such a thing?

Billy ONeal
  • 104,103
  • 58
  • 317
  • 552

2 Answers2

3

PHP supports constants in Ini files, but unfortunately not magic constants, so you cannot use __DIR__, which would solve the problem. The easiest and most obvious thing would be defining the path to the application.ini file as a constant just like you did with APPLICATION_PATH, e.g.

// application.ini
foo = INI_PATH '/../somewhere/else'

// index.php
const INI_PATH = '/path/to/config/folder';

Then just load your Zend_Application regularly or instantiate a new Zend_Config and the constant will be evaluated like you wanted.

Edit after comments

I find that argument about the above being not automagic enough moot. In a standard ZF project, the APPLICATION_PATH is defined in the index.php file and that's also where the default application.ini is loaded. All you have to do is add the constant there. The Ini file won't exist on it's own anyway so someone will have to call an external library at some point (likely you as the developer). The above solution requires one line of setup. Any other solution requires more work.

If that is not good enough for you, you can extend Zend_Application to automatically add that constant before the application.ini gets loaded:

class My_Zend_Application extends Zend_Application
{
    protected function _loadConfig($file)
    {
        if (!defined('PATH_TO_INI')) {
            define('PATH_TO_INI', dirname(realpath($file)));
        }
        return parent::_loadConfig($file);
    }
}

Of course, you will still have to change index.php to use your extended My_Zend_Application then which is why I find this approach rather pointless given that you can also just add the constant in the index.php file.

A custom Zend_Application will limit you to the application.ini of course because you cannot change the constant at runtime anymore. So if you need this functionality for multiple Ini files and not just the application.ini, extend Zend_Config_Ini and check each value for a Relative Path marker before it is returned, e.g.

class My_Config_Ini extends Zend_Config_Ini
{
    protected $_relativePath;
    protected $_relativePathMarker = '%REL_PATH%';
    public function __construct($filename, $section = null, $options = false)
    {
        $this->_relativePath = dirname(realpath($filename));
        parent::__construct($filename, $section, $options);
    }
    public function get($name, $default = null)
    {
        if (array_key_exists($name, $this->_data)) {
            return $this->_containsRelativePathMarker($this->_data[$name])
                ? $this->_expandRelativePath($this->_data[$name])
                : $this->_data[$name];
        }
        return $default;
    }
    protected function _containsRelativePathMarker($value)
    {
        return strpos($value, $this->_relativePathMarker) !== FALSE;
    }
    protected function _expandRelativePath($value)
    {
        return str_replace('%REL_PATH%', $this->_relativePath, $value);
    }
}

The above assumes you write your Ini files with something like

foo = %REL_PATH% '/../foo.txt'

If that is still not what you are looking I can only encourage you once more to put up precise requirements. There is no point in offering 500 reputation when you are not going to accept any answers here because we failed to read your mind.

Gordon
  • 312,688
  • 75
  • 539
  • 559
  • Yuck. I guess it works as a workaround. Perhaps I'll build a Zend_Application_Resource that will do it... :/ – Billy ONeal Nov 28 '11 at 18:01
  • Why Yuck? This is clean and simple solution. Given that you are already using `APPLICATION_PATH` why invent some additional way to achieve this over using the obvious `PATH_TO_THIS_IN_DIR` as another constant? – Gordon Dec 12 '11 at 23:10
  • Because it forces me to have clients call an external library in order to use the external config. The whole point of this was to allow someone to add one line to their config and have everything work out of the box. – Billy ONeal Dec 13 '11 at 02:56
2

Another option would be (if you have the allowModifications option set to true) is to change the working directory, then realpath the folder. Or even prepend the path on after loading the file.

$config = new Zend_Config_Ini('config.ini', 'section', array(
    'allowModifications' => true,
));
$dir = getcwd();
chdir('..');
$config->path = realpath($config->path);
chdir($dir);
Petah
  • 45,477
  • 28
  • 157
  • 213
  • But how will this automatically prepend before any paths like the OP shows in the example? – Gordon Dec 12 '11 at 23:11
  • @Gordon Its as automatic as manually a setting constant and putting it in the file – Petah Dec 12 '11 at 23:26
  • how so? if I set the constant it gets automatically evaluated, so a `PATH_TO_THIS_IN_DIR "../Foo` will be returned as the `/path/to/../foo`. As far as I understood your solution you are only adding a path config variable to config object. – Gordon Dec 13 '11 at 08:04