Source for file ALoader.php

Documentation is available at ALoader.php

  1. <?php
  2. /**
  3.  * Autoloader for Alia
  4.  *
  5.  * @author Jordan Wambaugh <jordan@wambaugh.org>
  6.  */
  7.  
  8.  
  9.  
  10. /**
  11.  * 
  12.  */
  13. require_once 'ASingletonInterface.php';
  14.  
  15. $loader ALoader::instance();
  16. $loader->addPathToEnd(".");
  17.  
  18. /**
  19.  * A class/file loader for Alia
  20.  * @author Jordan Wambaugh <jordan@wambaugh.org>
  21.  * @package Alia
  22.  * @subpackage Core
  23.  */
  24. class ALoader {
  25.     static private $instanceObject=null;
  26.     private $paths=array();
  27.     private $libPath;
  28.  
  29.     private $replacements=array();
  30.  
  31.  
  32.     /**
  33.      * returns the instance of the ALoader object
  34.      *
  35.      * @return ALoader 
  36.      */
  37.     public function instance(){
  38.         if(self::$instanceObject==null){
  39.             self::$instanceObject new ALoader();
  40.         }
  41.         return self::$instanceObject;
  42.     }
  43.     
  44.     
  45.     public function loadClass($class){
  46.         foreach ($this->paths as $path){
  47.         
  48.             if(file_exists($path."/$class.php")){
  49.                 include$path."/$class.php";
  50.             }
  51.         }
  52.     }
  53.  
  54.  
  55.     /**
  56.      * includes a file once
  57.      *
  58.      * @param string $fileName 
  59.      */
  60.     public function includeFileOnce($fileName){
  61.         require_once($this->getFilePath($fileName));
  62.     }
  63.  
  64.  
  65.     /**
  66.      * includes a file
  67.      *
  68.      * @param string $fileName 
  69.      */
  70.     public function includeFile($fileName){
  71.         require ($this->getFilePath($fileName));
  72.         return ;
  73.     }
  74.  
  75.     /**
  76.      * returns the full path to the file
  77.      *
  78.      * @param string $fileName 
  79.      */
  80.     public function getFilePath($fileName){
  81.  
  82.         foreach ($this->replacements as $replacement){
  83.             $fileName str_replace($replacement[0],$replacement[1],$fileName);
  84.         }
  85.  
  86.         foreach ($this->paths as $path){
  87.             //echo $path."/$fileName"."<br>";
  88.             if(file_exists($path."/$fileName")){
  89.                 return $path."/$fileName";
  90.             }
  91.         }
  92.         throw new Exception("File '$fileName' not found in any paths!");
  93.     }
  94.  
  95.  
  96.     /**
  97.      * get the contents of a file
  98.      *
  99.      * @param string $fileName 
  100.      * @return string 
  101.      */
  102.     public function getFileContents($fileName){
  103.         return file_get_contents($this->getFilePath($fileName));
  104.         
  105.     }
  106.  
  107.     
  108.     /**
  109.      * adds a path to the beginning of the path list. paths in the beginning are searched first.
  110.      *
  111.      * @param string $path 
  112.      */
  113.     public function addPathToBeginning($path){
  114.         if(!is_dir($path)){
  115.             throw new Exception("specified path $path does not exist");
  116.         }
  117.         array_unshift($this->paths,$path);
  118.     }
  119.  
  120.     /**
  121.      * Adds a path to the end of the path list. paths added to the end are searched last.
  122.      *
  123.      * @param unknown_type $path 
  124.      */
  125.     public function addPathToEnd($path){
  126.         if(!is_dir($path)){
  127.             throw new Exception("specified path $path does not exist");
  128.         }
  129.         array_push($this->paths,$path);
  130.     }
  131.  
  132.     /**
  133.      * sets the path to the alia libray
  134.      * @param string $path 
  135.      */
  136.     public function setLibPath($path){
  137.         $this->libPath = $path;
  138.         $this->addPathToEnd($this->libPath);
  139.         $this->addPathToEnd($this->libPath."/layouts");
  140.         $this->addPathToEnd($this->libPath."/widgets");
  141.         $this->addPathToEnd($this->libPath."/validator");
  142.     }
  143.  
  144.     
  145.     /**
  146.      * addReplacement
  147.      * Adds a string replacement rule to the loader.
  148.      * All instances of $search will be replaced with $replace while loading paths.
  149.      * 
  150.      * @param mixed $search 
  151.      * @param mixed $replace 
  152.      * @access public
  153.      * @return void 
  154.      */
  155.     public function addReplacement($search$replace){
  156.         $this->replacements[]=array($search,$replace);
  157.     }
  158.  
  159. }
  160. spl_autoload_register((array(ALoader::instance(),'loadClass')));

Documentation generated on Fri, 11 Jul 2008 13:28:41 -0400 by phpDocumentor 1.4.2