Deprecated: Constant E_STRICT is deprecated in /home/pastorz/old-espace-client/vendor/symfony/error-handler/ErrorHandler.php on line 58

Deprecated: Constant E_STRICT is deprecated in /home/pastorz/old-espace-client/vendor/symfony/error-handler/ErrorHandler.php on line 76
Symfony Profiler

vendor/symfony/asset/VersionStrategy/JsonManifestVersionStrategy.php line 43

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Component\Asset\VersionStrategy;
  11. use Symfony\Component\Asset\Exception\AssetNotFoundException;
  12. use Symfony\Component\Asset\Exception\LogicException;
  13. use Symfony\Component\Asset\Exception\RuntimeException;
  14. use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
  15. use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface;
  16. use Symfony\Contracts\HttpClient\HttpClientInterface;
  17. /**
  18.  * Reads the versioned path of an asset from a JSON manifest file.
  19.  *
  20.  * For example, the manifest file might look like this:
  21.  *     {
  22.  *         "main.js": "main.abc123.js",
  23.  *         "css/styles.css": "css/styles.555abc.css"
  24.  *     }
  25.  *
  26.  * You could then ask for the version of "main.js" or "css/styles.css".
  27.  */
  28. class JsonManifestVersionStrategy implements VersionStrategyInterface
  29. {
  30.     private $manifestPath;
  31.     private $manifestData;
  32.     private $httpClient;
  33.     private $strictMode;
  34.     /**
  35.      * @param string $manifestPath Absolute path to the manifest file
  36.      * @param bool   $strictMode   Throws an exception for unknown paths
  37.      */
  38.     public function __construct(string $manifestPathHttpClientInterface $httpClient null$strictMode false)
  39.     {
  40.         $this->manifestPath $manifestPath;
  41.         $this->httpClient $httpClient;
  42.         $this->strictMode $strictMode;
  43.         if (null === $this->httpClient && ($scheme parse_url($this->manifestPath\PHP_URL_SCHEME)) && === strpos($scheme'http')) {
  44.             throw new LogicException(sprintf('The "%s" class needs an HTTP client to use a remote manifest. Try running "composer require symfony/http-client".'self::class));
  45.         }
  46.     }
  47.     /**
  48.      * With a manifest, we don't really know or care about what
  49.      * the version is. Instead, this returns the path to the
  50.      * versioned file.
  51.      */
  52.     public function getVersion(string $path)
  53.     {
  54.         return $this->applyVersion($path);
  55.     }
  56.     public function applyVersion(string $path)
  57.     {
  58.         return $this->getManifestPath($path) ?: $path;
  59.     }
  60.     private function getManifestPath(string $path): ?string
  61.     {
  62.         if (null === $this->manifestData) {
  63.             if (null !== $this->httpClient && ($scheme parse_url($this->manifestPath\PHP_URL_SCHEME)) && === strpos($scheme'http')) {
  64.                 try {
  65.                     $this->manifestData $this->httpClient->request('GET'$this->manifestPath, [
  66.                         'headers' => ['accept' => 'application/json'],
  67.                     ])->toArray();
  68.                 } catch (DecodingExceptionInterface $e) {
  69.                     throw new RuntimeException(sprintf('Error parsing JSON from asset manifest URL "%s".'$this->manifestPath), 0$e);
  70.                 } catch (ClientExceptionInterface $e) {
  71.                     throw new RuntimeException(sprintf('Error loading JSON from asset manifest URL "%s".'$this->manifestPath), 0$e);
  72.                 }
  73.             } else {
  74.                 if (!is_file($this->manifestPath)) {
  75.                     throw new RuntimeException(sprintf('Asset manifest file "%s" does not exist. Did you forget to build the assets with npm or yarn?'$this->manifestPath));
  76.                 }
  77.                 $this->manifestData json_decode(file_get_contents($this->manifestPath), true);
  78.                 if (json_last_error()) {
  79.                     throw new RuntimeException(sprintf('Error parsing JSON from asset manifest file "%s": '$this->manifestPath).json_last_error_msg());
  80.                 }
  81.             }
  82.         }
  83.         if (isset($this->manifestData[$path])) {
  84.             return $this->manifestData[$path];
  85.         }
  86.         if ($this->strictMode) {
  87.             $message sprintf('Asset "%s" not found in manifest "%s".'$path$this->manifestPath);
  88.             $alternatives $this->findAlternatives($path$this->manifestData);
  89.             if (\count($alternatives) > 0) {
  90.                 $message .= sprintf(' Did you mean one of these? "%s".'implode('", "'$alternatives));
  91.             }
  92.             throw new AssetNotFoundException($message$alternatives);
  93.         }
  94.         return null;
  95.     }
  96.     private function findAlternatives(string $path, array $manifestData): array
  97.     {
  98.         $path strtolower($path);
  99.         $alternatives = [];
  100.         foreach ($manifestData as $key => $value) {
  101.             $lev levenshtein($pathstrtolower($key));
  102.             if ($lev <= \strlen($path) / || false !== stripos($key$path)) {
  103.                 $alternatives[$key] = isset($alternatives[$key]) ? min($lev$alternatives[$key]) : $lev;
  104.             }
  105.             $lev levenshtein($pathstrtolower($value));
  106.             if ($lev <= \strlen($path) / || false !== stripos($key$path)) {
  107.                 $alternatives[$key] = isset($alternatives[$key]) ? min($lev$alternatives[$key]) : $lev;
  108.             }
  109.         }
  110.         asort($alternatives);
  111.         return array_keys($alternatives);
  112.     }
  113. }