Hi everybody,

first of all, this is my first post on this list, but I've been using this framework for a long time, since it's very early stages, and it has been very fun since...

Yesterday I was working in a new client's infrastructure (we have to by contract) for a very large company in Brazil, and I had a simple idea when using a registry with variable scopes... For example the controller can only access the instances of database, router, request and response, the plugin can only use the configuration, database, request and response objects... How to deal with this kind of variable scope registry... Here is my suggestion:

function Application($Name = NULL, $Instance = NULL)
{
        static $Classes = array();
        static $Instances = array();
        
        if(empty($Name))
        {
throw new Application_Exception('Unable to utilize this instance (`'. $Name.'`): The supplied instance name must not be empty.');
        }

        if(is_object($Instance))
        {
                $Classes[get_class($Instance)] = $Name;

                $Instances[$Name] = $Instance;
                
                return $Instance;
        }
        elseif(isset($Instances[$Name]))
        {
                return $Instances[$Name];
        }
        elseif(isset($Instances[$Classes[$Name]]))
        {
                return $Instances[$Classes[$Name]];
        }
        else
        {
throw new Application_Exception('Unable to utilize this instance (`'. $Name.'`): It was not found within this specific scope (`application`).');
        }
}

For the controller scope:

function Controller($Name = NULL, $Instance = NULL)
{
        static $Classes = array();
        static $Instances = array();
        
        if(empty($Name))
        {
throw new Application_Controller_Exception('Unable to utilize this instance (`'.$Name.'`): The supplied instance name must not be empty.');
        }

        if(is_object($Instance))
        {
                $Classes[get_class($Instance)] = $Name;

                $Instances[$Name] = $Instance;
                
                return $Instance;
        }
        elseif(isset($Instances[$Name]))
        {
                return $Instances[$Name];
        }
        elseif(isset($Instances[$Classes[$Name]]))
        {
                return $Instances[$Classes[$Name]];
        }
        else
        {
throw new Application_Controller_Exception('Unable to utilize this instance (`'.$Name.'`): It was not found within this specific scope (`controller`).');
        }
}

And for the plugin scope

function Plugin($Name = NULL, $Instance = NULL)
{
        static $Classes = array();
        static $Instances = array();
        
        if(empty($Name))
        {
throw new Application_Plugin_Exception('Unable to utilize this instance (`'.$Name.'`): The supplied instance name must not be empty.');
        }

        if(is_object($Instance))
        {
                $Classes[get_class($Instance)] = $Name;

                $Instances[$Name] = $Instance;
                
                return $Instance;
        }
        elseif(isset($Instances[$Name]))
        {
                return $Instances[$Name];
        }
        elseif(isset($Instances[$Classes[$Name]]))
        {
                return $Instances[$Classes[$Name]];
        }
        else
        {
throw new Application_Plugin_Exception('Unable to utilize this instance (`'.$Name.'`): It was not found within this specific scope (`plugin`).');
        }
}

We would add instances to the registry like:
Application('Configuration', new Zend_Config());
Controller('Configuration', Application('Configuration'));
Plugin('Configuration', Application('Configuration'));

So we are able to use it like this, inside of a controller:

Application('Configuration')->GerParameter('application.address'); OR
Controller('Configuration')->GerParameter('application.address');

Os if this is available inside the plugin, we can use:

Application('Configuration')->GerParameter('application.address'); OR
Plugin('Configuration')->GerParameter('application.address');

Maybe we could have a function called Zend(), to "abstract" the Zend Registry? It makes the code extremely readable and fluid... Guys, am I crazy? Is this a pattern of somehow? :D

Best regards,
Bruno B B Magalhaes

Reply via email to