Object.php

Go to the documentation of this file.
00001 <?php
00002 
00031 class Robo47_Application_Resource_Object
00032 extends Zend_Application_Resource_ResourceAbstract
00033 {
00034 
00038     protected $_object = null;
00039 
00043     public function init()
00044     {
00045         if (!empty($this->_options)) {
00046             $this->_object = $this->_setupObject($this->_options);
00047         } else {
00048             $message = 'Empty options in resource ' .
00049                 'Robo47_Application_Resource_Object.';
00050             throw new Robo47_Application_Resource_Exception($message);
00051         }
00052     }
00053 
00060     protected function _setupObject($options)
00061     {
00062         if (!isset($options['classname'])) {
00063             $message = 'no classname found for Object resource';
00064             throw new Robo47_Application_Resource_Exception($message);
00065         }
00066 
00067         if (!isset($options['params'])) {
00068             $object = $this->_initObject($options['classname']);
00069         } else {
00070             $object = $this->_initObject(
00071                 $options['classname'],
00072                 $options['params']
00073             );
00074         }
00075 
00076         if (isset($options['variables'])) {
00077             $this->_setVariables($object, $options['variables']);
00078         }
00079 
00080         if (isset($options['staticVariables'])) {
00081             $this->_setStaticVariables(
00082                 $options['classname'],
00083                 $options['staticVariables']
00084             );
00085         }
00086 
00087         if (isset($options['functions'])) {
00088             $this->_callFunctions($object, $options['functions']);
00089         }
00090 
00091         if (isset($options['staticFunctions'])) {
00092             $this->_callFunctions(
00093                 $options['classname'],
00094                 $options['staticFunctions']
00095             );
00096         }
00097 
00098         if (isset($options['registryKey'])) {
00099             Zend_Registry::set($options['registryKey'], $object);
00100         }
00101 
00102         return $object;
00103     }
00104 
00112     protected function _setStaticVariables($classname, $variables)
00113     {
00114         foreach ($variables as $name => $value) {
00115             $this->_setStaticVariable($classname, $name, $value);
00116         }
00117         return $this;
00118     }
00119 
00128     protected function _setStaticVariable($classname, $name, $value)
00129     {
00130         $ref = new ReflectionClass($classname);
00131         $ref->setStaticPropertyValue($name, $value);
00132         return $this;
00133     }
00134 
00142     protected function _initObject($class, array $params = array())
00143     {
00144         $ref = new Zend_Reflection_Class($class);
00145         if (!empty($params)) {
00146             return $ref->newInstanceArgs($params);
00147         } else {
00148             return $ref->newInstance();
00149         }
00150     }
00151 
00159     protected function _setVariables($object, array $variables = array())
00160     {
00161         foreach ($variables as $variable => $value) {
00162             $this->_setVariable($object, $variable, $value);
00163         }
00164         return $this;
00165     }
00166 
00175     protected function _setVariable($object, $name, $value)
00176     {
00177         $object->{$name} = $value;
00178         return $this;
00179     }
00180 
00188     protected function _callFunctions($object, array $functions = array())
00189     {
00190         foreach ($functions as $function) {
00191             if (isset($function[0]) &&
00192                 is_string($function[0])) {
00193 
00194                 $func = $function[0];
00195                 $params = array();
00196                 if (isset($function[1])) {
00197                     $params = $function[1];
00198                 }
00199                 if (is_array($params)) {
00200                     $this->_callFunction($object, $func, $params);
00201                 } else {
00202                     $message = 'invalid parameters for function ' . $func;
00203                     throw new Robo47_Application_Resource_Exception($message);
00204                 }
00205             } else {
00206                 $message = 'no valid functionname found on function';
00207                 throw new Robo47_Application_Resource_Exception($message);
00208             }
00209         }
00210         return $this;
00211     }
00212 
00221     protected function _callFunction($object, $name, array $params = array())
00222     {
00223         call_user_func_array(array($object, $name), $params);
00224         return $this;
00225     }
00226 
00232     public function getObject()
00233     {
00234         return $this->_object;
00235     }
00236 }