18 public static function addDir( $path ) { static::addDirs( $path ); }
22 public static function addFile( $file ){ static::addFiles( $file ); }
26 public static function addSeparator( $sep ) { static::addSeparators( $sep ); }
32 static::addConventions( $conventions );
37 public static function addAppPath( $path ) { static::addAppPaths( $path ); }
41 public static function getAppPath( $type = null ) {
return static::getAppPaths( $type ); }
49 if ( !is_array( $paths ) ) { $paths = array( $paths ); }
50 foreach ( $paths as $k => $v )
52 $path = realpath( $v );
55 $result = ( !is_int( $k ) ) ? static::_addNamespaceDirectory(
56 str_replace(
'\\' ,
'' , $k ) , $path ) : static::_addDirectory( $path );
57 if ( !$result ) { unset( $paths[ $k ] );
continue; }
61 trigger_error(
'The path "' . $v .
62 '" does not exists or is not accessible!' , E_USER_ERROR );
67 static::_debug( $paths ,
'Added path(s) to autoloader' ,
'Autoloader' );
76 if ( !is_array( $files ) ) { $files = array( $files ); }
77 foreach ( $files as $k => $v )
79 $file = realpath( $v );
83 $key = ( substr( $k , 0 , 1 ) ==
'\\' ) ? substr( $k , 1 ) : $k;
85 if ( @array_key_exists( $key , @static::$_dirs[
'files' ] ) )
87 trigger_error(
'Cannot redeclare "' . $k .
'" as class name!' , E_USER_ERROR );
88 unset( $files[ $k ] );
91 @static::$_dirs[
'files' ][ $key ] = $file;
93 else { trigger_error(
'the file "' . $v .
'" cannot be found!' , E_USER_ERROR ); }
95 if ( $files ) { static::_debug( $files ,
'Added file(s) to autoloader' ,
'Autoloader' ); }
103 public static function register( $addThisPath = true , $useHelpers = true , $registerAutoLoader = true )
105 $this_class = get_called_class( );
106 if ( $addThisPath ) { static::addDir( dirname( __FILE__ ) ); }
107 if ( $registerAutoLoader ) { spl_autoload_register( array( $this_class ,
'load' ) ); }
108 if ( $useHelpers && file_exists( dirname( __FILE__ ) .
'/ptc-helpers.php' ) )
110 require_once( dirname( __FILE__ ) .
'/ptc-helpers.php' );
112 if ( $useHelpers && file_exists( dirname( __FILE__ ) .
'/PtcEvent.php' ) )
116 $namespace = @strtoupper( @str_replace(
'\\' ,
'_' , __NAMESPACE__ ) ) .
'_';
117 @define(
'_PTCHANDYMAN_' . $namespace , $this_class );
118 static::_debug(
'Autoloader registerd' ,
'' ,
'Autoloader' );
135 $seps = ( is_array( $sep ) ) ? $sep : array( $sep );
136 foreach ( $seps as $k => $v )
138 if ( in_array( $v , static::$_separators ) )
140 static::_debug(
'Separator "' . $v .
' " already present!' ,
'' ,
'Autoloader' );
143 static::$_separators[ ] = $v;
153 $conventions = ( is_array( $conventions ) ) ? $conventions : array( $conventions );
154 foreach ( $conventions as $k => $v)
156 if ( in_array( $v, static::$_namingConventions ) )
158 static::_debug(
'Separator "' . $v .
' " already present!' ,
'' ,
'Autoloader' );
161 static::$_namingConventions[ ] = $v;
169 public static function getDirs( $type = null )
171 if ( $type && !in_array( $type , array(
'directories' ,
'ns' ,
'files' ) ) )
173 trigger_error(
'No directories are present with the "' .
174 $type .
'" parameter!' , E_USER_WARNING );
177 else if ( $type && !@static::$_dirs[ $type ] ) {
return false; }
178 if ( @static::$_dirs[ $type ] ) {
return static::$_dirs[ $type ]; }
179 return @static::$_dirs;
188 if ( empty( static::$_appPaths ) ) { static::_buildAppPaths( ); }
189 if ( !$name ) {
return static::$_appPaths; }
190 if ( !@array_key_exists( $name , static::$_appPaths ) )
192 trigger_error(
'No paths found with the option "' . $name .
'"!' , E_USER_WARNING );
195 return static::$_appPaths[ $name ];
203 if ( empty( static::$_appPaths ) ) { static::_buildAppPaths( ); }
204 foreach ( $paths as $k => $v )
206 if ( !$path = @realpath( $v ) )
208 trigger_error(
'The file or path "' . $v .
209 '" does not exists or is not accessible!' , E_USER_ERROR );
212 @$add_paths[ $k ] = $path;
214 static::$_appPaths = array_merge( static::$_appPaths , $add_paths );
224 if ( !$object ){
return null; }
225 if ( is_string( $object ) )
227 if ( !class_exists( $object ) ){
return null; }
228 $reflection = new \ReflectionProperty( $object , $propertyName );
229 if ( !$reflection ){
return null; }
230 $reflection->setAccessible(
true );
231 return $reflection->getValue( );
233 $class = new \ReflectionClass( $object );
234 if ( !$class ){
return null; }
235 if( !$class->hasProperty( $propertyName ) )
237 trigger_error(
'Property "' . $propertyName .
'" not found in class "' .
238 get_class( $object ) .
'"!' , E_USER_WARNING );
241 $property = $class->getProperty( $propertyName );
242 $property->setAccessible(
true );
243 return $property->getValue( $object );
251 public static function load( $class )
254 if ( @array_key_exists( $class , @static::$_dirs[
'files' ] ) )
256 $msg = array(
'file' => static::$_dirs[
'files' ][ $class ] ,
'class' => $class );
257 static::_debug( $msg ,
'Included class file' ,
'Autoloader' );
258 require_once( static::$_dirs[
'files' ][ $class ] );
261 else if ( strpos( $class ,
'\\' ) )
263 $folders = explode(
'\\' , $class );
264 if ( @static::$_dirs[
'ns' ][ $folders[ 0 ] ] )
266 $path = static::$_dirs[
'ns' ][ $folders[ 0 ] ];
267 unset( $folders[ 0 ] );
268 $class_name = end( $folders );
269 if (
sizeof( $folders ) > 1 )
271 array_pop( $folders );
272 foreach ( $folders as $k => $v ) { $path .= DIRECTORY_SEPARATOR . $v; }
274 if ( !@realpath( $path ) )
276 trigger_error(
'The path "' . $path .
277 '" does not exists or is not accessible!' , E_USER_ERROR );
280 if ( $found = static::_loadClass( $class_name , $path , $class ) ) {
return true; }
283 else if ( @static::$_dirs[
'directories' ] )
285 foreach ( static::$_dirs[
'directories' ] as $k => $v )
287 if ( $found = static::_loadClass( $class , $v ) ) {
return true; }
315 protected static function _loadClass( $class , $path , $namespace = null )
317 $class_name = ( $namespace ) ? $namespace : $class;
318 $path = $path . DIRECTORY_SEPARATOR;
319 if ( file_exists( $path . $class .
'.php' ) )
321 static::_debug( array(
'file' => $path . $class .
'.php' ,
'class' => $class_name ),
322 'Included class file' ,
'Autoloader' );
323 require_once( $path . $class .
'.php');
326 else if ( file_exists( $path . $new_file = strtolower( $class ) .
'.php' ) )
328 static::_debug( array(
'file' => $path . $new_file ,
'class' => $class_name ),
329 'Included class file' ,
'Autoloader' );
330 require_once( $path . $new_file );
333 else{
return static::_guessFileName( $class , $path , $namespace ); }
345 $class_name = ( $namespace ) ? $namespace : $class;
346 foreach ( static::$_separators as $sep )
348 foreach ( static::$_namingConventions as $convention )
350 $filename = str_replace( array(
'{SEP}' ,
'{CLASS}' ) ,
351 array( $sep , $class ) , $convention );
352 if ( file_exists( $path . $filename .
'.php') )
354 static::_debug( array(
'file' => $path . $filename .
'.php' ,
355 'class' => $class_name ) ,
'Included class file' ,
'Autoloader' );
356 require_once( $path . $filename .
'.php');
359 else if ( file_exists( $path . $new_file = strtolower( $filename ) .
'.php') )
361 static::_debug( array(
'file' => $path . $new_file ,
'class' => $class_name ) ,
362 'Included class file' ,
'Autoloader' );
363 require_once( $path . $new_file );
366 else if ( $sep !=
'_')
368 $replaced = str_replace(
'_' , $sep , $filename );
369 if ( file_exists( $path . $replaced .
'.php' ) )
371 static::_debug( array(
'file' =>$path . $replaced .
'.php' ,
372 'class' => $class_name ) ,
'Included class file',
'Autoloader');
373 require_once( $path . $replaced .
'.php');
376 else if ( file_exists( $file = $path . strtolower( $replaced ) .
'.php' ) )
378 static::_debug( array(
'file' => $file ,
379 'class' => $class_name ) ,
'Included class file' ,
'Autoloader' );
380 require_once( $file );
393 static::$_appPaths = array
395 'docRoot' => @$_SERVER[
'DOCUMENT_ROOT' ] ,
396 'handyMan' => dirname( __FILE__ )
406 if ( @in_array( $directory , @static::$_dirs[
'directories' ] ) )
408 static::_debug( $directory ,
'Path already exists!' ,
'Autoloader' );
411 @static::$_dirs[
'directories' ][ ] = $directory;
422 if ( @array_key_exists( $namespace , @static::$_dirs[
'ns' ] ) )
424 trigger_error(
'Cannot redeclare namespace "' . $namespace .
'"!' , E_USER_ERROR );
427 @static::$_dirs[
'ns' ][ $namespace ] = $directory;
436 protected static function _debug( $string , $statement = null , $category = null )
438 if ( !defined(
'_PTCDEBUG_NAMESPACE_' ) ) {
return false; }
439 return @call_user_func_array( array(
'\\' . _PTCDEBUG_NAMESPACE_ ,
'bufferLog' ) ,
440 array( $string , $statement , $category ) );
static _guessFileName($class, $path, $namespace=null)
Tries to guess the filename to load based on the naming conventions and the separator properties...
static $_dirs
Paths for directories to autoload classes.
static register()
Registers the component with a constant for ptc helpers functions.
static addFiles($files)
Adds class file(s) to the autoloader.
static _buildAppPaths()
Builds application paths for the first time when PtcHandyMan::getAppPath( ) is called.
static getAppPaths($name=null)
Helper method to retrieve paths for the application.
static getSeparators()
Retrieves the separators used by the autoloader.
static _loadClass($class, $path, $namespace=null)
Classes autoloader engine, tries various possibilities for file names.
static addConventions($conventions)
Adds naming convention(s) to the PtcHandyMan::$_namingConventions property for the autoloader...
static _debug($string, $statement=null, $category=null)
Send messsages to the PtcDebug class if present.
static addDir($path)
Alias of addDirs()
static getAppPath($type=null)
Alias of getAppPaths()
static _addNamespaceDirectory($namespace, $directory)
Adds a directory to the PtcHandyMan::$_dirs array to load classes that use namespaces.
static addAppPaths($paths)
Adds paths to the PtcHandyMan::$_appPaths property for later usage.
static addFile($file)
Alias of addFiles()
static $_appPaths
Application paths property.
static addDirs($paths)
Adds path(s) to the PtcHandyMan::$_dirs property to load classes when needed.
static $_separators
Separators for naming conventions.
static addSeparator($sep)
Alias of addSeparator()
static addConvention($conventions)
Alias of addConventions()
static load($class)
Load classes automatically with namespaces support based on folder structure.
static getConventions()
Retrieves the naming conventions used by the autoloader.
static _addDirectory($directory)
Adds a directory to the PtcHandyMan::$_dirs array to autoload classes.
static $_namingConventions
Naming conventions to attempt to load with the autoloader.
static addAppPath($path)
Alias of addAppPaths()
static getDirs($type=null)
Returns the current included paths for the autoloader.
static addSeparators($sep)
Adds a separator to the PtcHandyMan::$_separators property for the autoloader.
static getProperty($object, $propertyName)
Retrieves inaccessible properties from a class or object.