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
37e60949
Commit
37e60949
authored
May 26, 2013
by
Qiang Xue
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #416 from klimov-paul/asset-command-i-3
#72 Improve the "asset" command Iteration 3
parents
f5d0bcbc
f65d1398
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
140 additions
and
2 deletions
+140
-2
AssetController.php
framework/yii/console/controllers/AssetController.php
+62
-2
AssetControllerTest.php
...nit/framework/console/controllers/AssetControllerTest.php
+78
-0
No files found.
framework/yii/console/controllers/AssetController.php
View file @
37e60949
...
@@ -517,17 +517,77 @@ EOD
...
@@ -517,17 +517,77 @@ EOD
*/
*/
public
function
combineCssFiles
(
$inputFiles
,
$outputFile
)
public
function
combineCssFiles
(
$inputFiles
,
$outputFile
)
{
{
// todo: adjust url() references in CSS files
$content
=
''
;
$content
=
''
;
foreach
(
$inputFiles
as
$file
)
{
foreach
(
$inputFiles
as
$file
)
{
$content
.=
"/*** BEGIN FILE:
$file
***/
\n
"
$content
.=
"/*** BEGIN FILE:
$file
***/
\n
"
.
file_get_contents
(
$file
)
.
$this
->
adjustCssUrl
(
file_get_contents
(
$file
),
dirname
(
$file
),
dirname
(
$outputFile
)
)
.
"/*** END FILE:
$file
***/
\n
"
;
.
"/*** END FILE:
$file
***/
\n
"
;
}
}
file_put_contents
(
$outputFile
,
$content
);
file_put_contents
(
$outputFile
,
$content
);
}
}
/**
/**
* Adjusts CSS content allowing URL references pointing to the original resources.
* @param string $cssContent source CSS content.
* @param string $inputFilePath input CSS file name.
* @param string $outputFilePath output CSS file name.
* @return string adjusted CSS content.
*/
protected
function
adjustCssUrl
(
$cssContent
,
$inputFilePath
,
$outputFilePath
)
{
$sharedPathParts
=
array
();
$inputFilePathParts
=
explode
(
'/'
,
$inputFilePath
);
$inputFilePathPartsCount
=
count
(
$inputFilePathParts
);
$outputFilePathParts
=
explode
(
'/'
,
$outputFilePath
);
$outputFilePathPartsCount
=
count
(
$outputFilePathParts
);
for
(
$i
=
0
;
$i
<
$inputFilePathPartsCount
&&
$i
<
$outputFilePathPartsCount
;
$i
++
)
{
if
(
$inputFilePathParts
[
$i
]
==
$outputFilePathParts
[
$i
])
{
$sharedPathParts
[]
=
$inputFilePathParts
[
$i
];
}
else
{
break
;
}
}
$sharedPath
=
implode
(
'/'
,
$sharedPathParts
);
$inputFileRelativePath
=
trim
(
str_replace
(
$sharedPath
,
''
,
$inputFilePath
),
'/'
);
$outputFileRelativePath
=
trim
(
str_replace
(
$sharedPath
,
''
,
$outputFilePath
),
'/'
);
$inputFileRelativePathParts
=
explode
(
'/'
,
$inputFileRelativePath
);
$outputFileRelativePathParts
=
explode
(
'/'
,
$outputFileRelativePath
);
$callback
=
function
(
$matches
)
use
(
$inputFileRelativePathParts
,
$outputFileRelativePathParts
)
{
$fullMatch
=
$matches
[
0
];
$inputUrl
=
$matches
[
1
];
if
(
preg_match
(
'/https?:\/\//is'
,
$inputUrl
))
{
return
$fullMatch
;
}
$outputUrlParts
=
array_fill
(
0
,
count
(
$outputFileRelativePathParts
),
'..'
);
$outputUrlParts
=
array_merge
(
$outputUrlParts
,
$inputFileRelativePathParts
);
if
(
strpos
(
$inputUrl
,
'/'
)
!==
false
)
{
$inputUrlParts
=
explode
(
'/'
,
$inputUrl
);
foreach
(
$inputUrlParts
as
$key
=>
$inputUrlPart
)
{
if
(
$inputUrlPart
==
'..'
)
{
array_pop
(
$outputUrlParts
);
unset
(
$inputUrlParts
[
$key
]);
}
}
$outputUrlParts
[]
=
implode
(
'/'
,
$inputUrlParts
);
}
else
{
$outputUrlParts
[]
=
$inputUrl
;
}
$outputUrl
=
implode
(
'/'
,
$outputUrlParts
);
return
str_replace
(
$inputUrl
,
$outputUrl
,
$fullMatch
);
};
$cssContent
=
preg_replace_callback
(
'/url\(["\']?([^"]*)["\']?\)/is'
,
$callback
,
$cssContent
);
return
$cssContent
;
}
/**
* 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.
*/
*/
...
...
tests/unit/framework/console/controllers/AssetControllerTest.php
View file @
37e60949
...
@@ -169,6 +169,23 @@ class AssetControllerTest extends TestCase
...
@@ -169,6 +169,23 @@ class AssetControllerTest extends TestCase
}
}
}
}
/**
* Invokes the asset controller method even if it is protected.
* @param string $methodName name of the method to be invoked.
* @param array $args method arguments.
* @return mixed method invoke result.
*/
protected
function
invokeAssetControllerMethod
(
$methodName
,
array
$args
=
array
())
{
$controller
=
$this
->
createAssetController
();
$controllerClassReflection
=
new
ReflectionClass
(
get_class
(
$controller
));
$methodReflection
=
$controllerClassReflection
->
getMethod
(
$methodName
);
$methodReflection
->
setAccessible
(
true
);
$result
=
$methodReflection
->
invokeArgs
(
$controller
,
$args
);
$methodReflection
->
setAccessible
(
false
);
return
$result
;
}
// Tests :
// Tests :
public
function
testActionTemplate
()
public
function
testActionTemplate
()
...
@@ -237,4 +254,65 @@ class AssetControllerTest extends TestCase
...
@@ -237,4 +254,65 @@ class AssetControllerTest extends TestCase
$this
->
assertContains
(
$content
,
$compressedJsFileContent
,
"Source of '
{
$name
}
' is missing in combined file!"
);
$this
->
assertContains
(
$content
,
$compressedJsFileContent
,
"Source of '
{
$name
}
' is missing in combined file!"
);
}
}
}
}
/**
* Data provider for [[testAdjustCssUrl()]].
* @return array test data.
*/
public
function
adjustCssUrlDataProvider
()
{
return
array
(
array
(
'.published-same-dir-class {background-image: url(published_same_dir.png);}'
,
'/test/base/path/assets/input'
,
'/test/base/path/assets/output'
,
'.published-same-dir-class {background-image: url(../input/published_same_dir.png);}'
,
),
array
(
'.published-relative-dir-class {background-image: url(../img/published_relative_dir.png);}'
,
'/test/base/path/assets/input'
,
'/test/base/path/assets/output'
,
'.published-relative-dir-class {background-image: url(../img/published_relative_dir.png);}'
,
),
array
(
'.static-same-dir-class {background-image: url(\'static_same_dir.png\');}'
,
'/test/base/path/css'
,
'/test/base/path/assets/output'
,
'.static-same-dir-class {background-image: url(\'../../css/static_same_dir.png\');}'
,
),
array
(
'.static-relative-dir-class {background-image: url("../img/static_relative_dir.png");}'
,
'/test/base/path/css'
,
'/test/base/path/assets/output'
,
'.static-relative-dir-class {background-image: url("../../img/static_relative_dir.png");}'
,
),
array
(
'.absolute-url-class {background-image: url(http://domain.com/img/image.gif);}'
,
'/test/base/path/assets/input'
,
'/test/base/path/assets/output'
,
'.absolute-url-class {background-image: url(http://domain.com/img/image.gif);}'
,
),
array
(
'.absolute-url-secure-class {background-image: url(https://secure.domain.com/img/image.gif);}'
,
'/test/base/path/assets/input'
,
'/test/base/path/assets/output'
,
'.absolute-url-secure-class {background-image: url(https://secure.domain.com/img/image.gif);}'
,
),
);
}
/**
* @dataProvider adjustCssUrlDataProvider
*
* @param $cssContent
* @param $inputFilePath
* @param $outputFilePath
* @param $expectedCssContent
*/
public
function
testAdjustCssUrl
(
$cssContent
,
$inputFilePath
,
$outputFilePath
,
$expectedCssContent
)
{
$adjustedCssContent
=
$this
->
invokeAssetControllerMethod
(
'adjustCssUrl'
,
array
(
$cssContent
,
$inputFilePath
,
$outputFilePath
));
$this
->
assertEquals
(
$expectedCssContent
,
$adjustedCssContent
,
'Unable to adjust CSS correctly!'
);
}
}
}
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