Commit 7865a56d by Qiang Xue

...

parent 21a8f40e
...@@ -9,6 +9,8 @@ ...@@ -9,6 +9,8 @@
namespace yii\base; namespace yii\base;
use yii\util\FileHelper;
/** /**
* Module is the base class for module and application classes. * Module is the base class for module and application classes.
* *
...@@ -192,12 +194,7 @@ abstract class Module extends Component implements Initable ...@@ -192,12 +194,7 @@ abstract class Module extends Component implements Initable
*/ */
public function setBasePath($path) public function setBasePath($path)
{ {
$p = \Yii::getAlias($path); $this->_basePath = FileHelper::ensureDirectory($path);
if ($p === false || !is_dir($p)) {
throw new Exception('Invalid base path: ' . $path);
} else {
$this->_basePath = realpath($p);
}
} }
/** /**
...@@ -222,12 +219,7 @@ abstract class Module extends Component implements Initable ...@@ -222,12 +219,7 @@ abstract class Module extends Component implements Initable
*/ */
public function setControllerPath($path) public function setControllerPath($path)
{ {
$p = \Yii::getAlias($path); $this->_controllerPath = FileHelper::ensureDirectory($path);
if ($p === false || !is_dir($p)) {
throw new Exception('Invalid controller path: ' . $path);
} else {
$this->_controllerPath = realpath($p);
}
} }
/** /**
...@@ -244,15 +236,13 @@ abstract class Module extends Component implements Initable ...@@ -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. * @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) public function setViewPath($path)
{ {
if (($this->_viewPath = realpath($path)) === false || !is_dir($this->_viewPath)) { $this->_viewPath = FileHelper::ensureDirectory($path);
throw new CException(Yii::t('yii', 'The view path "{path}" is not a valid directory.',
array('{path}' => $path)));
}
} }
/** /**
...@@ -269,15 +259,13 @@ abstract class Module extends Component implements Initable ...@@ -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. * @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) public function setLayoutPath($path)
{ {
if (($this->_layoutPath = realpath($path)) === false || !is_dir($this->_layoutPath)) { $this->_layoutPath = FileHelper::ensureDirectory($path);
throw new CException(Yii::t('yii', 'The layout path "{path}" is not a valid directory.',
array('{path}' => $path)));
}
} }
/** /**
......
...@@ -9,6 +9,8 @@ ...@@ -9,6 +9,8 @@
namespace yii\util; namespace yii\util;
use yii\base\Exception;
/** /**
* Filesystem helper * Filesystem helper
* *
...@@ -30,6 +32,24 @@ class FileHelper ...@@ -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. * Returns the localized version of a specified file.
* *
* The searching is based on the specified language code. In particular, * The searching is based on the specified language code. In particular,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment