Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
41ea9485
Commit
41ea9485
authored
Apr 19, 2013
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactoring and documentation for asset/script management.
parent
4e8f9cea
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
39 additions
and
252 deletions
+39
-252
View.php
framework/base/View.php
+0
-0
ViewContent.php
framework/base/ViewContent.php
+0
-236
Application.php
framework/web/Application.php
+3
-3
AssetBundle.php
framework/web/AssetBundle.php
+27
-12
AssetManager.php
framework/web/AssetManager.php
+9
-1
No files found.
framework/base/View.php
View file @
41ea9485
This diff is collapsed.
Click to expand it.
framework/base/ViewContent.php
deleted
100644 → 0
View file @
4e8f9cea
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace
yii\base
;
use
Yii
;
use
yii\helpers\Html
;
/**
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class
ViewContent
extends
Component
{
const
POS_HEAD
=
1
;
const
POS_BEGIN
=
2
;
const
POS_END
=
3
;
const
TOKEN_HEAD
=
'<![CDATA[YII-BLOCK-HEAD]]>'
;
const
TOKEN_BODY_BEGIN
=
'<![CDATA[YII-BLOCK-BODY-BEGIN]]>'
;
const
TOKEN_BODY_END
=
'<![CDATA[YII-BLOCK-BODY-END]]>'
;
public
$assetBundles
;
public
$title
;
public
$metaTags
;
public
$linkTags
;
public
$css
;
public
$cssFiles
;
public
$js
;
public
$jsFiles
;
public
$jsInHead
;
public
$jsFilesInHead
;
public
$jsInBody
;
public
$jsFilesInBody
;
public
function
reset
()
{
$this
->
assetBundles
=
null
;
$this
->
title
=
null
;
$this
->
metaTags
=
null
;
$this
->
linkTags
=
null
;
$this
->
css
=
null
;
$this
->
cssFiles
=
null
;
$this
->
js
=
null
;
$this
->
jsFiles
=
null
;
$this
->
jsInHead
=
null
;
$this
->
jsFilesInHead
=
null
;
$this
->
jsInBody
=
null
;
$this
->
jsFilesInBody
=
null
;
}
private
$_assetManager
;
/**
* @return \yii\web\AssetManager
*/
public
function
getAssetManager
()
{
return
$this
->
_assetManager
?:
Yii
::
$app
->
getAssets
();
}
public
function
setAssetManager
(
$value
)
{
$this
->
_assetManager
=
$value
;
}
public
function
begin
()
{
ob_start
();
ob_implicit_flush
(
false
);
}
public
function
end
()
{
$content
=
ob_get_clean
();
echo
$this
->
populate
(
$content
);
}
public
function
beginBody
()
{
echo
self
::
TOKEN_BODY_BEGIN
;
}
public
function
endBody
()
{
echo
self
::
TOKEN_BODY_END
;
}
public
function
head
()
{
echo
self
::
TOKEN_HEAD
;
}
public
function
registerAssetBundle
(
$name
)
{
if
(
!
isset
(
$this
->
assetBundles
[
$name
]))
{
$am
=
$this
->
getAssetManager
();
$bundle
=
$am
->
getBundle
(
$name
);
if
(
$bundle
!==
null
)
{
$this
->
assetBundles
[
$name
]
=
false
;
$bundle
->
registerAssets
(
$this
,
$am
);
$this
->
assetBundles
[
$name
]
=
true
;
}
else
{
throw
new
InvalidConfigException
(
"Unknown asset bundle:
$name
"
);
}
}
elseif
(
$this
->
assetBundles
[
$name
]
===
false
)
{
throw
new
InvalidConfigException
(
"A cyclic dependency is detected for bundle '
$name
'."
);
}
}
public
function
registerMetaTag
(
$options
,
$key
=
null
)
{
if
(
$key
===
null
)
{
$this
->
metaTags
[]
=
Html
::
tag
(
'meta'
,
''
,
$options
);
}
else
{
$this
->
metaTags
[
$key
]
=
Html
::
tag
(
'meta'
,
''
,
$options
);
}
}
public
function
registerLinkTag
(
$options
,
$key
=
null
)
{
if
(
$key
===
null
)
{
$this
->
linkTags
[]
=
Html
::
tag
(
'link'
,
''
,
$options
);
}
else
{
$this
->
linkTags
[
$key
]
=
Html
::
tag
(
'link'
,
''
,
$options
);
}
}
public
function
registerCss
(
$css
,
$options
=
array
(),
$key
=
null
)
{
$key
=
$key
?:
$css
;
$this
->
css
[
$key
]
=
Html
::
style
(
$css
,
$options
);
}
public
function
registerCssFile
(
$url
,
$options
=
array
(),
$key
=
null
)
{
$key
=
$key
?:
$url
;
$this
->
cssFiles
[
$key
]
=
Html
::
cssFile
(
$url
,
$options
);
}
public
function
registerJs
(
$js
,
$options
=
array
(),
$key
=
null
)
{
$position
=
isset
(
$options
[
'position'
])
?
$options
[
'position'
]
:
self
::
POS_END
;
unset
(
$options
[
'position'
]);
$key
=
$key
?:
$js
;
$html
=
Html
::
script
(
$js
,
$options
);
if
(
$position
==
self
::
POS_END
)
{
$this
->
js
[
$key
]
=
$html
;
}
elseif
(
$position
==
self
::
POS_HEAD
)
{
$this
->
jsInHead
[
$key
]
=
$html
;
}
elseif
(
$position
==
self
::
POS_BEGIN
)
{
$this
->
jsInBody
[
$key
]
=
$html
;
}
else
{
throw
new
InvalidParamException
(
"Unknown position:
$position
"
);
}
}
public
function
registerJsFile
(
$url
,
$options
=
array
(),
$key
=
null
)
{
$position
=
isset
(
$options
[
'position'
])
?
$options
[
'position'
]
:
self
::
POS_END
;
unset
(
$options
[
'position'
]);
$key
=
$key
?:
$url
;
$html
=
Html
::
jsFile
(
$url
,
$options
);
if
(
$position
==
self
::
POS_END
)
{
$this
->
jsFiles
[
$key
]
=
$html
;
}
elseif
(
$position
==
self
::
POS_HEAD
)
{
$this
->
jsFilesInHead
[
$key
]
=
$html
;
}
elseif
(
$position
==
self
::
POS_BEGIN
)
{
$this
->
jsFilesInBody
[
$key
]
=
$html
;
}
else
{
throw
new
InvalidParamException
(
"Unknown position:
$position
"
);
}
}
protected
function
populate
(
$content
)
{
return
strtr
(
$content
,
array
(
self
::
TOKEN_HEAD
=>
$this
->
getHeadHtml
(),
self
::
TOKEN_BODY_BEGIN
=>
$this
->
getBodyBeginHtml
(),
self
::
TOKEN_BODY_END
=>
$this
->
getBodyEndHtml
(),
));
}
protected
function
getHeadHtml
()
{
$lines
=
array
();
if
(
!
empty
(
$this
->
metaTags
))
{
$lines
[]
=
implode
(
"
\n
"
,
$this
->
cssFiles
);
}
if
(
!
empty
(
$this
->
linkTags
))
{
$lines
[]
=
implode
(
"
\n
"
,
$this
->
cssFiles
);
}
if
(
!
empty
(
$this
->
cssFiles
))
{
$lines
[]
=
implode
(
"
\n
"
,
$this
->
cssFiles
);
}
if
(
!
empty
(
$this
->
css
))
{
$lines
[]
=
implode
(
"
\n
"
,
$this
->
css
);
}
if
(
!
empty
(
$this
->
jsFilesInHead
))
{
$lines
[]
=
implode
(
"
\n
"
,
$this
->
jsFilesInHead
);
}
if
(
!
empty
(
$this
->
jsInHead
))
{
$lines
[]
=
implode
(
"
\n
"
,
$this
->
jsInHead
);
}
return
implode
(
"
\n
"
,
$lines
);
}
protected
function
getBodyBeginHtml
()
{
$lines
=
array
();
if
(
!
empty
(
$this
->
jsFilesInBody
))
{
$lines
[]
=
implode
(
"
\n
"
,
$this
->
jsFilesInBody
);
}
if
(
!
empty
(
$this
->
jsInHead
))
{
$lines
[]
=
implode
(
"
\n
"
,
$this
->
jsInBody
);
}
return
implode
(
"
\n
"
,
$lines
);
}
protected
function
getBodyEndHtml
()
{
$lines
=
array
();
if
(
!
empty
(
$this
->
jsFiles
))
{
$lines
[]
=
implode
(
"
\n
"
,
$this
->
jsFiles
);
}
if
(
!
empty
(
$this
->
js
))
{
$lines
[]
=
implode
(
"
\n
"
,
$this
->
js
);
}
return
implode
(
"
\n
"
,
$lines
);
}
}
\ No newline at end of file
framework/web/Application.php
View file @
41ea9485
...
...
@@ -101,9 +101,9 @@ class Application extends \yii\base\Application
* Returns the asset manager.
* @return AssetManager the asset manager component
*/
public
function
getAsset
s
()
public
function
getAsset
Manager
()
{
return
$this
->
getComponent
(
'asset
s
'
);
return
$this
->
getComponent
(
'asset
Manager
'
);
}
/**
...
...
@@ -126,7 +126,7 @@ class Application extends \yii\base\Application
'user'
=>
array
(
'class'
=>
'yii\web\User'
,
),
'asset
s
'
=>
array
(
'asset
Manager
'
=>
array
(
'class'
=>
'yii\web\AssetManager'
,
),
));
...
...
framework/web/AssetBundle.php
View file @
41ea9485
...
...
@@ -12,6 +12,14 @@ use yii\base\InvalidConfigException;
use
yii\base\Object
;
/**
* AssetBundle represents a collection of asset files, such as CSS, JS, images.
*
* Each asset bundle has a unique name that globally identifies it among all asset bundles
* used in an application.
*
* An asset bundle can depend on other asset bundles. When registering an asset bundle
* with a view, all its dependent asset bundles will be automatically registered.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
...
...
@@ -66,7 +74,7 @@ class AssetBundle extends Object
*
* Each JavaScript file may be associated with options. In this case, the array key
* should be the JavaScript file path, while the corresponding array value should
* be the option array. The options will be passed to [[View
Content
::registerJsFile()]].
* be the option array. The options will be passed to [[View::registerJsFile()]].
*/
public
$js
=
array
();
/**
...
...
@@ -78,7 +86,7 @@ class AssetBundle extends Object
*
* Each CSS file may be associated with options. In this case, the array key
* should be the CSS file path, while the corresponding array value should
* be the option array. The options will be passed to [[View
Content
::registerCssFile()]].
* be the option array. The options will be passed to [[View::registerCssFile()]].
*/
public
$css
=
array
();
/**
...
...
@@ -108,14 +116,20 @@ class AssetBundle extends Object
}
/**
* @param \yii\base\ViewContent $page
* @param AssetManager $am
* @throws InvalidConfigException
* Registers the CSS and JS files with the given view.
* This method will first register all dependent asset bundles.
* It will then try to convert non-CSS or JS files (e.g. LESS, Sass) into the corresponding
* CSS or JS files using [[AssetManager::converter|asset converter]].
* @param \yii\base\View $view the view that the asset files to be registered with.
* @throws InvalidConfigException if [[baseUrl]] or [[basePath]] is not set when the bundle
* contains internal CSS or JS files.
*/
public
function
registerAssets
(
$
page
,
$am
)
public
function
registerAssets
(
$
view
)
{
$am
=
$view
->
getAssetManager
();
foreach
(
$this
->
depends
as
$name
)
{
$
page
->
registerAssetBundle
(
$name
);
$
view
->
registerAssetBundle
(
$name
);
}
if
(
$this
->
sourcePath
!==
null
)
{
...
...
@@ -128,23 +142,23 @@ class AssetBundle extends Object
$js
=
is_string
(
$options
)
?
$options
:
$js
;
if
(
strpos
(
$js
,
'/'
)
!==
0
&&
strpos
(
$js
,
'://'
)
===
false
)
{
if
(
isset
(
$this
->
basePath
,
$this
->
baseUrl
))
{
$js
=
$converter
->
convert
(
ltrim
(
$js
,
'/'
)
,
$this
->
basePath
,
$this
->
baseUrl
);
$js
=
$converter
->
convert
(
$js
,
$this
->
basePath
,
$this
->
baseUrl
);
}
else
{
throw
new
InvalidConfigException
(
'Both of the "baseUrl" and "basePath" properties must be set.'
);
}
}
$
page
->
registerJsFile
(
$js
,
is_array
(
$options
)
?
$options
:
array
());
$
view
->
registerJsFile
(
$js
,
is_array
(
$options
)
?
$options
:
array
());
}
foreach
(
$this
->
css
as
$css
=>
$options
)
{
$css
=
is_string
(
$options
)
?
$options
:
$css
;
if
(
strpos
(
$css
,
'/
/
'
)
!==
0
&&
strpos
(
$css
,
'://'
)
===
false
)
{
if
(
strpos
(
$css
,
'/'
)
!==
0
&&
strpos
(
$css
,
'://'
)
===
false
)
{
if
(
isset
(
$this
->
basePath
,
$this
->
baseUrl
))
{
$css
=
$converter
->
convert
(
ltrim
(
$css
,
'/'
)
,
$this
->
basePath
,
$this
->
baseUrl
);
$css
=
$converter
->
convert
(
$css
,
$this
->
basePath
,
$this
->
baseUrl
);
}
else
{
throw
new
InvalidConfigException
(
'Both of the "baseUrl" and "basePath" properties must be set.'
);
}
}
$
page
->
registerCssFile
(
$css
,
is_array
(
$options
)
?
$options
:
array
());
$
view
->
registerCssFile
(
$css
,
is_array
(
$options
)
?
$options
:
array
());
}
}
}
\ No newline at end of file
framework/web/AssetManager.php
View file @
41ea9485
...
...
@@ -14,6 +14,7 @@ use yii\base\InvalidParamException;
use
yii\helpers\FileHelper
;
/**
* AssetManager manages asset bundles and asset publishing.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
...
...
@@ -135,7 +136,8 @@ class AssetManager extends Component
private
$_converter
;
/**
* @return IAssetConverter
* Returns the asset converter.
* @return IAssetConverter the asset converter.
*/
public
function
getConverter
()
{
...
...
@@ -149,6 +151,12 @@ class AssetManager extends Component
return
$this
->
_converter
;
}
/**
* Sets the asset converter.
* @param array|IAssetConverter $value the asset converter. This can be either
* an object implementing the [[IAssetConverter]] interface, or a configuration
* array that can be used to create the asset converter object.
*/
public
function
setConverter
(
$value
)
{
$this
->
_converter
=
$value
;
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment