/** * Instantiates objects by class and id, respecting pattern implemented by given class * * @param string $class Class name * @param $id * @return unknown_type */ public static function instantiate($class, $id = null) { if (!strlen($class)) { require_once 'Oops/Exception.php'; throw new Oops_Exception("Empty class name given"); } if (!Oops_Loader::find($class)) { require_once 'Oops/Exception.php'; throw new Oops_Exception("Class '{$class}' not found"); } $reflectionClass = new ReflectionClass($class); if ($reflectionClass->implementsInterface('Oops_Pattern_Identifiable_Factored_Interface')) { /** * Object can be instantiated using corresponding factory */ $factoryCallback = call_user_func($class, 'getFactoryCallback'); $result = call_user_func($factoryCallback, $id); } elseif ($reflectionClass->implementsInterface('Oops_Pattern_Identifiable_Singleton_Interface')) { /** * This object can be instantiated using $class::getInstance($id) */ $result = call_user_func(array($class, 'getInstance'), $id); } elseif ($reflectionClass->implementsInterface('Oops_Pattern_Singleton_Interface')) { /** * This object is the single available instance of this class, so it can be instantiated using $class::getInstance() */ $result = call_user_func(array($class, 'getInstance')); } else { /** * This type of object should be constructed with given $id */ $result = $reflectionClass->newInstance($id); } return $result; }
private function __construct($class) { require_once 'Oops/Loader.php'; if (Oops_Loader::find($class)) { $reflectionClass = new ReflectionClass($class); if (!$reflectionClass->isSubclassOf('Oops_Process_Abstract')) { require_once 'Oops/Process/Exception.php'; throw new Oops_Process_Exception("Requested class not found", OOPS_PROCESS_EXCEPTION_INVALID_CLASS); } $this->_class = $class; $this->_reflection = $reflectionClass; $classVars = $reflectionClass->getDefaultProperties(); $this->_info = array('states' => $classVars['_states'], 'variables' => $classVars['_variables'], 'transitions' => $classVars['_transition']); } else { require_once 'Oops/Process/Exception.php'; throw new Oops_Process_Exception("Requested class {$class} not found", OOPS_PROCESS_EXCEPTION_INVALID_CLASS); } }
/** * Decompose stored data to PHP object or value * * @param $class * @param $id * @param $serialized * @return mixed */ protected function _decomposeData($class, $id, $serialized) { if (strlen($serialized)) { /** * Object (or data) should be restored from serialized string */ require_once 'Oops/Error/Handler.php'; $eH = new Oops_Error_Handler(); $result = unserialize($serialized); restore_error_handler(); if (!$eH->isClear()) { require_once 'Oops/Process/Exception.php'; throw new Oops_Process_Exception("Process stored data decomposition error", OOPS_PROCESS_EXCEPTION_DECOMPOSITION_ERROR); } } elseif (strlen($class) && Oops_Loader::find($class)) { $reflectionClass = new ReflectionClass($class); if ($reflectionClass->implementsInterface('Oops_Pattern_Identifiable_Factored_Interface')) { /** * Object can be restored using corresponding factory */ $factoryCallback = call_user_func($class, 'getFactoryCallback'); $result = call_user_func($factoryCallback, $id); } if ($reflectionClass->implementsInterface('Oops_Pattern_Identifiable_Singleton_Interface')) { /** * This object can be restored using $class::getInstance($id) */ $result = call_user_func(array($class, 'getInstance'), $id); } elseif ($reflectionClass->implementsInterface('Oops_Pattern_Singleton_Interface')) { /** * This object is the single available instance of this class, so it can be restored using $class::getInstance() */ $result = call_user_func(array($class, 'getInstance')); } else { /** * This type of object should be constructed with given $id */ $result = $reflectionClass->newInstance($id); } } else { require_once 'Oops/Process/Exception.php'; throw new Oops_Process_Exception("Process stored data decomposition error", OOPS_PROCESS_EXCEPTION_DECOMPOSITION_ERROR); } return $result; }
/** * * @todo Set error response code if there's no controller class found * * Controller instantiation. Uses $this->_controller as a class name * (detected in DetectController), or starts default controller * Oops_Controller */ function _initController() { $ctrl = $this->_router->controller; if (!Oops_Loader::find($ctrl)) { trigger_error("Controller {$ctrl} not found", E_USER_ERROR); $this->_response->setHeader("Oops-Error", "Controller {$ctrl} not found"); $this->_response->setCode(500); return; } $this->_controller_instance = new $ctrl(); }