vendor/symfony/asset/VersionStrategy/RemoteJsonManifestVersionStrategy.php line 16

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\Contracts\HttpClient\HttpClientInterface;
  12. trigger_deprecation('symfony/asset', '5.3', 'The "%s" class is deprecated, use "%s" instead.', RemoteJsonManifestVersionStrategy::class, JsonManifestVersionStrategy::class);
  13. /**
  14. * Reads the versioned path of an asset from a remote JSON manifest file.
  15. *
  16. * For example, the manifest file might look like this:
  17. * {
  18. * "main.js": "main.abc123.js",
  19. * "css/styles.css": "css/styles.555abc.css"
  20. * }
  21. *
  22. * You could then ask for the version of "main.js" or "css/styles.css".
  23. *
  24. * @deprecated since Symfony 5.3, use JsonManifestVersionStrategy instead.
  25. */
  26. class RemoteJsonManifestVersionStrategy implements VersionStrategyInterface
  27. {
  28. private $manifestData;
  29. private $manifestUrl;
  30. private $httpClient;
  31. /**
  32. * @param string $manifestUrl Absolute URL to the manifest file
  33. */
  34. public function __construct(string $manifestUrl, HttpClientInterface $httpClient)
  35. {
  36. $this->manifestUrl = $manifestUrl;
  37. $this->httpClient = $httpClient;
  38. }
  39. /**
  40. * With a manifest, we don't really know or care about what
  41. * the version is. Instead, this returns the path to the
  42. * versioned file.
  43. */
  44. public function getVersion(string $path)
  45. {
  46. return $this->applyVersion($path);
  47. }
  48. public function applyVersion(string $path)
  49. {
  50. if (null === $this->manifestData) {
  51. $this->manifestData = $this->httpClient->request('GET', $this->manifestUrl, [
  52. 'headers' => ['accept' => 'application/json'],
  53. ])->toArray();
  54. }
  55. return $this->manifestData[$path] ?? $path;
  56. }
  57. }