AssetConverter.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\web;
  8. use Yii;
  9. use yii\base\Component;
  10. use yii\base\Exception;
  11. /**
  12. * AssetConverter supports conversion of several popular script formats into JS or CSS scripts.
  13. *
  14. * It is used by [[AssetManager]] to convert files after they have been published.
  15. *
  16. * @author Qiang Xue <[email protected]>
  17. * @since 2.0
  18. */
  19. class AssetConverter extends Component implements AssetConverterInterface
  20. {
  21. /**
  22. * @var array the commands that are used to perform the asset conversion.
  23. * The keys are the asset file extension names, and the values are the corresponding
  24. * target script types (either "css" or "js") and the commands used for the conversion.
  25. */
  26. public $commands = [
  27. 'less' => ['css', 'lessc {from} {to} --no-color'],
  28. 'scss' => ['css', 'sass {from} {to}'],
  29. 'sass' => ['css', 'sass {from} {to}'],
  30. 'styl' => ['js', 'stylus < {from} > {to}'],
  31. 'coffee' => ['js', 'coffee -p {from} > {to}'],
  32. 'ts' => ['js', 'tsc --out {to} {from}'],
  33. ];
  34. /**
  35. * Converts a given asset file into a CSS or JS file.
  36. * @param string $asset the asset file path, relative to $basePath
  37. * @param string $basePath the directory the $asset is relative to.
  38. * @return string the converted asset file path, relative to $basePath.
  39. */
  40. public function convert($asset, $basePath)
  41. {
  42. $pos = strrpos($asset, '.');
  43. if ($pos !== false) {
  44. $ext = substr($asset, $pos + 1);
  45. if (isset($this->commands[$ext])) {
  46. list ($ext, $command) = $this->commands[$ext];
  47. $result = substr($asset, 0, $pos + 1) . $ext;
  48. if (@filemtime("$basePath/$result") < filemtime("$basePath/$asset")) {
  49. $this->runCommand($command, $basePath, $asset, $result);
  50. }
  51. return $result;
  52. }
  53. }
  54. return $asset;
  55. }
  56. /**
  57. * Runs a command to convert asset files.
  58. * @param string $command the command to run
  59. * @param string $basePath asset base path and command working directory
  60. * @param string $asset the name of the asset file
  61. * @param string $result the name of the file to be generated by the converter command
  62. * @return bool true on success, false on failure. Failures will be logged.
  63. * @throws \yii\base\Exception when the command fails and YII_DEBUG is true.
  64. * In production mode the error will be logged.
  65. */
  66. protected function runCommand($command, $basePath, $asset, $result)
  67. {
  68. $command = strtr($command, [
  69. '{from}' => escapeshellarg("$basePath/$asset"),
  70. '{to}' => escapeshellarg("$basePath/$result"),
  71. ]);
  72. $descriptor = [
  73. 1 => ['pipe', 'w'],
  74. 2 => ['pipe', 'w'],
  75. ];
  76. $pipes = [];
  77. $proc = proc_open($command, $descriptor, $pipes, $basePath);
  78. $stdout = stream_get_contents($pipes[1]);
  79. $stderr = stream_get_contents($pipes[2]);
  80. foreach($pipes as $pipe) {
  81. fclose($pipe);
  82. }
  83. $status = proc_close($proc);
  84. if ($status === 0) {
  85. Yii::trace("Converted $asset into $result:\nSTDOUT:\n$stdout\nSTDERR:\n$stderr", __METHOD__);
  86. } elseif (YII_DEBUG) {
  87. throw new Exception("AssetConverter command '$command' failed with exit code $status:\nSTDOUT:\n$stdout\nSTDERR:\n$stderr");
  88. } else {
  89. Yii::error("AssetConverter command '$command' failed with exit code $status:\nSTDOUT:\n$stdout\nSTDERR:\n$stderr");
  90. }
  91. return $status === 0;
  92. }
  93. }