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
e45e26ea
Commit
e45e26ea
authored
Jun 04, 2013
by
Qiang Xue
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #486 from klimov-paul/asset-command-i-4
#72 Improve the "asset" command Iteration 4
parents
084d709e
152e5a0e
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
47 additions
and
23 deletions
+47
-23
AssetController.php
framework/yii/console/controllers/AssetController.php
+46
-23
AssetControllerTest.php
...nit/framework/console/controllers/AssetControllerTest.php
+1
-0
No files found.
framework/yii/console/controllers/AssetController.php
View file @
e45e26ea
...
@@ -14,6 +14,20 @@ use yii\console\Controller;
...
@@ -14,6 +14,20 @@ use yii\console\Controller;
/**
/**
* This command allows you to combine and compress your JavaScript and CSS files.
* This command allows you to combine and compress your JavaScript and CSS files.
*
*
* Usage:
* 1. Create a configuration file using 'template' action:
* yii asset/template /path/to/myapp/config.php
* 2. Edit the created config file, adjusting it for your web application needs.
* 3. Run the 'compress' action, using created config:
* yii asset /path/to/myapp/config.php /path/to/myapp/config/assets_compressed.php
* 4. Adjust your web application config to use compressed assets.
*
* Note: in the console environment some path aliases like '@wwwroot' and '@www' may not exist,
* so corresponding paths inside the configuration should be specified directly.
*
* Note: by default this command relies on an external tools to perform actual files compression,
* check [[jsCompressor]] and [[cssCompressor]] for more details.
*
* @property array|\yii\web\AssetManager $assetManager asset manager, which will be used for assets processing.
* @property array|\yii\web\AssetManager $assetManager asset manager, which will be used for assets processing.
*
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @author Qiang Xue <qiang.xue@gmail.com>
...
@@ -43,7 +57,7 @@ class AssetController extends Controller
...
@@ -43,7 +57,7 @@ class AssetController extends Controller
* ~~~
* ~~~
* 'all' => array(
* 'all' => array(
* 'css' => 'all.css',
* 'css' => 'all.css',
* 'js' => '
js.cs
s',
* 'js' => '
all.j
s',
* 'depends' => array( ... ),
* 'depends' => array( ... ),
* )
* )
* ~~~
* ~~~
...
@@ -57,7 +71,7 @@ class AssetController extends Controller
...
@@ -57,7 +71,7 @@ class AssetController extends Controller
*/
*/
private
$_assetManager
=
array
();
private
$_assetManager
=
array
();
/**
/**
* @var string|callback Java
Script file compressor.
* @var string|callback JavaScript file compressor.
* If a string, it is treated as shell command template, which should contain
* If a string, it is treated as shell command template, which should contain
* placeholders {from} - source file name - and {to} - output file name.
* placeholders {from} - source file name - and {to} - output file name.
* Otherwise, it is treated as PHP callback, which should perform the compression.
* Otherwise, it is treated as PHP callback, which should perform the compression.
...
@@ -159,7 +173,7 @@ class AssetController extends Controller
...
@@ -159,7 +173,7 @@ class AssetController extends Controller
}
}
}
}
$this
->
getAssetManager
();
// check
asset manager configuration
$this
->
getAssetManager
();
// check
if asset manager configuration is correct
}
}
/**
/**
...
@@ -308,7 +322,7 @@ class AssetController extends Controller
...
@@ -308,7 +322,7 @@ class AssetController extends Controller
/**
/**
* Builds output asset bundle.
* Builds output asset bundle.
* @param \yii\web\AssetBundle $target output asset bundle
* @param \yii\web\AssetBundle $target output asset bundle
* @param string $type either
"js" or "css"
.
* @param string $type either
'js' or 'css'
.
* @param \yii\web\AssetBundle[] $bundles source asset bundles.
* @param \yii\web\AssetBundle[] $bundles source asset bundles.
* @param integer $timestamp current timestamp.
* @param integer $timestamp current timestamp.
* @throws Exception on failure.
* @throws Exception on failure.
...
@@ -420,24 +434,23 @@ class AssetController extends Controller
...
@@ -420,24 +434,23 @@ class AssetController extends Controller
}
}
$array
=
var_export
(
$array
,
true
);
$array
=
var_export
(
$array
,
true
);
$version
=
date
(
'Y-m-d H:i:s'
,
time
());
$version
=
date
(
'Y-m-d H:i:s'
,
time
());
$b
ytesWritten
=
file_put_contents
(
$bundleFile
,
<<<EOD
$b
undleFileContent
=
<<<EOD
<?php
<?php
/**
/**
* This file is generated by the "yii
script
" command.
* This file is generated by the "yii
{$this->id}
" command.
* DO NOT MODIFY THIS FILE DIRECTLY.
* DO NOT MODIFY THIS FILE DIRECTLY.
* @version
$version
* @version
{$version}
*/
*/
return $array;
return {$array};
EOD
EOD;
);
if
(
!
file_put_contents
(
$bundleFile
,
$bundleFileContent
))
{
if
(
$bytesWritten
<=
0
)
{
throw
new
Exception
(
"Unable to write output bundle configuration at '
{
$bundleFile
}
'."
);
throw
new
Exception
(
"Unable to write output bundle configuration at '
{
$bundleFile
}
'."
);
}
}
echo
"Output bundle configuration created at '
{
$bundleFile
}
'.
\n
"
;
echo
"Output bundle configuration created at '
{
$bundleFile
}
'.
\n
"
;
}
}
/**
/**
* Compresses given Java
Script files and combines them into the single one.
* Compresses given JavaScript files and combines them into the single one.
* @param array $inputFiles list of source file names.
* @param array $inputFiles list of source file names.
* @param string $outputFile output file name.
* @param string $outputFile output file name.
* @throws \yii\console\Exception on failure
* @throws \yii\console\Exception on failure
...
@@ -495,9 +508,10 @@ EOD
...
@@ -495,9 +508,10 @@ EOD
}
}
/**
/**
* Combines Java
Script files into a single one.
* Combines JavaScript files into a single one.
* @param array $inputFiles source file names.
* @param array $inputFiles source file names.
* @param string $outputFile output file name.
* @param string $outputFile output file name.
* @throws \yii\console\Exception on failure.
*/
*/
public
function
combineJsFiles
(
$inputFiles
,
$outputFile
)
public
function
combineJsFiles
(
$inputFiles
,
$outputFile
)
{
{
...
@@ -507,13 +521,16 @@ EOD
...
@@ -507,13 +521,16 @@ EOD
.
file_get_contents
(
$file
)
.
file_get_contents
(
$file
)
.
"/*** END FILE:
$file
***/
\n
"
;
.
"/*** END FILE:
$file
***/
\n
"
;
}
}
file_put_contents
(
$outputFile
,
$content
);
if
(
!
file_put_contents
(
$outputFile
,
$content
))
{
throw
new
Exception
(
"Unable to write output JavaScript file '
{
$outputFile
}
'."
);
}
}
}
/**
/**
* Combines CSS files into a single one.
* Combines CSS files into a single one.
* @param array $inputFiles source file names.
* @param array $inputFiles source file names.
* @param string $outputFile output file name.
* @param string $outputFile output file name.
* @throws \yii\console\Exception on failure.
*/
*/
public
function
combineCssFiles
(
$inputFiles
,
$outputFile
)
public
function
combineCssFiles
(
$inputFiles
,
$outputFile
)
{
{
...
@@ -523,7 +540,9 @@ EOD
...
@@ -523,7 +540,9 @@ EOD
.
$this
->
adjustCssUrl
(
file_get_contents
(
$file
),
dirname
(
$file
),
dirname
(
$outputFile
))
.
$this
->
adjustCssUrl
(
file_get_contents
(
$file
),
dirname
(
$file
),
dirname
(
$outputFile
))
.
"/*** END FILE:
$file
***/
\n
"
;
.
"/*** END FILE:
$file
***/
\n
"
;
}
}
file_put_contents
(
$outputFile
,
$content
);
if
(
!
file_put_contents
(
$outputFile
,
$content
))
{
throw
new
Exception
(
"Unable to write output CSS file '
{
$outputFile
}
'."
);
}
}
}
/**
/**
...
@@ -590,18 +609,23 @@ EOD
...
@@ -590,18 +609,23 @@ EOD
/**
/**
* Creates template of configuration file for [[actionCompress]].
* Creates template of configuration file for [[actionCompress]].
* @param string $configFile output file name.
* @param string $configFile output file name.
* @throws \yii\console\Exception on failure.
*/
*/
public
function
actionTemplate
(
$configFile
)
public
function
actionTemplate
(
$configFile
)
{
{
$template
=
<<<EOD
$template
=
<<<EOD
<?php
<?php
/**
* Configuration file for the "yii asset" console command.
* Note: in the console environment some path aliases like '@wwwroot' and '@www' may not exist,
* so corresponding paths should be specified directly.
*/
return array(
return array(
//
//
The list of asset bundles to compress:
'bundles' => require('path/to/bundles.php'),
'bundles' => require('path/to/bundles.php'),
//
//
The list of extensions to compress:
'extensions' => require('path/to/namespaces.php'),
'extensions' => require('path/to/namespaces.php'),
//
//
Asset bundle for compression output:
'targets' => array(
'targets' => array(
'all' => array(
'all' => array(
'basePath' => __DIR__,
'basePath' => __DIR__,
...
@@ -610,7 +634,7 @@ return array(
...
@@ -610,7 +634,7 @@ return array(
'css' => 'all-{ts}.css',
'css' => 'all-{ts}.css',
),
),
),
),
// Asset manager configuration:
'assetManager' => array(
'assetManager' => array(
'basePath' => __DIR__,
'basePath' => __DIR__,
'baseUrl' => '/test',
'baseUrl' => '/test',
...
@@ -622,9 +646,8 @@ EOD;
...
@@ -622,9 +646,8 @@ EOD;
return
;
return
;
}
}
}
}
$bytesWritten
=
file_put_contents
(
$configFile
,
$template
);
if
(
!
file_put_contents
(
$configFile
,
$template
))
{
if
(
$bytesWritten
<=
0
)
{
throw
new
Exception
(
"Unable to write template file '
{
$configFile
}
'."
);
echo
"Error: unable to write file '
{
$configFile
}
'!
\n\n
"
;
}
else
{
}
else
{
echo
"Configuration file template created at '
{
$configFile
}
'.
\n\n
"
;
echo
"Configuration file template created at '
{
$configFile
}
'.
\n\n
"
;
}
}
...
...
tests/unit/framework/console/controllers/AssetControllerTest.php
View file @
e45e26ea
...
@@ -239,6 +239,7 @@ class AssetControllerTest extends TestCase
...
@@ -239,6 +239,7 @@ class AssetControllerTest extends TestCase
// Then :
// Then :
$this
->
assertTrue
(
file_exists
(
$bundleFile
),
'Unable to create output bundle file!'
);
$this
->
assertTrue
(
file_exists
(
$bundleFile
),
'Unable to create output bundle file!'
);
$this
->
assertTrue
(
is_array
(
require
(
$bundleFile
)),
'Output bundle file has incorrect format!'
);
$compressedCssFileName
=
$this
->
testAssetsBasePath
.
DIRECTORY_SEPARATOR
.
'all.css'
;
$compressedCssFileName
=
$this
->
testAssetsBasePath
.
DIRECTORY_SEPARATOR
.
'all.css'
;
$this
->
assertTrue
(
file_exists
(
$compressedCssFileName
),
'Unable to compress CSS files!'
);
$this
->
assertTrue
(
file_exists
(
$compressedCssFileName
),
'Unable to compress CSS files!'
);
...
...
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