From 7865a56d4edf0758c7085c14ac47b5e33ca5a668 Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Thu, 24 May 2012 23:12:08 -0400 Subject: [PATCH] ... --- framework/base/Module.php | 32 ++++++++++---------------------- framework/util/FileHelper.php | 20 ++++++++++++++++++++ 2 files changed, 30 insertions(+), 22 deletions(-) diff --git a/framework/base/Module.php b/framework/base/Module.php index 4c0fe08..2d90785 100644 --- a/framework/base/Module.php +++ b/framework/base/Module.php @@ -9,6 +9,8 @@ namespace yii\base; +use yii\util\FileHelper; + /** * Module is the base class for module and application classes. * @@ -192,12 +194,7 @@ abstract class Module extends Component implements Initable */ public function setBasePath($path) { - $p = \Yii::getAlias($path); - if ($p === false || !is_dir($p)) { - throw new Exception('Invalid base path: ' . $path); - } else { - $this->_basePath = realpath($p); - } + $this->_basePath = FileHelper::ensureDirectory($path); } /** @@ -222,12 +219,7 @@ abstract class Module extends Component implements Initable */ public function setControllerPath($path) { - $p = \Yii::getAlias($path); - if ($p === false || !is_dir($p)) { - throw new Exception('Invalid controller path: ' . $path); - } else { - $this->_controllerPath = realpath($p); - } + $this->_controllerPath = FileHelper::ensureDirectory($path); } /** @@ -244,15 +236,13 @@ abstract class Module extends Component implements Initable } /** + * Sets the directory that contains the view files. * @param string $path the root directory of view files. - * @throws CException if the directory does not exist. + * @throws Exception if the directory is invalid */ public function setViewPath($path) { - if (($this->_viewPath = realpath($path)) === false || !is_dir($this->_viewPath)) { - throw new CException(Yii::t('yii', 'The view path "{path}" is not a valid directory.', - array('{path}' => $path))); - } + $this->_viewPath = FileHelper::ensureDirectory($path); } /** @@ -269,15 +259,13 @@ abstract class Module extends Component implements Initable } /** + * Sets the directory that contains the layout files. * @param string $path the root directory of layout files. - * @throws CException if the directory does not exist. + * @throws Exception if the directory is invalid */ public function setLayoutPath($path) { - if (($this->_layoutPath = realpath($path)) === false || !is_dir($this->_layoutPath)) { - throw new CException(Yii::t('yii', 'The layout path "{path}" is not a valid directory.', - array('{path}' => $path))); - } + $this->_layoutPath = FileHelper::ensureDirectory($path); } /** diff --git a/framework/util/FileHelper.php b/framework/util/FileHelper.php index c7b7bb3..7b15089 100644 --- a/framework/util/FileHelper.php +++ b/framework/util/FileHelper.php @@ -9,6 +9,8 @@ namespace yii\util; +use yii\base\Exception; + /** * Filesystem helper * @@ -30,6 +32,24 @@ class FileHelper } /** + * 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); + } + } + + /** * Returns the localized version of a specified file. * * The searching is based on the specified language code. In particular,