Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add: settings to stop serving scripts in dev & test mode #28

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ use Syntro\SilverstripeKlaro\KlaroRequirements;

// ...

KlaroRequirements::klaroJavascript('path/to/file.js', 'myservice');
KlaroRequirements::klaroJavascript('path/to/file.js', 'myservice', [], KlaroRequirements::SERVE_ALWAYS);

```
In order to add this service to the notice, create a config like so:
Expand Down
4 changes: 2 additions & 2 deletions docs/en/service_management.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ functions:
use Syntro\SilverstripeKlaro\KlaroRequirements;

// Add a javascript file
KlaroRequirements::klaroJavascript('path/to/file.js', 'myservice');
KlaroRequirements::klaroJavascript('path/to/file.js', 'myservice', [], KlaroRequirements::SERVE_ALWAYS);
// Add a css file
KlaroRequirements::klaroCss('path/to/file.css', 'myservice');
KlaroRequirements::klaroCss('path/to/file.css', 'myservice', [], KlaroRequirements::SERVE_ALWAYS);
// Add a custom script
KlaroRequirements::customKlaroScript(<<<JS alert('hello') JS, 'myservice');
```
Expand Down
51 changes: 49 additions & 2 deletions src/KlaroRequirements.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use SilverStripe\View\Requirements;
use SilverStripe\View\Requirements_Backend;
use SilverStripe\Control\Director;
use Syntro\SilverstripeKlaro\KlaroRequirements_Backend;

/**
Expand All @@ -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.
Expand All @@ -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);
Expand All @@ -62,10 +85,17 @@ public static function klaroJavascript($file, $klaroName, $options = [])
* @param string $script The script content as a string (without enclosing `<script>` tag)
* @param string $klaroName the name used to identify the service
* @param string $uniquenessID A unique ID that ensures a piece of code is only added once
* @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 customKlaroScript($script, $klaroName, $uniquenessID = null)
public static function customKlaroScript($script, $klaroName, $uniquenessID = null, $env = KlaroRequirements::SERVE_LIVETEST)
{
if (!static::canServeWith($env)) {
return;
}
/** @var KlaroRequirements_Backend */
$backend = self::backend();
$backend->customKlaroScript($script, $klaroName, $uniquenessID);
Expand All @@ -90,4 +120,21 @@ public static function klaroCss($file, $klaroName, $media = null, $options = [])
$backend = self::backend();
$backend->klaroCss($file, $klaroName, $media, $options);
}

/**
* canServeWith - checks if the current environment allows serving with the
* given Serving mode
*
* @param string $key the key
* @return boolean
*/
public static function canServeWith($key)
{
if (Director::isTest()) {
return $key === self::SERVE_LIVETEST || $key === self::SERVE_ALWAYS;
} elseif (Director::isDev()) {
return $key === self::SERVE_ALWAYS;
}
return true;
}
}
51 changes: 48 additions & 3 deletions tests/KlaroRequirementsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
namespace Syntro\SilverstripeKlaro\Tests;

use SilverStripe\Dev\SapphireTest;
use SilverStripe\Control\Director;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Core\Kernel;
use Syntro\SilverstripeKlaro\KlaroRequirements_Backend;
use Syntro\SilverstripeKlaro\KlaroRequirements;

Expand Down Expand Up @@ -35,14 +38,23 @@ public function testGetBackend()
*/
public function testKlaroJavascript()
{
$kernel = Injector::inst()->get(Kernel::class);
$currentType = $kernel->getEnvironment();
$backend = KlaroRequirements::backend();

$kernel->setEnvironment(Kernel::LIVE);
$this->assertEquals(0, count($backend->getKlaroJavascript()));

KlaroRequirements::klaroJavascript('/file.js', 'name');

$this->assertEquals(1, count($backend->getKlaroJavascript()));
$this->assertArrayHasKey('/file.js', $backend->getKlaroJavascript());

$kernel->setEnvironment(Kernel::DEV);
$this->assertEquals(1, count($backend->getKlaroJavascript()));
KlaroRequirements::klaroJavascript('/file_dev.js', 'namedev');
$this->assertEquals(1, count($backend->getKlaroJavascript()));
$this->assertArrayNotHasKey('/file_dev.js', $backend->getKlaroJavascript());

$kernel->setEnvironment($currentType);
}

/**
Expand All @@ -52,13 +64,21 @@ public function testKlaroJavascript()
*/
public function testCustomKlaroScript()
{
$kernel = Injector::inst()->get(Kernel::class);
$currentType = $kernel->getEnvironment();
$backend = KlaroRequirements::backend();

$kernel->setEnvironment(Kernel::LIVE);
$this->assertEquals(0, count($backend->getcustomKlaroScript()));

KlaroRequirements::customKlaroScript('script', 'name');
$this->assertEquals(1, count($backend->getcustomKlaroScript()));

$kernel->setEnvironment(Kernel::DEV);
$this->assertEquals(1, count($backend->getcustomKlaroScript()));
KlaroRequirements::customKlaroScript('scriptdev', 'namedev');
$this->assertEquals(1, count($backend->getcustomKlaroScript()));

$kernel->setEnvironment($currentType);
}

/**
Expand All @@ -77,4 +97,29 @@ public function testKlaroCss()
$this->assertEquals(1, count($backend->getKlaroCss()));
$this->assertArrayHasKey('/file.css', $backend->getKlaroCss());
}

/**
* testCanServeWith
*
* @return void
*/
public function testCanServeWith()
{

$kernel = Injector::inst()->get(Kernel::class);
$currentType = $kernel->getEnvironment();
$kernel->setEnvironment(Kernel::DEV);
$this->assertFalse(KlaroRequirements::canServeWith(KlaroRequirements::SERVE_LIVE));
$this->assertFalse(KlaroRequirements::canServeWith(KlaroRequirements::SERVE_LIVETEST));
$this->assertTrue(KlaroRequirements::canServeWith(KlaroRequirements::SERVE_ALWAYS));
$kernel->setEnvironment(Kernel::TEST);
$this->assertFalse(KlaroRequirements::canServeWith(KlaroRequirements::SERVE_LIVE));
$this->assertTrue(KlaroRequirements::canServeWith(KlaroRequirements::SERVE_LIVETEST));
$this->assertTrue(KlaroRequirements::canServeWith(KlaroRequirements::SERVE_ALWAYS));
$kernel->setEnvironment(Kernel::LIVE);
$this->assertTrue(KlaroRequirements::canServeWith(KlaroRequirements::SERVE_LIVE));
$this->assertTrue(KlaroRequirements::canServeWith(KlaroRequirements::SERVE_LIVETEST));
$this->assertTrue(KlaroRequirements::canServeWith(KlaroRequirements::SERVE_ALWAYS));
$kernel->setEnvironment($currentType);
}
}