Issues

ZF-7951: Add allow bootstrap dependent resources

Description

Add allow bootstrap dependent resources in config resources.{resourcename}.dependencies[] = Db resources.{resourcename}.dependencies[] = FrontController or


class System_Application_Resource_Modules extends Zend_Application_Resource_ResourceAbstract
{

    protected $_dependencies = array('FrontController');

/**
 * Class for bootstrap dependent resources
 *
 * @author Andrii Kasian 
 * @package system.application.resource
 */
abstract class System_Application_Resource_ResourceAbstract extends Zend_Application_Resource_ResourceAbstract
{
     * List of dependent resources
    protected $_dependencies = array();

     * Set list of dependent resources
    public function setDependencies($dependencies)
    {
        $this->_dependencies =$this->mergeOptions($this->_dependencies, (array) $dependencies);
    }

    /**
     * Get list of dependent resources
     * @return array
     */
    public function getDependencies()
    {
        return $this->_dependencies;
    }


    /**
     * Add dependent resource
     * @param string $dependency
     */
    public function addDependency($dependency)
    {
        $this->_dependencies[] = $dependency;
    }

    /**
     * Clear dependent resource
     * @param string $dependency
     */
    public function clearDependencis($dependency)
    {
        $this->_dependencies = array();
    }

     * Bootstrap dependent resources
    public function bootstrapDependencies()
    {
        foreach ($this->getDependencies() as $dependency){
            $this->getBootstrap()->bootstrap($dependency);
        }
    }

    public function init()
    {
        $this->bootstrapDependencies();
    }
}

Comments

Could you maybe explain the idea of this? What does it try to accomplish that cannot be done with zend_app already?

{quote}Zend_Application provides a bootstrapping facility for applications which provides reusable resources, common- and module-based bootstrap classes and dependency checking.{quote} If resource a depend b, and b depend c then I must first write first c, then b and a. It's simple if dependence static ( in code $this->getBootstrap()->bootstrap('Dependet resource')), but if dependencies dynamic it's can lead to errors.

For example: I have load acl rules. In config I write:


resource.acl.adapter = Config
resource.db.adapter = Pdo_Mysql

Then i have load acl from database I change adapter, set to db "resource.acl.adapter = Db" and get error. I need write "resource.db.adapter = Pdo_Mysql" before "resource.acl.adapter" It is not hard, but program can do better :)
Example: I write


resource.acl.adapter = Db
resource.acl.dependencis[] = Db
resource.db.adapter = Pdo_Mysql

And all is beautiful :))

PS: SORRY, for my bad English

Ressources can already define depencies by themself

There is already a mechanism for this built into ZF. Instead of being configuration driven, however, we require that the dependency is called as part of the resource. For example, if you write an ACL resource that depends on the DB resource, it might look like this:


class My_Resource_Acl extends Zend_Application_Resource_ResourceAbstract
{
    public function init()
    {
        $this->getBootstrap()->bootstrap('db');
        $db = $this->getBootstrap()->getResource('db');
        ....
    }
}

Basically, calling the bootstrap() method with the name of a resource is how you enforce dependencies. Using configuration seems to me to add some unnecessary and unwanted magic to the situation; the dependencies should be defined at the resource level, not configuration.

@Ben Scholzen - Yes Ben, resources can already define dependinces, but the Zend_Application_Resource_Session should bootstrap the multidb resource when session database save handler requested. You can make this from the settings ini for eg, but when you should have session first defined and then multidb is not working.

Maybe a Zend_Application_Resource_ResourceBootstrapper sould fix this.

Cosmin