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
30e6d28b
Commit
30e6d28b
authored
Apr 18, 2013
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Finished asset converter.
parent
767a78f8
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
85 additions
and
25 deletions
+85
-25
ViewContent.php
framework/base/ViewContent.php
+1
-0
AssetBundle.php
framework/web/AssetBundle.php
+4
-2
AssetConverter.php
framework/web/AssetConverter.php
+46
-0
AssetManager.php
framework/web/AssetManager.php
+16
-23
IAssetConverter.php
framework/web/IAssetConverter.php
+18
-0
No files found.
framework/base/ViewContent.php
View file @
30e6d28b
...
@@ -39,6 +39,7 @@ class ViewContent extends Component
...
@@ -39,6 +39,7 @@ class ViewContent extends Component
public
function
reset
()
public
function
reset
()
{
{
$this
->
assetBundles
=
null
;
$this
->
title
=
null
;
$this
->
title
=
null
;
$this
->
metaTags
=
null
;
$this
->
metaTags
=
null
;
$this
->
linkTags
=
null
;
$this
->
linkTags
=
null
;
...
...
framework/web/AssetBundle.php
View file @
30e6d28b
...
@@ -122,11 +122,13 @@ class AssetBundle extends Object
...
@@ -122,11 +122,13 @@ class AssetBundle extends Object
list
(
$this
->
basePath
,
$this
->
baseUrl
)
=
$am
->
publish
(
$this
->
sourcePath
,
$this
->
publishOptions
);
list
(
$this
->
basePath
,
$this
->
baseUrl
)
=
$am
->
publish
(
$this
->
sourcePath
,
$this
->
publishOptions
);
}
}
$converter
=
$am
->
getConverter
();
foreach
(
$this
->
js
as
$js
=>
$options
)
{
foreach
(
$this
->
js
as
$js
=>
$options
)
{
$js
=
is_string
(
$options
)
?
$options
:
$js
;
$js
=
is_string
(
$options
)
?
$options
:
$js
;
if
(
strpos
(
$js
,
'/'
)
!==
0
&&
strpos
(
$js
,
'://'
)
===
false
)
{
if
(
strpos
(
$js
,
'/'
)
!==
0
&&
strpos
(
$js
,
'://'
)
===
false
)
{
if
(
isset
(
$this
->
basePath
,
$this
->
baseUrl
))
{
if
(
isset
(
$this
->
basePath
,
$this
->
baseUrl
))
{
$js
=
$
am
->
processAsse
t
(
ltrim
(
$js
,
'/'
),
$this
->
basePath
,
$this
->
baseUrl
);
$js
=
$
converter
->
conver
t
(
ltrim
(
$js
,
'/'
),
$this
->
basePath
,
$this
->
baseUrl
);
}
else
{
}
else
{
throw
new
InvalidConfigException
(
'Both of the "baseUrl" and "basePath" properties must be set.'
);
throw
new
InvalidConfigException
(
'Both of the "baseUrl" and "basePath" properties must be set.'
);
}
}
...
@@ -137,7 +139,7 @@ class AssetBundle extends Object
...
@@ -137,7 +139,7 @@ class AssetBundle extends Object
$css
=
is_string
(
$options
)
?
$options
:
$css
;
$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
))
{
if
(
isset
(
$this
->
basePath
,
$this
->
baseUrl
))
{
$css
=
$
am
->
processAsse
t
(
ltrim
(
$css
,
'/'
),
$this
->
basePath
,
$this
->
baseUrl
);
$css
=
$
converter
->
conver
t
(
ltrim
(
$css
,
'/'
),
$this
->
basePath
,
$this
->
baseUrl
);
}
else
{
}
else
{
throw
new
InvalidConfigException
(
'Both of the "baseUrl" and "basePath" properties must be set.'
);
throw
new
InvalidConfigException
(
'Both of the "baseUrl" and "basePath" properties must be set.'
);
}
}
...
...
framework/web/AssetConverter.php
0 → 100644
View file @
30e6d28b
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace
yii\web
;
use
Yii
;
use
yii\base\Component
;
/**
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class
AssetConverter
extends
Component
implements
IAssetConverter
{
public
$commands
=
array
(
'less'
=>
array
(
'css'
,
'lessc %s %s'
),
'scss'
=>
array
(
'css'
,
'sass %s %s'
),
'sass'
=>
array
(
'css'
,
'sass %s %s'
),
'styl'
=>
array
(
'js'
,
'stylus < %s > %s'
),
);
public
function
convert
(
$asset
,
$basePath
,
$baseUrl
)
{
$pos
=
strrpos
(
$asset
,
'.'
);
if
(
$pos
!==
false
)
{
$ext
=
substr
(
$asset
,
$pos
+
1
);
if
(
isset
(
$this
->
commands
[
$ext
]))
{
list
(
$ext
,
$command
)
=
$this
->
commands
[
$ext
];
$result
=
substr
(
$asset
,
0
,
$pos
+
1
)
.
$ext
;
if
(
@
filemtime
(
"
$basePath
/
$result
"
)
<
filemtime
(
"
$basePath
/
$asset
"
))
{
$output
=
array
();
$command
=
sprintf
(
$command
,
"
$basePath
/
$asset
"
,
"
$basePath
/
$result
"
);
exec
(
$command
,
$output
);
Yii
::
info
(
"Converted
$asset
into
$result
: "
.
implode
(
"
\n
"
,
$output
),
__METHOD__
);
return
"
$baseUrl
/
$result
"
;
}
}
}
return
"
$baseUrl
/
$asset
"
;
}
}
\ No newline at end of file
framework/web/AssetManager.php
View file @
30e6d28b
...
@@ -26,13 +26,6 @@ class AssetManager extends Component
...
@@ -26,13 +26,6 @@ class AssetManager extends Component
*/
*/
public
$bundles
;
public
$bundles
;
/**
/**
* @var array list of asset processors. An asset processor will convert a special type of asset files
* (e.g. LESS, Sass, TypeScript) into JS or CSS files. The array keys are the file extension names
* (e.g. "less", "sass", "ts"), and the array values are the corresponding configuration arrays
* for creating the processor objects.
*/
public
$processors
;
/**
* @return string the root directory storing the published asset files.
* @return string the root directory storing the published asset files.
*/
*/
public
$basePath
=
'@wwwroot/assets'
;
public
$basePath
=
'@wwwroot/assets'
;
...
@@ -139,26 +132,26 @@ class AssetManager extends Component
...
@@ -139,26 +132,26 @@ class AssetManager extends Component
return
$this
->
bundles
[
$name
];
return
$this
->
bundles
[
$name
];
}
}
private
$_converter
;
/**
/**
* Processes the given asset file and returns a URL to the processed one.
* @return IAssetConverter
* This method can be overwritten to support various types of asset files, such as LESS, Sass, TypeScript.
* @param string $asset the asset file path to be processed. The file path is relative
* to $basePath, and it may contain forward slashes to indicate sub-directories (e.g. "js/main.js").
* @param string $basePath the directory that contains the asset file.
* @param string $baseUrl the corresponding URL of $basePath.
* @return string the processed asset file path.
*/
*/
public
function
processAsset
(
$asset
,
$basePath
,
$baseUrl
)
public
function
getConverter
(
)
{
{
$ext
=
pathinfo
(
$asset
,
PATHINFO_EXTENSION
);
if
(
$this
->
_converter
===
null
)
{
if
(
isset
(
$this
->
processors
[
$ext
]))
{
$this
->
_converter
=
Yii
::
createObject
(
array
(
if
(
is_array
(
$this
->
processors
[
$ext
]))
{
'class'
=>
'yii\\web\\AssetConverter'
,
$this
->
processors
[
$ext
]
=
Yii
::
createObject
(
$this
->
processors
[
$ext
]);
));
}
}
elseif
(
is_array
(
$this
->
_converter
)
||
is_string
(
$this
->
_converter
))
{
return
$this
->
processors
[
$ext
]
->
process
(
$asset
,
$basePath
,
$baseUrl
);
$this
->
_converter
=
Yii
::
createObject
(
$this
->
_converter
);
}
else
{
return
$baseUrl
.
'/'
.
$asset
;
}
}
return
$this
->
_converter
;
}
public
function
setConverter
(
$value
)
{
$this
->
_converter
=
$value
;
}
}
/**
/**
...
...
framework/web/IAssetConverter.php
0 → 100644
View file @
30e6d28b
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace
yii\web
;
/**
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
interface
IAssetConverter
{
public
function
convert
(
$asset
,
$basePath
,
$baseUrl
);
}
\ No newline at end of file
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