/** * */ public final function __construct() { // Calls the parent constructor parent::__construct(); // Checks if the static variables are set if (!self::$_hasStatic) { // Sets the static variables self::_setStaticVars(); } }
/** * Class constructor * * @return void */ public function __construct() { // Call the parent constructor parent::__construct(); // Checks if the static variables are set if (!self::$_hasStatic) { // Sets the static variables self::_setStaticVars(); } $this->_modClass = get_class($this); $this->_modName = substr($this->_modClass, 10, strpos($this->_modClass, '_', 10) - 10); $this->_modPath = self::$_modManager->getModulePath($this->_modName); $this->_modRelPath = self::$_modManager->getModuleRelativePath($this->_modName); try { $this->_lang = Woops_Core_Lang_Getter::getInstance($this->_modPath . 'lang' . DIRECTORY_SEPARATOR . str_replace('Woops_Mod_' . $this->_modName . '_', '', $this->_modClass) . '.'); } catch (Woops_Core_Lang_Getter_Exception $e) { if ($e->getCode() === Woops_Core_Lang_Getter_Exception::EXCEPTION_NO_LANG_FILE) { $this->_lang = Woops_Core_Lang_Getter::getDefaultInstance(); } else { throw $e; } } }
/** * Adds an AOP advice on a target * * This method registers a callback as an AOP advice, for a target. The * target can be a class name or an instance of class. In the first case, * the advice callback will be executed on all instances of the class. In * the second case, it will only be executed for the given instance. * * Two categories of advice types are available: * * - The first one allows you to execute code when a PHP magic method is * called on the target. The advices types are, in such a case: * * -# Woops_Core_Aop_Advisor::ADVICE_TYPE_CONSTRUCT * Called when the constructor of the target class is called * -# Woops_Core_Aop_Advisor::ADVICE_TYPE_DESTRUCT * Called when the destructor of the target class is called * -# Woops_Core_Aop_Advisor::ADVICE_TYPE_CLONE * Called when an object of the target class is cloned * -# Woops_Core_Aop_Advisor::ADVICE_TYPE_SLEEP * Called when an object of the target class is serialized * -# Woops_Core_Aop_Advisor::ADVICE_TYPE_WAKEUP * Called when an object of the target class is unserialized * -# Woops_Core_Aop_Advisor::ADVICE_TYPE_TO_STRING * Called when an object of the target class is converted to a string * * - The second one allows you to execute specific code on specific join * points, registered in the target class. In that case, you must specify * the name of the join point on which to place the advice as the fourth * parameter. Available advices types in such a case are: * * -# Woops_Core_Aop_Advisor::ADVICE_TYPE_VALID_CALL * Called before the join point is executed, and may prevents the join * point to be executed * -# Woops_Core_Aop_Advisor::ADVICE_TYPE_BEFORE_CALL * Called before the join point is executed * -# Woops_Core_Aop_Advisor::ADVICE_TYPE_BEFORE_RETURN * Called before the return value of the join point is return, and may * change the return value * -# Woops_Core_Aop_Advisor::ADVICE_TYPE_AFTER_CALL * Called after the join point is executed * -# Woops_Core_Aop_Advisor::ADVICE_TYPE_AFTER_THROWING * Called after an exception is thrown from the join point. The * original exception won't be thrown if the callback method * returns true * * @param int The type of the advice (one of the Woops_Core_Aop_Advisor::ADVICE_TYPE_XXX constant) * @param callback The callback to invoke (must be a valid PHP callback) * @param mixed The target on which to place the advice (either a class name or an object) * @param string The join point on which to place the advice * @return boolean * @throws Woops_Core_Aop_Advisor_Exception If the passed callback is not a valid PHP callback * @throws Woops_Core_Aop_Advisor_Exception If the target class does not exist * @throws Woops_Core_Aop_Advisor_Exception If the no join point is specified, when trying to register a user advice type * @throws Woops_Core_Aop_Advisor_Exception If the join point has not been registered in the target * @throws Woops_Core_Aop_Advisor_Exception If the advice type is not allowed for the join point * @throws Woops_Core_Aop_Advisor_Exception If the advice type does not exist */ public static final function addAdvice($type, $callback, $target, $joinPoint = '') { // Checks if the configuration object exists if (!is_object(self::$_conf)) { // Gets the instance of the configuration object self::$_conf = Woops_Core_Config_Getter::getInstance(); // Gets the AOP mode from the configuration to know if it's turned on or not self::$_enableAop = self::$_conf->getVar('aop', 'enable'); } // If we are not using class caching, the AOP features must be turned off if (defined('WOOPS_CLASS_CACHE_MODE_OFF') || !self::$_enableAop) { // Nothing to do, as AOP is disabled return true; } // Checks if the callback must be executed for a specific object or for all instances if (is_object($target)) { // Gets the class name and object hash, so the callback will be added for the specific object only $className = $target->_className; $objectHash = $target->_objectHash; } else { // Callback will be executed for all instances $className = $target; $objectHash = false; // Checks if the class exists if (!class_exists($className)) { // Error - Unexisting class throw new Woops_Core_Aop_Advisor_Exception('Cannot add an advice on unexisting class ' . $className, Woops_Core_Aop_Advisor_Exception::EXCEPTION_NO_CLASS); } } // Checks if the advice type is for a specific join point or not if ($type & self::ADVICE_TYPE_GLOBAL) { // Process the global advice types foreach (self::$_globalAdvices as $adviceType) { // Checks if the requested type matches a global advice type if ($type & $adviceType) { // Checks if the storage array for the advice exists if (!isset(self::$_advices[$adviceType][$className])) { // Creates the storage array for the advice self::$_advices[$adviceType][$className] = array(); } // Adds the advice callback for the join point self::$_advices[$adviceType][$className][] = array(new Woops_Core_Callback($callback), $objectHash); } } // The advice(s) was(were) added return true; } elseif ($type & self::ADVICE_TYPE_USER_DEFINED) { // Checks if a join point is specified if (!$joinPoint) { // Error - A join point must be specified throw new Woops_Core_Aop_Advisor_Exception('A join point must be specified for that type of advice', Woops_Core_Aop_Advisor_Exception::EXCEPTION_NO_JOINPOINT); } // Checks if the join point exists if (!isset(self::$_joinPointsByName[$className][$joinPoint])) { // Creates a reflection object for the class // If no instance exist at the moment the advice is added, the automatic join points won't be declared, so we'll have to check it manually $reflection = Woops_Core_Reflection_Class::getInstance($className); // Name of the method $methodName = $joinPoint . self::JOINPOINT_METHOD_SUFFIX; // Checks if we are processing an automatic join point if (!$reflection->hasMethod($methodName) || !$reflection->getMethod($methodName)->isPublic()) { // Error - No such join point in the target class throw new Woops_Core_Aop_Advisor_Exception('The join point ' . $joinPoint . ' does not exist in class ' . $className, Woops_Core_Aop_Advisor_Exception::EXCEPTION_NO_JOINPOINT); } // Process the user advice types foreach (self::$_userAdvices as $adviceType) { // Checks if the requested type matches a user advice type if ($type & $adviceType) { // Checks if the storage array for the requested join point exists if (!isset(self::$_advices[$adviceType][$className][$joinPoint])) { // Creates the storage array self::$_advices[$adviceType][$className][$joinPoint] = array(); } // Adds the advice self::$_advices[$adviceType][$className][$joinPoint][] = array(new Woops_Core_Callback($callback), $objectHash); } } // The advice(s) was(were) added return true; } // Storage for the allowed advice types $allowedAdviceTypes = 0; // Process the join points for each instance of the target class foreach (self::$_joinPoints[$className] as $joinPoints) { // Adds the allowed types of advices for the joint point $allowedAdviceTypes |= $joinPoints[$joinPoint][1]; } // Process the user advice types foreach (self::$_userAdvices as $adviceType) { // Checks if the requested type matches a user advice type if ($type & $adviceType) { // Checks if the advice type is allowed if ($adviceType & $allowedAdviceTypes) { // Adds the advice callback for the join point self::$_advices[$adviceType][$className][$joinPoint][] = array(new Woops_Core_Callback($callback), $objectHash); return true; } else { // Error - The advice type is not allowed for the join point throw new Woops_Core_Aop_Advisor_Exception('Advice of type ' . $adviceType . ' is not permitted for join point ' . $joinPoint, Woops_Core_Aop_Advisor_Exception::EXCEPTION_ADVICE_TYPE_NOT_PERMITTED); } } } } // Error - Advice type is invalid throw new Woops_Core_Aop_Advisor_Exception('Invalid advice type (' . $type . ')', Woops_Core_Aop_Advisor_Exception::EXCEPTION_INVALID_ADVICE_TYPE); }
<?php ################################################################################ # # # WOOPS - Web Object Oriented Programming System # # # # COPYRIGHT NOTICE # # # # Copyright (C) 2009 Jean-David Gadina - www.xs-labs.com # # All rights reserved # ################################################################################ # $Id$ Woops_Core_Aop_Advisor::addAdvice(Woops_Core_Aop_Advisor::ADVICE_TYPE_BEFORE_CALL, array('Woops_Mod_AopTest_Interceptor', 'interceptBefore'), 'Woops_Mod_HelloWorld_SayHello', 'getBlockContent'); Woops_Core_Aop_Advisor::addAdvice(Woops_Core_Aop_Advisor::ADVICE_TYPE_AFTER_CALL, array('Woops_Mod_AopTest_Interceptor', 'interceptAfter'), 'Woops_Mod_HelloWorld_SayHello', 'getBlockContent');