lithium\core\Libraries::path()
Get the corresponding physical file path for a class or namespace name.
Parameters
-
string
$class
The class name to locate the physical file for. If
$options['dirs']
is set totrue
,$class
may also be a namespace name, in which case the corresponding directory will be located. -
array
$options
Options for converting
$class
to a physical path:'dirs'
: Defaults tofalse
. Iftrue
, will attempt to case-sensitively look up directories in addition to files (in which case$class
is assumed to actually be a namespace).
Returns
stringReturns the absolute path to the file containing $class
, or null
if the
file cannot be found.
Source
public static function path($class, array $options = []) {
$defaults = ['dirs' => false];
$options += $defaults;
$class = ltrim($class, '\\');
if (isset(static::$_cachedPaths[$class]) && !$options['dirs']) {
return static::$_cachedPaths[$class];
}
if (isset(static::$_map[$class]) && !$options['dirs']) {
return static::$_map[$class];
}
foreach (static::$_configurations as $name => $config) {
$params = $options + $config;
$suffix = $params['suffix'];
if ($params['prefix'] && strpos($class, $params['prefix']) !== 0) {
continue;
}
if ($transform = $params['transform']) {
if ($file = static::_transformPath($transform, $class, $params)) {
return $file;
}
continue;
}
$path = str_replace("\\", '/', substr($class, strlen($params['prefix'])));
$fullPath = "{$params['path']}/{$path}";
if (!$options['dirs']) {
return static::$_cachedPaths[$class] = static::realPath($fullPath . $suffix);
}
$list = glob(dirname($fullPath) . '/*');
$list = array_map(function($i) { return str_replace('\\', '/', $i); }, $list);
if (in_array($fullPath . $suffix, $list)) {
return static::$_cachedPaths[$class] = static::realPath($fullPath . $suffix);
}
return is_dir($fullPath) ? static::realPath($fullPath) : null;
}
}