rfc:splclassloader

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
Next revision Both sides next revision
rfc:splclassloader [2011/11/08 16:13]
guilhermeblanco
rfc:splclassloader [2012/06/29 06:53]
laruence
Line 77: Line 77:
          
     /**     /**
-     Creates a new Autoloader instance.+     Define the autoloader work mode.
      *      *
      * @param integer $mode Autoloader work mode.      * @param integer $mode Autoloader work mode.
      */      */
-    public function __construct($mode = \SplAutoloader::MODE_NORMAL);+    public function setMode($mode);
          
     /**     /**
Line 145: Line 145:
  
 <code php> <code php>
-$classLoader = new \SplClassLoader(\SplClassLoader::MODE_SILENT);+$classLoader = new \SplClassLoader(); 
 +$classLoader->setMode(\SplClassLoader::MODE_SILENT);
 $classLoader->setIncludePathLookup(true); $classLoader->setIncludePathLookup(true);
 $classLoader->add('PEAR'); $classLoader->add('PEAR');
Line 154: Line 155:
  
 <code php> <code php>
-$classLoader = new \SplClassLoader(\SplClassLoader::MODE_NORMAL | \SplClassLoader::MODE_DEBUG);+$classLoader = new \SplClassLoader(); 
 +$classLoader->setMode(\SplClassLoader::MODE_NORMAL | \SplClassLoader::MODE_DEBUG);
 $classLoader->add('Symfony', '/path/to/symfony'); $classLoader->add('Symfony', '/path/to/symfony');
 $classLoader->add('Zend', '/path/to/zf'); $classLoader->add('Zend', '/path/to/zf');
Line 195: Line 197:
  *  *
  * Example usage:  * Example usage:
 + *
 +     $classLoader = new \SplClassLoader();
  *  *
      // Configure the SplClassLoader to act normally or silently      // Configure the SplClassLoader to act normally or silently
-     $classLoader = new \SplClassLoader(\SplClassLoader::MODE_NORMAL);+     $classLoader->setMode(\SplClassLoader::MODE_NORMAL);
  *  *
      // Add a namespace of classes      // Add a namespace of classes
Line 246: Line 250:
      * @var integer      * @var integer
      */      */
-    private $mode;+    private $mode = self::MODE_NORMAL;
  
     /**     /**
-     Creates a new <tt>SplClassLoader</tt> that loads classes of the specified namespace. +     {@inheritdoc}
-     * +
-     @param integer $mode Autoloader work mode.+
      */      */
-    public function __construct($mode = self::MODE_NORMAL)+    public function setMode($mode)
     {     {
     if ($mode & self::MODE_SILENT && $mode & self::MODE_NORMAL) {     if ($mode & self::MODE_SILENT && $mode & self::MODE_NORMAL) {
-    throw new \InvalidArgumentException( +        throw new \InvalidArgumentException( 
-         sprintf('Cannot have %s working normally and silently at the same time!', __CLASS__) +             sprintf('Cannot have %s working normally and silently at the same time!', __CLASS__) 
-     );+         );
     }     }
          
Line 336: Line 338:
                  
         switch (true) {         switch (true) {
-        case ($this->mode & self::MODE_SILENT): +            case ($this->mode & self::MODE_SILENT): 
-             if ($resourceAbsolutePath !== false) { +                if ($resourceAbsolutePath !== false) { 
-             require $resourceAbsolutePath; +                    require $resourceAbsolutePath; 
-             +                
-             break;+                break;
                  
             case ($this->mode & self::MODE_NORMAL):             case ($this->mode & self::MODE_NORMAL):
Line 349: Line 351:
                  
         if ($this->mode & self::MODE_DEBUG && ! $this->isResourceDeclared($resourceName)) {         if ($this->mode & self::MODE_DEBUG && ! $this->isResourceDeclared($resourceName)) {
-        throw new \RuntimeException( +            throw new \RuntimeException( 
-         sprintf('Autoloader expected resource "%s" to be declared in file "%s".', $resourceName, $resourceAbsolutePath) +                sprintf('Autoloader expected resource "%s" to be declared in file "%s".', $resourceName, $resourceAbsolutePath) 
-         );+            );
         }         }
     }     }
Line 363: Line 365:
      */      */
     private function getResourceAbsolutePath($resourceName)     private function getResourceAbsolutePath($resourceName)
 +    {
         $resourceRelativePath = $this->getResourceRelativePath($resourceName);         $resourceRelativePath = $this->getResourceRelativePath($resourceName);
                  
Line 380: Line 383:
                  
         if ($this->includePathLookup && ($resourceAbsolutePath = stream_resolve_include_path($resourceRelativePath)) !== false) {         if ($this->includePathLookup && ($resourceAbsolutePath = stream_resolve_include_path($resourceRelativePath)) !== false) {
-        return $resourceAbsolutePath;+            return $resourceAbsolutePath;
         }         }
                  
Line 439: Line 442:
 ===== Changelog ===== ===== Changelog =====
  
 +  * 2011-11-09 Christian Kaps: Update examples to use the new interface(setMode)
 +  * 2011-11-08 Guilherme Blanco: Removed constructor prototype and created setMode.
   * 2011-11-08 Guilherme Blanco: Updated SplClassLoader implementation.   * 2011-11-08 Guilherme Blanco: Updated SplClassLoader implementation.
   * 2011-11-07 Guilherme Blanco: Expanded extended rules.   * 2011-11-07 Guilherme Blanco: Expanded extended rules.
Line 445: Line 450:
   * 2010-03-22 Guilherme Blanco: Initial RFC creation.   * 2010-03-22 Guilherme Blanco: Initial RFC creation.
  
 +===== Comments =====
 +  * laruence : I have already got a similar loader implemented in Yaf, called Yaf_Loader, you can found the source here: [[http://svn.php.net/viewvc/pecl/yaf/trunk/yaf_loader.c?view=markup]]
rfc/splclassloader.txt · Last modified: 2017/09/22 13:28 (external edit)