Commit bd320533 by Qiang Xue

MVC WIP

parent 9165a159
......@@ -8,9 +8,8 @@
*/
use yii\base\Exception;
use yii\logging\Logger;
use yii\base\InvalidCallException;
use yii\base\InvalidConfigException;
use yii\logging\Logger;
/**
* Gets the application start timestamp.
......@@ -189,14 +188,14 @@ class YiiBase
*
* Note, this method does not ensure the existence of the resulting path.
* @param string $alias alias
* @param boolean $throwException whether to throw exception if the alias is invalid.
* @return string|boolean path corresponding to the alias, false if the root alias is not previously registered.
* @throws Exception if the alias is invalid and $throwException is true.
* @see setAlias
*/
public static function getAlias($alias, $throwException = false)
public static function getAlias($alias)
{
if (isset(self::$aliases[$alias])) {
if (!is_string($alias)) {
return false;
} elseif (isset(self::$aliases[$alias])) {
return self::$aliases[$alias];
} elseif ($alias === '' || $alias[0] !== '@') { // not an alias
return $alias;
......@@ -206,11 +205,7 @@ class YiiBase
return self::$aliases[$alias] = self::$aliases[$rootAlias] . substr($alias, $pos);
}
}
if ($throwException) {
throw new Exception("Invalid path alias: $alias");
} else {
return false;
}
return false;
}
/**
......@@ -361,7 +356,7 @@ class YiiBase
$class = $config['class'];
unset($config['class']);
} else {
throw new InvalidCallException('Object configuration must be an array containing a "class" element.');
throw new InvalidConfigException('Object configuration must be an array containing a "class" element.');
}
if (!class_exists($class, false)) {
......
......@@ -279,4 +279,15 @@ class Controller extends Component
{
return new View($this);
}
/**
* Returns the directory containing view files for this controller.
* The default implementation returns the directory named as controller [[id]] under the [[module]]'s
* [[viewPath]] directory.
* @return string the directory containing the view files for this controller.
*/
public function getViewPath()
{
return $this->module->getViewPath() . DIRECTORY_SEPARATOR . $this->id;
}
}
......@@ -321,7 +321,7 @@ class ErrorHandler extends Component
public function renderAsHtml($exception)
{
$view = new View;
$view->context = $this;
$view->_owner = $this;
$name = !YII_DEBUG || $exception instanceof HttpException ? $this->errorView : $this->exceptionView;
echo $view->render($name, array(
'exception' => $exception,
......
......@@ -9,55 +9,114 @@
namespace yii\base;
use Yii;
use yii\base\InvalidConfigException;
use yii\util\FileHelper;
/**
* Theme represents an application theme.
*
* A theme is directory consisting of view and layout files which are meant to replace their
* non-themed counterparts.
*
* Theme uses [[pathMap]] to achieve the file replacement. A view or layout file will be replaced
* with its themed version if part of its path matches one of the keys in [[pathMap]].
* Then the matched part will be replaced with the corresponding array value.
*
* For example, if [[pathMap]] is `array('/www/views' => '/www/themes/basic')`,
* then the themed version for a view file `/www/views/site/index.php` will be
* `/www/themes/basic/site/index.php`.
*
* @property string $baseUrl the base URL for this theme. This is mainly used by [[getUrl()]].
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class Theme extends Component
{
/**
* @var string the root path of this theme.
* @see pathMap
*/
public $basePath;
public $baseUrl;
/**
* @var array the mapping between view directories and their corresponding themed versions.
* If not set, it will be initialized as a mapping from [[Application::basePath]] to [[basePath]].
* This property is used by [[apply()]] when a view is trying to apply the theme.
*/
public $pathMap;
private $_baseUrl;
/**
* Initializes the theme.
* @throws InvalidConfigException if [[basePath]] is not set.
*/
public function init()
{
if ($this->basePath !== null) {
$this->basePath = \Yii::getAlias($this->basePath, true);
} else {
throw new InvalidConfigException("Theme.basePath must be set.");
parent::init();
if (empty($this->pathMap)) {
if ($this->basePath !== null) {
$this->basePath = FileHelper::ensureDirectory($this->basePath);
$this->pathMap = array(Yii::$application->getBasePath() => $this->basePath);
} else {
throw new InvalidConfigException("Theme::basePath must be set.");
}
}
if ($this->baseUrl !== null) {
$this->baseUrl = \Yii::getAlias($this->baseUrl, true);
} else {
throw new InvalidConfigException("Theme.baseUrl must be set.");
$paths = array();
foreach ($this->pathMap as $from => $to) {
$paths[FileHelper::normalizePath($from) . DIRECTORY_SEPARATOR] = FileHelper::normalizePath($to) . DIRECTORY_SEPARATOR;
}
$this->pathMap = $paths;
}
/**
* Returns the base URL for this theme.
* The method [[getUrl()]] will prefix this to the given URL.
* @return string the base URL for this theme.
*/
public function getBaseUrl()
{
return $this->_baseUrl;
}
/**
* Sets the base URL for this theme.
* @param string $value the base URL for this theme.
*/
public function setBaseUrl($value)
{
$this->_baseUrl = rtrim(Yii::getAlias($value), '/');
}
/**
* @param Application|Module|Controller|Object $context
* @return string
* Converts a file to a themed file if possible.
* If there is no corresponding themed file, the original file will be returned.
* @param string $path the file to be themed
* @return string the themed file, or the original file if the themed version is not available.
*/
public function getViewPath($context = null)
public function apply($path)
{
$viewPath = $this->basePath . DIRECTORY_SEPARATOR . 'views';
if ($context === null || $context instanceof Application) {
return $viewPath;
} elseif ($context instanceof Controller || $context instanceof Module) {
return $viewPath . DIRECTORY_SEPARATOR . $context->getUniqueId();
} else {
return $viewPath . DIRECTORY_SEPARATOR . str_replace('\\', '_', get_class($context));
$path = FileHelper::normalizePath($path);
foreach ($this->pathMap as $from => $to) {
if (strpos($path, $from) === 0) {
$n = strlen($from);
$file = $to . substr($path, $n);
if (is_file($file)) {
return $file;
}
}
}
return $path;
}
/**
* @param Module $module
* @return string
* Converts a relative URL into an absolute URL using [[basePath]].
* @param string $url the relative URL to be converted.
* @return string the absolute URL
*/
public function getLayoutPath($module = null)
public function getUrl($url)
{
return $this->getViewPath($module) . DIRECTORY_SEPARATOR . 'layouts';
return $this->baseUrl . '/' . ltrim($url, '/');
}
}
......@@ -102,4 +102,16 @@ class Widget extends Component
{
return new View($this);
}
/**
* Returns the directory containing the view files for this widget.
* The default implementation returns the 'views' subdirectory under the directory containing the widget class file.
* @return string the directory containing the view files for this widget.
*/
public function getViewPath()
{
$className = get_class($this);
$class = new \ReflectionClass($className);
return dirname($class->getFileName()) . DIRECTORY_SEPARATOR . 'views';
}
}
\ No newline at end of file
......@@ -51,6 +51,20 @@ class FileHelper
}
/**
* 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,
......
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