You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
330 lines
13 KiB
330 lines
13 KiB
12 years ago
|
<?php
|
||
|
/**
|
||
|
* Filesystem helper class file.
|
||
|
*
|
||
|
* @link http://www.yiiframework.com/
|
||
|
* @copyright Copyright (c) 2008 Yii Software LLC
|
||
|
* @license http://www.yiiframework.com/license/
|
||
|
*/
|
||
|
|
||
12 years ago
|
namespace yii\helpers;
|
||
12 years ago
|
|
||
12 years ago
|
use Yii;
|
||
12 years ago
|
|
||
|
/**
|
||
12 years ago
|
* FileHelperBase provides concrete implementation for [[FileHelper]].
|
||
|
*
|
||
|
* Do not use FileHelperBase. Use [[FileHelper]] instead.
|
||
12 years ago
|
*
|
||
|
* @author Qiang Xue <qiang.xue@gmail.com>
|
||
|
* @author Alex Makarov <sam@rmcreative.ru>
|
||
|
* @since 2.0
|
||
|
*/
|
||
12 years ago
|
class FileHelperBase
|
||
12 years ago
|
{
|
||
|
/**
|
||
|
* Normalizes a file/directory path.
|
||
|
* After normalization, the directory separators in the path will be `DIRECTORY_SEPARATOR`,
|
||
|
* and any trailing directory separators will be removed. For example, '/home\demo/' on Linux
|
||
|
* will be normalized as '/home/demo'.
|
||
|
* @param string $path the file/directory path to be normalized
|
||
|
* @param string $ds the directory separator to be used in the normalized result. Defaults to `DIRECTORY_SEPARATOR`.
|
||
|
* @return string the normalized file/directory path
|
||
|
*/
|
||
|
public static function normalizePath($path, $ds = DIRECTORY_SEPARATOR)
|
||
|
{
|
||
|
return rtrim(strtr($path, array('/' => $ds, '\\' => $ds)), $ds);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Returns the localized version of a specified file.
|
||
|
*
|
||
|
* The searching is based on the specified language code. In particular,
|
||
|
* a file with the same name will be looked for under the subdirectory
|
||
12 years ago
|
* whose name is the same as the language code. For example, given the file "path/to/view.php"
|
||
|
* and language code "zh_CN", the localized file will be looked for as
|
||
|
* "path/to/zh_CN/view.php". If the file is not found, the original file
|
||
12 years ago
|
* will be returned.
|
||
|
*
|
||
|
* If the target and the source language codes are the same,
|
||
|
* the original file will be returned.
|
||
|
*
|
||
|
* @param string $file the original file
|
||
|
* @param string $language the target language that the file should be localized to.
|
||
|
* If not set, the value of [[\yii\base\Application::language]] will be used.
|
||
|
* @param string $sourceLanguage the language that the original file is in.
|
||
|
* If not set, the value of [[\yii\base\Application::sourceLanguage]] will be used.
|
||
|
* @return string the matching localized file, or the original file if the localized version is not found.
|
||
|
* If the target and the source language codes are the same, the original file will be returned.
|
||
|
*/
|
||
|
public static function localize($file, $language = null, $sourceLanguage = null)
|
||
|
{
|
||
|
if ($language === null) {
|
||
12 years ago
|
$language = Yii::$app->language;
|
||
12 years ago
|
}
|
||
|
if ($sourceLanguage === null) {
|
||
12 years ago
|
$sourceLanguage = Yii::$app->sourceLanguage;
|
||
12 years ago
|
}
|
||
|
if ($language === $sourceLanguage) {
|
||
|
return $file;
|
||
|
}
|
||
12 years ago
|
$desiredFile = dirname($file) . DIRECTORY_SEPARATOR . $sourceLanguage . DIRECTORY_SEPARATOR . basename($file);
|
||
12 years ago
|
return is_file($desiredFile) ? $desiredFile : $file;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Determines the MIME type of the specified file.
|
||
|
* This method will first try to determine the MIME type based on
|
||
|
* [finfo_open](http://php.net/manual/en/function.finfo-open.php). If this doesn't work, it will
|
||
|
* fall back to [[getMimeTypeByExtension()]].
|
||
|
* @param string $file the file name.
|
||
|
* @param string $magicFile name of the optional magic database file, usually something like `/path/to/magic.mime`.
|
||
|
* This will be passed as the second parameter to [finfo_open](http://php.net/manual/en/function.finfo-open.php).
|
||
|
* @param boolean $checkExtension whether to use the file extension to determine the MIME type in case
|
||
|
* `finfo_open()` cannot determine it.
|
||
|
* @return string the MIME type (e.g. `text/plain`). Null is returned if the MIME type cannot be determined.
|
||
|
*/
|
||
|
public static function getMimeType($file, $magicFile = null, $checkExtension = true)
|
||
|
{
|
||
|
if (function_exists('finfo_open')) {
|
||
|
$info = finfo_open(FILEINFO_MIME_TYPE, $magicFile);
|
||
12 years ago
|
if ($info) {
|
||
|
$result = finfo_file($info, $file);
|
||
12 years ago
|
finfo_close($info);
|
||
12 years ago
|
if ($result !== false) {
|
||
|
return $result;
|
||
|
}
|
||
12 years ago
|
}
|
||
|
}
|
||
|
|
||
12 years ago
|
return $checkExtension ? static::getMimeTypeByExtension($file) : null;
|
||
12 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* Determines the MIME type based on the extension name of the specified file.
|
||
|
* This method will use a local map between extension names and MIME types.
|
||
|
* @param string $file the file name.
|
||
|
* @param string $magicFile the path of the file that contains all available MIME type information.
|
||
|
* If this is not set, the default file aliased by `@yii/util/mimeTypes.php` will be used.
|
||
|
* @return string the MIME type. Null is returned if the MIME type cannot be determined.
|
||
|
*/
|
||
|
public static function getMimeTypeByExtension($file, $magicFile = null)
|
||
|
{
|
||
12 years ago
|
static $mimeTypes = array();
|
||
12 years ago
|
if ($magicFile === null) {
|
||
12 years ago
|
$magicFile = __DIR__ . '/mimeTypes.php';
|
||
|
}
|
||
|
if (!isset($mimeTypes[$magicFile])) {
|
||
|
$mimeTypes[$magicFile] = require($magicFile);
|
||
12 years ago
|
}
|
||
|
if (($ext = pathinfo($file, PATHINFO_EXTENSION)) !== '') {
|
||
|
$ext = strtolower($ext);
|
||
12 years ago
|
if (isset($mimeTypes[$magicFile][$ext])) {
|
||
|
return $mimeTypes[$magicFile][$ext];
|
||
12 years ago
|
}
|
||
|
}
|
||
|
return null;
|
||
|
}
|
||
|
|
||
12 years ago
|
/**
|
||
|
* Copies a whole directory as another one.
|
||
|
* The files and sub-directories will also be copied over.
|
||
|
* @param string $src the source directory
|
||
|
* @param string $dst the destination directory
|
||
|
* @param array $options options for directory copy. Valid options are:
|
||
|
*
|
||
|
* - dirMode: integer, the permission to be set for newly copied directories. Defaults to 0777.
|
||
|
* - fileMode: integer, the permission to be set for newly copied files. Defaults to the current environment setting.
|
||
12 years ago
|
* - filter: callback, a PHP callback that is called for each directory or file.
|
||
|
* The signature of the callback should be: `function ($path)`, where `$path` refers the full path to be filtered.
|
||
|
* The callback can return one of the following values:
|
||
|
*
|
||
|
* * true: the directory or file will be copied (the "only" and "except" options will be ignored)
|
||
|
* * false: the directory or file will NOT be copied (the "only" and "except" options will be ignored)
|
||
|
* * null: the "only" and "except" options will determine whether the directory or file should be copied
|
||
|
*
|
||
12 years ago
|
* - only: array, list of patterns that the file paths should match if they want to be copied.
|
||
|
* A path matches a pattern if it contains the pattern string at its end.
|
||
|
* For example, '.php' matches all file paths ending with '.php'.
|
||
|
* Note, the '/' characters in a pattern matches both '/' and '\' in the paths.
|
||
|
* If a file path matches a pattern in both "only" and "except", it will NOT be copied.
|
||
|
* - except: array, list of patterns that the files or directories should match if they want to be excluded from being copied.
|
||
12 years ago
|
* A path matches a pattern if it contains the pattern string at its end.
|
||
|
* Patterns ending with '/' apply to directory paths only, and patterns not ending with '/'
|
||
|
* apply to file paths only. For example, '/a/b' matches all file paths ending with '/a/b';
|
||
|
* and '.svn/' matches directory paths ending with '.svn'. Note, the '/' characters in a pattern matches
|
||
12 years ago
|
* both '/' and '\' in the paths.
|
||
12 years ago
|
* - recursive: boolean, whether the files under the subdirectories should also be copied. Defaults to true.
|
||
|
* - afterCopy: callback, a PHP callback that is called after each sub-directory or file is successfully copied.
|
||
12 years ago
|
* The signature of the callback should be: `function ($from, $to)`, where `$from` is the sub-directory or
|
||
12 years ago
|
* file copied from, while `$to` is the copy target.
|
||
12 years ago
|
*/
|
||
|
public static function copyDirectory($src, $dst, $options = array())
|
||
|
{
|
||
|
if (!is_dir($dst)) {
|
||
12 years ago
|
static::mkdir($dst, isset($options['dirMode']) ? $options['dirMode'] : 0777, true);
|
||
12 years ago
|
}
|
||
|
|
||
|
$handle = opendir($src);
|
||
|
while (($file = readdir($handle)) !== false) {
|
||
|
if ($file === '.' || $file === '..') {
|
||
|
continue;
|
||
|
}
|
||
12 years ago
|
$from = $src . DIRECTORY_SEPARATOR . $file;
|
||
|
$to = $dst . DIRECTORY_SEPARATOR . $file;
|
||
12 years ago
|
if (static::filterPath($from, $options)) {
|
||
12 years ago
|
if (is_file($from)) {
|
||
|
copy($from, $to);
|
||
12 years ago
|
if (isset($options['fileMode'])) {
|
||
12 years ago
|
@chmod($to, $options['fileMode']);
|
||
12 years ago
|
}
|
||
|
} else {
|
||
12 years ago
|
static::copyDirectory($from, $to, $options);
|
||
|
}
|
||
|
if (isset($options['afterCopy'])) {
|
||
|
call_user_func($options['afterCopy'], $from, $to);
|
||
12 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
closedir($handle);
|
||
|
}
|
||
12 years ago
|
|
||
|
/**
|
||
12 years ago
|
* Removes a directory (and all its content) recursively.
|
||
|
* @param string $dir the directory to be deleted recursively.
|
||
12 years ago
|
*/
|
||
12 years ago
|
public static function removeDirectory($dir)
|
||
12 years ago
|
{
|
||
12 years ago
|
if (!is_dir($dir) || !($handle = opendir($dir))) {
|
||
|
return;
|
||
|
}
|
||
|
while (($file = readdir($handle)) !== false) {
|
||
|
if ($file === '.' || $file === '..') {
|
||
12 years ago
|
continue;
|
||
|
}
|
||
12 years ago
|
$path = $dir . DIRECTORY_SEPARATOR . $file;
|
||
|
if (is_file($path)) {
|
||
|
unlink($path);
|
||
12 years ago
|
} else {
|
||
12 years ago
|
static::removeDirectory($path);
|
||
12 years ago
|
}
|
||
|
}
|
||
12 years ago
|
closedir($handle);
|
||
|
rmdir($dir);
|
||
12 years ago
|
}
|
||
12 years ago
|
|
||
|
/**
|
||
|
* Returns the files found under the specified directory and subdirectories.
|
||
|
* @param string $dir the directory under which the files will be looked for.
|
||
|
* @param array $options options for file searching. Valid options are:
|
||
12 years ago
|
*
|
||
12 years ago
|
* - filter: callback, a PHP callback that is called for each directory or file.
|
||
12 years ago
|
* The signature of the callback should be: `function ($path)`, where `$path` refers the full path to be filtered.
|
||
12 years ago
|
* The callback can return one of the following values:
|
||
|
*
|
||
|
* * true: the directory or file will be returned (the "only" and "except" options will be ignored)
|
||
|
* * false: the directory or file will NOT be returned (the "only" and "except" options will be ignored)
|
||
|
* * null: the "only" and "except" options will determine whether the directory or file should be returned
|
||
|
*
|
||
12 years ago
|
* - only: array, list of patterns that the file paths should match if they want to be returned.
|
||
|
* A path matches a pattern if it contains the pattern string at its end.
|
||
|
* For example, '.php' matches all file paths ending with '.php'.
|
||
|
* Note, the '/' characters in a pattern matches both '/' and '\' in the paths.
|
||
|
* If a file path matches a pattern in both "only" and "except", it will NOT be returned.
|
||
|
* - except: array, list of patterns that the file paths or directory paths should match if they want to be excluded from the result.
|
||
12 years ago
|
* A path matches a pattern if it contains the pattern string at its end.
|
||
|
* Patterns ending with '/' apply to directory paths only, and patterns not ending with '/'
|
||
|
* apply to file paths only. For example, '/a/b' matches all file paths ending with '/a/b';
|
||
|
* and '.svn/' matches directory paths ending with '.svn'. Note, the '/' characters in a pattern matches
|
||
12 years ago
|
* both '/' and '\' in the paths.
|
||
12 years ago
|
* - recursive: boolean, whether the files under the subdirectories should also be looked for. Defaults to true.
|
||
12 years ago
|
* @return array files found under the directory. The file list is sorted.
|
||
|
*/
|
||
12 years ago
|
public static function findFiles($dir, $options = array())
|
||
12 years ago
|
{
|
||
|
$list = array();
|
||
|
$handle = opendir($dir);
|
||
|
while (($file = readdir($handle)) !== false) {
|
||
|
if ($file === '.' || $file === '..') {
|
||
|
continue;
|
||
|
}
|
||
|
$path = $dir . DIRECTORY_SEPARATOR . $file;
|
||
12 years ago
|
if (static::filterPath($path, $options)) {
|
||
12 years ago
|
if (is_file($path)) {
|
||
12 years ago
|
$list[] = $path;
|
||
12 years ago
|
} elseif (!isset($options['recursive']) || $options['recursive']) {
|
||
|
$list = array_merge($list, static::findFiles($path, $options));
|
||
12 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
closedir($handle);
|
||
|
return $list;
|
||
|
}
|
||
|
|
||
|
/**
|
||
12 years ago
|
* Checks if the given file path satisfies the filtering options.
|
||
|
* @param string $path the path of the file or directory to be checked
|
||
|
* @param array $options the filtering options. See [[findFiles()]] for explanations of
|
||
|
* the supported options.
|
||
|
* @return boolean whether the file or directory satisfies the filtering options.
|
||
12 years ago
|
*/
|
||
12 years ago
|
public static function filterPath($path, $options)
|
||
12 years ago
|
{
|
||
12 years ago
|
if (isset($options['filter'])) {
|
||
|
$result = call_user_func($options['filter'], $path);
|
||
|
if (is_bool($result)) {
|
||
|
return $result;
|
||
|
}
|
||
12 years ago
|
}
|
||
12 years ago
|
$path = str_replace('\\', '/', $path);
|
||
12 years ago
|
if ($isDir = is_dir($path)) {
|
||
12 years ago
|
$path .= '/';
|
||
|
}
|
||
12 years ago
|
$n = StringHelper::strlen($path);
|
||
12 years ago
|
|
||
12 years ago
|
if (!empty($options['except'])) {
|
||
|
foreach ($options['except'] as $name) {
|
||
12 years ago
|
if (StringHelper::substr($path, -StringHelper::strlen($name), $n) === $name) {
|
||
12 years ago
|
return false;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
12 years ago
|
|
||
|
if (!$isDir && !empty($options['only'])) {
|
||
12 years ago
|
foreach ($options['only'] as $name) {
|
||
12 years ago
|
if (StringHelper::substr($path, -StringHelper::strlen($name), $n) === $name) {
|
||
12 years ago
|
return true;
|
||
12 years ago
|
}
|
||
|
}
|
||
12 years ago
|
return false;
|
||
12 years ago
|
}
|
||
12 years ago
|
return true;
|
||
12 years ago
|
}
|
||
12 years ago
|
|
||
|
/**
|
||
12 years ago
|
* Makes directory.
|
||
|
*
|
||
|
* This method is similar to the PHP `mkdir()` function except that
|
||
|
* it uses `chmod()` to set the permission of the created directory
|
||
|
* in order to avoid the impact of the `umask` setting.
|
||
12 years ago
|
*
|
||
|
* @param string $path path to be created.
|
||
12 years ago
|
* @param integer $mode the permission to be set for created directory.
|
||
|
* @param boolean $recursive whether to create parent directories if they do not exist.
|
||
|
* @return boolean whether the directory is created successfully
|
||
12 years ago
|
*/
|
||
12 years ago
|
public static function mkdir($path, $mode = 0777, $recursive = true)
|
||
12 years ago
|
{
|
||
12 years ago
|
if (is_dir($path)) {
|
||
|
return true;
|
||
|
}
|
||
|
$parentDir = dirname($path);
|
||
|
if ($recursive && !is_dir($parentDir)) {
|
||
|
static::mkdir($parentDir, $mode, true);
|
||
12 years ago
|
}
|
||
|
$result = mkdir($path, $mode);
|
||
|
chmod($path, $mode);
|
||
|
return $result;
|
||
|
}
|
||
12 years ago
|
}
|