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
58589e03
Commit
58589e03
authored
Nov 05, 2013
by
Carsten Brandt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
improved asset converter command running
now also gets stderr output. fixes #1140
parent
27404d5d
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
78 additions
and
6 deletions
+78
-6
AssetConverter.php
framework/yii/web/AssetConverter.php
+35
-6
AssetConverterTest.php
tests/unit/framework/web/AssetConverterTest.php
+43
-0
No files found.
framework/yii/web/AssetConverter.php
View file @
58589e03
...
...
@@ -47,17 +47,46 @@ class AssetConverter extends Component implements AssetConverterInterface
list
(
$ext
,
$command
)
=
$this
->
commands
[
$ext
];
$result
=
substr
(
$asset
,
0
,
$pos
+
1
)
.
$ext
;
if
(
@
filemtime
(
"
$basePath
/
$result
"
)
<
filemtime
(
"
$basePath
/
$asset
"
))
{
$output
=
[];
$this
->
runCommand
(
$command
,
$basePath
,
$asset
,
$result
);
}
return
$result
;
}
}
return
$asset
;
}
/**
* Runs a command to convert asset files.
* @param string $command the command to run
* @param string $basePath asset base path and command working directory
* @param string $asset the name of the asset file
* @param string $result the name of the file to be generated by the converter command
* @return bool true on success, false on failure. Failures will be logged.
*/
protected
function
runCommand
(
$command
,
$basePath
,
$asset
,
$result
)
{
$command
=
strtr
(
$command
,
[
'{from}'
=>
escapeshellarg
(
"
$basePath
/
$asset
"
),
'{to}'
=>
escapeshellarg
(
"
$basePath
/
$result
"
),
]);
exec
(
$command
,
$output
);
Yii
::
trace
(
"Converted
$asset
into
$result
: "
.
implode
(
"
\n
"
,
$output
),
__METHOD__
);
}
return
$result
;
$descriptor
=
array
(
1
=>
array
(
'pipe'
,
'w'
),
2
=>
array
(
'pipe'
,
'w'
),
);
$pipes
=
array
();
$proc
=
proc_open
(
$command
,
$descriptor
,
$pipes
,
$basePath
);
$stdout
=
stream_get_contents
(
$pipes
[
1
]);
$stderr
=
stream_get_contents
(
$pipes
[
2
]);
foreach
(
$pipes
as
$pipe
)
{
fclose
(
$pipe
);
}
$status
=
proc_close
(
$proc
);
if
(
$status
!==
0
)
{
Yii
::
error
(
"AssetConverter command '
$command
' failed with exit code
$status
:
\n
STDOUT:
\n
$stdout
\n
STDERR:
\n
$stderr
\n
"
);
}
else
{
Yii
::
trace
(
"Converted
$asset
into
$result
:
\n
STDOUT:
\n
$stdout
\n
STDERR:
\n
$stderr
"
,
__METHOD__
);
}
return
$
asset
;
return
$
status
===
0
;
}
}
tests/unit/framework/web/AssetConverterTest.php
0 → 100644
View file @
58589e03
<?php
/**
* @author Carsten Brandt <mail@cebe.cc>
*/
namespace
yiiunit\framework\web
;
use
yii\web\AssetConverter
;
/**
* @group web
*/
class
AssetConverterTest
extends
\yiiunit\TestCase
{
protected
function
setUp
()
{
parent
::
setUp
();
$this
->
mockApplication
();
}
public
function
testConvert
()
{
$tmpPath
=
\Yii
::
$app
->
runtimePath
.
'/assetConverterTest'
;
if
(
!
is_dir
(
$tmpPath
))
{
mkdir
(
$tmpPath
,
0777
,
true
);
}
file_put_contents
(
$tmpPath
.
'/test.php'
,
<<<EOF
<?php
echo "Hello World!\n";
echo "Hello Yii!";
EOF
);
$converter
=
new
AssetConverter
();
$converter
->
commands
[
'php'
]
=
[
'txt'
,
'php {from} > {to}'
];
$this
->
assertEquals
(
'test.txt'
,
$converter
->
convert
(
'test.php'
,
$tmpPath
));
$this
->
assertTrue
(
file_exists
(
$tmpPath
.
'/test.txt'
),
'Failed asserting that asset output file exists.'
);
$this
->
assertEquals
(
"Hello World!
\n
Hello Yii!"
,
file_get_contents
(
$tmpPath
.
'/test.txt'
));
}
}
\ 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