From 927e9dddb43942d02b3068e4144ee239e8d56ada Mon Sep 17 00:00:00 2001 From: Alexander Makarov Date: Wed, 6 Nov 2013 22:52:27 +0400 Subject: [PATCH] Refactored View --- apps/advanced/backend/views/layouts/main.php | 2 +- apps/advanced/backend/views/site/error.php | 2 +- apps/advanced/backend/views/site/index.php | 2 +- apps/advanced/backend/views/site/login.php | 2 +- .../frontend/views/emails/passwordResetToken.php | 2 +- apps/advanced/frontend/views/layouts/main.php | 2 +- apps/advanced/frontend/views/site/about.php | 2 +- apps/advanced/frontend/views/site/contact.php | 2 +- apps/advanced/frontend/views/site/error.php | 2 +- apps/advanced/frontend/views/site/index.php | 2 +- apps/advanced/frontend/views/site/login.php | 2 +- .../views/site/requestPasswordResetToken.php | 2 +- .../advanced/frontend/views/site/resetPassword.php | 2 +- apps/advanced/frontend/views/site/signup.php | 2 +- apps/basic/views/layouts/main.php | 2 +- apps/basic/views/site/about.php | 2 +- apps/basic/views/site/contact.php | 2 +- apps/basic/views/site/error.php | 2 +- apps/basic/views/site/index.php | 2 +- apps/basic/views/site/login.php | 2 +- docs/guide/template.md | 2 +- docs/guide/view.md | 4 +- extensions/debug/Module.php | 2 +- extensions/debug/views/default/index.php | 2 +- extensions/debug/views/default/toolbar.php | 2 +- extensions/debug/views/default/view.php | 2 +- extensions/debug/views/layouts/main.php | 2 +- extensions/gii/Generator.php | 2 +- extensions/gii/generators/controller/form.php | 2 +- .../generators/controller/templates/controller.php | 2 +- .../gii/generators/controller/templates/view.php | 4 +- extensions/gii/generators/crud/form.php | 2 +- .../gii/generators/crud/templates/controller.php | 2 +- .../gii/generators/crud/templates/search.php | 2 +- .../gii/generators/crud/templates/views/_form.php | 4 +- .../generators/crud/templates/views/_search.php | 4 +- .../gii/generators/crud/templates/views/create.php | 4 +- .../gii/generators/crud/templates/views/index.php | 4 +- .../gii/generators/crud/templates/views/update.php | 4 +- .../gii/generators/crud/templates/views/view.php | 4 +- extensions/gii/generators/form/form.php | 2 +- .../gii/generators/form/templates/action.php | 2 +- extensions/gii/generators/form/templates/form.php | 4 +- extensions/gii/generators/model/form.php | 2 +- .../gii/generators/model/templates/model.php | 2 +- extensions/gii/generators/module/form.php | 2 +- .../gii/generators/module/templates/controller.php | 2 +- .../gii/generators/module/templates/module.php | 2 +- .../gii/generators/module/templates/view.php | 2 +- extensions/gii/views/default/diff.php | 2 +- extensions/gii/views/default/index.php | 2 +- extensions/gii/views/default/view.php | 2 +- extensions/gii/views/default/view/files.php | 2 +- extensions/gii/views/default/view/results.php | 2 +- extensions/gii/views/layouts/generator.php | 2 +- extensions/gii/views/layouts/main.php | 2 +- framework/yii/base/Application.php | 2 +- framework/yii/base/View.php | 405 +------------------ framework/yii/captcha/CaptchaValidator.php | 2 +- framework/yii/classes.php | 1 + framework/yii/validators/BooleanValidator.php | 2 +- framework/yii/validators/CompareValidator.php | 2 +- framework/yii/validators/EmailValidator.php | 2 +- framework/yii/validators/InlineValidator.php | 2 +- framework/yii/validators/NumberValidator.php | 2 +- framework/yii/validators/RangeValidator.php | 2 +- .../yii/validators/RegularExpressionValidator.php | 2 +- framework/yii/validators/RequiredValidator.php | 2 +- framework/yii/validators/StringValidator.php | 2 +- framework/yii/validators/UrlValidator.php | 2 +- framework/yii/validators/Validator.php | 2 +- framework/yii/web/AssetBundle.php | 8 +- framework/yii/web/View.php | 430 +++++++++++++++++++++ tests/unit/data/views/layout.php | 2 +- tests/unit/data/views/rawlayout.php | 2 +- tests/unit/framework/web/AssetBundleTest.php | 2 +- 76 files changed, 519 insertions(+), 487 deletions(-) create mode 100644 framework/yii/web/View.php diff --git a/apps/advanced/backend/views/layouts/main.php b/apps/advanced/backend/views/layouts/main.php index 85a4d2b..0e9d501 100644 --- a/apps/advanced/backend/views/layouts/main.php +++ b/apps/advanced/backend/views/layouts/main.php @@ -6,7 +6,7 @@ use yii\bootstrap\NavBar; use yii\widgets\Breadcrumbs; /** - * @var $this \yii\base\View + * @var $this \yii\web\View * @var $content string */ AppAsset::register($this); diff --git a/apps/advanced/backend/views/site/error.php b/apps/advanced/backend/views/site/error.php index 15a8eee..1b7ce04 100644 --- a/apps/advanced/backend/views/site/error.php +++ b/apps/advanced/backend/views/site/error.php @@ -3,7 +3,7 @@ use yii\helpers\Html; /** - * @var yii\base\View $this + * @var yii\web\View $this * @var string $name * @var string $message * @var Exception $exception diff --git a/apps/advanced/backend/views/site/index.php b/apps/advanced/backend/views/site/index.php index f2e6d5e..bcb2278 100644 --- a/apps/advanced/backend/views/site/index.php +++ b/apps/advanced/backend/views/site/index.php @@ -1,6 +1,6 @@ title = 'My Yii Application'; ?> diff --git a/apps/advanced/backend/views/site/login.php b/apps/advanced/backend/views/site/login.php index 65495bf..41973e5 100644 --- a/apps/advanced/backend/views/site/login.php +++ b/apps/advanced/backend/views/site/login.php @@ -3,7 +3,7 @@ use yii\helpers\Html; use yii\widgets\ActiveForm; /** - * @var yii\base\View $this + * @var yii\web\View $this * @var yii\widgets\ActiveForm $form * @var app\models\LoginForm $model */ diff --git a/apps/advanced/frontend/views/emails/passwordResetToken.php b/apps/advanced/frontend/views/emails/passwordResetToken.php index fd386bf..ac2155c 100644 --- a/apps/advanced/frontend/views/emails/passwordResetToken.php +++ b/apps/advanced/frontend/views/emails/passwordResetToken.php @@ -2,7 +2,7 @@ use yii\helpers\Html; /** - * @var yii\base\View $this + * @var yii\web\View $this * @var common\models\User $user; */ diff --git a/apps/advanced/frontend/views/layouts/main.php b/apps/advanced/frontend/views/layouts/main.php index 4f1510e..5da179c 100644 --- a/apps/advanced/frontend/views/layouts/main.php +++ b/apps/advanced/frontend/views/layouts/main.php @@ -7,7 +7,7 @@ use yii\widgets\Breadcrumbs; use frontend\widgets\Alert; /** - * @var $this \yii\base\View + * @var $this \yii\web\View * @var $content string */ AppAsset::register($this); diff --git a/apps/advanced/frontend/views/site/about.php b/apps/advanced/frontend/views/site/about.php index 32b84eb..ac4ab24 100644 --- a/apps/advanced/frontend/views/site/about.php +++ b/apps/advanced/frontend/views/site/about.php @@ -2,7 +2,7 @@ use yii\helpers\Html; /** - * @var yii\base\View $this + * @var yii\web\View $this */ $this->title = 'About'; $this->params['breadcrumbs'][] = $this->title; diff --git a/apps/advanced/frontend/views/site/contact.php b/apps/advanced/frontend/views/site/contact.php index 4202508..17c4f79 100644 --- a/apps/advanced/frontend/views/site/contact.php +++ b/apps/advanced/frontend/views/site/contact.php @@ -4,7 +4,7 @@ use yii\widgets\ActiveForm; use yii\captcha\Captcha; /** - * @var yii\base\View $this + * @var yii\web\View $this * @var yii\widgets\ActiveForm $form * @var app\models\ContactForm $model */ diff --git a/apps/advanced/frontend/views/site/error.php b/apps/advanced/frontend/views/site/error.php index 15a8eee..1b7ce04 100644 --- a/apps/advanced/frontend/views/site/error.php +++ b/apps/advanced/frontend/views/site/error.php @@ -3,7 +3,7 @@ use yii\helpers\Html; /** - * @var yii\base\View $this + * @var yii\web\View $this * @var string $name * @var string $message * @var Exception $exception diff --git a/apps/advanced/frontend/views/site/index.php b/apps/advanced/frontend/views/site/index.php index f2e6d5e..bcb2278 100644 --- a/apps/advanced/frontend/views/site/index.php +++ b/apps/advanced/frontend/views/site/index.php @@ -1,6 +1,6 @@ title = 'My Yii Application'; ?> diff --git a/apps/advanced/frontend/views/site/login.php b/apps/advanced/frontend/views/site/login.php index 3069f90..74c5ffa 100644 --- a/apps/advanced/frontend/views/site/login.php +++ b/apps/advanced/frontend/views/site/login.php @@ -3,7 +3,7 @@ use yii\helpers\Html; use yii\widgets\ActiveForm; /** - * @var yii\base\View $this + * @var yii\web\View $this * @var yii\widgets\ActiveForm $form * @var app\models\LoginForm $model */ diff --git a/apps/advanced/frontend/views/site/requestPasswordResetToken.php b/apps/advanced/frontend/views/site/requestPasswordResetToken.php index 88d23be..bb13a5f 100644 --- a/apps/advanced/frontend/views/site/requestPasswordResetToken.php +++ b/apps/advanced/frontend/views/site/requestPasswordResetToken.php @@ -3,7 +3,7 @@ use yii\helpers\Html; use yii\widgets\ActiveForm; /** - * @var yii\base\View $this + * @var yii\web\View $this * @var yii\widgets\ActiveForm $form * @var common\models\User $model */ diff --git a/apps/advanced/frontend/views/site/resetPassword.php b/apps/advanced/frontend/views/site/resetPassword.php index dc5beba..ec9f949 100644 --- a/apps/advanced/frontend/views/site/resetPassword.php +++ b/apps/advanced/frontend/views/site/resetPassword.php @@ -3,7 +3,7 @@ use yii\helpers\Html; use yii\widgets\ActiveForm; /** - * @var yii\base\View $this + * @var yii\web\View $this * @var yii\widgets\ActiveForm $form * @var common\models\User $model */ diff --git a/apps/advanced/frontend/views/site/signup.php b/apps/advanced/frontend/views/site/signup.php index bfebceb..3bb57fc 100644 --- a/apps/advanced/frontend/views/site/signup.php +++ b/apps/advanced/frontend/views/site/signup.php @@ -3,7 +3,7 @@ use yii\helpers\Html; use yii\widgets\ActiveForm; /** - * @var yii\base\View $this + * @var yii\web\View $this * @var yii\widgets\ActiveForm $form * @var common\models\User $model */ diff --git a/apps/basic/views/layouts/main.php b/apps/basic/views/layouts/main.php index 88066de..278aa1d 100644 --- a/apps/basic/views/layouts/main.php +++ b/apps/basic/views/layouts/main.php @@ -5,7 +5,7 @@ use yii\bootstrap\NavBar; use yii\widgets\Breadcrumbs; /** - * @var $this \yii\base\View + * @var $this \yii\web\View * @var $content string */ app\config\AppAsset::register($this); diff --git a/apps/basic/views/site/about.php b/apps/basic/views/site/about.php index 5010642..0608dd2 100644 --- a/apps/basic/views/site/about.php +++ b/apps/basic/views/site/about.php @@ -2,7 +2,7 @@ use yii\helpers\Html; /** - * @var yii\base\View $this + * @var yii\web\View $this */ $this->title = 'About'; $this->params['breadcrumbs'][] = $this->title; diff --git a/apps/basic/views/site/contact.php b/apps/basic/views/site/contact.php index cd6d9ce..d2e59af 100644 --- a/apps/basic/views/site/contact.php +++ b/apps/basic/views/site/contact.php @@ -4,7 +4,7 @@ use yii\widgets\ActiveForm; use yii\captcha\Captcha; /** - * @var yii\base\View $this + * @var yii\web\View $this * @var yii\widgets\ActiveForm $form * @var app\models\ContactForm $model */ diff --git a/apps/basic/views/site/error.php b/apps/basic/views/site/error.php index 15a8eee..1b7ce04 100644 --- a/apps/basic/views/site/error.php +++ b/apps/basic/views/site/error.php @@ -3,7 +3,7 @@ use yii\helpers\Html; /** - * @var yii\base\View $this + * @var yii\web\View $this * @var string $name * @var string $message * @var Exception $exception diff --git a/apps/basic/views/site/index.php b/apps/basic/views/site/index.php index f2e6d5e..bcb2278 100644 --- a/apps/basic/views/site/index.php +++ b/apps/basic/views/site/index.php @@ -1,6 +1,6 @@ title = 'My Yii Application'; ?> diff --git a/apps/basic/views/site/login.php b/apps/basic/views/site/login.php index 14522af..da022d6 100644 --- a/apps/basic/views/site/login.php +++ b/apps/basic/views/site/login.php @@ -3,7 +3,7 @@ use yii\helpers\Html; use yii\widgets\ActiveForm; /** - * @var yii\base\View $this + * @var yii\web\View $this * @var yii\widgets\ActiveForm $form * @var app\models\LoginForm $model */ diff --git a/docs/guide/template.md b/docs/guide/template.md index aec1a70..26c9707 100644 --- a/docs/guide/template.md +++ b/docs/guide/template.md @@ -10,7 +10,7 @@ a custom template engines by reconfiguring this component's behavior: [ 'components' => [ 'view' => [ - 'class' => 'yii\base\View', + 'class' => 'yii\web\View', 'renderers' => [ 'tpl' => [ 'class' => 'yii\renderers\SmartyViewRenderer', diff --git a/docs/guide/view.md b/docs/guide/view.md index 1069e89..1b366a6 100644 --- a/docs/guide/view.md +++ b/docs/guide/view.md @@ -113,7 +113,7 @@ to learn more refer to [Using template engines](template.md) section of the guid Using View object in templates ------------------------------ -An instance of `yii\base\View` component is available in view templates as `$this` variable. Using it in templates you +An instance of `yii\web\View` component is available in view templates as `$this` variable. Using it in templates you can do many useful things including setting page title and meta, registering scripts and accessing the context. ### Setting page title @@ -342,7 +342,7 @@ Customizing View component -------------------------- Since view is also an application component named `view` you can replace it with your own component that extends -from `yii\base\View`. It can be done via application configuration file such as `config/web.php`: +from `yii\base\View` or `yii\web\View`. It can be done via application configuration file such as `config/web.php`: ```php return [ diff --git a/extensions/debug/Module.php b/extensions/debug/Module.php index e75d14b..b315c6c 100644 --- a/extensions/debug/Module.php +++ b/extensions/debug/Module.php @@ -9,7 +9,7 @@ namespace yii\debug; use Yii; use yii\base\Application; -use yii\base\View; +use yii\web\View; use yii\web\HttpException; /** diff --git a/extensions/debug/views/default/index.php b/extensions/debug/views/default/index.php index c01230c..93737e2 100644 --- a/extensions/debug/views/default/index.php +++ b/extensions/debug/views/default/index.php @@ -3,7 +3,7 @@ use yii\helpers\Html; /** - * @var \yii\base\View $this + * @var \yii\web\View $this * @var array $manifest */ diff --git a/extensions/debug/views/default/toolbar.php b/extensions/debug/views/default/toolbar.php index bc76a67..7d88822 100644 --- a/extensions/debug/views/default/toolbar.php +++ b/extensions/debug/views/default/toolbar.php @@ -1,6 +1,6 @@ /** - * @var yii\base\View $this + * @var yii\web\View $this */ " ?> diff --git a/extensions/gii/generators/crud/form.php b/extensions/gii/generators/crud/form.php index fd7df05..1b101c2 100644 --- a/extensions/gii/generators/crud/form.php +++ b/extensions/gii/generators/crud/form.php @@ -1,6 +1,6 @@ modelClass, '\\') ?> $model * @var yii\widgets\ActiveForm $form */ diff --git a/extensions/gii/generators/crud/templates/views/_search.php b/extensions/gii/generators/crud/templates/views/_search.php index 03e21f2..0e23a5c 100644 --- a/extensions/gii/generators/crud/templates/views/_search.php +++ b/extensions/gii/generators/crud/templates/views/_search.php @@ -4,7 +4,7 @@ use yii\helpers\Inflector; use yii\helpers\StringHelper; /** - * @var yii\base\View $this + * @var yii\web\View $this * @var yii\gii\generators\crud\Generator $generator */ @@ -15,7 +15,7 @@ use yii\helpers\Html; use yii\widgets\ActiveForm; /** - * @var yii\base\View $this + * @var yii\web\View $this * @var searchModelClass, '\\') ?> $model * @var yii\widgets\ActiveForm $form */ diff --git a/extensions/gii/generators/crud/templates/views/create.php b/extensions/gii/generators/crud/templates/views/create.php index 02c30bf..68d08ba 100644 --- a/extensions/gii/generators/crud/templates/views/create.php +++ b/extensions/gii/generators/crud/templates/views/create.php @@ -4,7 +4,7 @@ use yii\helpers\Inflector; use yii\helpers\StringHelper; /** - * @var yii\base\View $this + * @var yii\web\View $this * @var yii\gii\generators\crud\Generator $generator */ @@ -14,7 +14,7 @@ echo "modelClass, '\\') ?> $model */ diff --git a/extensions/gii/generators/crud/templates/views/index.php b/extensions/gii/generators/crud/templates/views/index.php index 8543fad..b47c3f3 100644 --- a/extensions/gii/generators/crud/templates/views/index.php +++ b/extensions/gii/generators/crud/templates/views/index.php @@ -4,7 +4,7 @@ use yii\helpers\Inflector; use yii\helpers\StringHelper; /** - * @var yii\base\View $this + * @var yii\web\View $this * @var yii\gii\generators\crud\Generator $generator */ @@ -18,7 +18,7 @@ use yii\helpers\Html; use indexWidgetType === 'grid' ? "yii\\grid\\GridView" : "yii\\widgets\\ListView" ?>; /** - * @var yii\base\View $this + * @var yii\web\View $this * @var yii\data\ActiveDataProvider $dataProvider * @var searchModelClass, '\\') ?> $searchModel */ diff --git a/extensions/gii/generators/crud/templates/views/update.php b/extensions/gii/generators/crud/templates/views/update.php index 903a880..2fbbecf 100644 --- a/extensions/gii/generators/crud/templates/views/update.php +++ b/extensions/gii/generators/crud/templates/views/update.php @@ -4,7 +4,7 @@ use yii\helpers\Inflector; use yii\helpers\StringHelper; /** - * @var yii\base\View $this + * @var yii\web\View $this * @var yii\gii\generators\crud\Generator $generator */ @@ -16,7 +16,7 @@ echo "modelClass, '\\') ?> $model */ diff --git a/extensions/gii/generators/crud/templates/views/view.php b/extensions/gii/generators/crud/templates/views/view.php index a15a7b6..9b5391e 100644 --- a/extensions/gii/generators/crud/templates/views/view.php +++ b/extensions/gii/generators/crud/templates/views/view.php @@ -4,7 +4,7 @@ use yii\helpers\Inflector; use yii\helpers\StringHelper; /** - * @var yii\base\View $this + * @var yii\web\View $this * @var yii\gii\generators\crud\Generator $generator */ @@ -17,7 +17,7 @@ use yii\helpers\Html; use yii\widgets\DetailView; /** - * @var yii\base\View $this + * @var yii\web\View $this * @var modelClass, '\\') ?> $model */ diff --git a/extensions/gii/generators/form/form.php b/extensions/gii/generators/form/form.php index c04a26e..3d46777 100644 --- a/extensions/gii/generators/form/form.php +++ b/extensions/gii/generators/form/form.php @@ -1,6 +1,6 @@ modelClass ?> $model * @var ActiveForm $form */ diff --git a/extensions/gii/generators/model/form.php b/extensions/gii/generators/model/form.php index 9eca27c..ddc40f8 100644 --- a/extensions/gii/generators/model/form.php +++ b/extensions/gii/generators/model/form.php @@ -1,6 +1,6 @@ moduleClass; diff --git a/extensions/gii/generators/module/templates/view.php b/extensions/gii/generators/module/templates/view.php index 41e8bbc..f5f4248 100644 --- a/extensions/gii/generators/module/templates/view.php +++ b/extensions/gii/generators/module/templates/view.php @@ -1,6 +1,6 @@ diff --git a/extensions/gii/views/default/diff.php b/extensions/gii/views/default/diff.php index 6766528..6ee91a1 100644 --- a/extensions/gii/views/default/diff.php +++ b/extensions/gii/views/default/diff.php @@ -1,6 +1,6 @@ diff --git a/extensions/gii/views/default/index.php b/extensions/gii/views/default/index.php index 011a44b..f6d6855 100644 --- a/extensions/gii/views/default/index.php +++ b/extensions/gii/views/default/index.php @@ -2,7 +2,7 @@ use yii\helpers\Html; /** - * @var $this \yii\base\View + * @var $this \yii\web\View * @var $content string * @var yii\gii\Generator[] $generators * @var yii\gii\Generator $activeGenerator diff --git a/extensions/gii/views/default/view.php b/extensions/gii/views/default/view.php index 2104d75..dabcf39 100644 --- a/extensions/gii/views/default/view.php +++ b/extensions/gii/views/default/view.php @@ -6,7 +6,7 @@ use yii\gii\components\ActiveField; use yii\gii\CodeFile; /** - * @var yii\base\View $this + * @var yii\web\View $this * @var yii\gii\Generator $generator * @var string $id * @var yii\widgets\ActiveForm $form diff --git a/extensions/gii/views/default/view/files.php b/extensions/gii/views/default/view/files.php index 299f12b..3da2d97 100644 --- a/extensions/gii/views/default/view/files.php +++ b/extensions/gii/views/default/view/files.php @@ -4,7 +4,7 @@ use yii\helpers\Html; use yii\gii\CodeFile; /** - * @var $this \yii\base\View + * @var $this \yii\web\View * @var $generator \yii\gii\Generator * @var CodeFile[] $files * @var array $answers diff --git a/extensions/gii/views/default/view/results.php b/extensions/gii/views/default/view/results.php index e506a4e..0e9b7d9 100644 --- a/extensions/gii/views/default/view/results.php +++ b/extensions/gii/views/default/view/results.php @@ -1,6 +1,6 @@ ['class' => 'yii\base\Formatter'], 'i18n' => ['class' => 'yii\i18n\I18N'], 'urlManager' => ['class' => 'yii\web\UrlManager'], - 'view' => ['class' => 'yii\base\View'], + 'view' => ['class' => 'yii\web\View'], ]); } diff --git a/framework/yii/base/View.php b/framework/yii/base/View.php index 222f94b..c22b3c0 100644 --- a/framework/yii/base/View.php +++ b/framework/yii/base/View.php @@ -9,9 +9,6 @@ namespace yii\base; use Yii; use yii\helpers\FileHelper; -use yii\helpers\Html; -use yii\web\JqueryAsset; -use yii\web\AssetBundle; use yii\widgets\Block; use yii\widgets\ContentDecorator; use yii\widgets\FragmentCache; @@ -21,9 +18,6 @@ use yii\widgets\FragmentCache; * * View provides a set of methods (e.g. [[render()]]) for rendering purpose. * - * @property \yii\web\AssetManager $assetManager The asset manager. Defaults to the "assetManager" application - * component. - * * @author Qiang Xue * @since 2.0 */ @@ -38,14 +32,6 @@ class View extends Component */ const EVENT_END_PAGE = 'endPage'; /** - * @event Event an event that is triggered by [[beginBody()]]. - */ - const EVENT_BEGIN_BODY = 'beginBody'; - /** - * @event Event an event that is triggered by [[endBody()]]. - */ - const EVENT_END_BODY = 'endBody'; - /** * @event ViewEvent an event that is triggered by [[renderFile()]] right before it renders a view file. */ const EVENT_BEFORE_RENDER = 'beforeRender'; @@ -55,40 +41,6 @@ class View extends Component const EVENT_AFTER_RENDER = 'afterRender'; /** - * The location of registered JavaScript code block or files. - * This means the location is in the head section. - */ - const POS_HEAD = 1; - /** - * The location of registered JavaScript code block or files. - * This means the location is at the beginning of the body section. - */ - const POS_BEGIN = 2; - /** - * The location of registered JavaScript code block or files. - * This means the location is at the end of the body section. - */ - const POS_END = 3; - /** - * The location of registered JavaScript code block. - * This means the JavaScript code block will be enclosed within `jQuery(document).ready()`. - */ - const POS_READY = 4; - /** - * This is internally used as the placeholder for receiving the content registered for the head section. - */ - const PH_HEAD = ''; - /** - * This is internally used as the placeholder for receiving the content registered for the beginning of the body section. - */ - const PH_BODY_BEGIN = ''; - /** - * This is internally used as the placeholder for receiving the content registered for the end of the body section. - */ - const PH_BODY_END = ''; - - - /** * @var ViewContextInterface the context under which the [[renderFile()]] method is being invoked. */ public $context; @@ -136,46 +88,6 @@ class View extends Component * @internal */ public $dynamicPlaceholders = []; - /** - * @var AssetBundle[] list of the registered asset bundles. The keys are the bundle names, and the values - * are the registered [[AssetBundle]] objects. - * @see registerAssetBundle - */ - public $assetBundles = []; - /** - * @var string the page title - */ - public $title; - /** - * @var array the registered meta tags. - * @see registerMetaTag - */ - public $metaTags; - /** - * @var array the registered link tags. - * @see registerLinkTag - */ - public $linkTags; - /** - * @var array the registered CSS code blocks. - * @see registerCss - */ - public $css; - /** - * @var array the registered CSS files. - * @see registerCssFile - */ - public $cssFiles; - /** - * @var array the registered JS code blocks - * @see registerJs - */ - public $js; - /** - * @var array the registered JS files. - * @see registerJsFile - */ - public $jsFiles; /** @@ -516,29 +428,8 @@ class View extends Component FragmentCache::end(); } - - private $_assetManager; - - /** - * Registers the asset manager being used by this view object. - * @return \yii\web\AssetManager the asset manager. Defaults to the "assetManager" application component. - */ - public function getAssetManager() - { - return $this->_assetManager ?: Yii::$app->getAssetManager(); - } - - /** - * Sets the asset manager. - * @param \yii\web\AssetManager $value the asset manager - */ - public function setAssetManager($value) - { - $this->_assetManager = $value; - } - /** - * Marks the beginning of an HTML page. + * Marks the beginning of a page. */ public function beginPage() { @@ -549,301 +440,11 @@ class View extends Component } /** - * Marks the ending of an HTML page. + * Marks the ending of a page. */ public function endPage() { $this->trigger(self::EVENT_END_PAGE); - - $content = ob_get_clean(); - foreach (array_keys($this->assetBundles) as $bundle) { - $this->registerAssetFiles($bundle); - } - echo strtr($content, [ - self::PH_HEAD => $this->renderHeadHtml(), - self::PH_BODY_BEGIN => $this->renderBodyBeginHtml(), - self::PH_BODY_END => $this->renderBodyEndHtml(), - ]); - - unset( - $this->metaTags, - $this->linkTags, - $this->css, - $this->cssFiles, - $this->js, - $this->jsFiles - ); - } - - /** - * Registers all files provided by an asset bundle including depending bundles files. - * Removes a bundle from [[assetBundles]] once files are registered. - * @param string $name name of the bundle to register - */ - private function registerAssetFiles($name) - { - if (!isset($this->assetBundles[$name])) { - return; - } - $bundle = $this->assetBundles[$name]; - foreach ($bundle->depends as $dep) { - $this->registerAssetFiles($dep); - } - $bundle->registerAssetFiles($this); - unset($this->assetBundles[$name]); - } - - /** - * Marks the beginning of an HTML body section. - */ - public function beginBody() - { - echo self::PH_BODY_BEGIN; - $this->trigger(self::EVENT_BEGIN_BODY); - } - - /** - * Marks the ending of an HTML body section. - */ - public function endBody() - { - $this->trigger(self::EVENT_END_BODY); - echo self::PH_BODY_END; - } - - /** - * Marks the position of an HTML head section. - */ - public function head() - { - echo self::PH_HEAD; - } - - /** - * Registers the named asset bundle. - * All dependent asset bundles will be registered. - * @param string $name the name of the asset bundle. - * @param integer|null $position if set, this forces a minimum position for javascript files. - * This will adjust depending assets javascript file position or fail if requirement can not be met. - * If this is null, asset bundles position settings will not be changed. - * See [[registerJsFile]] for more details on javascript position. - * @return AssetBundle the registered asset bundle instance - * @throws InvalidConfigException if the asset bundle does not exist or a circular dependency is detected - */ - public function registerAssetBundle($name, $position = null) - { - if (!isset($this->assetBundles[$name])) { - $am = $this->getAssetManager(); - $bundle = $am->getBundle($name); - $this->assetBundles[$name] = false; - // register dependencies - $pos = isset($bundle->jsOptions['position']) ? $bundle->jsOptions['position'] : null; - foreach ($bundle->depends as $dep) { - $this->registerAssetBundle($dep, $pos); - } - $this->assetBundles[$name] = $bundle; - } elseif ($this->assetBundles[$name] === false) { - throw new InvalidConfigException("A circular dependency is detected for bundle '$name'."); - } else { - $bundle = $this->assetBundles[$name]; - } - - if ($position !== null) { - $pos = isset($bundle->jsOptions['position']) ? $bundle->jsOptions['position'] : null; - if ($pos === null) { - $bundle->jsOptions['position'] = $pos = $position; - } elseif ($pos > $position) { - throw new InvalidConfigException("An asset bundle that depends on '$name' has a higher javascript file position configured than '$name'."); - } - // update position for all dependencies - foreach ($bundle->depends as $dep) { - $this->registerAssetBundle($dep, $pos); - } - } - return $bundle; - } - - /** - * Registers a meta tag. - * @param array $options the HTML attributes for the meta tag. - * @param string $key the key that identifies the meta tag. If two meta tags are registered - * with the same key, the latter will overwrite the former. If this is null, the new meta tag - * will be appended to the existing ones. - */ - public function registerMetaTag($options, $key = null) - { - if ($key === null) { - $this->metaTags[] = Html::tag('meta', '', $options); - } else { - $this->metaTags[$key] = Html::tag('meta', '', $options); - } - } - - /** - * Registers a link tag. - * @param array $options the HTML attributes for the link tag. - * @param string $key the key that identifies the link tag. If two link tags are registered - * with the same key, the latter will overwrite the former. If this is null, the new link tag - * will be appended to the existing ones. - */ - public function registerLinkTag($options, $key = null) - { - if ($key === null) { - $this->linkTags[] = Html::tag('link', '', $options); - } else { - $this->linkTags[$key] = Html::tag('link', '', $options); - } - } - - /** - * Registers a CSS code block. - * @param string $css the CSS code block to be registered - * @param array $options the HTML attributes for the style tag. - * @param string $key the key that identifies the CSS code block. If null, it will use - * $css as the key. If two CSS code blocks are registered with the same key, the latter - * will overwrite the former. - */ - public function registerCss($css, $options = [], $key = null) - { - $key = $key ?: md5($css); - $this->css[$key] = Html::style($css, $options); - } - - /** - * Registers a CSS file. - * @param string $url the CSS file to be registered. - * @param array $options the HTML attributes for the link tag. - * @param string $key the key that identifies the CSS script file. If null, it will use - * $url as the key. If two CSS files are registered with the same key, the latter - * will overwrite the former. - */ - public function registerCssFile($url, $options = [], $key = null) - { - $key = $key ?: $url; - $this->cssFiles[$key] = Html::cssFile($url, $options); - } - - /** - * Registers a JS code block. - * @param string $js the JS code block to be registered - * @param integer $position the position at which the JS script tag should be inserted - * in a page. The possible values are: - * - * - [[POS_HEAD]]: in the head section - * - [[POS_BEGIN]]: at the beginning of the body section - * - [[POS_END]]: at the end of the body section - * - [[POS_READY]]: enclosed within jQuery(document).ready(). This is the default value. - * Note that by using this position, the method will automatically register the jQuery js file. - * - * @param string $key the key that identifies the JS code block. If null, it will use - * $js as the key. If two JS code blocks are registered with the same key, the latter - * will overwrite the former. - */ - public function registerJs($js, $position = self::POS_READY, $key = null) - { - $key = $key ?: md5($js); - $this->js[$position][$key] = $js; - if ($position === self::POS_READY) { - JqueryAsset::register($this); - } - } - - /** - * Registers a JS file. - * Please note that when this file depends on other JS files to be registered before, - * for example jQuery, you should use [[registerAssetBundle]] instead. - * @param string $url the JS file to be registered. - * @param array $options the HTML attributes for the script tag. A special option - * named "position" is supported which specifies where the JS script tag should be inserted - * in a page. The possible values of "position" are: - * - * - [[POS_HEAD]]: in the head section - * - [[POS_BEGIN]]: at the beginning of the body section - * - [[POS_END]]: at the end of the body section. This is the default value. - * - * @param string $key the key that identifies the JS script file. If null, it will use - * $url as the key. If two JS files are registered with the same key, the latter - * will overwrite the former. - */ - public function registerJsFile($url, $options = [], $key = null) - { - $position = isset($options['position']) ? $options['position'] : self::POS_END; - unset($options['position']); - $key = $key ?: $url; - $this->jsFiles[$position][$key] = Html::jsFile($url, $options); - } - - /** - * Renders the content to be inserted in the head section. - * The content is rendered using the registered meta tags, link tags, CSS/JS code blocks and files. - * @return string the rendered content - */ - protected function renderHeadHtml() - { - $lines = []; - if (!empty($this->metaTags)) { - $lines[] = implode("\n", $this->metaTags); - } - - $request = Yii::$app->getRequest(); - if ($request instanceof \yii\web\Request && $request->enableCsrfValidation) { - $lines[] = Html::tag('meta', '', ['name' => 'csrf-var', 'content' => $request->csrfVar]); - $lines[] = Html::tag('meta', '', ['name' => 'csrf-token', 'content' => $request->getCsrfToken()]); - } - - if (!empty($this->linkTags)) { - $lines[] = implode("\n", $this->linkTags); - } - if (!empty($this->cssFiles)) { - $lines[] = implode("\n", $this->cssFiles); - } - if (!empty($this->css)) { - $lines[] = implode("\n", $this->css); - } - if (!empty($this->jsFiles[self::POS_HEAD])) { - $lines[] = implode("\n", $this->jsFiles[self::POS_HEAD]); - } - if (!empty($this->js[self::POS_HEAD])) { - $lines[] = Html::script(implode("\n", $this->js[self::POS_HEAD]), ['type' => 'text/javascript']); - } - return empty($lines) ? '' : implode("\n", $lines); - } - - /** - * Renders the content to be inserted at the beginning of the body section. - * The content is rendered using the registered JS code blocks and files. - * @return string the rendered content - */ - protected function renderBodyBeginHtml() - { - $lines = []; - if (!empty($this->jsFiles[self::POS_BEGIN])) { - $lines[] = implode("\n", $this->jsFiles[self::POS_BEGIN]); - } - if (!empty($this->js[self::POS_BEGIN])) { - $lines[] = Html::script(implode("\n", $this->js[self::POS_BEGIN]), ['type' => 'text/javascript']); - } - return empty($lines) ? '' : implode("\n", $lines); - } - - /** - * Renders the content to be inserted at the end of the body section. - * The content is rendered using the registered JS code blocks and files. - * @return string the rendered content - */ - protected function renderBodyEndHtml() - { - $lines = []; - if (!empty($this->jsFiles[self::POS_END])) { - $lines[] = implode("\n", $this->jsFiles[self::POS_END]); - } - if (!empty($this->js[self::POS_END])) { - $lines[] = Html::script(implode("\n", $this->js[self::POS_END]), ['type' => 'text/javascript']); - } - if (!empty($this->js[self::POS_READY])) { - $js = "jQuery(document).ready(function(){\n" . implode("\n", $this->js[self::POS_READY]) . "\n});"; - $lines[] = Html::script($js, ['type' => 'text/javascript']); - } - return empty($lines) ? '' : implode("\n", $lines); + ob_end_flush(); } } diff --git a/framework/yii/captcha/CaptchaValidator.php b/framework/yii/captcha/CaptchaValidator.php index 9d9d0fd..01c9d11 100644 --- a/framework/yii/captcha/CaptchaValidator.php +++ b/framework/yii/captcha/CaptchaValidator.php @@ -96,7 +96,7 @@ class CaptchaValidator extends Validator * Returns the JavaScript needed for performing client-side validation. * @param \yii\base\Model $object the data object being validated * @param string $attribute the name of the attribute to be validated. - * @param \yii\base\View $view the view object that is going to be used to render views or view files + * @param \yii\web\View $view the view object that is going to be used to render views or view files * containing a model form with this validator applied. * @return string the client-side validation script. */ diff --git a/framework/yii/classes.php b/framework/yii/classes.php index 27dac4c..c89e4ff 100644 --- a/framework/yii/classes.php +++ b/framework/yii/classes.php @@ -213,6 +213,7 @@ return [ 'yii\web\User' => YII_PATH . '/web/User.php', 'yii\web\UserEvent' => YII_PATH . '/web/UserEvent.php', 'yii\web\VerbFilter' => YII_PATH . '/web/VerbFilter.php', + 'yii\web\View' => YII_PATH . '/web/View.php', 'yii\web\XmlResponseFormatter' => YII_PATH . '/web/XmlResponseFormatter.php', 'yii\web\YiiAsset' => YII_PATH . '/web/YiiAsset.php', 'yii\widgets\ActiveField' => YII_PATH . '/widgets/ActiveField.php', diff --git a/framework/yii/validators/BooleanValidator.php b/framework/yii/validators/BooleanValidator.php index 02b11d7..9d74705 100644 --- a/framework/yii/validators/BooleanValidator.php +++ b/framework/yii/validators/BooleanValidator.php @@ -79,7 +79,7 @@ class BooleanValidator extends Validator * Returns the JavaScript needed for performing client-side validation. * @param \yii\base\Model $object the data object being validated * @param string $attribute the name of the attribute to be validated. - * @param \yii\base\View $view the view object that is going to be used to render views or view files + * @param \yii\web\View $view the view object that is going to be used to render views or view files * containing a model form with this validator applied. * @return string the client-side validation script. */ diff --git a/framework/yii/validators/CompareValidator.php b/framework/yii/validators/CompareValidator.php index 8a694ad..08d4809 100644 --- a/framework/yii/validators/CompareValidator.php +++ b/framework/yii/validators/CompareValidator.php @@ -179,7 +179,7 @@ class CompareValidator extends Validator * @param \yii\base\Model $object the data object being validated * @param string $attribute the name of the attribute to be validated * @return string the client-side validation script - * @param \yii\base\View $view the view object that is going to be used to render views or view files + * @param \yii\web\View $view the view object that is going to be used to render views or view files * containing a model form with this validator applied. * @throws InvalidConfigException if CompareValidator::operator is invalid */ diff --git a/framework/yii/validators/EmailValidator.php b/framework/yii/validators/EmailValidator.php index 7e28738..bc105a5 100644 --- a/framework/yii/validators/EmailValidator.php +++ b/framework/yii/validators/EmailValidator.php @@ -119,7 +119,7 @@ class EmailValidator extends Validator * Returns the JavaScript needed for performing client-side validation. * @param \yii\base\Model $object the data object being validated * @param string $attribute the name of the attribute to be validated. - * @param \yii\base\View $view the view object that is going to be used to render views or view files + * @param \yii\web\View $view the view object that is going to be used to render views or view files * containing a model form with this validator applied. * @return string the client-side validation script. */ diff --git a/framework/yii/validators/InlineValidator.php b/framework/yii/validators/InlineValidator.php index 8099977..dabdb4a 100644 --- a/framework/yii/validators/InlineValidator.php +++ b/framework/yii/validators/InlineValidator.php @@ -79,7 +79,7 @@ class InlineValidator extends Validator * * @param \yii\base\Model $object the data object being validated * @param string $attribute the name of the attribute to be validated. - * @param \yii\base\View $view the view object that is going to be used to render views or view files + * @param \yii\web\View $view the view object that is going to be used to render views or view files * containing a model form with this validator applied. * @return string the client-side validation script. Null if the validator does not support * client-side validation. diff --git a/framework/yii/validators/NumberValidator.php b/framework/yii/validators/NumberValidator.php index 0df73e6..c114300 100644 --- a/framework/yii/validators/NumberValidator.php +++ b/framework/yii/validators/NumberValidator.php @@ -114,7 +114,7 @@ class NumberValidator extends Validator * Returns the JavaScript needed for performing client-side validation. * @param \yii\base\Model $object the data object being validated * @param string $attribute the name of the attribute to be validated. - * @param \yii\base\View $view the view object that is going to be used to render views or view files + * @param \yii\web\View $view the view object that is going to be used to render views or view files * containing a model form with this validator applied. * @return string the client-side validation script. */ diff --git a/framework/yii/validators/RangeValidator.php b/framework/yii/validators/RangeValidator.php index 7abc41d..6668969 100644 --- a/framework/yii/validators/RangeValidator.php +++ b/framework/yii/validators/RangeValidator.php @@ -81,7 +81,7 @@ class RangeValidator extends Validator * Returns the JavaScript needed for performing client-side validation. * @param \yii\base\Model $object the data object being validated * @param string $attribute the name of the attribute to be validated. - * @param \yii\base\View $view the view object that is going to be used to render views or view files + * @param \yii\web\View $view the view object that is going to be used to render views or view files * containing a model form with this validator applied. * @return string the client-side validation script. */ diff --git a/framework/yii/validators/RegularExpressionValidator.php b/framework/yii/validators/RegularExpressionValidator.php index ae79d32..efa9e22 100644 --- a/framework/yii/validators/RegularExpressionValidator.php +++ b/framework/yii/validators/RegularExpressionValidator.php @@ -78,7 +78,7 @@ class RegularExpressionValidator extends Validator * Returns the JavaScript needed for performing client-side validation. * @param \yii\base\Model $object the data object being validated * @param string $attribute the name of the attribute to be validated. - * @param \yii\base\View $view the view object that is going to be used to render views or view files + * @param \yii\web\View $view the view object that is going to be used to render views or view files * containing a model form with this validator applied. * @return string the client-side validation script. */ diff --git a/framework/yii/validators/RequiredValidator.php b/framework/yii/validators/RequiredValidator.php index eb4a394..2b5e333 100644 --- a/framework/yii/validators/RequiredValidator.php +++ b/framework/yii/validators/RequiredValidator.php @@ -102,7 +102,7 @@ class RequiredValidator extends Validator * Returns the JavaScript needed for performing client-side validation. * @param \yii\base\Model $object the data object being validated * @param string $attribute the name of the attribute to be validated. - * @param \yii\base\View $view the view object that is going to be used to render views or view files + * @param \yii\web\View $view the view object that is going to be used to render views or view files * containing a model form with this validator applied. * @return string the client-side validation script. */ diff --git a/framework/yii/validators/StringValidator.php b/framework/yii/validators/StringValidator.php index 329cab0..92aad56 100644 --- a/framework/yii/validators/StringValidator.php +++ b/framework/yii/validators/StringValidator.php @@ -142,7 +142,7 @@ class StringValidator extends Validator * Returns the JavaScript needed for performing client-side validation. * @param \yii\base\Model $object the data object being validated * @param string $attribute the name of the attribute to be validated. - * @param \yii\base\View $view the view object that is going to be used to render views or view files + * @param \yii\web\View $view the view object that is going to be used to render views or view files * containing a model form with this validator applied. * @return string the client-side validation script. */ diff --git a/framework/yii/validators/UrlValidator.php b/framework/yii/validators/UrlValidator.php index f65c347..5400c32 100644 --- a/framework/yii/validators/UrlValidator.php +++ b/framework/yii/validators/UrlValidator.php @@ -115,7 +115,7 @@ class UrlValidator extends Validator * Returns the JavaScript needed for performing client-side validation. * @param \yii\base\Model $object the data object being validated * @param string $attribute the name of the attribute to be validated. - * @param \yii\base\View $view the view object that is going to be used to render views or view files + * @param \yii\web\View $view the view object that is going to be used to render views or view files * containing a model form with this validator applied. * @return string the client-side validation script. * @see \yii\Web\ActiveForm::enableClientValidation diff --git a/framework/yii/validators/Validator.php b/framework/yii/validators/Validator.php index 84e02de..012f392 100644 --- a/framework/yii/validators/Validator.php +++ b/framework/yii/validators/Validator.php @@ -215,7 +215,7 @@ abstract class Validator extends Component * * @param \yii\base\Model $object the data object being validated * @param string $attribute the name of the attribute to be validated. - * @param \yii\base\View $view the view object that is going to be used to render views or view files + * @param \yii\web\View $view the view object that is going to be used to render views or view files * containing a model form with this validator applied. * @return string the client-side validation script. Null if the validator does not support * client-side validation. diff --git a/framework/yii/web/AssetBundle.php b/framework/yii/web/AssetBundle.php index 5ea6f67..964fe98 100644 --- a/framework/yii/web/AssetBundle.php +++ b/framework/yii/web/AssetBundle.php @@ -10,7 +10,7 @@ namespace yii\web; use Yii; use yii\base\InvalidConfigException; use yii\base\Object; -use yii\base\View; +use yii\web\View; /** * AssetBundle represents a collection of asset files, such as CSS, JS, images. @@ -97,12 +97,12 @@ class AssetBundle extends Object */ public $css = []; /** - * @var array the options that will be passed to [[\yii\base\View::registerJsFile()]] + * @var array the options that will be passed to [[\yii\web\View::registerJsFile()]] * when registering the JS files in this bundle. */ public $jsOptions = []; /** - * @var array the options that will be passed to [[\yii\base\View::registerCssFile()]] + * @var array the options that will be passed to [[\yii\web\View::registerCssFile()]] * when registering the CSS files in this bundle. */ public $cssOptions = []; @@ -140,7 +140,7 @@ class AssetBundle extends Object /** * Registers the CSS and JS files with the given view. - * @param \yii\base\View $view the view that the asset files are to be registered with. + * @param \yii\web\View $view the view that the asset files are to be registered with. */ public function registerAssetFiles($view) { diff --git a/framework/yii/web/View.php b/framework/yii/web/View.php new file mode 100644 index 0000000..b646ef1 --- /dev/null +++ b/framework/yii/web/View.php @@ -0,0 +1,430 @@ + + * @since 2.0 + */ +class View extends \yii\base\View +{ + const EVENT_BEGIN_BODY = 'beginBody'; + /** + * @event Event an event that is triggered by [[endBody()]]. + */ + const EVENT_END_BODY = 'endBody'; + + /** + * The location of registered JavaScript code block or files. + * This means the location is in the head section. + */ + const POS_HEAD = 1; + /** + * The location of registered JavaScript code block or files. + * This means the location is at the beginning of the body section. + */ + const POS_BEGIN = 2; + /** + * The location of registered JavaScript code block or files. + * This means the location is at the end of the body section. + */ + const POS_END = 3; + /** + * The location of registered JavaScript code block. + * This means the JavaScript code block will be enclosed within `jQuery(document).ready()`. + */ + const POS_READY = 4; + /** + * This is internally used as the placeholder for receiving the content registered for the head section. + */ + const PH_HEAD = ''; + /** + * This is internally used as the placeholder for receiving the content registered for the beginning of the body section. + */ + const PH_BODY_BEGIN = ''; + /** + * This is internally used as the placeholder for receiving the content registered for the end of the body section. + */ + const PH_BODY_END = ''; + + /** + * @var AssetBundle[] list of the registered asset bundles. The keys are the bundle names, and the values + * are the registered [[AssetBundle]] objects. + * @see registerAssetBundle + */ + public $assetBundles = []; + /** + * @var string the page title + */ + public $title; + /** + * @var array the registered meta tags. + * @see registerMetaTag + */ + public $metaTags; + /** + * @var array the registered link tags. + * @see registerLinkTag + */ + public $linkTags; + /** + * @var array the registered CSS code blocks. + * @see registerCss + */ + public $css; + /** + * @var array the registered CSS files. + * @see registerCssFile + */ + public $cssFiles; + /** + * @var array the registered JS code blocks + * @see registerJs + */ + public $js; + /** + * @var array the registered JS files. + * @see registerJsFile + */ + public $jsFiles; + + private $_assetManager; + + /** + * Registers the asset manager being used by this view object. + * @return \yii\web\AssetManager the asset manager. Defaults to the "assetManager" application component. + */ + public function getAssetManager() + { + return $this->_assetManager ?: Yii::$app->getAssetManager(); + } + + /** + * Sets the asset manager. + * @param \yii\web\AssetManager $value the asset manager + */ + public function setAssetManager($value) + { + $this->_assetManager = $value; + } + + /** + * Marks the ending of an HTML page. + */ + public function endPage() + { + $this->trigger(self::EVENT_END_PAGE); + + $content = ob_get_clean(); + foreach (array_keys($this->assetBundles) as $bundle) { + $this->registerAssetFiles($bundle); + } + echo strtr($content, [ + self::PH_HEAD => $this->renderHeadHtml(), + self::PH_BODY_BEGIN => $this->renderBodyBeginHtml(), + self::PH_BODY_END => $this->renderBodyEndHtml(), + ]); + + unset( + $this->metaTags, + $this->linkTags, + $this->css, + $this->cssFiles, + $this->js, + $this->jsFiles + ); + } + + /** + * Registers all files provided by an asset bundle including depending bundles files. + * Removes a bundle from [[assetBundles]] once files are registered. + * @param string $name name of the bundle to register + */ + private function registerAssetFiles($name) + { + if (!isset($this->assetBundles[$name])) { + return; + } + $bundle = $this->assetBundles[$name]; + foreach ($bundle->depends as $dep) { + $this->registerAssetFiles($dep); + } + $bundle->registerAssetFiles($this); + unset($this->assetBundles[$name]); + } + + /** + * Marks the beginning of an HTML body section. + */ + public function beginBody() + { + echo self::PH_BODY_BEGIN; + $this->trigger(self::EVENT_BEGIN_BODY); + } + + /** + * Marks the ending of an HTML body section. + */ + public function endBody() + { + $this->trigger(self::EVENT_END_BODY); + echo self::PH_BODY_END; + } + + /** + * Marks the position of an HTML head section. + */ + public function head() + { + echo self::PH_HEAD; + } + + /** + * Registers the named asset bundle. + * All dependent asset bundles will be registered. + * @param string $name the name of the asset bundle. + * @param integer|null $position if set, this forces a minimum position for javascript files. + * This will adjust depending assets javascript file position or fail if requirement can not be met. + * If this is null, asset bundles position settings will not be changed. + * See [[registerJsFile]] for more details on javascript position. + * @return AssetBundle the registered asset bundle instance + * @throws InvalidConfigException if the asset bundle does not exist or a circular dependency is detected + */ + public function registerAssetBundle($name, $position = null) + { + if (!isset($this->assetBundles[$name])) { + $am = $this->getAssetManager(); + $bundle = $am->getBundle($name); + $this->assetBundles[$name] = false; + // register dependencies + $pos = isset($bundle->jsOptions['position']) ? $bundle->jsOptions['position'] : null; + foreach ($bundle->depends as $dep) { + $this->registerAssetBundle($dep, $pos); + } + $this->assetBundles[$name] = $bundle; + } elseif ($this->assetBundles[$name] === false) { + throw new InvalidConfigException("A circular dependency is detected for bundle '$name'."); + } else { + $bundle = $this->assetBundles[$name]; + } + + if ($position !== null) { + $pos = isset($bundle->jsOptions['position']) ? $bundle->jsOptions['position'] : null; + if ($pos === null) { + $bundle->jsOptions['position'] = $pos = $position; + } elseif ($pos > $position) { + throw new InvalidConfigException("An asset bundle that depends on '$name' has a higher javascript file position configured than '$name'."); + } + // update position for all dependencies + foreach ($bundle->depends as $dep) { + $this->registerAssetBundle($dep, $pos); + } + } + return $bundle; + } + + /** + * Registers a meta tag. + * @param array $options the HTML attributes for the meta tag. + * @param string $key the key that identifies the meta tag. If two meta tags are registered + * with the same key, the latter will overwrite the former. If this is null, the new meta tag + * will be appended to the existing ones. + */ + public function registerMetaTag($options, $key = null) + { + if ($key === null) { + $this->metaTags[] = Html::tag('meta', '', $options); + } else { + $this->metaTags[$key] = Html::tag('meta', '', $options); + } + } + + /** + * Registers a link tag. + * @param array $options the HTML attributes for the link tag. + * @param string $key the key that identifies the link tag. If two link tags are registered + * with the same key, the latter will overwrite the former. If this is null, the new link tag + * will be appended to the existing ones. + */ + public function registerLinkTag($options, $key = null) + { + if ($key === null) { + $this->linkTags[] = Html::tag('link', '', $options); + } else { + $this->linkTags[$key] = Html::tag('link', '', $options); + } + } + + /** + * Registers a CSS code block. + * @param string $css the CSS code block to be registered + * @param array $options the HTML attributes for the style tag. + * @param string $key the key that identifies the CSS code block. If null, it will use + * $css as the key. If two CSS code blocks are registered with the same key, the latter + * will overwrite the former. + */ + public function registerCss($css, $options = [], $key = null) + { + $key = $key ?: md5($css); + $this->css[$key] = Html::style($css, $options); + } + + /** + * Registers a CSS file. + * @param string $url the CSS file to be registered. + * @param array $options the HTML attributes for the link tag. + * @param string $key the key that identifies the CSS script file. If null, it will use + * $url as the key. If two CSS files are registered with the same key, the latter + * will overwrite the former. + */ + public function registerCssFile($url, $options = [], $key = null) + { + $key = $key ?: $url; + $this->cssFiles[$key] = Html::cssFile($url, $options); + } + + /** + * Registers a JS code block. + * @param string $js the JS code block to be registered + * @param integer $position the position at which the JS script tag should be inserted + * in a page. The possible values are: + * + * - [[POS_HEAD]]: in the head section + * - [[POS_BEGIN]]: at the beginning of the body section + * - [[POS_END]]: at the end of the body section + * - [[POS_READY]]: enclosed within jQuery(document).ready(). This is the default value. + * Note that by using this position, the method will automatically register the jQuery js file. + * + * @param string $key the key that identifies the JS code block. If null, it will use + * $js as the key. If two JS code blocks are registered with the same key, the latter + * will overwrite the former. + */ + public function registerJs($js, $position = self::POS_READY, $key = null) + { + $key = $key ?: md5($js); + $this->js[$position][$key] = $js; + if ($position === self::POS_READY) { + JqueryAsset::register($this); + } + } + + /** + * Registers a JS file. + * Please note that when this file depends on other JS files to be registered before, + * for example jQuery, you should use [[registerAssetBundle]] instead. + * @param string $url the JS file to be registered. + * @param array $options the HTML attributes for the script tag. A special option + * named "position" is supported which specifies where the JS script tag should be inserted + * in a page. The possible values of "position" are: + * + * - [[POS_HEAD]]: in the head section + * - [[POS_BEGIN]]: at the beginning of the body section + * - [[POS_END]]: at the end of the body section. This is the default value. + * + * @param string $key the key that identifies the JS script file. If null, it will use + * $url as the key. If two JS files are registered with the same key, the latter + * will overwrite the former. + */ + public function registerJsFile($url, $options = [], $key = null) + { + $position = isset($options['position']) ? $options['position'] : self::POS_END; + unset($options['position']); + $key = $key ?: $url; + $this->jsFiles[$position][$key] = Html::jsFile($url, $options); + } + + /** + * Renders the content to be inserted in the head section. + * The content is rendered using the registered meta tags, link tags, CSS/JS code blocks and files. + * @return string the rendered content + */ + protected function renderHeadHtml() + { + $lines = []; + if (!empty($this->metaTags)) { + $lines[] = implode("\n", $this->metaTags); + } + + $request = Yii::$app->getRequest(); + if ($request instanceof \yii\web\Request && $request->enableCsrfValidation) { + $lines[] = Html::tag('meta', '', ['name' => 'csrf-var', 'content' => $request->csrfVar]); + $lines[] = Html::tag('meta', '', ['name' => 'csrf-token', 'content' => $request->getCsrfToken()]); + } + + if (!empty($this->linkTags)) { + $lines[] = implode("\n", $this->linkTags); + } + if (!empty($this->cssFiles)) { + $lines[] = implode("\n", $this->cssFiles); + } + if (!empty($this->css)) { + $lines[] = implode("\n", $this->css); + } + if (!empty($this->jsFiles[self::POS_HEAD])) { + $lines[] = implode("\n", $this->jsFiles[self::POS_HEAD]); + } + if (!empty($this->js[self::POS_HEAD])) { + $lines[] = Html::script(implode("\n", $this->js[self::POS_HEAD]), ['type' => 'text/javascript']); + } + return empty($lines) ? '' : implode("\n", $lines); + } + + /** + * Renders the content to be inserted at the beginning of the body section. + * The content is rendered using the registered JS code blocks and files. + * @return string the rendered content + */ + protected function renderBodyBeginHtml() + { + $lines = []; + if (!empty($this->jsFiles[self::POS_BEGIN])) { + $lines[] = implode("\n", $this->jsFiles[self::POS_BEGIN]); + } + if (!empty($this->js[self::POS_BEGIN])) { + $lines[] = Html::script(implode("\n", $this->js[self::POS_BEGIN]), ['type' => 'text/javascript']); + } + return empty($lines) ? '' : implode("\n", $lines); + } + + /** + * Renders the content to be inserted at the end of the body section. + * The content is rendered using the registered JS code blocks and files. + * @return string the rendered content + */ + protected function renderBodyEndHtml() + { + $lines = []; + if (!empty($this->jsFiles[self::POS_END])) { + $lines[] = implode("\n", $this->jsFiles[self::POS_END]); + } + if (!empty($this->js[self::POS_END])) { + $lines[] = Html::script(implode("\n", $this->js[self::POS_END]), ['type' => 'text/javascript']); + } + if (!empty($this->js[self::POS_READY])) { + $js = "jQuery(document).ready(function(){\n" . implode("\n", $this->js[self::POS_READY]) . "\n});"; + $lines[] = Html::script($js, ['type' => 'text/javascript']); + } + return empty($lines) ? '' : implode("\n", $lines); + } +} diff --git a/tests/unit/data/views/layout.php b/tests/unit/data/views/layout.php index 0b5a0e1..ed7dc27 100644 --- a/tests/unit/data/views/layout.php +++ b/tests/unit/data/views/layout.php @@ -1,6 +1,6 @@ diff --git a/tests/unit/data/views/rawlayout.php b/tests/unit/data/views/rawlayout.php index 6864642..68cea54 100644 --- a/tests/unit/data/views/rawlayout.php +++ b/tests/unit/data/views/rawlayout.php @@ -1,5 +1,5 @@ beginPage(); ?>1head(); ?>2beginBody(); ?>3endBody(); ?>4endPage(); ?> \ No newline at end of file diff --git a/tests/unit/framework/web/AssetBundleTest.php b/tests/unit/framework/web/AssetBundleTest.php index 9cc3894..24c36d6 100644 --- a/tests/unit/framework/web/AssetBundleTest.php +++ b/tests/unit/framework/web/AssetBundleTest.php @@ -8,7 +8,7 @@ namespace yiiunit\framework\web; use Yii; -use yii\base\View; +use yii\web\View; use yii\web\AssetBundle; use yii\web\AssetManager;