FileHelper.php 3.11 KB
Newer Older
Qiang Xue committed
1 2 3 4 5 6 7 8 9 10 11
<?php
/**
 * Filesystem helper class file.
 *
 * @link http://www.yiiframework.com/
 * @copyright Copyright &copy; 2008-2012 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

namespace yii\util;

Qiang Xue committed
12 13
use yii\base\Exception;

Qiang Xue committed
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
/**
 * 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);
	}

Qiang Xue committed
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
	/**
	 * Checks the given path and ensures it is a directory.
	 * This method will call `realpath()` to "normalize" the given path.
	 * If the given path does not refer to an existing directory, an exception will be thrown.
	 * @param string $path the given path. This can also be a path alias.
	 * @return string the normalized path
	 * @throws Exception if the path does not refer to an existing directory.
	 */
	public static function ensureDirectory($path)
	{
		$p = \Yii::getAlias($path);
		if ($p !== false && ($p = realpath($p)) !== false && is_dir($p)) {
			return $p;
		} else {
			throw new Exception('Directory does not exist: ' . $path);
		}
	}

Qiang Xue committed
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
	/**
	 * 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
Qiang Xue committed
69
	 * @param string $language the target language that the file should be localized to.
Qiang Xue committed
70 71 72 73 74 75
	 * 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.
	 */
Qiang Xue committed
76
	public static function localize($file, $language = null, $sourceLanguage = null)
Qiang Xue committed
77
	{
Qiang Xue committed
78 79
		if ($language === null) {
			$language = \Yii::$application->getLanguage();
Qiang Xue committed
80 81 82 83
		}
		if ($sourceLanguage === null) {
			$sourceLanguage = \Yii::$application->sourceLanguage;
		}
Qiang Xue committed
84
		if ($language === $sourceLanguage) {
Qiang Xue committed
85 86 87 88 89 90
			return $file;
		}
		$desiredFile = dirname($file) . DIRECTORY_SEPARATOR . $sourceLanguage . DIRECTORY_SEPARATOR . basename($file);
		return is_file($desiredFile) ? $desiredFile : $file;
	}
}