| Total Complexity | 97 |
| Total Lines | 889 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like AssetManager often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use AssetManager, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 31 | class AssetManager |
||
| 32 | { |
||
| 33 | /** |
||
| 34 | * @var callable a PHP callback that is called after a sub-directory or file is successfully copied. This option is |
||
| 35 | * used only when publishing a directory. The signature of the callback is the same as for |
||
| 36 | * {@see {beforeCopy}. |
||
| 37 | * This is passed as a parameter `afterCopy` to {@see \Yiisoft\Files\FileHelper::copyDirectory()}. |
||
| 38 | */ |
||
| 39 | private $afterCopy; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Aliases component. |
||
| 43 | * |
||
| 44 | * @var Aliases $aliase |
||
| 45 | */ |
||
| 46 | private $aliases; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * directory path node_modules. |
||
| 50 | * |
||
| 51 | * @var array $alternatives |
||
| 52 | */ |
||
| 53 | private $alternatives = [ |
||
| 54 | '@npm' => '@root/node_modules', |
||
| 55 | ]; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var bool whether to append a timestamp to the URL of every published asset. When this is true, the URL of a |
||
| 59 | * published asset may look like `/path/to/asset?v=timestamp`, where `timestamp` is the last modification |
||
| 60 | * time of the published asset file. You normally would want to set this property to true when you have |
||
| 61 | * enabled HTTP caching for assets, because it allows you to bust caching when the assets are updated. |
||
| 62 | */ |
||
| 63 | private $appendTimestamp = false; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var array mapping from source asset files (keys) to target asset files (values). |
||
| 67 | * |
||
| 68 | * This property is provided to support fixing incorrect asset file paths in some asset bundles. When an asset |
||
| 69 | * bundle is registered with a view, each relative asset file in its {@see AssetBundle::css|css} and |
||
| 70 | * {@see AssetBundle::js|js} arrays will be examined against this map. If any of the keys is found to be the last |
||
| 71 | * part of an asset file (which is prefixed with {@see {AssetBundle::sourcePath} if available), the corresponding |
||
| 72 | * value will replace the asset and be registered with the view. For example, an asset file `my/path/to/jquery.js` |
||
| 73 | * matches a key `jquery.js`. |
||
| 74 | * |
||
| 75 | * Note that the target asset files should be absolute URLs, domain relative URLs (starting from '/') or paths |
||
| 76 | * relative to {@see baseUrl} and {@see basePath}. |
||
| 77 | * |
||
| 78 | * In the following example, any assets ending with `jquery.min.js` will be replaced with `jquery/dist/jquery.js` |
||
| 79 | * which is relative to {@see baseUrl} and {@see basePath}. |
||
| 80 | * |
||
| 81 | * ```php |
||
| 82 | * [ |
||
| 83 | * 'jquery.min.js' => 'jquery/dist/jquery.js', |
||
| 84 | * ] |
||
| 85 | * ``` |
||
| 86 | * |
||
| 87 | * You may also use aliases while specifying map value, for example: |
||
| 88 | * |
||
| 89 | * ```php |
||
| 90 | * [ |
||
| 91 | * 'jquery.min.js' => '@web/js/jquery/jquery.js', |
||
| 92 | * ] |
||
| 93 | * ``` |
||
| 94 | */ |
||
| 95 | private $assetMap = []; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @var string the root directory storing the published asset files. |
||
| 99 | */ |
||
| 100 | private $basePath = '@public/assets'; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @var string the base URL through which the published asset files can be accessed. |
||
| 104 | */ |
||
| 105 | private $baseUrl = '@web/assets'; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @var callable a PHP callback that is called before copying each sub-directory or file. This option is used only |
||
| 109 | * when publishing a directory. If the callback returns false, the copy operation for the |
||
| 110 | * sub-directory or file will be cancelled. |
||
| 111 | * |
||
| 112 | * The signature of the callback should be: `function ($from, $to)`, where `$from` is the sub-directory or file to |
||
| 113 | * be copied from, while `$to` is the copy target. |
||
| 114 | * |
||
| 115 | * This is passed as a parameter `beforeCopy` to {@see Yiisoft\Files\FileHelper::copyDirectory()}. |
||
| 116 | */ |
||
| 117 | private $beforeCopy; |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @var array|bool list of asset bundle configurations. This property is provided to customize asset bundles. |
||
| 121 | * When a bundle is being loaded by {@see getBundle()}, if it has a corresponding configuration |
||
| 122 | * specified here, the configuration will be applied to the bundle. |
||
| 123 | * |
||
| 124 | * The array keys are the asset bundle names, which typically are asset bundle class names without leading |
||
| 125 | * backslash. The array values are the corresponding configurations. If a value is false, it means the corresponding |
||
| 126 | * asset bundle is disabled and {@see getBundle()} should return null. |
||
| 127 | * |
||
| 128 | * If this property is false, it means the whole asset bundle feature is disabled and {@see {getBundle()} will |
||
| 129 | * always return null. |
||
| 130 | * |
||
| 131 | * The following example shows how to disable the bootstrap css file used by Bootstrap widgets (because you want to |
||
| 132 | * use your own styles): |
||
| 133 | * |
||
| 134 | * ```php |
||
| 135 | * [ |
||
| 136 | * \Yiisoft\Bootstrap4\BootstrapAsset::class => [ |
||
| 137 | * 'css' => [], |
||
| 138 | * ], |
||
| 139 | * ] |
||
| 140 | * ``` |
||
| 141 | */ |
||
| 142 | private $bundles = []; |
||
| 143 | |||
| 144 | /** |
||
| 145 | * AssetConverter component. |
||
| 146 | * |
||
| 147 | * @var AssetConverterInterface $converter |
||
| 148 | */ |
||
| 149 | private $converter; |
||
| 150 | |||
| 151 | /** |
||
| 152 | * @var int the permission to be set for newly generated asset directories. This value will be used by PHP chmod() |
||
| 153 | * function. No umask will be applied. Defaults to 0775, meaning the directory is read-writable by owner |
||
| 154 | * and group, but read-only for other users. |
||
| 155 | */ |
||
| 156 | private $dirMode = 0775; |
||
| 157 | |||
| 158 | /** |
||
| 159 | * @var AssetBundle $dummyBundles |
||
| 160 | */ |
||
| 161 | private $dummyBundles; |
||
| 162 | |||
| 163 | /** |
||
| 164 | * @var int the permission to be set for newly published asset files. This value will be used by PHP chmod() |
||
| 165 | * function. No umask will be applied. If not set, the permission will be determined by the current |
||
| 166 | * environment. |
||
| 167 | */ |
||
| 168 | private $fileMode; |
||
| 169 | |||
| 170 | /** |
||
| 171 | * @var bool whether the directory being published should be copied even if it is found in the target directory. |
||
| 172 | * This option is used only when publishing a directory. You may want to set this to be `true` during the |
||
| 173 | * development stage to make sure the published directory is always up-to-date. Do not set this to true |
||
| 174 | * on production servers as it will significantly degrade the performance. |
||
| 175 | */ |
||
| 176 | private $forceCopy = false; |
||
| 177 | |||
| 178 | /** |
||
| 179 | * @var callable a callback that will be called to produce hash for asset directory generation. The signature of the |
||
| 180 | * callback should be as follows: |
||
| 181 | * |
||
| 182 | * ``` |
||
| 183 | * function ($path) |
||
| 184 | * ``` |
||
| 185 | * |
||
| 186 | * where `$path` is the asset path. Note that the `$path` can be either directory where the asset files reside or a |
||
| 187 | * single file. For a CSS file that uses relative path in `url()`, the hash implementation should use the directory |
||
| 188 | * path of the file instead of the file path to include the relative asset files in the copying. |
||
| 189 | * |
||
| 190 | * If this is not set, the asset manager will use the default CRC32 and filemtime in the `hash` method. |
||
| 191 | * |
||
| 192 | * Example of an implementation using MD4 hash: |
||
| 193 | * |
||
| 194 | * ```php |
||
| 195 | * function ($path) { |
||
| 196 | * return hash('md4', $path); |
||
| 197 | * } |
||
| 198 | * ``` |
||
| 199 | */ |
||
| 200 | private $hashCallback; |
||
| 201 | |||
| 202 | /** |
||
| 203 | * @var bool whether to use symbolic link to publish asset files. Defaults to false, meaning asset files are copied |
||
| 204 | * to {@see basePath}. Using symbolic links has the benefit that the published assets will always be |
||
| 205 | * consistent with the source assets and there is no copy operation required. This is especially useful |
||
| 206 | * during development. |
||
| 207 | * |
||
| 208 | * However, there are special requirements for hosting environments in order to use symbolic links. In particular, |
||
| 209 | * symbolic links are supported only on Linux/Unix, and Windows Vista/2008 or greater. |
||
| 210 | * |
||
| 211 | * Moreover, some Web servers need to be properly configured so that the linked assets are accessible to Web users. |
||
| 212 | * For example, for Apache Web server, the following configuration directive should be added for the Web folder: |
||
| 213 | * |
||
| 214 | * ```apache |
||
| 215 | * Options FollowSymLinks |
||
| 216 | * ``` |
||
| 217 | */ |
||
| 218 | private $linkAssets = false; |
||
| 219 | |||
| 220 | /** |
||
| 221 | * @var LoggerInterface $logger |
||
| 222 | */ |
||
| 223 | private $logger; |
||
| 224 | |||
| 225 | /** |
||
| 226 | * @var array published assets |
||
| 227 | */ |
||
| 228 | private $published = []; |
||
| 229 | |||
| 230 | /** |
||
| 231 | * @var string $realBasePath |
||
| 232 | */ |
||
| 233 | private $realBasePath; |
||
| 234 | |||
| 235 | /** |
||
| 236 | * AssetManager constructor. |
||
| 237 | * |
||
| 238 | * @param Aliases $aliases |
||
| 239 | */ |
||
| 240 | public function __construct(Aliases $aliases, LoggerInterface $logger) |
||
| 241 | { |
||
| 242 | $this->aliases = $aliases; |
||
| 243 | $this->logger = $logger; |
||
| 244 | $this->setDefaultPaths(); |
||
| 245 | } |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Returns the actual URL for the specified asset. |
||
| 249 | * The actual URL is obtained by prepending either {@see AssetBundle::$baseUrl} or {@see AssetManager::$baseUrl} to |
||
| 250 | * the given asset path. |
||
| 251 | * |
||
| 252 | * @param AssetBundle $bundle the asset bundle which the asset file belongs to |
||
| 253 | * @param string $asset the asset path. This should be one of the assets listed in {@see AssetBundle::$js} or |
||
| 254 | * {@see AssetBundle::$css}. |
||
| 255 | * |
||
| 256 | * @return string the actual URL for the specified asset. |
||
| 257 | */ |
||
| 258 | public function getAssetUrl(AssetBundle $bundle, string $asset): string |
||
| 259 | { |
||
| 260 | if (($actualAsset = $this->resolveAsset($bundle, $asset)) !== false) { |
||
| 261 | if (strncmp((string) $actualAsset, '@web/', 5) === 0) { |
||
| 262 | $asset = substr((string) $actualAsset, 5); |
||
| 263 | $basePath = $this->aliases->get('@public'); |
||
| 264 | $baseUrl = $this->aliases->get('@web'); |
||
| 265 | } else { |
||
| 266 | $asset = $this->aliases->get($actualAsset); |
||
| 267 | $basePath = $this->getRealBasePath(); |
||
| 268 | $baseUrl = $this->baseUrl; |
||
| 269 | } |
||
| 270 | } else { |
||
| 271 | $basePath = $this->aliases->get($bundle->basePath); |
||
| 272 | $baseUrl = $this->aliases->get($bundle->baseUrl); |
||
| 273 | } |
||
| 274 | |||
| 275 | if (!Info::isRelative($asset) || strncmp($asset, '/', 1) === 0) { |
||
| 276 | return $asset; |
||
| 277 | } |
||
| 278 | |||
| 279 | if ($this->appendTimestamp && ($timestamp = @filemtime("$basePath/$asset")) > 0) { |
||
| 280 | return "$baseUrl/$asset?v=$timestamp"; |
||
| 281 | } |
||
| 282 | |||
| 283 | return "$baseUrl/$asset"; |
||
| 284 | } |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Returns the actual file path for the specified asset. |
||
| 288 | * |
||
| 289 | * @param AssetBundle $bundle the asset bundle which the asset file belongs to |
||
| 290 | * @param string $asset the asset path. This should be one of the assets listed in {@see AssetBundle::$js} or |
||
| 291 | * {@see AssetBundle::$css}. |
||
| 292 | * |
||
| 293 | * @return false|string the actual file path, or `false` if the asset is specified as an absolute URL |
||
| 294 | */ |
||
| 295 | public function getAssetPath(AssetBundle $bundle, string $asset) |
||
| 296 | { |
||
| 297 | if (($actualAsset = $this->resolveAsset($bundle, $asset)) !== false) { |
||
| 298 | return Info::isRelative((string) $actualAsset) ? $this->getRealBasePath() . '/' . $actualAsset : false; |
||
| 299 | } |
||
| 300 | |||
| 301 | return Info::isRelative($asset) ? $bundle->basePath . '/' .$asset : false; |
||
| 302 | } |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Returns the named asset bundle. |
||
| 306 | * |
||
| 307 | * This method will first look for the bundle in {@see bundles()}. If not found, it will treat `$name` as the class |
||
| 308 | * of the asset bundle and create a new instance of it. |
||
| 309 | * |
||
| 310 | * @param string $name the class name of the asset bundle (without the leading backslash) |
||
| 311 | * @param bool $publish whether to publish the asset files in the asset bundle before it is returned. If you set |
||
| 312 | * this false, you must manually call `AssetBundle::publish()` to publish the asset files. |
||
| 313 | * |
||
| 314 | * @return AssetBundle the asset bundle instance |
||
| 315 | * |
||
| 316 | * @throws \InvalidArgumentException |
||
| 317 | */ |
||
| 318 | public function getBundle(string $name, bool $publish = true): AssetBundle |
||
| 341 | } |
||
| 342 | |||
| 343 | /** |
||
| 344 | * Returns the asset converter. |
||
| 345 | * |
||
| 346 | * @return AssetConverterInterface the asset converter. |
||
| 347 | */ |
||
| 348 | public function getConverter(): AssetConverterInterface |
||
| 349 | { |
||
| 350 | if ($this->converter === null) { |
||
| 351 | $this->converter = new AssetConverter($this->aliases, $this->logger); |
||
| 352 | } elseif (is_array($this->converter) || is_string($this->converter)) { |
||
| 353 | if (is_array($this->converter) && !isset($this->converter['__class'])) { |
||
| 354 | $this->converter['__class'] = AssetConverter::class; |
||
| 355 | } |
||
| 356 | $this->converter = new $this->converter($this->aliases, $this->logger); |
||
| 357 | } |
||
| 358 | |||
| 359 | return $this->converter; |
||
| 360 | } |
||
| 361 | |||
| 362 | /** |
||
| 363 | * Returns the published path of a file path. |
||
| 364 | * |
||
| 365 | * This method does not perform any publishing. It merely tells you if the file or directory is published, where it |
||
| 366 | * will go. |
||
| 367 | * |
||
| 368 | * @param string $path directory or file path being published |
||
| 369 | * |
||
| 370 | * @return string|bool string the published file path. False if the file or directory does not exist |
||
| 371 | */ |
||
| 372 | public function getPublishedPath(string $path) |
||
| 373 | { |
||
| 374 | $path = $this->aliases->get($path); |
||
| 375 | |||
| 376 | if (isset($this->published[$path])) { |
||
| 377 | return $this->published[$path][0]; |
||
| 378 | } |
||
| 379 | if (is_string($path) && ($path = realpath($path)) !== false) { |
||
| 380 | return $this->getRealBasePath() . DIRECTORY_SEPARATOR . $this->hash($path) . (is_file($path) ? |
||
| 381 | DIRECTORY_SEPARATOR . basename($path) : ''); |
||
| 382 | } |
||
| 383 | |||
| 384 | return false; |
||
| 385 | } |
||
| 386 | |||
| 387 | /** |
||
| 388 | * Returns the URL of a published file path. |
||
| 389 | * |
||
| 390 | * This method does not perform any publishing. It merely tells you if the file path is published, what the URL will |
||
| 391 | * be to access it. |
||
| 392 | * |
||
| 393 | * @param string $path directory or file path being published |
||
| 394 | * |
||
| 395 | * @return string|bool string the published URL for the file or directory. False if the file or directory does not |
||
| 396 | * exist. |
||
| 397 | */ |
||
| 398 | public function getPublishedUrl(string $path) |
||
| 399 | { |
||
| 400 | if (isset($this->published[$path])) { |
||
| 401 | return $this->published[$path][1]; |
||
| 402 | } |
||
| 403 | if (is_string($path) && ($path = realpath($path)) !== false) { |
||
| 404 | return $this->baseUrl.'/'.$this->hash($path).(is_file($path) ? '/'.basename($path) : ''); |
||
| 405 | } |
||
| 406 | |||
| 407 | return false; |
||
| 408 | } |
||
| 409 | |||
| 410 | /** |
||
| 411 | * Get RealBasePath. |
||
| 412 | * |
||
| 413 | * @return bool|string |
||
| 414 | */ |
||
| 415 | public function getRealBasePath() |
||
| 416 | { |
||
| 417 | if ($this->realBasePath === null) { |
||
| 418 | $this->realBasePath = (string) $this->prepareBasePath($this->basePath); |
||
| 419 | } |
||
| 420 | |||
| 421 | return $this->realBasePath; |
||
| 422 | } |
||
| 423 | |||
| 424 | /** |
||
| 425 | * prepareBasePath |
||
| 426 | * |
||
| 427 | * @param string $basePath |
||
| 428 | * |
||
| 429 | * @throws \InvalidArgumentException |
||
| 430 | * |
||
| 431 | * @return string|bool |
||
| 432 | */ |
||
| 433 | public function prepareBasePath(string $basePath) |
||
| 434 | { |
||
| 435 | $basePath = $this->aliases->get($basePath); |
||
| 436 | |||
| 437 | if (!is_dir($basePath)) { |
||
| 438 | throw new \InvalidArgumentException("The directory does not exist: {$basePath}"); |
||
| 439 | } |
||
| 440 | |||
| 441 | if (!is_writable($basePath)) { |
||
| 442 | throw new \InvalidArgumentException("The directory is not writable by the Web process: {$basePath}"); |
||
| 443 | } |
||
| 444 | |||
| 445 | return realpath($basePath); |
||
| 446 | } |
||
| 447 | |||
| 448 | /** |
||
| 449 | * Publishes a file or a directory. |
||
| 450 | * |
||
| 451 | * This method will copy the specified file or directory to {@see basePath} so that it can be accessed via the Web |
||
| 452 | * server. |
||
| 453 | * |
||
| 454 | * If the asset is a file, its file modification time will be checked to avoid unnecessary file copying. |
||
| 455 | * |
||
| 456 | * If the asset is a directory, all files and subdirectories under it will be published recursively. Note, in case |
||
| 457 | * $forceCopy is false the method only checks the existence of the target directory to avoid repetitive copying |
||
| 458 | * (which is very expensive). |
||
| 459 | * |
||
| 460 | * By default, when publishing a directory, subdirectories and files whose name starts with a dot "." will NOT be |
||
| 461 | * published. If you want to change this behavior, you may specify the "beforeCopy" option as explained in the |
||
| 462 | * `$options` parameter. |
||
| 463 | * |
||
| 464 | * Note: On rare scenario, a race condition can develop that will lead to a one-time-manifestation of a |
||
| 465 | * non-critical problem in the creation of the directory that holds the published assets. This problem can be |
||
| 466 | * avoided altogether by 'requesting' in advance all the resources that are supposed to trigger a 'publish()' call, |
||
| 467 | * and doing that in the application deployment phase, before system goes live. See more in the following |
||
| 468 | * discussion: http://code.google.com/p/yii/issues/detail?id=2579 |
||
| 469 | * |
||
| 470 | * @param string $path the asset (file or directory) to be published |
||
| 471 | * @param array $options the options to be applied when publishing a directory. The following options are |
||
| 472 | * supported: |
||
| 473 | * |
||
| 474 | * - only: array, list of patterns that the file paths should match if they want to be copied. |
||
| 475 | * - except: array, list of patterns that the files or directories should match if they want to be excluded from |
||
| 476 | * being copied. |
||
| 477 | * - caseSensitive: boolean, whether patterns specified at "only" or "except" should be case sensitive. Defaults to |
||
| 478 | * true. |
||
| 479 | * - beforeCopy: callback, a PHP callback that is called before copying each sub-directory or file. |
||
| 480 | * This overrides {@see beforeCopy} if set. |
||
| 481 | * - afterCopy: callback, a PHP callback that is called after a sub-directory or file is successfully copied. This |
||
| 482 | * overrides {@seee afterCopy} if set. |
||
| 483 | * - forceCopy: boolean, whether the directory being published should be copied even if it is found in the target |
||
| 484 | * directory. This option is used only when publishing a directory. This overrides {@see forceCopy} if set. |
||
| 485 | * |
||
| 486 | * @throws \InvalidArgumentException if the asset to be published does not exist. |
||
| 487 | * |
||
| 488 | * @return array the path (directory or file path) and the URL that the asset is published as. |
||
| 489 | */ |
||
| 490 | public function publish(string $path, array $options = []): array |
||
| 491 | { |
||
| 492 | $path = $this->aliases->get($path); |
||
| 493 | |||
| 494 | if (isset($this->published[$path])) { |
||
| 495 | return $this->published[$path]; |
||
| 496 | } |
||
| 497 | |||
| 498 | if (!is_string($path) || ($src = realpath($path)) === false) { |
||
| 499 | throw new \InvalidArgumentException("The file or directory to be published does not exist: $path"); |
||
| 500 | } |
||
| 501 | |||
| 502 | if (is_file($src)) { |
||
| 503 | return $this->published[$path] = $this->publishFile($src); |
||
| 504 | } |
||
| 505 | |||
| 506 | return $this->published[$path] = $this->publishDirectory($src, $options); |
||
| 507 | } |
||
| 508 | |||
| 509 | /** |
||
| 510 | * Set afterCopy. |
||
| 511 | * |
||
| 512 | * @param callable $value |
||
| 513 | * |
||
| 514 | * @return void |
||
| 515 | * |
||
| 516 | * {@see afterCopy} |
||
| 517 | */ |
||
| 518 | public function setAfterCopy(callable $value): void |
||
| 519 | { |
||
| 520 | $this->afterCopy = $value; |
||
| 521 | } |
||
| 522 | |||
| 523 | /** |
||
| 524 | * Set alternatives. |
||
| 525 | * |
||
| 526 | * @param array $value |
||
| 527 | * |
||
| 528 | * @return void |
||
| 529 | * |
||
| 530 | * {@see alternatives} |
||
| 531 | */ |
||
| 532 | public function setAlternatives(array $value): void |
||
| 533 | { |
||
| 534 | $this->alternatives = $value; |
||
| 535 | $this->setAlternativesAlias(); |
||
| 536 | } |
||
| 537 | |||
| 538 | /** |
||
| 539 | * Set appendTimestamp. |
||
| 540 | * |
||
| 541 | * @param bool $value |
||
| 542 | * |
||
| 543 | * @return void |
||
| 544 | * |
||
| 545 | * {@see appendTimestamp} |
||
| 546 | */ |
||
| 547 | public function setAppendTimestamp(bool $value): void |
||
| 548 | { |
||
| 549 | $this->appendTimestamp = $value; |
||
| 550 | } |
||
| 551 | |||
| 552 | /** |
||
| 553 | * Set assetMap. |
||
| 554 | * |
||
| 555 | * @param array $value |
||
| 556 | * |
||
| 557 | * @return void |
||
| 558 | * |
||
| 559 | * {@see assetMap} |
||
| 560 | */ |
||
| 561 | public function setAssetMap(array $value): void |
||
| 562 | { |
||
| 563 | $this->assetMap = $value; |
||
| 564 | } |
||
| 565 | |||
| 566 | /** |
||
| 567 | * Set basePath. |
||
| 568 | * |
||
| 569 | * @param string $value |
||
| 570 | * |
||
| 571 | * @return void |
||
| 572 | * |
||
| 573 | * {@see basePath} |
||
| 574 | */ |
||
| 575 | public function setBasePath(string $value): void |
||
| 576 | { |
||
| 577 | $this->basePath = $value; |
||
| 578 | } |
||
| 579 | |||
| 580 | /** |
||
| 581 | * Set baseUrl. |
||
| 582 | * |
||
| 583 | * @param string $value |
||
| 584 | * |
||
| 585 | * @return void |
||
| 586 | * |
||
| 587 | * {@see baseUrl} |
||
| 588 | */ |
||
| 589 | public function setBaseUrl(string $value): void |
||
| 590 | { |
||
| 591 | $this->baseUrl = $value; |
||
| 592 | } |
||
| 593 | |||
| 594 | /** |
||
| 595 | * Set beforeCopy. |
||
| 596 | * |
||
| 597 | * @param callable $value |
||
| 598 | * |
||
| 599 | * @return void |
||
| 600 | * |
||
| 601 | * {@see beforeCopy} |
||
| 602 | */ |
||
| 603 | public function setBeforeCopy(callable $value): void |
||
| 604 | { |
||
| 605 | $this->beforeCopy = $value; |
||
| 606 | } |
||
| 607 | |||
| 608 | /** |
||
| 609 | * Set bundles. |
||
| 610 | * |
||
| 611 | * @param array $value |
||
| 612 | * |
||
| 613 | * @return void |
||
| 614 | * |
||
| 615 | * {@see beforeCopy} |
||
| 616 | */ |
||
| 617 | public function setBundles(array $value): void |
||
| 618 | { |
||
| 619 | $this->bundles = $value; |
||
| 620 | } |
||
| 621 | |||
| 622 | /** |
||
| 623 | * Sets the asset converter. |
||
| 624 | * |
||
| 625 | * @param AssetConverterInterface $value the asset converter. This can be eitheran object implementing the |
||
| 626 | * {@see AssetConverterInterface}, or a configuration array that can be used |
||
| 627 | * to create the asset converter object. |
||
| 628 | */ |
||
| 629 | public function setConverter(AssetConverterInterface $value): void |
||
| 630 | { |
||
| 631 | $this->converter = $value; |
||
| 632 | } |
||
| 633 | |||
| 634 | /** |
||
| 635 | * Set dirMode. |
||
| 636 | * |
||
| 637 | * @param int $value |
||
| 638 | * |
||
| 639 | * @return void |
||
| 640 | * |
||
| 641 | * {@see dirMode} |
||
| 642 | */ |
||
| 643 | public function setDirMode(int $value): void |
||
| 644 | { |
||
| 645 | $this->dirMode = $value; |
||
| 646 | } |
||
| 647 | |||
| 648 | /** |
||
| 649 | * Set fileMode. |
||
| 650 | * |
||
| 651 | * @param int $value |
||
| 652 | * |
||
| 653 | * @return void |
||
| 654 | * |
||
| 655 | * {@see fileMode} |
||
| 656 | */ |
||
| 657 | public function setFileMode(int $value): void |
||
| 658 | { |
||
| 659 | $this->fileMode = $value; |
||
| 660 | } |
||
| 661 | |||
| 662 | /** |
||
| 663 | * Set hashCallback. |
||
| 664 | * |
||
| 665 | * @param callable $value |
||
| 666 | * |
||
| 667 | * @return void |
||
| 668 | * |
||
| 669 | * {@see hashCallback} |
||
| 670 | */ |
||
| 671 | public function setHashCallback(callable $value): void |
||
| 672 | { |
||
| 673 | $this->hashCallback = $value; |
||
| 674 | } |
||
| 675 | |||
| 676 | /** |
||
| 677 | * Set linkAssets. |
||
| 678 | * |
||
| 679 | * @param bool $value |
||
| 680 | * |
||
| 681 | * @return void |
||
| 682 | * |
||
| 683 | * {@see linkAssets} |
||
| 684 | */ |
||
| 685 | public function setLinkAssets(bool $value): void |
||
| 686 | { |
||
| 687 | $this->linkAssets = $value; |
||
| 688 | } |
||
| 689 | |||
| 690 | /** |
||
| 691 | * Generate a CRC32 hash for the directory path. Collisions are higher than MD5 but generates a much smaller hash |
||
| 692 | * string. |
||
| 693 | * |
||
| 694 | * @param string $path string to be hashed. |
||
| 695 | * |
||
| 696 | * @return string hashed string. |
||
| 697 | */ |
||
| 698 | protected function hash(string $path): string |
||
| 699 | { |
||
| 700 | if (is_callable($this->hashCallback)) { |
||
| 701 | return call_user_func($this->hashCallback, $path); |
||
| 702 | } |
||
| 703 | $path = (is_file($path) ? dirname($path) : $path).filemtime($path); |
||
| 704 | |||
| 705 | return sprintf('%x', crc32($path . Info::frameworkVersion() . '|' . $this->linkAssets)); |
||
| 706 | } |
||
| 707 | |||
| 708 | /** |
||
| 709 | * Loads asset bundle class by name. |
||
| 710 | * |
||
| 711 | * @param string $name bundle name |
||
| 712 | * @param array $config bundle object configuration |
||
| 713 | * @param bool $publish if bundle should be published |
||
| 714 | * |
||
| 715 | * @return AssetBundle |
||
| 716 | */ |
||
| 717 | protected function loadBundle(string $name, array $config = [], bool $publish = true): AssetBundle |
||
| 718 | { |
||
| 719 | if (!isset($config['__class'])) { |
||
| 720 | $config['__class'] = $name; |
||
| 721 | } |
||
| 722 | /* @var $bundle AssetBundle */ |
||
| 723 | $bundle = new $config['__class'](); |
||
| 724 | |||
| 725 | if ($publish) { |
||
| 726 | $bundle->publish($this); |
||
| 727 | } |
||
| 728 | |||
| 729 | return $bundle; |
||
| 730 | } |
||
| 731 | |||
| 732 | /** |
||
| 733 | * Loads dummy bundle by name. |
||
| 734 | * |
||
| 735 | * @param string $name |
||
| 736 | * |
||
| 737 | * @return AssetBundle |
||
| 738 | */ |
||
| 739 | protected function loadDummyBundle(string $name): AssetBundle |
||
| 740 | { |
||
| 741 | if (!isset($this->dummyBundles[$name])) { |
||
| 742 | $this->dummyBundles[$name] = $this->loadBundle($name, [ |
||
| 743 | 'sourcePath' => null, |
||
| 744 | 'js' => [], |
||
| 745 | 'css' => [], |
||
| 746 | 'depends' => [], |
||
| 747 | ]); |
||
| 748 | } |
||
| 749 | |||
| 750 | return $this->dummyBundles[$name]; |
||
| 751 | } |
||
| 752 | |||
| 753 | /** |
||
| 754 | * Publishes a file. |
||
| 755 | * |
||
| 756 | * @param string $src the asset file to be published |
||
| 757 | * |
||
| 758 | * @throws \Exception if the asset to be published does not exist. |
||
| 759 | * |
||
| 760 | * @return array the path and the URL that the asset is published as. |
||
| 761 | */ |
||
| 762 | protected function publishFile(string $src): array |
||
| 763 | { |
||
| 764 | $dir = $this->hash($src); |
||
| 765 | $fileName = basename($src); |
||
| 766 | $dstDir = $this->getRealBasePath() .DIRECTORY_SEPARATOR . $dir; |
||
| 767 | $dstFile = $dstDir . DIRECTORY_SEPARATOR . $fileName; |
||
| 768 | |||
| 769 | if (!is_dir($dstDir)) { |
||
| 770 | FileHelper::createDirectory($dstDir, $this->dirMode); |
||
| 771 | } |
||
| 772 | |||
| 773 | if ($this->linkAssets) { |
||
| 774 | if (!is_file($dstFile)) { |
||
| 775 | try { // fix #6226 symlinking multi threaded |
||
| 776 | symlink($src, $dstFile); |
||
| 777 | } catch (\Exception $e) { |
||
| 778 | if (!is_file($dstFile)) { |
||
| 779 | throw $e; |
||
| 780 | } |
||
| 781 | } |
||
| 782 | } |
||
| 783 | } elseif (@filemtime($dstFile) < @filemtime($src)) { |
||
| 784 | copy($src, $dstFile); |
||
| 785 | if ($this->fileMode !== null) { |
||
| 786 | @chmod($dstFile, $this->fileMode); |
||
| 787 | } |
||
| 788 | } |
||
| 789 | |||
| 790 | return [$dstFile, $this->baseUrl . "/$dir/$fileName"]; |
||
| 791 | } |
||
| 792 | |||
| 793 | /** |
||
| 794 | * Publishes a directory. |
||
| 795 | * |
||
| 796 | * @param string $src the asset directory to be published |
||
| 797 | * @param array $options the options to be applied when publishing a directory. The following options are |
||
| 798 | * supported: |
||
| 799 | * |
||
| 800 | * - only: array, list of patterns that the file paths should match if they want to be copied. |
||
| 801 | * - except: array, list of patterns that the files or directories should match if they want to be excluded from |
||
| 802 | * being copied. |
||
| 803 | * - caseSensitive: boolean, whether patterns specified at "only" or "except" should be case sensitive. Defaults |
||
| 804 | * to true. |
||
| 805 | * - beforeCopy: callback, a PHP callback that is called before copying each sub-directory or file. This overrides |
||
| 806 | * {@see beforeCopy} if set. |
||
| 807 | * - afterCopy: callback, a PHP callback that is called after a sub-directory or file is successfully copied. This |
||
| 808 | * overrides {@see afterCopy} if set. |
||
| 809 | * - forceCopy: boolean, whether the directory being published should be copied even if it is found in the target |
||
| 810 | * directory. This option is used only when publishing a directory. This overrides {@see forceCopy} if set. |
||
| 811 | * |
||
| 812 | * @throws \Exception if the asset to be published does not exist. |
||
| 813 | * |
||
| 814 | * @return array the path directory and the URL that the asset is published as. |
||
| 815 | */ |
||
| 816 | protected function publishDirectory(string $src, array $options): array |
||
| 817 | { |
||
| 818 | $dir = $this->hash($src); |
||
| 819 | $dstDir = $this->getRealBasePath() . DIRECTORY_SEPARATOR . $dir; |
||
| 820 | |||
| 821 | if ($this->linkAssets) { |
||
| 822 | if (!is_dir($dstDir)) { |
||
| 823 | FileHelper::createDirectory(dirname($dstDir), $this->dirMode); |
||
| 824 | |||
| 825 | try { // fix #6226 symlinking multi threaded |
||
| 826 | symlink($src, $dstDir); |
||
| 827 | } catch (\Exception $e) { |
||
| 828 | if (!is_dir($dstDir)) { |
||
| 829 | throw $e; |
||
| 830 | } |
||
| 831 | } |
||
| 832 | } |
||
| 833 | } elseif (!empty($options['forceCopy']) || ($this->forceCopy && !isset($options['forceCopy'])) || !is_dir($dstDir)) { |
||
| 834 | $opts = array_merge( |
||
| 835 | $options, |
||
| 836 | [ |
||
| 837 | 'dirMode' => $this->dirMode, |
||
| 838 | 'fileMode' => $this->fileMode, |
||
| 839 | 'copyEmptyDirectories' => false, |
||
| 840 | ] |
||
| 841 | ); |
||
| 842 | |||
| 843 | if (!isset($opts['beforeCopy'])) { |
||
| 844 | if ($this->beforeCopy !== null) { |
||
| 845 | $opts['beforeCopy'] = $this->beforeCopy; |
||
| 846 | } else { |
||
| 847 | $opts['beforeCopy'] = function ($from, $to) { |
||
| 848 | return strncmp(basename($from), '.', 1) !== 0; |
||
| 849 | }; |
||
| 850 | } |
||
| 851 | } |
||
| 852 | |||
| 853 | if (!isset($opts['afterCopy']) && $this->afterCopy !== null) { |
||
| 854 | $opts['afterCopy'] = $this->afterCopy; |
||
| 855 | } |
||
| 856 | |||
| 857 | FileHelper::copyDirectory($src, $dstDir, $opts); |
||
| 858 | } |
||
| 859 | |||
| 860 | |||
| 861 | return [$dstDir, $this->baseUrl.'/'.$dir]; |
||
| 862 | } |
||
| 863 | |||
| 864 | /** |
||
| 865 | * @param AssetBundle $bundle |
||
| 866 | * @param string $asset |
||
| 867 | * |
||
| 868 | * @return string|bool |
||
| 869 | */ |
||
| 870 | protected function resolveAsset(AssetBundle $bundle, string $asset) |
||
| 890 | } |
||
| 891 | |||
| 892 | /** |
||
| 893 | * Set default paths asset manager. |
||
| 894 | * |
||
| 895 | * @return void |
||
| 896 | */ |
||
| 897 | private function setDefaultPaths(): void |
||
| 898 | { |
||
| 909 | } |
||
| 910 | |||
| 911 | /** |
||
| 912 | * Set alternatives aliases. |
||
| 913 | * |
||
| 914 | * @return void |
||
| 915 | */ |
||
| 916 | protected function setAlternativesAlias(): void |
||
| 917 | { |
||
| 918 | foreach ($this->alternatives as $alias => $path) { |
||
| 919 | $this->aliases->set($alias, $path); |
||
| 920 | } |
||
| 921 | } |
||
| 922 | } |
||
| 923 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths