Alexander Makarov
13 years ago
8 changed files with 240 additions and 251 deletions
@ -1,139 +0,0 @@ |
|||||||
<?php |
|
||||||
/** |
|
||||||
* Filesystem helper class file. |
|
||||||
* |
|
||||||
* @link http://www.yiiframework.com/ |
|
||||||
* @copyright Copyright © 2008-2012 Yii Software LLC |
|
||||||
* @license http://www.yiiframework.com/license/ |
|
||||||
*/ |
|
||||||
|
|
||||||
namespace yii\util; |
|
||||||
|
|
||||||
/** |
|
||||||
* Filesystem helper |
|
||||||
* |
|
||||||
* @author Qiang Xue <qiang.xue@gmail.com> |
|
||||||
* @author Alex Makarov <sam@rmcreative.ru> |
|
||||||
* @since 2.0 |
|
||||||
*/ |
|
||||||
class File |
|
||||||
{ |
|
||||||
/** |
|
||||||
* Copies a list of files from one place to another. |
|
||||||
* @param array $fileList the list of files to be copied (name=>spec). |
|
||||||
* The array keys are names displayed during the copy process, and array values are specifications |
|
||||||
* for files to be copied. Each array value must be an array of the following structure: |
|
||||||
* <ul> |
|
||||||
* <li>source: required, the full path of the file/directory to be copied from</li> |
|
||||||
* <li>target: required, the full path of the file/directory to be copied to</li> |
|
||||||
* <li>callback: optional, the callback to be invoked when copying a file. The callback function |
|
||||||
* should be declared as follows: |
|
||||||
* <pre> |
|
||||||
* function foo($source,$params) |
|
||||||
* </pre> |
|
||||||
* where $source parameter is the source file path, and the content returned |
|
||||||
* by the function will be saved into the target file.</li> |
|
||||||
* <li>params: optional, the parameters to be passed to the callback</li> |
|
||||||
* </ul> |
|
||||||
* @see buildFileList |
|
||||||
*/ |
|
||||||
public function copyFiles($fileList) |
|
||||||
{ |
|
||||||
$overwriteAll=false; |
|
||||||
foreach($fileList as $name=>$file) |
|
||||||
{ |
|
||||||
$source=strtr($file['source'],'/\\',DIRECTORY_SEPARATOR); |
|
||||||
$target=strtr($file['target'],'/\\',DIRECTORY_SEPARATOR); |
|
||||||
$callback=isset($file['callback']) ? $file['callback'] : null; |
|
||||||
$params=isset($file['params']) ? $file['params'] : null; |
|
||||||
|
|
||||||
if(is_dir($source)) |
|
||||||
{ |
|
||||||
$this->ensureDirectory($target); |
|
||||||
continue; |
|
||||||
} |
|
||||||
|
|
||||||
if($callback!==null) |
|
||||||
$content=call_user_func($callback,$source,$params); |
|
||||||
else |
|
||||||
$content=file_get_contents($source); |
|
||||||
if(is_file($target)) |
|
||||||
{ |
|
||||||
if($content===file_get_contents($target)) |
|
||||||
{ |
|
||||||
echo " unchanged $name\n"; |
|
||||||
continue; |
|
||||||
} |
|
||||||
if($overwriteAll) |
|
||||||
echo " overwrite $name\n"; |
|
||||||
else |
|
||||||
{ |
|
||||||
echo " exist $name\n"; |
|
||||||
echo " ...overwrite? [Yes|No|All|Quit] "; |
|
||||||
$answer=trim(fgets(STDIN)); |
|
||||||
if(!strncasecmp($answer,'q',1)) |
|
||||||
return; |
|
||||||
else if(!strncasecmp($answer,'y',1)) |
|
||||||
echo " overwrite $name\n"; |
|
||||||
else if(!strncasecmp($answer,'a',1)) |
|
||||||
{ |
|
||||||
echo " overwrite $name\n"; |
|
||||||
$overwriteAll=true; |
|
||||||
} else |
|
||||||
{ |
|
||||||
echo " skip $name\n"; |
|
||||||
continue; |
|
||||||
} |
|
||||||
} |
|
||||||
} else |
|
||||||
{ |
|
||||||
$this->ensureDirectory(dirname($target)); |
|
||||||
echo " generate $name\n"; |
|
||||||
} |
|
||||||
file_put_contents($target,$content); |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* Builds the file list of a directory. |
|
||||||
* This method traverses through the specified directory and builds |
|
||||||
* a list of files and subdirectories that the directory contains. |
|
||||||
* The result of this function can be passed to {@link copyFiles}. |
|
||||||
* @param string $sourceDir the source directory |
|
||||||
* @param string $targetDir the target directory |
|
||||||
* @param string $baseDir base directory |
|
||||||
* @return array the file list (see {@link copyFiles}) |
|
||||||
*/ |
|
||||||
public function buildFileList($sourceDir, $targetDir, $baseDir='') |
|
||||||
{ |
|
||||||
$list=array(); |
|
||||||
$handle=opendir($sourceDir); |
|
||||||
while(($file=readdir($handle))!==false) |
|
||||||
{ |
|
||||||
if($file==='.' || $file==='..' || $file==='.svn' ||$file==='.yii') |
|
||||||
continue; |
|
||||||
$sourcePath=$sourceDir.DIRECTORY_SEPARATOR.$file; |
|
||||||
$targetPath=$targetDir.DIRECTORY_SEPARATOR.$file; |
|
||||||
$name=$baseDir===''?$file : $baseDir.'/'.$file; |
|
||||||
$list[$name]=array('source'=>$sourcePath, 'target'=>$targetPath); |
|
||||||
if(is_dir($sourcePath)) |
|
||||||
$list=array_merge($list,$this->buildFileList($sourcePath,$targetPath,$name)); |
|
||||||
} |
|
||||||
closedir($handle); |
|
||||||
return $list; |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* Creates all parent directories if they do not exist. |
|
||||||
* @param string $directory the directory to be checked |
|
||||||
*/ |
|
||||||
public function ensureDirectory($directory) |
|
||||||
{ |
|
||||||
if(!is_dir($directory)) |
|
||||||
{ |
|
||||||
$this->ensureDirectory(dirname($directory)); |
|
||||||
echo " mkdir ".strtr($directory,'\\','/')."\n"; |
|
||||||
mkdir($directory); |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
@ -0,0 +1,70 @@ |
|||||||
|
<?php |
||||||
|
/** |
||||||
|
* Filesystem helper class file. |
||||||
|
* |
||||||
|
* @link http://www.yiiframework.com/ |
||||||
|
* @copyright Copyright © 2008-2012 Yii Software LLC |
||||||
|
* @license http://www.yiiframework.com/license/ |
||||||
|
*/ |
||||||
|
|
||||||
|
namespace yii\util; |
||||||
|
|
||||||
|
/** |
||||||
|
* Filesystem helper |
||||||
|
* |
||||||
|
* @author Qiang Xue <qiang.xue@gmail.com> |
||||||
|
* @author Alex Makarov <sam@rmcreative.ru> |
||||||
|
* @since 2.0 |
||||||
|
*/ |
||||||
|
class FileHelper |
||||||
|
{ |
||||||
|
/** |
||||||
|
* Returns the extension name of a file path. |
||||||
|
* For example, the path "path/to/something.php" would return "php". |
||||||
|
* @param string $path the file path |
||||||
|
* @return string the extension name without the dot character. |
||||||
|
*/ |
||||||
|
public static function getExtension($path) |
||||||
|
{ |
||||||
|
return pathinfo($path, PATHINFO_EXTENSION); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 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 |
||||||
|
* whose name is 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 |
||||||
|
* will be returned. |
||||||
|
* |
||||||
|
* If the target and the source language codes are the same, |
||||||
|
* the original file will be returned. |
||||||
|
* |
||||||
|
* For consistency, it is recommended that the language code is given |
||||||
|
* in lower case and in the format of LanguageID_RegionID (e.g. "en_us"). |
||||||
|
* |
||||||
|
* @param string $file the original file |
||||||
|
* @param string $targetLanguage 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, $targetLanguage = null, $sourceLanguage = null) |
||||||
|
{ |
||||||
|
if ($targetLanguage === null) { |
||||||
|
$targetLanguage = \Yii::$application->getLanguage(); |
||||||
|
} |
||||||
|
if ($sourceLanguage === null) { |
||||||
|
$sourceLanguage = \Yii::$application->sourceLanguage; |
||||||
|
} |
||||||
|
if ($targetLanguage === $sourceLanguage) { |
||||||
|
return $file; |
||||||
|
} |
||||||
|
$desiredFile = dirname($file) . DIRECTORY_SEPARATOR . $sourceLanguage . DIRECTORY_SEPARATOR . basename($file); |
||||||
|
return is_file($desiredFile) ? $desiredFile : $file; |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue