コード例 #1
0
ファイル: AbstractLoader.php プロジェクト: asherwunk/primus2
 /**
  * Add a path to the path pool
  * 
  * Use Identifier to override the built in identity engine
  * Otherwise to get the identifier you'll have to pass the path
  * back into the class.
  * 
  * @param string $path The actual path to add
  * @param string|int The desired path identifier
  * 
  */
 public function addPath($path, $identifier = '')
 {
     if ($path instanceof File\Path) {
         if (!$identifier) {
             $identifier = $path->getIdentifier();
         }
         if (!array_key_exists($identifier, $this->paths->getList())) {
             $this->paths[$identifier] = $path;
         }
         if ($this->conf->fileextension != 'php') {
             $this->paths[$identifier]->removeExtension('php');
             $this->paths[$identifier]->addExtension($this->conf->fileextension);
         }
         return $identifier;
     } else {
         if (!in_array($path, $this->getPaths())) {
             $addition = new File\Path($path, $identifier);
             $identifier = $addition->getIdentifier();
             $this->paths[$identifier] = $addition;
             return $identifier;
         } else {
             return $this->getIdentifier($path);
         }
     }
     return false;
 }
コード例 #2
0
ファイル: AutoLoader.php プロジェクト: asherwunk/primus2
 /**
  * Autoload a Class
  * 
  * This is the meat of the whole operation.  Everything up until now
  * (AbstractLoader, GenericInterface, Module, Path, etc.) has been
  * constructed for this function to occur.
  * 
  * This function first establishes a base path either from a predefined
  * constant (presumably from the bootstrap), the default path set in the
  * autoloader (this is obviously overwritten by the constant), or, in the
  * case that these are not specified, two folders up ( /library/ ).
  * 
  * Then it parses the class string with the appropriate delimiters.
  * 
  * It moves on to parse through the modules, constructing a path,
  * by concatenating relative paths and replacing with absolute paths.
  * 
  * If path remnants are left over, it constructs a NEW path (losing
  * extensions but not prefixes).  This is important, if you want
  * extensions the path object must be in the terminating module.
  * 
  * It then checks file existence with prefixes, and then it processes
  * through independent paths (like an include path string)
  * 
  * NOTE: This function uses require_once
  * 
  * @param string $class The fully qualified class name to load
  * 
  */
 public function autoload($class)
 {
     $realBasePath = '';
     if (defined('FALCRAFT_APPLICATION_PATH')) {
         $realBasePath = FALCRAFT_APPLICATION_PATH;
     } else {
         if ($this->default) {
             // default from abstract
             $realBasePath = $this->getPath($this->default);
         } else {
             $realBasePath = realpath(__DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR);
         }
     }
     // http://stackoverflow.com/questions/2860238/exploding-by-array-of-delimiters
     $udelim = $this->delimiters[0];
     $step = str_replace($this->delimiters, $udelim, $class);
     $keys = explode($udelim, $step);
     $classToLoad = array_pop($keys);
     if (!$keys) {
         // This is a built in non namespaced class (or should be)
         return;
     }
     $prefixes = array();
     $basePath = $realBasePath;
     if ($this->default) {
         $path = $this->getPathObject($this->default);
     } else {
         $path = new File\Path($this->libraries->getPath());
     }
     if ($this->isLibrary($keys[0])) {
         $module = $this->getLibrary($keys[0]);
         while ($keys) {
             if ($this->isIdentifier($module->getPath())) {
                 $path = $this->getPathObject($module->getPath());
                 if (!$path->isRelative()) {
                     $basePath = $path->getPath() . DIRECTORY_SEPARATOR;
                 } else {
                     $basePath .= DIRECTORY_SEPARATOR . $path->getPath() . DIRECTORY_SEPARATOR;
                 }
                 if ($this->hasPrefix($module->getPath())) {
                     $prefixes = $this->prefixes[$module->getPath()];
                 }
             }
             array_shift($keys);
             if ($keys) {
                 if ($module->isModule($keys[0])) {
                     $module = $module->getModule($keys[0]);
                 } else {
                     break;
                 }
             }
         }
     }
     if (count($keys)) {
         $path = implode(DIRECTORY_SEPARATOR, $keys);
         $path = new File\Path($path);
     }
     $unprefixedClassToLoad = '';
     if ($prefixes) {
         foreach ($prefixes as $prefix) {
             if (strpos($classToLoad, $prefix) === 0) {
                 $unprefixedClassToLoad = substr($classToLoad, strlen($prefix));
                 /* ?? - $unprefixedClassToLoad =
                    substr( $classToLoad, count( $prefix ) ); */
             }
         }
     }
     if ($unprefixedClassToLoad && ($retPath = $path->isFilename($unprefixedClassToLoad, '', $basePath))) {
         require_once $retPath;
         return;
     } else {
         if ($retPath = $path->isFilename($classToLoad, '', $basePath)) {
             require_once $retPath;
             return;
         }
     }
     foreach ($this->getIndependentPathObjects() as $ipath) {
         $base = $realBasePath;
         if (!$ipath->isRelative()) {
             $base = '';
         }
         $relativeClassToLoad = $path->getPath() . DIRECTORY_SEPARATOR . $classToLoad;
         if ($unprefixedClassToLoad) {
             $relativeUnprefixedClassToLoad = $path->getPath . DIRECTORY_SEPARATOR . $unprefixedClassToLoad;
         } else {
             $relativeUnprefixedClassToLoad = '';
         }
         if ($unprefixedClassToLoad && ($retPath = $ipath->isFilename($unprefixedClassToLoad, '', $base))) {
             require_once $retPath;
             return;
         } else {
             if ($retPath = $ipath->isFilename($classToLoad, '', $base)) {
                 require_once $retPath;
                 return;
             } else {
                 if ($relativeUnprefixedClassToLoad && ($retPath = $ipath->isFilename($relativeUnprefixedClassToLoad, '', $base))) {
                     require_once $retPath;
                     return;
                 } else {
                     if ($retPath = $ipath->isFilename($relativeClassToLoad, '', $basePath)) {
                         require_once $retPath;
                         return;
                     }
                 }
             }
         }
     }
     return;
 }