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 | |||
8 | namespace yii\web; |
||
9 | |||
10 | use Yii; |
||
11 | use yii\base\Component; |
||
12 | use yii\base\InvalidArgumentException; |
||
13 | use yii\base\InvalidConfigException; |
||
14 | use yii\helpers\FileHelper; |
||
15 | use yii\helpers\Url; |
||
16 | |||
17 | /** |
||
18 | * AssetManager manages asset bundle configuration and loading. |
||
19 | * |
||
20 | * AssetManager is configured as an application component in [[\yii\web\Application]] by default. |
||
21 | * You can access that instance via `Yii::$app->assetManager`. |
||
22 | * |
||
23 | * You can modify its configuration by adding an array to your application config under `components` |
||
24 | * as shown in the following example: |
||
25 | * |
||
26 | * ```php |
||
27 | * 'assetManager' => [ |
||
28 | * 'bundles' => [ |
||
29 | * // you can override AssetBundle configs here |
||
30 | * ], |
||
31 | * ] |
||
32 | * ``` |
||
33 | * |
||
34 | * For more details and usage information on AssetManager, see the [guide article on assets](guide:structure-assets). |
||
35 | * |
||
36 | * @property AssetConverterInterface $converter The asset converter. Note that the type of this property |
||
37 | * differs in getter and setter. See [[getConverter()]] and [[setConverter()]] for details. |
||
38 | * |
||
39 | * @author Qiang Xue <[email protected]> |
||
40 | * @since 2.0 |
||
41 | */ |
||
42 | class AssetManager extends Component |
||
43 | { |
||
44 | /** |
||
45 | * @var array|bool list of asset bundle configurations. This property is provided to customize asset bundles. |
||
46 | * When a bundle is being loaded by [[getBundle()]], if it has a corresponding configuration specified here, |
||
47 | * the configuration will be applied to the bundle. |
||
48 | * |
||
49 | * The array keys are the asset bundle names, which typically are asset bundle class names without leading backslash. |
||
50 | * The array values are the corresponding configurations. If a value is false, it means the corresponding asset |
||
51 | * bundle is disabled and [[getBundle()]] should return null. |
||
52 | * |
||
53 | * If this property is false, it means the whole asset bundle feature is disabled and [[getBundle()]] |
||
54 | * will always return null. |
||
55 | * |
||
56 | * The following example shows how to disable the bootstrap css file used by Bootstrap widgets |
||
57 | * (because you want to use your own styles): |
||
58 | * |
||
59 | * ```php |
||
60 | * [ |
||
61 | * 'yii\bootstrap\BootstrapAsset' => [ |
||
62 | * 'css' => [], |
||
63 | * ], |
||
64 | * ] |
||
65 | * ``` |
||
66 | */ |
||
67 | public $bundles = []; |
||
68 | /** |
||
69 | * @var string the root directory storing the published asset files. |
||
70 | */ |
||
71 | public $basePath = '@webroot/assets'; |
||
72 | /** |
||
73 | * @var string the base URL through which the published asset files can be accessed. |
||
74 | */ |
||
75 | public $baseUrl = '@web/assets'; |
||
76 | /** |
||
77 | * @var array mapping from source asset files (keys) to target asset files (values). |
||
78 | * |
||
79 | * This property is provided to support fixing incorrect asset file paths in some asset bundles. |
||
80 | * When an asset bundle is registered with a view, each relative asset file in its [[AssetBundle::css|css]] |
||
81 | * and [[AssetBundle::js|js]] arrays will be examined against this map. If any of the keys is found |
||
82 | * to be the last part of an asset file (which is prefixed with [[AssetBundle::sourcePath]] if available), |
||
83 | * the corresponding value will replace the asset and be registered with the view. |
||
84 | * For example, an asset file `my/path/to/jquery.js` matches a key `jquery.js`. |
||
85 | * |
||
86 | * Note that the target asset files should be absolute URLs, domain relative URLs (starting from '/') or paths |
||
87 | * relative to [[baseUrl]] and [[basePath]]. |
||
88 | * |
||
89 | * In the following example, any assets ending with `jquery.min.js` will be replaced with `jquery/dist/jquery.js` |
||
90 | * which is relative to [[baseUrl]] and [[basePath]]. |
||
91 | * |
||
92 | * ```php |
||
93 | * [ |
||
94 | * 'jquery.min.js' => 'jquery/dist/jquery.js', |
||
95 | * ] |
||
96 | * ``` |
||
97 | * |
||
98 | * You may also use aliases while specifying map value, for example: |
||
99 | * |
||
100 | * ```php |
||
101 | * [ |
||
102 | * 'jquery.min.js' => '@web/js/jquery/jquery.js', |
||
103 | * ] |
||
104 | * ``` |
||
105 | */ |
||
106 | public $assetMap = []; |
||
107 | /** |
||
108 | * @var bool whether to use symbolic link to publish asset files. Defaults to false, meaning |
||
109 | * asset files are copied to [[basePath]]. Using symbolic links has the benefit that the published |
||
110 | * assets will always be consistent with the source assets and there is no copy operation required. |
||
111 | * This is especially useful during development. |
||
112 | * |
||
113 | * However, there are special requirements for hosting environments in order to use symbolic links. |
||
114 | * In particular, symbolic links are supported only on Linux/Unix, and Windows Vista/2008 or greater. |
||
115 | * |
||
116 | * Moreover, some Web servers need to be properly configured so that the linked assets are accessible |
||
117 | * to Web users. For example, for Apache Web server, the following configuration directive should be added |
||
118 | * for the Web folder: |
||
119 | * |
||
120 | * ```apache |
||
121 | * Options FollowSymLinks |
||
122 | * ``` |
||
123 | */ |
||
124 | public $linkAssets = false; |
||
125 | /** |
||
126 | * @var int the permission to be set for newly published asset files. |
||
127 | * This value will be used by PHP chmod() function. No umask will be applied. |
||
128 | * If not set, the permission will be determined by the current environment. |
||
129 | */ |
||
130 | public $fileMode; |
||
131 | /** |
||
132 | * @var int the permission to be set for newly generated asset directories. |
||
133 | * This value will be used by PHP chmod() function. No umask will be applied. |
||
134 | * Defaults to 0775, meaning the directory is read-writable by owner and group, |
||
135 | * but read-only for other users. |
||
136 | */ |
||
137 | public $dirMode = 0775; |
||
138 | /** |
||
139 | * @var callback a PHP callback that is called before copying each sub-directory or file. |
||
140 | * This option is used only when publishing a directory. If the callback returns false, the copy |
||
141 | * operation for the sub-directory or file will be cancelled. |
||
142 | * |
||
143 | * The signature of the callback should be: `function ($from, $to)`, where `$from` is the sub-directory or |
||
144 | * file to be copied from, while `$to` is the copy target. |
||
145 | * |
||
146 | * This is passed as a parameter `beforeCopy` to [[\yii\helpers\FileHelper::copyDirectory()]]. |
||
147 | */ |
||
148 | public $beforeCopy; |
||
149 | /** |
||
150 | * @var callback a PHP callback that is called after a sub-directory or file is successfully copied. |
||
151 | * This option is used only when publishing a directory. The signature of the callback is the same as |
||
152 | * for [[beforeCopy]]. |
||
153 | * This is passed as a parameter `afterCopy` to [[\yii\helpers\FileHelper::copyDirectory()]]. |
||
154 | */ |
||
155 | public $afterCopy; |
||
156 | /** |
||
157 | * @var bool whether the directory being published should be copied even if |
||
158 | * it is found in the target directory. This option is used only when publishing a directory. |
||
159 | * You may want to set this to be `true` during the development stage to make sure the published |
||
160 | * directory is always up-to-date. Do not set this to true on production servers as it will |
||
161 | * significantly degrade the performance. |
||
162 | */ |
||
163 | public $forceCopy = false; |
||
164 | /** |
||
165 | * @var bool whether to append a timestamp to the URL of every published asset. When this is true, |
||
166 | * the URL of a published asset may look like `/path/to/asset?v=timestamp`, where `timestamp` is the |
||
167 | * last modification time of the published asset file. |
||
168 | * You normally would want to set this property to true when you have enabled HTTP caching for assets, |
||
169 | * because it allows you to bust caching when the assets are updated. |
||
170 | * @since 2.0.3 |
||
171 | */ |
||
172 | public $appendTimestamp = false; |
||
173 | /** |
||
174 | * @var callable a callback that will be called to produce hash for asset directory generation. |
||
175 | * The signature of the callback should be as follows: |
||
176 | * |
||
177 | * ``` |
||
178 | * function ($path) |
||
179 | * ``` |
||
180 | * |
||
181 | * where `$path` is the asset path. Note that the `$path` can be either directory where the asset |
||
182 | * files reside or a single file. For a CSS file that uses relative path in `url()`, the hash |
||
183 | * implementation should use the directory path of the file instead of the file path to include |
||
184 | * the relative asset files in the copying. |
||
185 | * |
||
186 | * If this is not set, the asset manager will use the default CRC32 and filemtime in the `hash` |
||
187 | * method. |
||
188 | * |
||
189 | * Example of an implementation using MD4 hash: |
||
190 | * |
||
191 | * ```php |
||
192 | * function ($path) { |
||
193 | * return hash('md4', $path); |
||
194 | * } |
||
195 | * ``` |
||
196 | * |
||
197 | * @since 2.0.6 |
||
198 | */ |
||
199 | public $hashCallback; |
||
200 | |||
201 | private $_dummyBundles = []; |
||
202 | |||
203 | |||
204 | /** |
||
205 | * Initializes the component. |
||
206 | * @throws InvalidConfigException if [[basePath]] does not exist. |
||
207 | */ |
||
208 | 97 | public function init() |
|
209 | { |
||
210 | 97 | parent::init(); |
|
211 | 97 | $this->basePath = Yii::getAlias($this->basePath); |
|
212 | |||
213 | 97 | $this->basePath = realpath($this->basePath); |
|
214 | 97 | $this->baseUrl = rtrim(Yii::getAlias($this->baseUrl), '/'); |
|
215 | 97 | } |
|
216 | |||
217 | private $_isBasePathPermissionChecked; |
||
218 | |||
219 | /** |
||
220 | * Check whether the basePath exists and is writeable. |
||
221 | * |
||
222 | * @since 2.0.40 |
||
223 | */ |
||
224 | 14 | public function checkBasePathPermission() |
|
225 | { |
||
226 | // if the check is been done already, skip further checks |
||
227 | 14 | if ($this->_isBasePathPermissionChecked) { |
|
228 | return; |
||
229 | } |
||
230 | |||
231 | 14 | if (!is_dir($this->basePath)) { |
|
232 | throw new InvalidConfigException("The directory does not exist: {$this->basePath}"); |
||
233 | } |
||
234 | |||
235 | 14 | if (!is_writable($this->basePath)) { |
|
236 | 1 | throw new InvalidConfigException("The directory is not writable by the Web process: {$this->basePath}"); |
|
237 | } |
||
238 | |||
239 | 13 | $this->_isBasePathPermissionChecked = true; |
|
240 | 13 | } |
|
241 | |||
242 | /** |
||
243 | * Returns the named asset bundle. |
||
244 | * |
||
245 | * This method will first look for the bundle in [[bundles]]. If not found, |
||
246 | * it will treat `$name` as the class of the asset bundle and create a new instance of it. |
||
247 | * |
||
248 | * @param string $name the class name of the asset bundle (without the leading backslash) |
||
249 | * @param bool $publish whether to publish the asset files in the asset bundle before it is returned. |
||
250 | * If you set this false, you must manually call `AssetBundle::publish()` to publish the asset files. |
||
251 | * @return AssetBundle the asset bundle instance |
||
252 | * @throws InvalidConfigException if $name does not refer to a valid asset bundle |
||
253 | */ |
||
254 | 39 | public function getBundle($name, $publish = true) |
|
255 | { |
||
256 | 39 | if ($this->bundles === false) { |
|
257 | return $this->loadDummyBundle($name); |
||
258 | 39 | } elseif (!isset($this->bundles[$name])) { |
|
259 | 35 | return $this->bundles[$name] = $this->loadBundle($name, [], $publish); |
|
260 | 25 | } elseif ($this->bundles[$name] instanceof AssetBundle) { |
|
261 | 4 | return $this->bundles[$name]; |
|
262 | 21 | } elseif (is_array($this->bundles[$name])) { |
|
263 | 13 | return $this->bundles[$name] = $this->loadBundle($name, $this->bundles[$name], $publish); |
|
264 | 8 | } elseif ($this->bundles[$name] === false) { |
|
265 | 8 | return $this->loadDummyBundle($name); |
|
266 | } |
||
267 | |||
268 | throw new InvalidConfigException("Invalid asset bundle configuration: $name"); |
||
269 | } |
||
270 | |||
271 | /** |
||
272 | * Loads asset bundle class by name. |
||
273 | * |
||
274 | * @param string $name bundle name |
||
275 | * @param array $config bundle object configuration |
||
276 | * @param bool $publish if bundle should be published |
||
277 | * @return AssetBundle |
||
278 | * @throws InvalidConfigException if configuration isn't valid |
||
279 | */ |
||
280 | 36 | protected function loadBundle($name, $config = [], $publish = true) |
|
281 | { |
||
282 | 36 | if (!isset($config['class'])) { |
|
283 | 36 | $config['class'] = $name; |
|
284 | } |
||
285 | /* @var $bundle AssetBundle */ |
||
286 | 36 | $bundle = Yii::createObject($config); |
|
287 | 36 | if ($publish) { |
|
288 | 36 | $bundle->publish($this); |
|
289 | } |
||
290 | |||
291 | 36 | return $bundle; |
|
292 | } |
||
293 | |||
294 | /** |
||
295 | * Loads dummy bundle by name. |
||
296 | * |
||
297 | * @param string $name |
||
298 | * @return AssetBundle |
||
299 | */ |
||
300 | 8 | protected function loadDummyBundle($name) |
|
301 | { |
||
302 | 8 | if (!isset($this->_dummyBundles[$name])) { |
|
303 | 8 | $bundle = Yii::createObject(['class' => $name]); |
|
304 | 8 | $bundle->sourcePath = null; |
|
305 | 8 | $bundle->js = []; |
|
306 | 8 | $bundle->css = []; |
|
307 | |||
308 | 8 | $this->_dummyBundles[$name] = $bundle; |
|
309 | } |
||
310 | |||
311 | 8 | return $this->_dummyBundles[$name]; |
|
312 | } |
||
313 | |||
314 | /** |
||
315 | * Returns the actual URL for the specified asset. |
||
316 | * The actual URL is obtained by prepending either [[AssetBundle::$baseUrl]] or [[AssetManager::$baseUrl]] to the given asset path. |
||
317 | * @param AssetBundle $bundle the asset bundle which the asset file belongs to |
||
318 | * @param string $asset the asset path. This should be one of the assets listed in [[AssetBundle::$js]] or [[AssetBundle::$css]]. |
||
319 | * @param bool|null $appendTimestamp Whether to append timestamp to the URL. |
||
320 | * @return string the actual URL for the specified asset. |
||
321 | */ |
||
322 | 15 | public function getAssetUrl($bundle, $asset, $appendTimestamp = null) |
|
323 | { |
||
324 | 15 | $assetUrl = $this->getActualAssetUrl($bundle, $asset); |
|
325 | 15 | $assetPath = $this->getAssetPath($bundle, $asset); |
|
326 | |||
327 | 15 | $withTimestamp = $this->appendTimestamp; |
|
328 | 15 | if ($appendTimestamp !== null) { |
|
329 | 2 | $withTimestamp = $appendTimestamp; |
|
330 | } |
||
331 | |||
332 | 15 | if ($withTimestamp && $assetPath && ($timestamp = @filemtime($assetPath)) > 0) { |
|
333 | 3 | return "$assetUrl?v=$timestamp"; |
|
334 | } |
||
335 | |||
336 | 14 | return $assetUrl; |
|
337 | } |
||
338 | |||
339 | /** |
||
340 | * Returns the actual file path for the specified asset. |
||
341 | * @param AssetBundle $bundle the asset bundle which the asset file belongs to |
||
342 | * @param string $asset the asset path. This should be one of the assets listed in [[AssetBundle::$js]] or [[AssetBundle::$css]]. |
||
343 | * @return string|false the actual file path, or `false` if the asset is specified as an absolute URL |
||
344 | */ |
||
345 | 15 | public function getAssetPath($bundle, $asset) |
|
346 | { |
||
347 | 15 | if (($actualAsset = $this->resolveAsset($bundle, $asset)) !== false) { |
|
348 | return Url::isRelative($actualAsset) ? $this->basePath . '/' . $actualAsset : false; |
||
349 | } |
||
350 | |||
351 | 15 | return Url::isRelative($asset) ? $bundle->basePath . '/' . $asset : false; |
|
352 | } |
||
353 | |||
354 | /** |
||
355 | * @param AssetBundle $bundle |
||
356 | * @param string $asset |
||
357 | * @return string|bool |
||
358 | */ |
||
359 | 15 | protected function resolveAsset($bundle, $asset) |
|
360 | { |
||
361 | 15 | if (isset($this->assetMap[$asset])) { |
|
362 | return $this->assetMap[$asset]; |
||
363 | } |
||
364 | 15 | if ($bundle->sourcePath !== null && Url::isRelative($asset)) { |
|
365 | $asset = $bundle->sourcePath . '/' . $asset; |
||
366 | } |
||
367 | |||
368 | 15 | $n = mb_strlen($asset, Yii::$app->charset); |
|
369 | 15 | foreach ($this->assetMap as $from => $to) { |
|
370 | $n2 = mb_strlen($from, Yii::$app->charset); |
||
371 | if ($n2 <= $n && substr_compare($asset, $from, $n - $n2, $n2) === 0) { |
||
372 | return $to; |
||
373 | } |
||
374 | } |
||
375 | |||
376 | 15 | return false; |
|
377 | } |
||
378 | |||
379 | private $_converter; |
||
380 | |||
381 | /** |
||
382 | * Returns the asset converter. |
||
383 | * @return AssetConverterInterface the asset converter. |
||
384 | */ |
||
385 | 33 | public function getConverter() |
|
386 | { |
||
387 | 33 | if ($this->_converter === null) { |
|
388 | 33 | $this->_converter = Yii::createObject(AssetConverter::className()); |
|
389 | 21 | } elseif (is_array($this->_converter) || is_string($this->_converter)) { |
|
390 | if (is_array($this->_converter) && !isset($this->_converter['class'])) { |
||
391 | $this->_converter['class'] = AssetConverter::className(); |
||
392 | } |
||
393 | $this->_converter = Yii::createObject($this->_converter); |
||
394 | } |
||
395 | |||
396 | 33 | return $this->_converter; |
|
397 | } |
||
398 | |||
399 | /** |
||
400 | * Sets the asset converter. |
||
401 | * @param array|AssetConverterInterface $value the asset converter. This can be either |
||
402 | * an object implementing the [[AssetConverterInterface]], or a configuration |
||
403 | * array that can be used to create the asset converter object. |
||
404 | */ |
||
405 | public function setConverter($value) |
||
406 | { |
||
407 | $this->_converter = $value; |
||
408 | } |
||
409 | |||
410 | /** |
||
411 | * @var array published assets |
||
412 | */ |
||
413 | private $_published = []; |
||
414 | |||
415 | /** |
||
416 | * Publishes a file or a directory. |
||
417 | * |
||
418 | * This method will copy the specified file or directory to [[basePath]] so that |
||
419 | * it can be accessed via the Web server. |
||
420 | * |
||
421 | * If the asset is a file, its file modification time will be checked to avoid |
||
422 | * unnecessary file copying. |
||
423 | * |
||
424 | * If the asset is a directory, all files and subdirectories under it will be published recursively. |
||
425 | * Note, in case $forceCopy is false the method only checks the existence of the target |
||
426 | * directory to avoid repetitive copying (which is very expensive). |
||
427 | * |
||
428 | * By default, when publishing a directory, subdirectories and files whose name starts with a dot "." |
||
429 | * will NOT be published. If you want to change this behavior, you may specify the "beforeCopy" option |
||
430 | * as explained in the `$options` parameter. |
||
431 | * |
||
432 | * Note: On rare scenario, a race condition can develop that will lead to a |
||
433 | * one-time-manifestation of a non-critical problem in the creation of the directory |
||
434 | * that holds the published assets. This problem can be avoided altogether by 'requesting' |
||
435 | * in advance all the resources that are supposed to trigger a 'publish()' call, and doing |
||
436 | * that in the application deployment phase, before system goes live. See more in the following |
||
437 | * discussion: http://code.google.com/p/yii/issues/detail?id=2579 |
||
438 | * |
||
439 | * @param string $path the asset (file or directory) to be published |
||
440 | * @param array $options the options to be applied when publishing a directory. |
||
441 | * The following options are supported: |
||
442 | * |
||
443 | * - only: array, list of patterns that the file paths should match if they want to be copied. |
||
444 | * - except: array, list of patterns that the files or directories should match if they want to be excluded from being copied. |
||
445 | * - caseSensitive: boolean, whether patterns specified at "only" or "except" should be case sensitive. Defaults to true. |
||
446 | * - beforeCopy: callback, a PHP callback that is called before copying each sub-directory or file. |
||
447 | * This overrides [[beforeCopy]] if set. |
||
448 | * - afterCopy: callback, a PHP callback that is called after a sub-directory or file is successfully copied. |
||
449 | * This overrides [[afterCopy]] if set. |
||
450 | * - forceCopy: boolean, whether the directory being published should be copied even if |
||
451 | * it is found in the target directory. This option is used only when publishing a directory. |
||
452 | * This overrides [[forceCopy]] if set. |
||
453 | * |
||
454 | * @return array the path (directory or file path) and the URL that the asset is published as. |
||
455 | * @throws InvalidArgumentException if the asset to be published does not exist. |
||
456 | * @throws InvalidConfigException if the target directory [[basePath]] is not writeable. |
||
457 | */ |
||
458 | 14 | public function publish($path, $options = []) |
|
459 | { |
||
460 | 14 | $path = Yii::getAlias($path); |
|
461 | |||
462 | 14 | if (isset($this->_published[$path])) { |
|
463 | 1 | return $this->_published[$path]; |
|
464 | } |
||
465 | |||
466 | 14 | if (!is_string($path) || ($src = realpath($path)) === false) { |
|
467 | throw new InvalidArgumentException("The file or directory to be published does not exist: $path"); |
||
468 | } |
||
469 | |||
470 | 14 | if (is_file($src)) { |
|
471 | 1 | return $this->_published[$path] = $this->publishFile($src); |
|
472 | } |
||
473 | |||
474 | 13 | return $this->_published[$path] = $this->publishDirectory($src, $options); |
|
475 | } |
||
476 | |||
477 | /** |
||
478 | * Publishes a file. |
||
479 | * @param string $src the asset file to be published |
||
480 | * @return string[] the path and the URL that the asset is published as. |
||
481 | * @throws InvalidArgumentException if the asset to be published does not exist. |
||
482 | */ |
||
483 | 1 | protected function publishFile($src) |
|
484 | { |
||
485 | 1 | $this->checkBasePathPermission(); |
|
486 | |||
487 | 1 | $dir = $this->hash($src); |
|
488 | 1 | $fileName = basename($src); |
|
489 | 1 | $dstDir = $this->basePath . DIRECTORY_SEPARATOR . $dir; |
|
490 | 1 | $dstFile = $dstDir . DIRECTORY_SEPARATOR . $fileName; |
|
491 | |||
492 | 1 | if (!is_dir($dstDir)) { |
|
493 | 1 | FileHelper::createDirectory($dstDir, $this->dirMode, true); |
|
494 | } |
||
495 | |||
496 | 1 | if ($this->linkAssets) { |
|
497 | if (!is_file($dstFile)) { |
||
498 | try { // fix #6226 symlinking multi threaded |
||
499 | symlink($src, $dstFile); |
||
500 | } catch (\Exception $e) { |
||
501 | if (!is_file($dstFile)) { |
||
502 | throw $e; |
||
503 | } |
||
504 | } |
||
505 | } |
||
506 | 1 | } elseif (@filemtime($dstFile) < @filemtime($src)) { |
|
507 | 1 | copy($src, $dstFile); |
|
508 | 1 | if ($this->fileMode !== null) { |
|
509 | @chmod($dstFile, $this->fileMode); |
||
510 | } |
||
511 | } |
||
512 | |||
513 | 1 | if ($this->appendTimestamp && ($timestamp = @filemtime($dstFile)) > 0) { |
|
514 | 1 | $fileName = $fileName . "?v=$timestamp"; |
|
515 | } |
||
516 | |||
517 | 1 | return [$dstFile, $this->baseUrl . "/$dir/$fileName"]; |
|
518 | } |
||
519 | |||
520 | /** |
||
521 | * Publishes a directory. |
||
522 | * @param string $src the asset directory to be published |
||
523 | * @param array $options the options to be applied when publishing a directory. |
||
524 | * The following options are supported: |
||
525 | * |
||
526 | * - only: array, list of patterns that the file paths should match if they want to be copied. |
||
527 | * - except: array, list of patterns that the files or directories should match if they want to be excluded from being copied. |
||
528 | * - caseSensitive: boolean, whether patterns specified at "only" or "except" should be case sensitive. Defaults to true. |
||
529 | * - beforeCopy: callback, a PHP callback that is called before copying each sub-directory or file. |
||
530 | * This overrides [[beforeCopy]] if set. |
||
531 | * - afterCopy: callback, a PHP callback that is called after a sub-directory or file is successfully copied. |
||
532 | * This overrides [[afterCopy]] if set. |
||
533 | * - forceCopy: boolean, whether the directory being published should be copied even if |
||
534 | * it is found in the target directory. This option is used only when publishing a directory. |
||
535 | * This overrides [[forceCopy]] if set. |
||
536 | * |
||
537 | * @return string[] the path directory and the URL that the asset is published as. |
||
538 | * @throws InvalidArgumentException if the asset to be published does not exist. |
||
539 | */ |
||
540 | 13 | protected function publishDirectory($src, $options) |
|
541 | { |
||
542 | 13 | $this->checkBasePathPermission(); |
|
543 | |||
544 | 12 | $dir = $this->hash($src); |
|
545 | 12 | $dstDir = $this->basePath . DIRECTORY_SEPARATOR . $dir; |
|
546 | 12 | if ($this->linkAssets) { |
|
547 | 2 | if (!is_dir($dstDir)) { |
|
548 | 2 | FileHelper::createDirectory(dirname($dstDir), $this->dirMode, true); |
|
549 | try { // fix #6226 symlinking multi threaded |
||
550 | 2 | symlink($src, $dstDir); |
|
551 | } catch (\Exception $e) { |
||
552 | if (!is_dir($dstDir)) { |
||
553 | 2 | throw $e; |
|
554 | } |
||
555 | } |
||
556 | } |
||
557 | 10 | } elseif (!empty($options['forceCopy']) || ($this->forceCopy && !isset($options['forceCopy'])) || !is_dir($dstDir)) { |
|
558 | 6 | $opts = array_merge( |
|
559 | 6 | $options, |
|
560 | [ |
||
561 | 6 | 'dirMode' => $this->dirMode, |
|
562 | 6 | 'fileMode' => $this->fileMode, |
|
563 | 'copyEmptyDirectories' => false, |
||
564 | ] |
||
565 | ); |
||
566 | 6 | if (!isset($opts['beforeCopy'])) { |
|
567 | 5 | if ($this->beforeCopy !== null) { |
|
568 | 1 | $opts['beforeCopy'] = $this->beforeCopy; |
|
569 | } else { |
||
570 | 4 | $opts['beforeCopy'] = function ($from, $to) { |
|
571 | 4 | return strncmp(basename($from), '.', 1) !== 0; |
|
572 | }; |
||
573 | } |
||
574 | } |
||
575 | 6 | if (!isset($opts['afterCopy']) && $this->afterCopy !== null) { |
|
576 | $opts['afterCopy'] = $this->afterCopy; |
||
577 | } |
||
578 | 6 | FileHelper::copyDirectory($src, $dstDir, $opts); |
|
579 | } |
||
580 | |||
581 | 12 | return [$dstDir, $this->baseUrl . '/' . $dir]; |
|
582 | } |
||
583 | |||
584 | /** |
||
585 | * Returns the published path of a file path. |
||
586 | * This method does not perform any publishing. It merely tells you |
||
587 | * if the file or directory is published, where it will go. |
||
588 | * @param string $path directory or file path being published |
||
589 | * @return string|false string the published file path. False if the file or directory does not exist |
||
590 | */ |
||
591 | public function getPublishedPath($path) |
||
592 | { |
||
593 | $path = Yii::getAlias($path); |
||
594 | |||
595 | if (isset($this->_published[$path])) { |
||
596 | return $this->_published[$path][0]; |
||
597 | } |
||
598 | if (is_string($path) && ($path = realpath($path)) !== false) { |
||
599 | return $this->basePath . DIRECTORY_SEPARATOR . $this->hash($path) . (is_file($path) ? DIRECTORY_SEPARATOR . basename($path) : ''); |
||
600 | } |
||
601 | |||
602 | return false; |
||
603 | } |
||
604 | |||
605 | /** |
||
606 | * Returns the URL of a published file path. |
||
607 | * This method does not perform any publishing. It merely tells you |
||
608 | * if the file path is published, what the URL will be to access it. |
||
609 | * @param string $path directory or file path being published |
||
610 | * @return string|false string the published URL for the file or directory. False if the file or directory does not exist. |
||
611 | */ |
||
612 | public function getPublishedUrl($path) |
||
613 | { |
||
614 | $path = Yii::getAlias($path); |
||
615 | |||
616 | if (isset($this->_published[$path])) { |
||
617 | return $this->_published[$path][1]; |
||
618 | } |
||
619 | if (is_string($path) && ($path = realpath($path)) !== false) { |
||
620 | return $this->baseUrl . '/' . $this->hash($path) . (is_file($path) ? '/' . basename($path) : ''); |
||
621 | } |
||
622 | |||
623 | return false; |
||
624 | } |
||
625 | |||
626 | /** |
||
627 | * Generate a CRC32 hash for the directory path. Collisions are higher |
||
628 | * than MD5 but generates a much smaller hash string. |
||
629 | * @param string $path string to be hashed. |
||
630 | * @return string hashed string. |
||
631 | */ |
||
632 | 13 | protected function hash($path) |
|
633 | { |
||
634 | 13 | if (is_callable($this->hashCallback)) { |
|
635 | 1 | return call_user_func($this->hashCallback, $path); |
|
636 | } |
||
637 | 12 | $path = (is_file($path) ? dirname($path) : $path) . filemtime($path); |
|
638 | 12 | return sprintf('%x', crc32($path . Yii::getVersion() . '|' . $this->linkAssets)); |
|
639 | } |
||
640 | |||
641 | /** |
||
642 | * Returns the actual URL for the specified asset. Without parameters. |
||
643 | * The actual URL is obtained by prepending either [[AssetBundle::$baseUrl]] or [[AssetManager::$baseUrl]] to the given asset path. |
||
644 | * @param AssetBundle $bundle the asset bundle which the asset file belongs to |
||
645 | * @param string $asset the asset path. This should be one of the assets listed in [[AssetBundle::$js]] or [[AssetBundle::$css]]. |
||
646 | * @return string the actual URL for the specified asset. |
||
647 | * @since 2.0.39 |
||
648 | */ |
||
649 | 15 | public function getActualAssetUrl($bundle, $asset) |
|
650 | { |
||
651 | 15 | if (($actualAsset = $this->resolveAsset($bundle, $asset)) !== false) { |
|
652 | if (strncmp($actualAsset, '@web/', 5) === 0) { |
||
653 | $asset = substr($actualAsset, 5); |
||
654 | $baseUrl = Yii::getAlias('@web'); |
||
655 | } else { |
||
656 | $asset = Yii::getAlias($actualAsset); |
||
657 | $baseUrl = $this->baseUrl; |
||
658 | } |
||
659 | } else { |
||
660 | 15 | $baseUrl = $bundle->baseUrl; |
|
661 | } |
||
662 | |||
663 | 15 | if (!Url::isRelative($asset) || strncmp($asset, '/', 1) === 0) { |
|
664 | 2 | return $asset; |
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
665 | } |
||
666 | |||
667 | 15 | return "$baseUrl/$asset"; |
|
668 | } |
||
669 | } |
||
670 |