[Solar-talk] batch register function

Rodrigo Moraes rodrigo.moraes at gmail.com
Fri Apr 20 03:04:07 PDT 2007


Hi,

I have a simple function to share which I've been using for quite some
time to register objects inside apps. Normally we start an app
registering some basic objects or dependencies, so this is a method
for registering them in "batch" mode. It takes care of checking if the
object is already registered and so reduces and cleans the code a bit.
I keep it in the base class for all my apps. First, an example of
usage:

        $this->_register(array(
            // Registers a key for an object or dependency set in config
            'model_item',
            // Registers a dependency
            'model_poll'     => 'Tipos_Model_Tipos_Poll',
            // Registers a dependency with config
            'model_keywords' => array('Tipos_Model_Keywords', $some_config),
        ));

And this is the method:

    /**
     *
     * Registers objects or dependencies.
     *
     * @param array List of dependency keys set in config or key-value pairs
     * for the registry.
     *
     */
    protected function _register($spec)
    {
        foreach($spec as $key => $value) {
            if(is_int($key)) {
                $key   = $value;
                $value = $this->_config[$key];
            }

            if(!Solar::isRegistered($key)) {
                if(is_array($value)) {
                    $config = isset($value[1]) ? $value[1] : null;
                    Solar::register($key, $value[0], $config);
                } else {
                    Solar::register($key, $value);
                }
            }
        }
    }

I hope someone will find it handy. :)

rodrigo


More information about the solar-talk mailing list