1 | <?php |
||
22 | class AssetConverter extends Component implements AssetConverterInterface |
||
23 | { |
||
24 | /** |
||
25 | * @var array the commands that are used to perform the asset conversion. |
||
26 | * The keys are the asset file extension names, and the values are the corresponding |
||
27 | * target script types (either "css" or "js") and the commands used for the conversion. |
||
28 | * |
||
29 | * You may also use a [path alias](guide:concept-aliases) to specify the location of the command: |
||
30 | * |
||
31 | * ```php |
||
32 | * [ |
||
33 | * 'styl' => ['css', '@app/node_modules/bin/stylus < {from} > {to}'], |
||
34 | * ] |
||
35 | * ``` |
||
36 | */ |
||
37 | public $commands = [ |
||
38 | 'less' => ['css', 'lessc {from} {to} --no-color --source-map'], |
||
39 | 'scss' => ['css', 'sass {from} {to} --sourcemap'], |
||
40 | 'sass' => ['css', 'sass {from} {to} --sourcemap'], |
||
41 | 'styl' => ['css', 'stylus < {from} > {to}'], |
||
42 | 'coffee' => ['js', 'coffee -p {from} > {to}'], |
||
43 | 'ts' => ['js', 'tsc --out {to} {from}'], |
||
44 | ]; |
||
45 | /** |
||
46 | * @var bool whether the source asset file should be converted even if its result already exists. |
||
47 | * You may want to set this to be `true` during the development stage to make sure the converted |
||
48 | * assets are always up-to-date. Do not set this to true on production servers as it will |
||
49 | * significantly degrade the performance. |
||
50 | */ |
||
51 | public $forceConvert = false; |
||
52 | /** |
||
53 | * @var callable a PHP callback, which should be invoked to check whether asset conversion result is outdated. |
||
54 | * It will be invoked only if conversion target file exists and its modification time is older then the one of source file. |
||
55 | * Callback should match following signature: |
||
56 | * |
||
57 | * ```php |
||
58 | * function (string $basePath, string $sourceFile, string $targetFile, string $sourceExtension, string $targetExtension) : bool |
||
59 | * ``` |
||
60 | * |
||
61 | * where $basePath is the asset source directory; $sourceFile is the asset source file path, relative to $basePath; |
||
62 | * $targetFile is the asset target file path, relative to $basePath; $sourceExtension is the source asset file extension |
||
63 | * and $targetExtension is the target asset file extension, respectively. |
||
64 | * |
||
65 | * It should return `true` is case asset should be reconverted. |
||
66 | * For example: |
||
67 | * |
||
68 | * ```php |
||
69 | * function ($basePath, $sourceFile, $targetFile, $sourceExtension, $targetExtension) { |
||
70 | * if (YII_ENV !== 'dev') { |
||
71 | * return false; |
||
72 | * } |
||
73 | * |
||
74 | * $resultModificationTime = @filemtime("$basePath/$result"); |
||
75 | * foreach (FileHelper::findFiles($basePath, ['only' => ["*.{$sourceExtension}"]]) as $filename) { |
||
76 | * if ($resultModificationTime < @filemtime($filename)) { |
||
77 | * return true; |
||
78 | * } |
||
79 | * } |
||
80 | * |
||
81 | * return false; |
||
82 | * } |
||
83 | * ``` |
||
84 | * |
||
85 | * @since 2.1.0 |
||
86 | */ |
||
87 | public $isOutdatedCallback; |
||
88 | |||
89 | |||
90 | /** |
||
91 | * Converts a given asset file into a CSS or JS file. |
||
92 | * @param string $asset the asset file path, relative to $basePath |
||
93 | * @param string $basePath the directory the $asset is relative to. |
||
94 | * @return string the converted asset file path, relative to $basePath. |
||
95 | */ |
||
96 | 30 | public function convert($asset, $basePath) |
|
114 | |||
115 | /** |
||
116 | * Checks whether asset convert result is outdated, and thus should be reconverted. |
||
117 | * @param string $basePath the directory the $asset is relative to. |
||
118 | * @param string $sourceFile the asset source file path, relative to [[$basePath]]. |
||
119 | * @param string $targetFile the converted asset file path, relative to [[$basePath]]. |
||
120 | * @param string $sourceExtension source asset file extension. |
||
121 | * @param string $targetExtension target asset file extension. |
||
122 | * @return bool whether asset is outdated or not. |
||
123 | * @since 2.1.0 |
||
124 | */ |
||
125 | 4 | protected function isOutdated($basePath, $sourceFile, $targetFile, $sourceExtension, $targetExtension) |
|
142 | |||
143 | /** |
||
144 | * Runs a command to convert asset files. |
||
145 | * @param string $command the command to run. If prefixed with an `@` it will be treated as a [path alias](guide:concept-aliases). |
||
146 | * @param string $basePath asset base path and command working directory |
||
147 | * @param string $asset the name of the asset file |
||
148 | * @param string $result the name of the file to be generated by the converter command |
||
149 | * @return bool true on success, false on failure. Failures will be logged. |
||
150 | * @throws \yii\base\Exception when the command fails and YII_DEBUG is true. |
||
151 | * In production mode the error will be logged. |
||
152 | */ |
||
153 | 4 | protected function runCommand($command, $basePath, $asset, $result) |
|
184 | } |
||
185 |
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.