From 832f6a55b9fc1434d03a7bc5dfc3f33b98467471 Mon Sep 17 00:00:00 2001 From: Matthias Leutenegger <2-mleutenegger@users.noreply.git.syntro.ch> Date: Tue, 7 Dec 2021 17:58:36 +0100 Subject: [PATCH 1/7] add: settings to stop serving scripts in dev mode --- src/KlaroRequirements.php | 51 +++++++++++++++++++++++++++++++-- tests/KlaroRequirementsTest.php | 24 ++++++++++++++++ 2 files changed, 73 insertions(+), 2 deletions(-) diff --git a/src/KlaroRequirements.php b/src/KlaroRequirements.php index c1f41e1..f373079 100644 --- a/src/KlaroRequirements.php +++ b/src/KlaroRequirements.php @@ -4,6 +4,7 @@ use SilverStripe\View\Requirements; use SilverStripe\View\Requirements_Backend; +use SilverStripe\Control\Director; use Syntro\SilverstripeKlaro\KlaroRequirements_Backend; /** @@ -22,6 +23,21 @@ class KlaroRequirements { + /** + * @var string + */ + const SERVE_LIVE = 'SERVELIVE'; + + /** + * @var string + */ + const SERVE_LIVETEST = 'SERVELIVETEST'; + + /** + * @var string + */ + const SERVE_ALWAYS = 'SERVE_ALWAYS'; + /** * Instance of the requirements for storage. You can create your own backend to change the * default JS and CSS inclusion behaviour. @@ -47,10 +63,17 @@ public static function backend() * - 'type' : Override script type= value. * - 'integrity' : SubResource Integrity hash * - 'crossorigin' : Cross-origin policy for the resource + * @param string $env = KlaroRequirements::SERVE_LIVETEST where to serve this. available options are: + * - KlaroRequirements::SERVE_LIVE : serve only in live env. + * - KlaroRequirements::SERVE_LIVETEST : serve only in dev mode + * - KlaroRequirements::SERVE_ALWAYS : serve always * @return void */ - public static function klaroJavascript($file, $klaroName, $options = []) + public static function klaroJavascript($file, $klaroName, $options = [], $env = KlaroRequirements::SERVE_LIVETEST) { + if (!static::canServeWith($env)) { + return; + } /** @var KlaroRequirements_Backend */ $backend = self::backend(); $backend->klaroJavascript($file, $klaroName, $options); @@ -62,10 +85,17 @@ public static function klaroJavascript($file, $klaroName, $options = []) * @param string $script The script content as a string (without enclosing `