diff --git a/framework/YiiBase.php b/framework/YiiBase.php index fb2967a..c32cc28 100644 --- a/framework/YiiBase.php +++ b/framework/YiiBase.php @@ -198,6 +198,32 @@ class YiiBase } /** + * Returns the root alias part of a given alias. + * A root alias is an alias that has been registered via [[setAlias()]] previously. + * If a given alias matches multiple root aliases, the longest one will be returned. + * @param string $alias the alias + * @return string|boolean the root alias, or false if no root alias is found + */ + public static function getRootAlias($alias) + { + $pos = strpos($alias, '/'); + $root = $pos === false ? $alias : substr($alias, 0, $pos); + + if (isset(self::$aliases[$root])) { + if (is_string(self::$aliases[$root])) { + return $root; + } else { + foreach (self::$aliases[$root] as $name => $path) { + if (strpos($alias . '/', $name . '/') === 0) { + return $name; + } + } + } + } + return false; + } + + /** * Registers a path alias. * * A path alias is a short name representing a long path (a file path, a URL, etc.) @@ -222,13 +248,13 @@ class YiiBase * - a path alias (e.g. `@yii/base`). In this case, the path alias will be converted into the * actual path first by calling [[getAlias()]]. * - * @throws InvalidParamException the alias does not start with '@', or if $path is an invalid alias. + * @throws InvalidParamException if $path is an invalid alias. * @see getAlias */ public static function setAlias($alias, $path) { if (strncmp($alias, '@', 1)) { - throw new InvalidParamException('The alias must start with the "@" character.'); + $alias = '@' . $alias; } $pos = strpos($alias, '/'); $root = $pos === false ? $alias : substr($alias, 0, $pos); diff --git a/framework/base/Application.php b/framework/base/Application.php index b9f01d7..fd6527f 100644 --- a/framework/base/Application.php +++ b/framework/base/Application.php @@ -56,6 +56,11 @@ class Application extends Module * If this is false, layout will be disabled. */ public $layout = 'main'; + /** + * @var array list of installed extensions. The array keys are the extension names, and the array + * values are the corresponding extension root source directories or path aliases. + */ + public $extensions = array(); private $_ended = false; @@ -81,12 +86,19 @@ class Application extends Module if (isset($config['basePath'])) { $this->setBasePath($config['basePath']); + Yii::setAlias('@app', $this->getBasePath()); unset($config['basePath']); - Yii::$aliases['@app'] = $this->getBasePath(); } else { throw new InvalidConfigException('The "basePath" configuration is required.'); } + if (isset($config['extensions'])) { + foreach ($config['extensions'] as $name => $path) { + Yii::setAlias("@$name", $path); + } + unset($config['extensions']); + } + $this->registerErrorHandlers(); $this->registerCoreComponents(); @@ -206,7 +218,7 @@ class Application extends Module */ public function getVendorPath() { - if ($this->_vendorPath !== null) { + if ($this->_vendorPath === null) { $this->setVendorPath($this->getBasePath() . DIRECTORY_SEPARATOR . 'vendor'); } return $this->_vendorPath; diff --git a/framework/base/ViewContent.php b/framework/base/ViewContent.php index 797dba2..8b4e835 100644 --- a/framework/base/ViewContent.php +++ b/framework/base/ViewContent.php @@ -28,6 +28,7 @@ class ViewContent extends Component * @var \yii\web\AssetManager */ public $assetManager; + public $assetBundles; public $title; public $metaTags; @@ -45,7 +46,7 @@ class ViewContent extends Component { parent::init(); if ($this->assetManager === null) { - $this->assetManager = Yii::$app->getAssetManager(); + $this->assetManager = Yii::$app->getAssets(); } } diff --git a/framework/web/Application.php b/framework/web/Application.php index f9b615d..32f6479 100644 --- a/framework/web/Application.php +++ b/framework/web/Application.php @@ -98,6 +98,15 @@ class Application extends \yii\base\Application } /** + * Returns the asset manager. + * @return AssetManager the asset manager component + */ + public function getAssets() + { + return $this->getComponent('user'); + } + + /** * Registers the core application components. * @see setComponents */ @@ -117,6 +126,9 @@ class Application extends \yii\base\Application 'user' => array( 'class' => 'yii\web\User', ), + 'assets' => array( + 'class' => 'yii\web\AssetManager', + ), )); } } diff --git a/framework/web/AssetManager.php b/framework/web/AssetManager.php index 6c32687..1f82e7c 100644 --- a/framework/web/AssetManager.php +++ b/framework/web/AssetManager.php @@ -91,7 +91,13 @@ class AssetManager extends Component } else { $this->base = realpath($this->basePath); } - $this->baseUrl = rtrim(Yii::getAlias($this->getBaseUrl), '/'); + $this->baseUrl = rtrim(Yii::getAlias($this->baseUrl), '/'); + + foreach (require(YII_PATH . '/assets.php') as $name => $bundle) { + if (!isset($this->bundles[$name])) { + $this->bundles[$name] = $bundle; + } + } } /** @@ -103,11 +109,18 @@ class AssetManager extends Component public function getBundle($name, $publish = true) { if (!isset($this->bundles[$name])) { - $manifest = Yii::getAlias("@{$name}/assets.php", false); - if ($manifest === false) { + $rootAlias = Yii::getRootAlias("@$name"); + if ($rootAlias !== false) { + $manifest = Yii::getAlias("$rootAlias/assets.php", false); + if ($manifest !== false && is_file($manifest)) { + foreach (require($manifest) as $bn => $config) { + $this->bundles[$bn] = $config; + } + } + } + if (!isset($this->bundles[$name])) { throw new InvalidParamException("Unable to find the asset bundle: $name"); } - $this->bundles[$name] = require($manifest); } if (is_array($this->bundles[$name])) { $config = $this->bundles[$name]; @@ -116,7 +129,11 @@ class AssetManager extends Component $this->bundles[$name] = Yii::createObject($config); } } - // todo: publish bundle + + if ($publish) { + + } + return $this->bundles[$name]; }