[ Index ]
 

Code source de LifeType 1.2.4

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/class/controller/ -> resourceclassloader.class.php (source)

   1  <?php
   2  
   3      define("DEFAULT_CLASSFILE_SUFFIX", ".class.php");
   4  
   5      /**
   6       * \ingroup Controller
   7       *
   8        * This class takes care of dynamically loading classes for the controller. 
   9        *
  10        * This is a very simple class that keeps a list of folders where action classes can be found. Whenever
  11        * the controller requests to load an action class, this loader will go one by one through all the folders
  12        * until the class is found, or it will fail if none can be found. 
  13        *
  14        * New folders to be scanned can be added via the static method ResourceClassLoader::addSearchFolder(),
  15        * and the loading of classes takes place in the ResourceClassLoader::load() method.
  16        *
  17        * This object should never be instantiated directly but instead, please use the 
  18        * ResourceClassLoader::getLoader() method which will return a reference to an already existing instance.
  19       */
  20      class ResourceClassLoader 
  21      {
  22  
  23          var $_paths;
  24          var $_classFileSuffix;
  25  
  26          /**
  27           * initializes the class loader. It is advisable to use the 
  28           * static ResourceClassLoader::getLoader() method
  29           *
  30            * @param path The starting path where classes can be loaded. It defaults to "./"
  31           * @param classFileSuffix default suffix that each class file will have. It defaults to
  32           * ".class.php"
  33           */
  34  		function ResourceClassLoader( $path = "./", $classFileSuffix = DEFAULT_CLASSFILE_SUFFIX )
  35          {
  36              $this->_paths = Array( $path );
  37              $this->_classFileSuffix = $classFileSuffix;
  38          }
  39  
  40          /**
  41           * static method that returns a single instance of this class. 
  42           *
  43           * @static
  44           * @param path If the object is being created for the first time, this will be passed to the constructor
  45           * of the class as the first parameter. If a class instance already exists, this path will be added to the
  46           * list of already existing paths to scan.
  47           * @return a ResourceClassLoader object
  48           */
  49          function &getLoader( $path = null )
  50          {
  51              static $instance;
  52  
  53              if( $instance == null ) {
  54                  // create an instance if it does not exist yet...
  55                  $instance = new ResourceClassLoader();
  56              }
  57  
  58              // if a path is given and the object already exists, then
  59              // we can also automatically add it to the list of searched folders...
  60              if( $path != null ) 
  61                  $instance->addSearchFolder( $path );
  62          
  63              return $instance;
  64          }
  65  
  66          /**
  67           * Adds a new folder to the list of folders to be searched
  68           * 
  69           * @param folder The new folder that will be added
  70           * @return always true
  71           */
  72  		function addSearchFolder( $folder )
  73          {        
  74              $this->_paths[] = $folder;
  75  
  76              return true;
  77          }
  78  
  79          /**
  80           * sets a new suffix for class files, in case our class files do not end with .class.php. Please
  81           * note that only <b>one</b> suffix can be used at the same time.
  82           *
  83           * @param suffix the new suffix
  84           * @return always true
  85           */
  86  		function setClassFileSuffix( $suffix )
  87          {
  88              $this->_classFileSuffix = $suffix;
  89  
  90              return true;
  91          }
  92          
  93          /**
  94           * Loads classes from disk using the list of folders that has been provided 
  95           * via ResourceClassLoader::addSearchFolder() The class will go through all the folders where 
  96           * classes can be located and if it can be found, it will proceed to load it. 
  97           * If not, an exception will be thrown
  98           * 
  99           * @param actionClassName name of the class that we are going to load, <b>without the class suffix</b>
 100            * @return True if successful
 101           */
 102  		function load( $actionClassName )
 103          {
 104              lt_include(PLOG_CLASS_PATH . "class/file/file.class.php");            
 105              
 106              //foreach( $this->_paths as $path ) {
 107              $i = 0;
 108              $loaded = false;
 109              
 110              $numPaths = count( $this->_paths );
 111              while( ($i < $numPaths ) && !$loaded ) {
 112                  // get the current folder
 113                  $path = $this->_paths[$i];
 114                  // build up the file name
 115                  $fileName = $path.strtolower($actionClassName).$this->_classFileSuffix;
 116                  // and see if it exists and can be loaded
 117                  lt_include( PLOG_CLASS_PATH."class/file/file.class.php" );
 118                  if( File::exists( $fileName ) && File::isReadable( $fileName )) {
 119                      lt_include( $fileName );
 120                      $loaded = true;
 121                  }
 122                  // increase the counter
 123                  $i++;
 124              }
 125  
 126              // did we load anything??
 127              if( !$loaded ) {
 128                  die( "Could not load $actionClassName!" );
 129              }
 130  
 131              // otherwise return everything ok!
 132              return true;
 133          }
 134      }
 135  ?>


Généré le : Mon Nov 26 21:04:15 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics