<?php
	class ClassLoaderMapGenerator {
		
		function generateClassLoaderMapArray( $directories = Array( "class" ) )
		{
			$classLoaderMap = array();

			foreach( $directories as $directory )
			{
				$rdi = new RecursiveDirectoryIterator( $directory );
				foreach( new RecursiveIteratorIterator( $rdi ) as $entry )
				{
        			$fileName = $entry->getFilename();
					$pattern = '/\.class\.php$/';
					if( preg_match( $pattern, $fileName ) )
						$classLoaderMap[strtolower( substr( $fileName, 0, -10 ) )] = $entry;
				}
			}
			
			return $classLoaderMap;
		}

		function writeClassLoaderMapToFile( $directories = Array( "class" ), $targetFile = "" )
		{
			if( $targetFile == "" ) 
				$targetFile = PLOG_CLASS_PATH."class/classloadermap.properties.php"; 
			
			$classLoaderMap = ClassLoaderMapGenerator::generateClassLoaderMapArray( $directories );
			
			// open the file and write the headers
			$file = new File( $targetFile );
			$file->open( "w+" );
			$file->write( "<?php\n" );
			$file->write( "global \$LTClassLoaderMap;\n" );
			$file->write( "\$LTClassLoaderMap = Array(\n" );
			
			$line = 1;
			foreach( $classLoaderMap as $fileName => $entry ) {
				$file->write( "\"$fileName\" => \"$entry\"" );
				if( $line < count($classLoaderMap ) )
					$file->write( "," );
				$file->write( "\n" );
				$line++;
			}
			
			// write the footer and close the file
			$file->write( ");\n" );
			$file->write( "?>" );
			$file->close();
			
			return( true );
		}
	}
?>