Beispiel #1
0
 /**
  * 
  */
 private static function _initLexer($class)
 {
     $reflection = Woops_Core_Reflection_Class::getInstance($class);
     $constants = $reflection->getConstants();
     $staticProps = $reflection->getStaticProperties();
     if (!isset($staticProps['_tokens'])) {
         throw new Woops_Lexical_Analyser_Exception('', Woops_Lexical_Analyser_Exception::EXCEPTION_NO_TOKENS);
     }
     self::$_lexers[$class] = true;
     self::$_tokensByNames[$class] = array();
     self::$_tokensByCode[$class] = array();
     self::$_tokensByValues[$class] = array();
     self::$_tokensByChar[$class] = array();
     foreach ($staticProps['_tokens'] as $name => $str) {
         if (!isset($constants[$name])) {
             throw new Woops_Lexical_Analyser_Exception('', Woops_Lexical_Analyser_Exception::EXCEPTION_NO_TOKEN_CONSTANT);
         }
         $code = $constants[$name];
         if (isset(self::$_tokensByCode[$class][$code])) {
             throw new Woops_Lexical_Analyser_Exception('', Woops_Lexical_Analyser_Exception::EXCEPTION_DUPLICATE_TOKEN_CODE);
         }
         self::$_tokensByNames[$class][$name] = $code;
         self::$_tokensByCode[$class][$code] = $name;
         self::$_tokensByValues[$class][$str] = $code;
         $strLen = strlen($str);
         $storage =& self::$_tokensByChar[$class];
         for ($i = 0; $i < $strLen; $i++) {
             if (!isset($storage[$str[$i]])) {
                 $storage[$str[$i]] = array();
             }
             $storage =& $storage[$str[$i]];
         }
     }
 }
Beispiel #2
0
 /**
  * Class constructor
  * 
  * @param   string  The name of the class for which to build an AOP version
  * @return  void
  */
 public function __construct($className)
 {
     // Checks if the static variables are set
     if (!self::$_hasStatic) {
         // Sets the static variables
         self::_setStaticVars();
     }
     // Checks if the class exists
     if (!class_exists($className)) {
         // Error - No such class
         throw new Woops_Core_Aop_Class_Builder_Exception('The class ' . $className . ' does not exist', Woops_Core_Aop_Class_Builder_Exception::EXCEPTION_NO_CLASS);
     }
     // Gets a reflection class object
     $reflection = Woops_Core_Reflection_Class::getInstance($className);
     // Gets the path to the PHP file
     $filePath = $reflection->getFileName();
     // Gets the PHP code
     $this->_classCode = file_get_contents($filePath);
     // Checks if the class is a subclass of the AOP advisor class
     if ($reflection->isAopReady()) {
         // Adds the AOP method suffix to all the public methods
         $this->_classAopCode = preg_replace(self::PUBLIC_METHODS_REGEXP, '\\1\\2' . Woops_Core_Aop_Advisor::JOINPOINT_METHOD_SUFFIX, $this->_classCode);
     } else {
         // Nothing to do, the class does not have AOP features
         $this->_classAopCode = $this->_classCode;
     }
 }
Beispiel #3
0
 $CLASSNAME = $GETVARS['className'];
 // Path to the cache directory
 $CACHEDIR = Woops_Core_Env_Getter::getInstance()->getPath('cache/classes/');
 // Checks if the class caching is enabled (or the AOP), and checks the cache directory
 if (Woops_Core_Config_Getter::getInstance()->getVar('classCache', 'enable') || Woops_Core_Config_Getter::getInstance()->getVar('aop', 'enable') && $CACHEDIR && is_dir($CACHEDIR) && is_writeable($CACHEDIR) && !file_exists($CACHEDIR . $CLASSNAME)) {
     // We don't want any error here
     try {
         // Checks if AOP is enabled, and if the class is not an interface
         if (Woops_Core_Config_Getter::getInstance()->getVar('aop', 'enable') && substr($CLASSNAME, -9) !== 'Interface') {
             // Creates an AOP version of the class
             $AOP = new Woops_Core_Aop_Class_Builder($CLASSNAME);
             // Gets the code of the AOP version
             $CLASSCODE = (string) $AOP;
         } else {
             // Creates a reflection object for the class
             $REF = Woops_Core_Reflection_Class::getInstance($CLASSNAME);
             // Gets the PHP source code
             $CLASSCODE = file_get_contents($REF->getFileName());
         }
         // Checks if the source code must be optimized
         if (Woops_Core_Config_Getter::getInstance()->getVar('classCache', 'optimize')) {
             // Creates a source optimizer
             $OPT = new Woops_Php_Source_Optimizer($CLASSCODE);
             // Gets the optimized source code
             $CLASSCODE = (string) $OPT;
         }
         // Writes the class in the cache
         file_put_contents($CACHEDIR . $CLASSNAME . '.class.php', $CLASSCODE);
         // The cached version of the class was built
         header('X-WOOPS-CLASS-CACHE-BUILD-STATUS: OK');
         // Aborts the script
Beispiel #4
0
 /**
  * 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);
 }
Beispiel #5
0
 /**
  * Gets an instance of a multi-singleton class
  * 
  * @param   string  The name of the class
  * @param   string  The name of the instance
  * @return  object  The instance of the class
  */
 public function getMultiSingleton($className, $instanceName)
 {
     // Creates a reflection object for the requested class
     $reflection = Woops_Core_Reflection_Class::getInstance($className);
     // Checks if the class is a multi-singleton
     if ($reflection->isMultiSingleton()) {
         // Returns the singleton instance
         return $reflection->getMethod('getInstance')->invoke(array($instanceName));
     } else {
         // Error, the class is not a multi-singleton
         throw new Woops_Core_Class_Manager_Exception('The class \'' . $className . '\' is not a multi-singleton', Woops_Core_Class_Manager_Exception::EXCEPTION_NOT_MULTISINGLETON);
     }
 }
/**
 * Creates the cached version of a class
 * 
 * @param   string  The name of the class for which to build a cached version
 * @return  void
 */
function createClassCache($className)
{
    // The environment object
    static $env;
    // The configuration object
    static $conf;
    // The path to the cache directory for the PHP classes
    static $cacheDir;
    // Whether AOP is enabled
    static $aop;
    // Whether the PHP code must be optimized
    static $optimize;
    // Checks if we already have the environment object
    if (!is_object($env)) {
        // Gets the environment object
        $env = Woops_Core_Env_Getter::getInstance();
        // Gets the configuration object
        $conf = Woops_Core_Config_Getter::getInstance();
        // Sets the path to the cache directory
        $cacheDir = $env->getPath('cache/classes/');
        // Gets the AOP state
        $aop = $conf->getVar('aop', 'enable');
        // Gets the optimize state
        $optimize = $conf->getVar('classCache', 'optimize');
    }
    // Checks if the cached version already exists
    if (file_exists($cacheDir . $className . '.class.php')) {
        // Nothing to do, the cached version already exists
        return;
    }
    // Checks if AOP is enabled, and if the class is not an interface
    if ($aop && substr($className, -9) !== 'Interface') {
        // Creates an AOP version of the class
        $aopBuilder = new Woops_Core_Aop_Class_Builder($className);
        // Gets the code of the AOP version
        $classCode = (string) $aopBuilder;
    } else {
        // Creates a reflection object
        $reflection = Woops_Core_Reflection_Class::getInstance($className);
        // Gets the PHP source code
        $classCode = file_get_contents($reflection->getFileName());
    }
    // Checks if the source code must be optimized
    if ($optimize) {
        // Creates a source optimizer
        $optimizer = new Woops_Php_Source_Optimizer($classCode);
        // Gets the optimized source code
        $classCode = (string) $optimizer;
    }
    // Writes the class in the cache
    file_put_contents($cacheDir . $className . '.class.php', $classCode);
}
Beispiel #7
0
 /**
  * Creates a new IFD in the current TIFF file
  * 
  * @param   string          An optionnal PHP classname, for a custom IFD object (the class MUST extends the Woops_Tiff_Ifd class)
  * @return  Woops_Tiff_Ifd  The IFD object
  * @throws  Woops_Tiff_File_Exception   If the custom class does not extends Woops_Tiff_Ifd
  */
 public function newIfd($customClass = '')
 {
     // Checks for a custom class
     if ($customClass) {
         // Creates a reflection object for the custom class
         $ref = Woops_Core_Reflection_Class::getInstance($customClass);
         // Checks if the custom class implements the Woops_Tiff_Ifd_Interface class
         if (!$ref->implementsInterface('Woops_Tiff_Ifd_Interface')) {
             // Error - INvalid IFD class
             throw new Woops_Tiff_File_Exception('Invalid IFD custom class \'' . $customClass . '\'. It must implement the Woops_Tiff_Ifd_Interface interface', Woops_Tiff_File_Exception::EXCEPTION_INVALID_IFD_CLASS);
         }
         // Creates the IFD
         $ifd = new $customClass($this);
     } else {
         // Creates the IFD
         $ifd = new Woops_Tiff_Ifd($this);
     }
     // Stores the IFD
     $this->_ifds[] = $ifd;
     // Returns the IFD
     return $ifd;
 }
Beispiel #8
0
 /**
  * Class constructor
  * 
  * @param   mixed                           The class that will handle the SOAP requests, either as a string or as an object
  * @param   string                          The URL of the web service
  * @return  void
  * @throws  Soap_Wsdl_Generator_Exception   If the XmlWriter class is not available
  */
 public function __construct($handlerClass, $url)
 {
     // Checks if the static variables are set
     if (!self::$_hasStatic) {
         // Sets the static variables
         self::_setStaticVars();
     }
     // Checks for the XmlWriter class
     if (!class_exists('XMLWriter')) {
         // Error - XmlWriter is not available
         throw new Soap_Wsdl_Generator_Exception('The XMLWriter class is not available', Soap_Wsdl_Generator_Exception::EXCEPTION_NO_XML_WRITER);
     }
     // Checks if we have an object or a class name
     if (is_object($handlerClass)) {
         // Creates the reflection object
         $this->_reflection = Woop_Core_Reflection_Object::getInstance($handlerClass);
         // Stores the web service name
         $this->_name = get_class($handlerClass);
     } else {
         // Creates the reflection object
         $this->_reflection = Woops_Core_Reflection_Class::getInstance($handlerClass);
         // Stores the web service name
         $this->_name = $handlerClass;
     }
     // Stores the URL of the web service
     $this->_url = $url;
     // Stores the web service target namespace
     $this->_tns = self::$_env->SSL ? 'https://' . self::$_env->HTTP_HOST . '/' . $this->_name : 'http://' . self::$_env->HTTP_HOST . '/' . $this->_name;
     // Gets and stores the available SOAP procedures
     $this->_soapProcedures = $this->_getSoapProcedures();
     // Creates the XML writer object
     $this->_xml = new XmlWriter();
     $this->_xml->openMemory();
     $this->_xml->setIndent(4);
     // Creates the WSDL document
     $this->_createWsdl();
     // Ends the XML documents
     $this->_xml->endDocument();
     // Stores the XML output
     $this->_wsdl = $this->_xml->flush();
 }
Beispiel #9
0
 /**
  * 
  */
 private final function _getParentClass()
 {
     if (!$this->_hasParentClass) {
         $parentClass = $this->_reflector->getParentClass();
         if ($parentClass) {
             $this->_parentClass = Woops_Core_Reflection_Class::getInstance($parentClass->getName());
         } else {
             $this->_parentClass = $parentClass;
         }
         $this->_hasParentClass = true;
     }
     return $this->_parentClass;
 }
Beispiel #10
0
 /**
  * 
  */
 public static function getExceptionString($class, $code = 0)
 {
     if (is_object($class)) {
         $code = $class->getCode();
         $reflector = Woops_Core_Reflection_Object::getInstance($class);
     } else {
         $reflector = Woops_Core_Reflection_Class::getInstance($class);
     }
     $constants = array_flip($reflector->getConstants());
     return isset($constants[$code]) ? $constants[$code] : '';
 }