phpzend-frameworkinizend-applicationzend-config

How does one specify a file path relative to the location of the config file when Zend_Config_Ini is in use?


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?


Solution

  • 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.