[pLog-svn] r465 - plog/trunk/class/controller

oscar at devel.plogworld.net oscar at devel.plogworld.net
Fri Dec 10 14:48:47 GMT 2004


Author: oscar
Date: 2004-12-10 14:48:47 +0000 (Fri, 10 Dec 2004)
New Revision: 465

Added:
   plog/trunk/class/controller/resourceclassloader.class.php
Modified:
   plog/trunk/class/controller/controller.class.php
Log:
separated the logic for finding and loading classes from the controller since it's really not its main concern...


Modified: plog/trunk/class/controller/controller.class.php
===================================================================
--- plog/trunk/class/controller/controller.class.php	2004-12-10 13:59:39 UTC (rev 464)
+++ plog/trunk/class/controller/controller.class.php	2004-12-10 14:48:47 UTC (rev 465)
@@ -8,6 +8,7 @@
     include_once( PLOG_CLASS_PATH."class/object/object.class.php" );
     include_once( PLOG_CLASS_PATH."class/action/action.class.php" );
     include_once( PLOG_CLASS_PATH."class/action/actioninfo.class.php" );
+	include_once( PLOG_CLASS_PATH."class/controller/resourceclassloader.class.php" );
 
     //
     // various constants that will come handy
@@ -94,6 +95,9 @@
 			
 			// default folder where actions are located
 			$this->actionFolderPath = PLOG_CLASS_PATH.'class/action/';
+
+		// folder where classes can be loaded
+		$this->_loader = new ResourceClassLoader( $this->actionFolderPath );
         }
 		
 		/**
@@ -104,7 +108,7 @@
 		function setActionFolderPath( $newActionFolderPath )
 		{
 			$this->log->debug("new folder path = $newActionFolderPath" );
-			$this->actionFolderPath = $newActionFolderPath;
+			$this->_loader->addSearchFolder( $newActionFolderPath );
 		}
 
         /**
@@ -176,8 +180,7 @@
         function loadActionClass( $actionClass )
         {
             if( !class_exists($actionClass)) {
-                $fileName = $this->actionFolderPath.strtolower($actionClass).'.class.php';
-                include_once( $fileName );
+		$this->_loader->load( $actionClass );
             }
 
             return true;

Added: plog/trunk/class/controller/resourceclassloader.class.php
===================================================================
--- plog/trunk/class/controller/resourceclassloader.class.php	2004-12-10 13:59:39 UTC (rev 464)
+++ plog/trunk/class/controller/resourceclassloader.class.php	2004-12-10 14:48:47 UTC (rev 465)
@@ -0,0 +1,79 @@
+<?php
+
+	include_once( PLOG_CLASS_PATH.'class/object/object.class.php' );
+
+	/**
+ 	 * takes care of dynamically loading classes for the controller
+	 */
+	class ResourceClassLoader extends Object
+	{
+
+		var $_paths;
+		var $_classFileSuffix;
+
+		/**
+		 * initializes the class loader
+		 *
+ 		 * @param path The starting path where classes can be loaded
+		 * @param classFileSuffix default suffix that each class file will have
+		 */
+		function ResourceClassLoader( $path, $classFileSuffix = '.class.php' )
+		{
+			$this->Object();
+
+			$this->_paths = Array( $path );
+			$this->_classFileSuffix = $classFileSuffix;
+		}
+
+		/**
+		 * adds a new folder to the list of folders to be searched
+		 * 
+		 * @param folder
+		 * @return always true
+		 */
+		function addSearchFolder( $folder )
+		{
+			$this->_paths[] = $folder;
+
+			return true;
+		}
+		
+		/**
+		 * loads classes from disk using the list of folders that has been provided. The
+		 * class will loop through all the folders where classes can be located and if it
+		 * can be found, it will proceed to load it. If not, an exception will be thrown
+		 * 
+		 * @param actionClassName name of the class that we are going to load. 
+	 	 * @return True if successful
+		 */
+		function load( $actionClassName )
+		{
+			//foreach( $this->_paths as $path ) {
+			$i = 0;
+			$loaded = false;
+			while( ($i < count( $this->_paths )) && !$loaded ) {
+				// get the current folder
+				$path = $this->_paths[$i];
+				// build up the file name
+	                	$fileName = $path.strtolower($actionClassName).$this->_classFileSuffix;
+				// and see if it exists and can be loaded
+				print("file = $fileName<br/>");
+				if( File::exists( $fileName ) && File::isReadable( $fileName )) {
+					include_once( $fileName );
+					$loaded = true;
+				}
+				// increase the counter
+				$i++;
+			}
+
+			// did we load anything??
+			if( !$loaded ) {
+				throw( new Exception( "Could not load $actionClassName!" ));
+				die();
+			}
+
+			// otherwise return everything ok!
+			return true;
+		}
+	}
+?>
\ No newline at end of file




More information about the pLog-svn mailing list