Skip to content

Instantly share code, notes, and snippets.

@tom--
Created April 13, 2012 17:41
Show Gist options
  • Save tom--/2378674 to your computer and use it in GitHub Desktop.
Save tom--/2378674 to your computer and use it in GitHub Desktop.
CS file check in autoloader
<?php
private static function _checkClassPath($path)
{
if (basename(realpath($path . '.php')) !== $path . '.php')
throw new CException("Class name does not match file name.");
}
/**
* Class autoload loader.
* This method is provided to be invoked within an __autoload() magic method.
* @param string $className class name
* @return boolean whether the class has been loaded successfully
*/
public static function autoload($className)
{
// use include so that the error PHP file may appear
if(isset(self::$classMap[$className]))
{
include(self::$classMap[$className]);
if (YII_DEBUG)
self::_checkClassPath(self::$classMap[$className]);
}
else if(isset(self::$_coreClasses[$className]))
{
include(YII_PATH . self::$_coreClasses[$className]);
if (YII_DEBUG)
self::_checkClassPath(YII_PATH . self::$_coreClasses[$className]);
}
else
{
// include class file relying on include_path
if(strpos($className,'\\')===false) // class without namespace
{
if(self::$enableIncludePath===false)
{
foreach(self::$_includePaths as $path)
{
$classFile=$path.DIRECTORY_SEPARATOR.$className.'.php';
if(is_file($classFile))
{
include($classFile);
if (YII_DEBUG)
self::_checkClassPath($classFile);
break;
}
}
}
else
{
include($className . '.php');
if (YII_DEBUG)
self::_checkClassPath($className . '.php');
}
}
else // class name with namespace in PHP 5.3
{
$namespace=str_replace('\\','.',ltrim($className,'\\'));
if(($path=self::getPathOfAlias($namespace))!==false)
{
include($path . '.php');
if (YII_DEBUG)
self::_checkClassPath($path . '.php');
}
else
return false;
}
return class_exists($className,false) || interface_exists($className,false);
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment