Skip to content

Commit dd89fe0

Browse files
committed
minor #14414 [Asset] Add option $strictMode to JsonManifestVersionStrategy (GromNaN)
This PR was merged into the 5.4 branch. Discussion ---------- [Asset] Add option $strictMode to JsonManifestVersionStrategy Documentation for symfony/symfony#38495 Commits ------- c4e19eb [Assets] Add doc for strict mode strategy
2 parents fdd1210 + c4e19eb commit dd89fe0

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

components/asset.rst

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,26 @@ In those cases, use the
167167
echo $package->getUrl('css/app.css');
168168
// result: build/css/app.b916426ea1d10021f3f17ce8031f93c2.css
169169

170+
If you request an asset that is *not found* in the ``rev-manifest.json`` file, the original -
171+
*unmodified* - asset path will be returned.
172+
The ``$strictMode`` argument helps for debugging as it throws an exception when the asset is
173+
not listed in the manifest::
174+
175+
use Symfony\Component\Asset\Package;
176+
use Symfony\Component\Asset\VersionStrategy\JsonManifestVersionStrategy;
177+
178+
// The value of $strictMode can be specific per environment "true" for debugging and "false" for stability.
179+
$strictMode = true;
180+
// assumes the JSON file above is called "rev-manifest.json"
181+
$package = new Package(new JsonManifestVersionStrategy(__DIR__.'/rev-manifest.json', null, $strictMode));
182+
183+
echo $package->getUrl('not-found.css');
184+
// error:
185+
186+
.. versionadded:: 5.4
187+
188+
The ``$strictMode`` option was introduced in Symfony 5.4.
189+
170190
If your JSON file is not on your local filesystem but is accessible over HTTP,
171191
use the :class:`Symfony\\Component\\Asset\\VersionStrategy\\RemoteJsonManifestVersionStrategy`
172192
with the :doc:`HttpClient component </http_client>`::

reference/configuration/framework.rst

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1879,6 +1879,7 @@ Each package can configure the following options:
18791879
* :ref:`version <reference-framework-assets-version>`
18801880
* :ref:`version_format <reference-assets-version-format>`
18811881
* :ref:`json_manifest_path <reference-assets-json-manifest-path>`
1882+
* :ref:`strict_mode <reference-assets-strict-mode>`
18821883

18831884
.. _reference-framework-assets-version:
18841885
.. _ref-framework-assets-version:
@@ -2122,6 +2123,8 @@ package:
21222123
foo_package:
21232124
# this package uses its own manifest (the default file is ignored)
21242125
json_manifest_path: "%kernel.project_dir%/public/build/a_different_manifest.json"
2126+
# Throws an exception when an asset is not found in the manifest
2127+
strict_mode: %kernel.debug%
21252128
bar_package:
21262129
# this package uses the global manifest (the default file is used)
21272130
base_path: '/images'
@@ -2142,9 +2145,10 @@ package:
21422145
<!-- you can use absolute URLs too and Symfony will download them automatically -->
21432146
<!-- <framework:assets json-manifest-path="https://cdn.example.com/manifest.json"> -->
21442147
<!-- this package uses its own manifest (the default file is ignored) -->
2148+
<!-- Throws an exception when an asset is not found in the manifest -->
21452149
<framework:package
21462150
name="foo_package"
2147-
json-manifest-path="%kernel.project_dir%/public/build/a_different_manifest.json"/>
2151+
json-manifest-path="%kernel.project_dir%/public/build/a_different_manifest.json" strict-mode="%kernel.debug%"/>
21482152
<!-- this package uses the global manifest (the default file is used) -->
21492153
<framework:package
21502154
name="bar_package"
@@ -2168,7 +2172,9 @@ package:
21682172
// 'json_manifest_path' => 'https://cdn.example.com/manifest.json',
21692173
$framework->assets()->package('foo_package')
21702174
// this package uses its own manifest (the default file is ignored)
2171-
->jsonManifestPath('%kernel.project_dir%/public/build/a_different_manifest.json');
2175+
->jsonManifestPath('%kernel.project_dir%/public/build/a_different_manifest.json')
2176+
// Throws an exception when an asset is not found in the manifest
2177+
->setStrictMode('%kernel.debug%');
21722178
21732179
$framework->assets()->package('bar_package')
21742180
// this package uses the global manifest (the default file is used)
@@ -2190,11 +2196,26 @@ package:
21902196

21912197
If you request an asset that is *not found* in the ``manifest.json`` file, the original -
21922198
*unmodified* - asset path will be returned.
2199+
Since Symfony 5.4, you can set ``strict_mode`` to ``true`` to get an exception when an asset is *not found*.
21932200

21942201
.. note::
21952202

21962203
If an URL is set, the JSON manifest is downloaded on each request using the `http_client`_.
21972204

2205+
.. _reference-assets-strict-mode:
2206+
2207+
strict_mode
2208+
...........
2209+
2210+
**type**: ``boolean`` **default**: ``false``
2211+
2212+
When enabled, the strict mode assert that all requested assets are in the manifest file.
2213+
This option is useful to detect typo or missing assets, the recommended value is ``%kernel.debug%``.
2214+
2215+
.. versionadded:: 5.4
2216+
2217+
This option was introduced in Symfony 5.4.
2218+
21982219
translator
21992220
~~~~~~~~~~
22002221

0 commit comments

Comments
 (0)