Skip to content

Commit

Permalink
deal with requirements defined before init
Browse files Browse the repository at this point in the history
  • Loading branch information
lekoala committed Jan 14, 2021
1 parent 009f038 commit e6fcd50
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -17,7 +17,7 @@ In order to defer your scripts, you need to replace in your `PageController` the
{
parent::init();

Requirements::set_backend(new DeferBackend);
DeferBackend::replaceBackend();
}

Once this is done, all scripts (provided by modules or yourself) will be deferred. This is great
Expand Down
26 changes: 26 additions & 0 deletions src/DeferBackend.php
Expand Up @@ -32,6 +32,32 @@ public static function getDeferBackend()
return $backend;
}

/**
* @param Requirements_Backend $oldBackend defaults to current backend
* @return $this
*/
public static function replaceBackend(Requirements_Backend $oldBackend = null)
{
if ($oldBackend === null) {
$oldBackend = Requirements::backend();
}
$deferBackend = new static;
foreach ($oldBackend->getCSS() as $file => $opts) {
$deferBackend->css($file, null, $opts);
}
foreach ($oldBackend->getJavascript() as $file => $opts) {
$deferBackend->javascript($file, null, $opts);
}
foreach ($oldBackend->getCustomCSS() as $id => $script) {
$deferBackend->customCSS($script, $id);
}
foreach ($oldBackend->getCustomScripts() as $id => $script) {
$deferBackend->customScript($script, $id);
}
Requirements::set_backend($deferBackend);
return $deferBackend;
}

/**
* @inheritDoc
*/
Expand Down

0 comments on commit e6fcd50

Please sign in to comment.