Why FileHelper
don't normalize path's by Yii::getAlias()
?
#19252
Answered
by
samdark
WinterSilence
asked this question in
Q&A
-
extended version: /**
* @inheritDoc
*/
class FileHelper extends BaseFileHelper
{
/**
* @var string[] An array of path aliases as path/alias pairs
*/
private static $aliases;
/**
* @var int the length of serialized `Yii::$aliases`, using to recalculate `self::$aliases` on
* change `Yii::$aliases`.
*/
private static $controlSumAliases = 0;
/**
* @inheritDoc
* @throws \yii\base\Exception
*/
public static function normalizePath($path, $ds = DIRECTORY_SEPARATOR)
{
return parent::normalizePath(Yii::getAlias($path), $ds);
}
/**
* @inheritDoc
* @throws \yii\base\Exception
*/
public static function copyDirectory($src, $dst, $options = [])
{
parent::copyDirectory(Yii::getAlias($src), Yii::getAlias($dst), $options);
}
/**
* @inheritDoc
* @throws \yii\base\Exception
*/
public static function removeDirectory($dir, $options = [])
{
parent::removeDirectory(Yii::getAlias($dir), $options);
}
/**
* @inheritDoc
* @throws \yii\base\Exception
*/
public static function unlink($path)
{
parent::unlink(Yii::getAlias($path));
}
/**
* @inheritDoc
* @throws \yii\base\Exception
*/
public static function createDirectory($path, $mode = 0775, $recursive = true)
{
return parent::createDirectory(Yii::getAlias($path), $mode, $recursive);
}
/**
* @inheritDoc
* @throws \yii\base\Exception
*/
public static function changeOwnership($path, $ownership, $mode = null)
{
parent::changeOwnership(Yii::getAlias($path), $ownership, $mode);
}
/**
* @inheritDoc
* @throws \yii\base\Exception
*/
public static function findDirectories($dir, $options = [])
{
return parent::findDirectories(Yii::getAlias($dir), $options);
}
/**
* @inheritDoc
* @throws \yii\base\Exception
*/
public static function findFiles($dir, $options = [])
{
return parent::findFiles(Yii::getAlias($dir), $options);
}
/**
* Returns flatten list of alias/path pairs.
*
* @return array
*/
public static function getAliases()
{
if (!isset(self::$aliases)) {
self::$aliases = [];
self::$controlSumAliases = strlen(serialize(Yii::$aliases));
/** @var string|string[] $basePaths */
foreach (Yii::$aliases as $alias => $basePaths) {
if (!is_array($basePaths)) {
$basePaths = [$alias => $basePaths];
}
foreach ($basePaths as $alias => $basePath) {
$basePath = str_replace(['\\', '/'], DIRECTORY_SEPARATOR, $basePath);
self::$aliases[$basePath] = $alias;
}
}
// sort by path length (from longest to shortest) for correct work `static::findAlias()`
uksort(self::$aliases, 'strcmp');
} elseif (self::$controlSumAliases !== strlen(serialize(Yii::$aliases))) {
// reset cache on update `Yii::$aliases`
self::$aliases = null;
}
return self::$aliases;
}
/**
* Returns the path alias, this is useful when need mask real path.
*
* @param string $path the real path
* @return string|false
*/
public static function findAlias($path)
{
$path = str_replace(['\\', '/'], DIRECTORY_SEPARATOR, $path);
foreach (static::getAliases() as $basePath => $alias) {
$length = strlen($basePath);
if (strncmp($path, $basePath, $length) === 0) {
return $alias . substr($path, $length);
}
}
return false;
}
} |
Beta Was this translation helpful? Give feedback.
Answered by
samdark
Feb 21, 2022
Replies: 1 comment
-
Because it's more low level and deals with file paths. It is like that by design. |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
WinterSilence
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Because it's more low level and deals with file paths. It is like that by design.