autoload.lib
上传用户:xiao730204
上传日期:2007-01-04
资源大小:141k
文件大小:9k
- <?php
- /*
- Php autoloader, even implements a rudimentry example of libary
- modularization and clode cleanlyness.
- Note that it does have some performance issues when using this
- program to load massive ammounts of code / files.
- There is a optimizer library builder.
- GLOBAL variable items :
- AutoLoad - Function
- $PHP_AUTO_LOADED_FILES = CACHE HASH ARRAY
- $PHP_AUTO_LOADED_FILES_ORDER = ARRAY_STACK
- $PHP_AUTO_LOADED_OPTIMIZED_MODE = 0/1
- $PHP_AUTO_LOADER_DEBUG = 0/1
- $PHP_AUTO_LOAD_SEARCH_PATH = ARRAY_STACK
- Two global functions :
- AutoLoad( 'myobject/functionname here', 'my file ext here - otherwize .object is assumed' );
- AutoLoadOptimize( './foo.lib' );
- AutoLoadDealloc(); # Deallocates all memory that it was using
- */
- if ( $PHP_AUTO_LOAD_LIB == 0 || ! isset( $PHP_AUTO_LOAD_LIB ) ) {
- $PHP_AUTO_LOAD_LIB = 1;
- }
- if ( ! defined( $PHP_AUTO_LOADER_DEBUG ) ) {
- $PHP_AUTO_LOADER_DEBUG = 0;
- }
- if ( ! defined( $PHP_AUTO_LOADER_FILES ) ) {
- $PHP_AUTO_LOADED_FILES = Array();
- }
- if ( ! defined( $PHP_AUTO_LOADED_FILES_ORDER ) ) {
- $PHP_AUTO_LOADED_FILES_ORDER = Array();
- }
- if ( ! defined( $PHP_AUTO_LOAD_SEARCH_PATH ) ) {
- $PHP_AUTO_LOAD_SEARCH_PATH = Array();
- }
- $PHP_AUTO_LOADED_OPTIMIZED_MODE = 0;
- /*
- Let's not set this so that the user can do :
- include( './myautooptimized.lib' );
- include( './lib/autoload.lib' );
- AutoLoad( 'Foo' );
- if ( ! defined( $PHP_AUTO_LOADED_OPTIMIZED_MODE ) ) {}
- */
- if ( ! function_exists( 'phpautoloader' ) ) {
- class PhpAutoLoader {
- var $dir_delim;
- Function PhpAutoLoader() {
- $this->dir_delim = '/';
- }
- Function AddToSearchPath( $this_dir ) {
- global $PHP_AUTO_LOAD_SEARCH_PATH;
- $PHP_AUTO_LOAD_SEARCH_PATH[] = $this_dir;
- }
- Function ImportSearchPath() { /* Deprecated */ }
- Function ExportSearchPath() { /* Deprecated */ }
- Function DebugMessage( $message, $severe = 0 ) {
- global $PHP_AUTO_LOADER_DEBUG;
- if ( $PHP_AUTO_LOADER_DEBUG == 1 ) {
- echo( '<!-- ' . $message . ' -->' . "n" );
- if ( $severe == 1 ) {
- echo( '<font color="#ff0000">' );
- }
- echo( 'AUTOLOAD: ' . $message . '<br>' . "n" );
- if ( $severe == 1 ) {
- echo( '</font>' );
- }
- }
- }
- Function AutoLoad( $object_name, $type = '.object' ) {
- global $PHP_AUTO_LOADED_FILES;
- global $PHP_AUTO_LOADED_FILES_ORDER;
- global $PHP_AUTO_LOADED_OPTIMIZED_MODE;
- global $PHP_AUTO_LOAD_SEARCH_PATH;
- if ( $PHP_AUTO_LOADED_OPTIMIZED_MODE ) {
- $this->DebugMessage( 'CACHE STATE : Running in optimized mode' );
- return 1;
- }
- if ( $PHP_AUTO_LOADED_FILES[ $object_name . $type ] == 1 ) {
- $this->DebugMessage( 'CACHE STATE : Loaded already : ' . $object_name . $type );
- return 1;
- }
- if ( ! is_array( $PHP_AUTO_LOAD_SEARCH_PATH ) ) {
- $this->DebugMessage(
- 'PHP_AUTO_LOAD_SEARCH_PATH is not usable it is not an array!',
- 1
- );
- return 0;
- }
- for( $i = 0; $i < count( $PHP_AUTO_LOAD_SEARCH_PATH ) ; $i++ ) {
- $short_path =
- $PHP_AUTO_LOAD_SEARCH_PATH[ $i ] .
- $this->dir_delim . $object_name;
- // -- JEO - This is expensive --
- //$this->DebugMessage( 'Searching for file : ' . $short_path . $type );
- $this_file = $short_path . $type;
- $this_file_auto = $short_path . '.auto';
- if ( file_exists( $this_file ) ) {
- /* We've found the item */
- $this->DebugMessage( 'STATE : LOADING : ' . $object_name . $type );
- /*
- We've placed the attempt to load here since someone might
- decided to have a "loop" created by asking for a file we are
- already attempting to load
- */
- $PHP_AUTO_LOADED_FILES[ $object_name . $type ] = 1;
- /* Look for a .auto to make it autoload prereqs */
- if ( file_exists( $this_file_auto ) ) {
- $this->DebugMessage( 'Loading auto file : ' . $this_file_auto );
- include( $this_file_auto );
- }
- $this->DebugMessage( 'LOADING : ' . $this_file );
- include( $this_file );
- $PHP_AUTO_LOADED_FILES_ORDER[] = $this_file;
- return 1;
- }
- } /* End search */
- /* Default fall through... unable to find item */
- $this->DebugMessage( 'Could not find : ' . $object_name . $type, 1 );
- return 0;
- }
- Function AutoLoadOptimize( $output_library ) {
- global $PHP_AUTO_LOADED_FILES_ORDER;
- global $PHP_AUTO_LOADED_OPTIMIZED_MODE;
- global $PHP_AUTO_LOAD_SEARCH_PATH;
- if ( $PHP_AUTO_LOADED_OPTIMIZED_MODE == 1 ) {
- /* Reasoning :
- To create a optimized library all of the source must be present
- */
- return 1;
- }
- echo( '<br>' ) ;
- echo( 'Preparing to write optimized library : ' );
- echo( '<ul>' );
- $file_output = '';
- $file_output =
- trim( '<?php $PHP_AUTO_LOADED_OPTIMIZED_MODE = 1; ?>' );
- $ac = count( $PHP_AUTO_LOADED_FILES_ORDER );
- for( $x = 0; $x < $ac; $x++ ) {
- $file_name = $PHP_AUTO_LOADED_FILES_ORDER[ $x ];
- /* We are going to asume if a object and it's .auto were loaded all
- prereqs were met so therefor we dont need to do the .auto */
- if ( ! ereg( '.auto$', $file_name, $regs ) ) {
- echo( '<li>' . $file_name . " - $xn" );
- $file_output .= trim( '<?php
- if ( $PHP_AUTO_LOADED_FILES[ "' . $file_name . '" ] != 1 ) {
- /* ' . $file_name . ' */
- $PHP_AUTO_LOADED_FILES[ "' . $file_name . '" ] = 1;
- $PHP_AUTO_LOADED_FILES_ORDER[] = "' . $file_name . '";
- ?>' ) ;
- /* Read the file in for processing */
- $temp_arr = file( $file_name );
- for( $i = 0; $i < count( $temp_arr ); $i++ ) {
- $good_stuff = '';
- $good_stuff = $temp_arr[ $i ];
- /* This peels off the comments # and */
- // $good_stuff = ereg_replace( '//(.*)+', '', $good_stuff );
- // $good_stuff = ereg_replace( '#(.*)+', '', $good_stuff );
- list( $mo_good_stuff, $bad ) = split( '//', $good_stuff );
- list( $good_stuff, $bad ) = split( '#', $mo_good_stuff );
- /* This peels off the other comments */
- $good_stuff = ereg_replace( '/*(.*)*/', '', $good_stuff );
- /* Strip most of the whitespace crap out */
- $file_output .= ' ' . trim( $good_stuff ) . ' ';
- //$file_output .= $good_stuff;
- }
- $file_output .= trim( '<?php } /* End if file is autoloaded */ ?>' );
- }
- }
- echo( '</ul>' );
- if ( $fh = fopen( $output_library, 'w' ) ) {
- $file_output =
- str_replace( '?> <?php', '?><?php', $file_output );
- fwrite( $fh, $file_output, strlen( trim( $file_output ) ) );
- fclose( $fh );
- echo( '<b>File written : ' . $output_library . '</b>' );
- } else {
- echo( '<font color="#FF0000"><center><H3>FAILED TO WRITE FILE!!!!!</H3></font></center>' );
- }
- }
- Function Deallocate() {
- global $PHP_AUTO_LOADED_FILES_ORDER;
- global $PHP_AUTO_LOADED_FILES;
- global $PHP_AUTO_LOAD_SEARCH_PATH;
- $PHP_AUTO_LOADED_FILES_ORDER = Array();
- $PHP_AUTO_LOADED_FILES = Array();
- $PHP_AUTO_LOAD_SEARCH_PATH = Array();
- }
- } /* End AutoLoader.class */
- } /* End if autoloader defined */
- if ( ! function_exists( 'autoloaddeallocate' ) ) {
- /* Global Namespace export */
- Function AutoLoadDeallocate() {
- $tmp_class = new PhpAutoLoader();
- $tmp_class->Deallocate();
- }
- } /* End if autoloaddeallocate is defined */
- if ( ! function_exists( 'autoload' ) ) {
- Function AutoLoad( $object_name, $type = '.object' ) {
- global $PHP_AUTO_LOADED_FILES_ORDER;
- global $PHP_AUTO_LOAADED_FILES;
- global $PHP_AUTO_LOADED_OPTIMIZED_MODE;
- global $PHP_AUTO_LOAD_SEARCH_PATH;
- global $PHP_AUTO_LOADER_DEBUG;
- $tmp_class = new PhpAutoLoader();
- if ( is_array( $object_name ) ) {
- for( $i = 0; $i < count( $object_name ); $i++ ) {
- $a_object = $object_name[ $i ];
- $tmp_class->AutoLoad( $a_object, $type );
- }
- return 1;
- } else {
- return $tmp_class->AutoLoad( $object_name, $type );
- }
- }
- } /* End if autoload is defined */
- if ( ! function_exists( 'autoloadoptimize' ) ) {
- Function AutoLoadOptimize( $output_lib ) {
- global $PHP_AUTO_LOAADED_FILES_ORDER;
- global $PHP_AUTO_LOAADED_FILES;
- global $PHP_AUTO_LOADED_OPTIMIZED_MODE;
- global $PHP_AUTO_LOAD_SEARCH_PATH;
- $tmp_class = new PhpAutoLoader();
- return $tmp_class->AutoLoadOptimize( $output_lib );
- }
- } /* end if autoloadoptimize exists */
- if ( ! function_exists( 'autoloadaddpath' ) ) {
- Function AutoLoadAddPath( $path_elem ) {
- global $PHP_AUTO_LOAADED_FILES_ORDER;
- global $PHP_AUTO_LOAADED_FILES;
- global $PHP_AUTO_LOADED_OPTIMIZED_MODE;
- global $PHP_AUTO_LOAD_SEARCH_PATH;
- $tmp_class = new PhpAutoLoader();
- if ( is_array( $path_elem ) ) {
- for( $i = 0; $i < count( $path_elem ); $i++ ) {
- $tmp_class->AddToSearchPath( $path_elem[ $i ] );
- }
- } else {
- $tmp_class->AddToSearchPath( $path_elem );
- }
- }
- } /* end if autoload add path defined */
- ?>