From ccac1b24a83afecef039f943d330c2ec8ad5210d Mon Sep 17 00:00:00 2001 From: Ryan Weaver Date: Fri, 17 Mar 2017 15:10:06 -0400 Subject: [PATCH 1/9] [Asset] Adding a new version strategy that reads from a manifest JSON file --- .../JsonManifestVersionStrategy.php | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 VersionStrategy/JsonManifestVersionStrategy.php diff --git a/VersionStrategy/JsonManifestVersionStrategy.php b/VersionStrategy/JsonManifestVersionStrategy.php new file mode 100644 index 0000000..378ad54 --- /dev/null +++ b/VersionStrategy/JsonManifestVersionStrategy.php @@ -0,0 +1,68 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Asset\VersionStrategy; + +/** + * Reads the versioned path of an asset from a JSON manifest file. + * + * For example, the manifest file might look like this: + * { + * "main.js": "main.abc123.js", + * "css/styles.css": "css/styles.555abc.css" + * } + * + * You could then ask for the version of "main.js" or "css/styles.css". + */ +class JsonManifestVersionStrategy implements VersionStrategyInterface +{ + private $manifestPath; + private $manifestData; + + /** + * @param string $manifestPath Absolute path to the manifest file + */ + public function __construct($manifestPath) + { + $this->manifestPath = $manifestPath; + } + + /** + * With a manifest, we don't really know or care about what + * the version is. Instead, this returns the path to the + * versioned file. + */ + public function getVersion($path) + { + return $this->applyVersion($path); + } + + public function applyVersion($path) + { + return $this->getManifestPath($path) ?: $path; + } + + private function getManifestPath($path) + { + if (null === $this->manifestData) { + if (!file_exists($this->manifestPath)) { + throw new \RuntimeException(sprintf('Asset manifest file "%s" does not exist.', $this->manifestPath)); + } + + $this->manifestData = json_decode(file_get_contents($this->manifestPath), true); + if (0 < json_last_error()) { + throw new \RuntimeException(sprintf('Error parsing JSON from asset manifest file "%s" - %s', $this->manifestPath, json_last_error_msg())); + } + } + + return isset($this->manifestData[$path]) ? $this->manifestData[$path] : null; + } +} From 2e917dd48139649b129873bc04222136f5f5c941 Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Sat, 28 Oct 2017 20:15:32 +0200 Subject: [PATCH 2/9] Replace more docblocks by type-hints --- VersionStrategy/JsonManifestVersionStrategy.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/VersionStrategy/JsonManifestVersionStrategy.php b/VersionStrategy/JsonManifestVersionStrategy.php index 378ad54..7bbfa90 100644 --- a/VersionStrategy/JsonManifestVersionStrategy.php +++ b/VersionStrategy/JsonManifestVersionStrategy.php @@ -30,7 +30,7 @@ class JsonManifestVersionStrategy implements VersionStrategyInterface /** * @param string $manifestPath Absolute path to the manifest file */ - public function __construct($manifestPath) + public function __construct(string $manifestPath) { $this->manifestPath = $manifestPath; } From 75a815ed55c9c5c84b5c8615d46f157622bc2bcf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Sch=C3=A4dlich?= Date: Wed, 26 Jun 2019 21:18:12 +0200 Subject: [PATCH 3/9] [Asset] Add type-hints to public interfaces --- VersionStrategy/JsonManifestVersionStrategy.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/VersionStrategy/JsonManifestVersionStrategy.php b/VersionStrategy/JsonManifestVersionStrategy.php index 7bbfa90..4752737 100644 --- a/VersionStrategy/JsonManifestVersionStrategy.php +++ b/VersionStrategy/JsonManifestVersionStrategy.php @@ -40,17 +40,17 @@ public function __construct(string $manifestPath) * the version is. Instead, this returns the path to the * versioned file. */ - public function getVersion($path) + public function getVersion(string $path) { return $this->applyVersion($path); } - public function applyVersion($path) + public function applyVersion(string $path) { return $this->getManifestPath($path) ?: $path; } - private function getManifestPath($path) + private function getManifestPath(string $path) { if (null === $this->manifestData) { if (!file_exists($this->manifestPath)) { From 0c1de9acc374a8421ee5b01162069e179f81a967 Mon Sep 17 00:00:00 2001 From: "Alexander M. Turek" Date: Wed, 25 Sep 2019 11:12:01 +0200 Subject: [PATCH 4/9] Add types to constructors and private/final/internal methods (Batch I) --- VersionStrategy/JsonManifestVersionStrategy.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/VersionStrategy/JsonManifestVersionStrategy.php b/VersionStrategy/JsonManifestVersionStrategy.php index 4752737..cc4bf5a 100644 --- a/VersionStrategy/JsonManifestVersionStrategy.php +++ b/VersionStrategy/JsonManifestVersionStrategy.php @@ -50,7 +50,7 @@ public function applyVersion(string $path) return $this->getManifestPath($path) ?: $path; } - private function getManifestPath(string $path) + private function getManifestPath(string $path): ?string { if (null === $this->manifestData) { if (!file_exists($this->manifestPath)) { From 116c9c4c177a04d8e654f50c6cad90763ceb9468 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Sun, 15 Mar 2020 10:01:00 +0100 Subject: [PATCH 5/9] Add missing dots at the end of exception messages --- VersionStrategy/JsonManifestVersionStrategy.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/VersionStrategy/JsonManifestVersionStrategy.php b/VersionStrategy/JsonManifestVersionStrategy.php index cc4bf5a..72b7c34 100644 --- a/VersionStrategy/JsonManifestVersionStrategy.php +++ b/VersionStrategy/JsonManifestVersionStrategy.php @@ -59,7 +59,7 @@ private function getManifestPath(string $path): ?string $this->manifestData = json_decode(file_get_contents($this->manifestPath), true); if (0 < json_last_error()) { - throw new \RuntimeException(sprintf('Error parsing JSON from asset manifest file "%s" - %s', $this->manifestPath, json_last_error_msg())); + throw new \RuntimeException(sprintf('Error parsing JSON from asset manifest file "%s" - %s.', $this->manifestPath, json_last_error_msg())); } } From 8226fd0efc39554a9ed10661ea4ba9d5f834fcb1 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Sun, 12 Apr 2020 16:22:30 +0200 Subject: [PATCH 6/9] Tweak the code to avoid fabbot false positives --- VersionStrategy/JsonManifestVersionStrategy.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/VersionStrategy/JsonManifestVersionStrategy.php b/VersionStrategy/JsonManifestVersionStrategy.php index 72b7c34..fe6e2ac 100644 --- a/VersionStrategy/JsonManifestVersionStrategy.php +++ b/VersionStrategy/JsonManifestVersionStrategy.php @@ -59,7 +59,7 @@ private function getManifestPath(string $path): ?string $this->manifestData = json_decode(file_get_contents($this->manifestPath), true); if (0 < json_last_error()) { - throw new \RuntimeException(sprintf('Error parsing JSON from asset manifest file "%s" - %s.', $this->manifestPath, json_last_error_msg())); + throw new \RuntimeException(sprintf('Error parsing JSON from asset manifest file "%s": '.json_last_error_msg(), $this->manifestPath)); } } From 38997403e3ce387c1b64f57d141c21be5c7016cc Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Fri, 24 Apr 2020 00:29:13 +0200 Subject: [PATCH 7/9] Use is_file() instead of file_exists() where possible --- VersionStrategy/JsonManifestVersionStrategy.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/VersionStrategy/JsonManifestVersionStrategy.php b/VersionStrategy/JsonManifestVersionStrategy.php index fe6e2ac..e48f6f2 100644 --- a/VersionStrategy/JsonManifestVersionStrategy.php +++ b/VersionStrategy/JsonManifestVersionStrategy.php @@ -53,7 +53,7 @@ public function applyVersion(string $path) private function getManifestPath(string $path): ?string { if (null === $this->manifestData) { - if (!file_exists($this->manifestPath)) { + if (!is_file($this->manifestPath)) { throw new \RuntimeException(sprintf('Asset manifest file "%s" does not exist.', $this->manifestPath)); } From 151452a4c4d9e5c8829f9fb12c479c2d807eec43 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Wed, 6 May 2020 08:02:17 +0300 Subject: [PATCH 8/9] Make JsonManifestVersionStrategy compatible with PHP 5.3 --- VersionStrategy/JsonManifestVersionStrategy.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/VersionStrategy/JsonManifestVersionStrategy.php b/VersionStrategy/JsonManifestVersionStrategy.php index e48f6f2..b264baf 100644 --- a/VersionStrategy/JsonManifestVersionStrategy.php +++ b/VersionStrategy/JsonManifestVersionStrategy.php @@ -40,12 +40,12 @@ public function __construct(string $manifestPath) * the version is. Instead, this returns the path to the * versioned file. */ - public function getVersion(string $path) + public function getVersion($path) { return $this->applyVersion($path); } - public function applyVersion(string $path) + public function applyVersion($path) { return $this->getManifestPath($path) ?: $path; } @@ -59,7 +59,7 @@ private function getManifestPath(string $path): ?string $this->manifestData = json_decode(file_get_contents($this->manifestPath), true); if (0 < json_last_error()) { - throw new \RuntimeException(sprintf('Error parsing JSON from asset manifest file "%s": '.json_last_error_msg(), $this->manifestPath)); + throw new \RuntimeException(sprintf('Error parsing JSON from asset manifest file: '. $this->manifestPath)); } } From f3785fddf0034d09ee1f5c8c93f141fbfcada27b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Wed, 6 May 2020 08:35:16 +0300 Subject: [PATCH 9/9] Remove string type hints unsupported by php 5.3. Follow-up to #1 --- VersionStrategy/JsonManifestVersionStrategy.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/VersionStrategy/JsonManifestVersionStrategy.php b/VersionStrategy/JsonManifestVersionStrategy.php index b264baf..7c3cf26 100644 --- a/VersionStrategy/JsonManifestVersionStrategy.php +++ b/VersionStrategy/JsonManifestVersionStrategy.php @@ -30,7 +30,7 @@ class JsonManifestVersionStrategy implements VersionStrategyInterface /** * @param string $manifestPath Absolute path to the manifest file */ - public function __construct(string $manifestPath) + public function __construct($manifestPath) { $this->manifestPath = $manifestPath; } @@ -50,7 +50,7 @@ public function applyVersion($path) return $this->getManifestPath($path) ?: $path; } - private function getManifestPath(string $path): ?string + private function getManifestPath($path) { if (null === $this->manifestData) { if (!is_file($this->manifestPath)) {