コード例 #1
0
ファイル: Callback.class.php プロジェクト: Sect0R/WOOPS
 /**
  * Class sonstructor
  * 
  * @param   callback    The PHP callback
  * @return  void
  */
 public function __construct($callback)
 {
     // Ensures the callback is valid
     if (!is_callable($callback)) {
         // Error - The callback is not valid
         throw new Woops_Core_Callback_Exception('Invalid PHP callback', Woops_Core_Callback_Exception::EXCEPTION_INVALID_CALLBACK);
     }
     // Stores the callback
     $this->_callback = $callback;
     // Checks the callback type
     if (is_array($this->_callback)) {
         // Checks if the callback returns a reference
         $ref = Woops_Core_Reflection_Method::getInstance($callback[0], $callback[1]);
         $this->_returnsReference = $ref->returnsReference();
     } else {
         // Checks if the callback returns a reference
         $ref = Woops_Core_Reflection_Function::getInstance($callback);
         $this->_returnsReference = $ref->returnsReference();
     }
 }
コード例 #2
0
ファイル: Advisor.class.php プロジェクト: Sect0R/WOOPS
 /**
  * Calls a specific join point
  * 
  * This method is used to redirect calls on a specific join point to it's
  * internal method, executing all available AOP advices registered for
  * the join point.
  * 
  * @param   string                              The name of the join point to invoke
  * @param   array                               The arguments to pass to the join point method
  * @return  mixed                               The return value of the join point method
  * @throws  Woops_Core_Aop_Advisor_Exception    If the called join point has not been registered
  */
 public function __call($name, array $args = array())
 {
     // Checks if the join point has been registered
     if (!isset(self::$_joinPoints[$this->_className][$this->_objectHash][$name])) {
         // Error - The join point has not been registered
         throw new Woops_Core_Aop_Advisor_Exception('No joint point named ' . $name . '. Call to undefined method ' . $this->_className . '::' . $name . '()', Woops_Core_Aop_Advisor_Exception::EXCEPTION_NO_JOINPOINT);
     }
     // Gets the method to use and the allowed advices type for the join point
     $method = self::$_joinPoints[$this->_className][$this->_objectHash][$name][0];
     $allowedAdviceType = self::$_joinPoints[$this->_className][$this->_objectHash][$name][1];
     // Creates a callback for the internal method
     $methodCallback = new Woops_Core_Callback(array($this, $method));
     // Creates a reflection object for the internal method
     $ref = Woops_Core_Reflection_Method::getInstance($this, $method);
     // By default, the call on the join point internal method is allowed
     $valid = true;
     // Checks if we can have advices to validate the call to the internal method
     if (self::ADVICE_TYPE_VALID_CALL & $allowedAdviceType) {
         // Process each validation advice
         foreach (self::$_advices[self::ADVICE_TYPE_VALID_CALL][$this->_className][$name] as $advice) {
             // Checks if the advice can be called on the current instance
             if ($advice[1] === false || $advice[1] === $this->_objectHash) {
                 // Invokes the validation advice
                 $valid = $advice[0]->invoke($args);
                 // Checks if an advice is preventing the execution of the internal method
                 if ($valid === false) {
                     // Internal method won't be executed - No need to process the following advices
                     break;
                 }
             }
         }
     }
     // Checks if the internal method can be executed
     if ($valid) {
         // Checks if we can have advices before the call to the internal method
         if (self::ADVICE_TYPE_BEFORE_CALL & $allowedAdviceType) {
             // Process each 'beforeCall' advice
             foreach (self::$_advices[self::ADVICE_TYPE_BEFORE_CALL][$this->_className][$name] as $advice) {
                 // Checks if the advice can be called on the current instance
                 if ($advice[1] === false || $advice[1] === $this->_objectHash) {
                     // Invokes the advice
                     $advice[0]->invoke($args);
                 }
             }
         }
         // Flag to know if an exception was thrown
         $exceptionThrown = false;
         // We are catching exceptions from the internal method, so we'll be able to run the 'afterThrowing' advices
         try {
             // Checks if the internal method returns a reference
             if ($ref->returnsReference()) {
                 // Gets the return value of the internal method
                 $returnValue =& $methodCallback->invoke($args);
             } else {
                 // Gets the return value of the internal method
                 $returnValue = $methodCallback->invoke($args);
             }
         } catch (Exception $e) {
             // An exception was thrown
             $exceptionThrown = true;
             // Checks if we can have advices to catch exceptions thrown from the internal method, and if so, if such advices are available
             if (!count(self::$_advices[self::ADVICE_TYPE_AFTER_THROWING][$this->_className][$name]) || !(self::ADVICE_TYPE_AFTER_THROWING & $allowedAdviceType)) {
                 // No - Throws the original exception back
                 throw $e;
             } else {
                 // Boolean value to check if the exception has been caught, and so should not be thrown
                 $exceptionCaught = false;
                 // Process each 'afterThrowing' advice
                 foreach (self::$_advices[self::ADVICE_TYPE_AFTER_THROWING][$this->_className][$name] as $advice) {
                     // Checks if the advice can be called on the current instance
                     if ($advice[1] === false || $advice[1] === $this->_objectHash) {
                         // Invokes the advice
                         $exceptionCaught = $advice[0]->invoke(array($e, $this));
                         // Checks if the exception has been caught by the advice
                         if ($exceptionCaught === true) {
                             // Exception was caught - Do not executes the next advices
                             break;
                         }
                     }
                 }
                 // Checks if the exception has been caught by the advice
                 if ($exceptionCaught !== true) {
                     // Exception was not caught by an advice - Throws it back
                     throw $e;
                 }
             }
         }
         // Checks if we have a return value, meaning no exception was thrown
         if (!$exceptionThrown) {
             // Checks if we ca have advices that will be able to modify the return value of the internal method
             if (self::ADVICE_TYPE_BEFORE_RETURN & $allowedAdviceType) {
                 // Process each 'beforeReturn' advice
                 foreach (self::$_advices[self::ADVICE_TYPE_BEFORE_RETURN][$this->_className][$name] as $advice) {
                     // Checks if the advice can be called on the current instance
                     if ($advice[1] === false || $advice[1] === $this->_objectHash) {
                         // Invokes the advice and stores the return value
                         $returnValue = $advice[0]->invoke(array($returnValue, $args));
                     }
                 }
             }
             // Checks if we can have advices after the call to the internal method
             if (self::ADVICE_TYPE_AFTER_CALL & $allowedAdviceType) {
                 // Process each 'afterCall' advice
                 foreach (self::$_advices[self::ADVICE_TYPE_AFTER_CALL][$this->_className][$name] as $advice) {
                     // Checks if the advice can be called on the current instance
                     if ($advice[1] === false || $advice[1] === $this->_objectHash) {
                         // Invokes the advice
                         $advice[0]->invoke($args);
                     }
                 }
             }
             // Returns the return value
             return $returnValue;
         }
     }
 }
コード例 #3
0
ファイル: Base.class.php プロジェクト: Sect0R/WOOPS
 /**
  * 
  */
 private final function _getConstructor()
 {
     if (!$this->_hasConstructor) {
         $constructor = $this->_reflector->getConstructor();
         if ($constructor) {
             $this->_constructor = Woops_Core_Reflection_Method::getInstance($constructor->getDeclaringClass()->getName(), $constructor->getName());
         } else {
             $this->_constructor = $constructor;
         }
         $this->_hasConstructor = true;
     }
     return $this->_constructor;
 }