Example #1
0
 /**
  * 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;
 }
Example #2
0
 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);
     }
 }
Example #3
0
 /**
  * Instantiate new process of a given class. Throws exception if class is not valid
  * 
  * @param string $processClass
  * @return Oops_Process
  * @throws Oops_Process_Exception
  */
 public static function newProcess($processClass, $inputValues)
 {
     Oops_Loader::load($processClass);
     $reflectionClass = new ReflectionClass($processClass);
     if (!$reflectionClass->isSubclassOf('Oops_Process_Abstract')) {
         throw new Oops_Process_Exception("Invalid process class {$processClass}");
     }
     /**
      * 
      * @var Oops_Process_Abstract $process
      */
     $process = $reflectionClass->newInstance();
     $process->init($inputValues);
     if (!$process->pid) {
         throw new Oops_Process_Exception("Process init error, no pid for instance of {$processClass}", OOPS_PROCESS_EXCEPTION_NO_PID);
     }
     self::$_processes[$process->pid] = $process;
     return $process;
 }
Example #4
0
 /**
  * Notifies registered observers and nested dispatchers (if implemented)
  *
  * @param
  *        	object The notification object
  * @return object The notification object //!!!! not necessary
  */
 public function postNotification($notification)
 {
     $event = strtolower($notification->getEvent());
     if (!isset($this->_ro[$event])) {
         return $notification;
     }
     foreach ($this->_ro[$event] as $callback) {
         if ($notification->isCancelled()) {
             return $notification;
         }
         if (is_array($callback) && !is_object($callback[0])) {
             require_once 'Oops/Loader.php';
             Oops_Loader::load($callback[0]);
         }
         call_user_func_array($callback, array($notification));
     }
     /* Here to call nested dispatchers and pending observers */
     foreach ($this->_nestedDispatchers as $nestedDispatcher) {
         $notification = $nestedDispatcher->postNotification($notification);
     }
     return $notification;
 }
Example #5
0
 /**
  * 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;
 }
Example #6
0
 /**
  *
  * @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();
 }