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
e132ed8d
Commit
e132ed8d
authored
Apr 22, 2013
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
initial implementation of asset command.
parent
f21499dd
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
108 additions
and
10 deletions
+108
-10
Application.php
framework/console/Application.php
+1
-1
AssetController.php
framework/console/controllers/AssetController.php
+107
-9
No files found.
framework/console/Application.php
View file @
e132ed8d
...
...
@@ -129,7 +129,7 @@ class Application extends \yii\base\Application
'migrate'
=>
'yii\console\controllers\MigrateController'
,
'app'
=>
'yii\console\controllers\AppController'
,
'cache'
=>
'yii\console\controllers\CacheController'
,
'
script'
=>
'yii\console\controllers\Scrip
tController'
,
'
asset'
=>
'yii\console\controllers\Asse
tController'
,
);
}
...
...
framework/console/controllers/
Scrip
tController.php
→
framework/console/controllers/
Asse
tController.php
View file @
e132ed8d
...
...
@@ -15,7 +15,7 @@ use yii\console\Controller;
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class
Scrip
tController
extends
Controller
class
Asse
tController
extends
Controller
{
public
$defaultAction
=
'compress'
;
...
...
@@ -33,13 +33,15 @@ class ScriptController extends Controller
*/
public
$targets
=
array
();
public
$assetManager
=
array
();
public
$jsCompressor
=
'java -jar compiler.jar --js {from} --js_output_file {to}'
;
public
$cssCompressor
=
'java -jar yuicompressor.jar {from} -o {to}'
;
public
function
actionCompress
(
$configFile
,
$bundleFile
)
{
$this
->
loadConfiguration
(
$configFile
);
$bundles
=
$this
->
loadBundles
(
$this
->
bundles
,
$this
->
extensions
);
$targets
=
$this
->
loadTargets
(
$this
->
targets
,
$bundles
);
//
$this->publishBundles($bundles, $this->publishOptions);
$this
->
publishBundles
(
$bundles
,
$this
->
publishOptions
);
$timestamp
=
time
();
foreach
(
$targets
as
$target
)
{
if
(
!
empty
(
$target
->
js
))
{
...
...
@@ -96,11 +98,38 @@ class ScriptController extends Controller
protected
function
loadTargets
(
$targets
,
$bundles
)
{
// build the dependency order of bundles
$registered
=
array
();
foreach
(
$bundles
as
$name
=>
$bundle
)
{
$this
->
registerBundle
(
$bundles
,
$name
,
$registered
);
}
$bundleOrders
=
array_combine
(
array_keys
(
$registered
),
range
(
0
,
count
(
$bundles
)
-
1
));
// fill up the target which has empty 'depends'.
$referenced
=
array
();
foreach
(
$targets
as
$name
=>
$target
)
{
if
(
empty
(
$target
[
'depends'
]))
{
if
(
!
isset
(
$all
))
{
$all
=
$name
;
}
else
{
throw
new
Exception
(
"Only one target can have empty 'depends' option. Found two now:
$all
,
$name
"
);
}
}
else
{
foreach
(
$target
[
'depends'
]
as
$bundle
)
{
if
(
!
isset
(
$referenced
[
$bundle
]))
{
$referenced
[
$bundle
]
=
$name
;
}
else
{
throw
new
Exception
(
"Target '
{
$referenced
[
$bundle
]
}
' and '
$name
' cannot contain the bundle '
$bundle
' at the same time."
);
}
}
}
}
if
(
isset
(
$all
))
{
$targets
[
$all
][
'depends'
]
=
array_diff
(
array_keys
(
$registered
),
array_keys
(
$referenced
));
}
// adjust the 'depends' order for each target according to the dependency order of bundles
// create an AssetBundle object for each target
foreach
(
$targets
as
$name
=>
$target
)
{
if
(
!
isset
(
$target
[
'basePath'
]))
{
throw
new
Exception
(
"Please specify 'basePath' for the '
$name
' target."
);
...
...
@@ -172,11 +201,7 @@ class ScriptController extends Controller
$map
=
array
();
foreach
(
$targets
as
$name
=>
$target
)
{
foreach
(
$target
->
depends
as
$bundle
)
{
if
(
!
isset
(
$map
[
$bundle
]))
{
$map
[
$bundle
]
=
$name
;
}
else
{
throw
new
Exception
(
"Bundle '
$bundle
' is found in both target '
{
$map
[
$bundle
]
}
' and '
$name
'."
);
}
$map
[
$bundle
]
=
$name
;
}
}
...
...
@@ -236,7 +261,8 @@ class ScriptController extends Controller
file_put_contents
(
$bundleFile
,
<<<EOD
<?php
/**
* Do not modify this file manually as it is automatically generated by the "yiic script" command.
* This file is generated by the "yiic script" command.
* DO NOT MODIFY THIS FILE DIRECTLY.
* @version $version
*/
return $array;
...
...
@@ -246,11 +272,82 @@ EOD
protected
function
compressJsFiles
(
$inputFiles
,
$outputFile
)
{
if
(
is_string
(
$this
->
jsCompressor
))
{
$tmpFile
=
$outputFile
.
'.tmp'
;
$this
->
combineJsFiles
(
$inputFiles
,
$tmpFile
);
$log
=
shell_exec
(
strtr
(
$this
->
jsCompressor
,
array
(
'{from}'
=>
$tmpFile
,
'{to}'
=>
$outputFile
,
)));
@
unlink
(
$tmpFile
);
}
else
{
$log
=
call_user_func
(
$this
->
jsCompressor
,
$this
,
$inputFiles
,
$outputFile
);
}
}
protected
function
compressCssFiles
(
$inputFiles
,
$outputFile
)
{
if
(
is_string
(
$this
->
cssCompressor
))
{
$tmpFile
=
$outputFile
.
'.tmp'
;
$this
->
combineCssFiles
(
$inputFiles
,
$tmpFile
);
$log
=
shell_exec
(
strtr
(
$this
->
cssCompressor
,
array
(
'{from}'
=>
$inputFiles
,
'{to}'
=>
$outputFile
,
)));
}
else
{
$log
=
call_user_func
(
$this
->
cssCompressor
,
$this
,
$inputFiles
,
$outputFile
);
}
}
public
function
combineJsFiles
(
$files
,
$tmpFile
)
{
$content
=
''
;
foreach
(
$files
as
$file
)
{
$content
.=
"/*** BEGIN FILE:
$file
***/
\n
"
.
file_get_contents
(
$file
)
.
"/*** END FILE:
$file
***/
\n
"
;
}
file_put_contents
(
$tmpFile
,
$content
);
}
public
function
combineCssFiles
(
$files
,
$tmpFile
)
{
// todo: adjust url() references in CSS files
$content
=
''
;
foreach
(
$files
as
$file
)
{
$content
.=
"/*** BEGIN FILE:
$file
***/
\n
"
.
file_get_contents
(
$file
)
.
"/*** END FILE:
$file
***/
\n
"
;
}
file_put_contents
(
$tmpFile
,
$content
);
}
public
function
actionTemplate
(
$configFile
)
{
$template
=
<<<EOD
<?php
return array(
//
'bundles' => require('path/to/bundles.php'),
//
'extensions' => require('path/to/namespaces.php'),
//
'targets' => array(
'all' => array(
'basePath' => __DIR__,
'baseUrl' => '/test',
'js' => 'all-{ts}.js',
'css' => 'all-{ts}.css',
),
),
'assetManager' => array(
'basePath' => __DIR__,
'baseUrl' => '/test',
),
);
EOD;
file_put_contents
(
$configFile
,
$template
);
}
}
\ 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