summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoremkael <emkael@tlen.pl>2016-04-19 13:30:36 +0200
committeremkael <emkael@tlen.pl>2016-04-19 13:30:36 +0200
commit60841b3c92dc630d5d6dd03aadac27705bacdc62 (patch)
tree4cf9cc926fbb7a9fbbac49aa1173ef3937246b9e
parent1c5d3829b9e7f499da8aadf88fdbcc4301ea57ac (diff)
* external scripts support in ClientScriptManager
-rw-r--r--app/php/components/ClientScriptManager.php75
1 files changed, 60 insertions, 15 deletions
diff --git a/app/php/components/ClientScriptManager.php b/app/php/components/ClientScriptManager.php
index 64e5b58..8289c69 100644
--- a/app/php/components/ClientScriptManager.php
+++ b/app/php/components/ClientScriptManager.php
@@ -106,17 +106,62 @@ class ClientScriptManager extends TClientScriptManager {
return $this->Application->AssetManager->publishFilePath($cacheFile);
}
+ private function _determineLocalScriptFiles($files) {
+ $basePath = $this->_getBasePath();
+ return array_filter(
+ $files,
+ function($file) use($basePath) {
+ return file_exists($basePath . DIRECTORY_SEPARATOR . $file);
+ }
+ );
+ }
+
+ private function _renderLocalScriptFiles($writer, $localFiles) {
+ if ($localFiles) {
+ $assetPath = $this->_compileFiles($localFiles);
+ $writer->write(TJavascript::renderScriptFile($assetPath));
+ }
+ }
+
+ private function _renderExternalScriptFiles($writer, $externalFiles) {
+ if ($externalFiles) {
+ foreach ($externalFiles as $file) {
+ $this->markScriptFileAsRendered($file);
+ $this->_appendRenderedScripts([$file], $file);
+ }
+ parent::renderScriptFiles($writer, $externalFiles);
+ }
+ }
+
public function renderScriptFiles($writer, Array $files) {
if ($this->getApplication()->getMode() !== TApplicationMode::Debug) {
if ($files) {
- $assetPath = $this->_compileFiles($files);
- $writer->write(TJavascript::renderScriptFile($assetPath));
+ $localFiles = $this->_determineLocalScriptFiles($files);
+ $this->_renderLocalScriptFiles($writer, $localFiles);
+ $externalFiles = array_diff($files, $localFiles);
+ $this->_renderExternalScriptFiles($writer, $externalFiles);
}
} else {
parent::renderScriptFiles($writer, $files);
}
}
+ private function _getRenderedScriptUrl($registeredScript) {
+ $renderedScripts = $this->_getRenderedScripts();
+ foreach ($renderedScripts as $compiledFile => $scripts) {
+ if (in_array($registeredScript, $scripts)) {
+ if (file_exists($compiledFile)) {
+ return $this->Application->AssetManager->getPublishedUrl(
+ $compiledFile
+ );
+ } else {
+ return $registeredScript;
+ }
+ }
+ }
+ return FALSE;
+ }
+
public function getScriptUrls() {
if ($this->getApplication()->getMode() !== TApplicationMode::Debug) {
$registeredScripts = array_unique(
@@ -124,26 +169,26 @@ class ClientScriptManager extends TClientScriptManager {
$this->_scripts, $this->_headScripts
)
);
- $renderedScripts = $this->_getRenderedScripts();
$scriptUrls = [];
$newScripts = [];
foreach ($registeredScripts as $registeredScript) {
- $found = FALSE;
- foreach ($renderedScripts as $compiledFile => $scripts) {
- if (in_array($registeredScript, $scripts)) {
- $scriptUrls[] = $this->Application->AssetManager->getPublishedUrl(
- $compiledFile
- );
- $found = TRUE;
- }
- }
- if (!$found) {
+ $renderedScriptUrl = $this->_getRenderedScriptUrl(
+ $registeredScript
+ );
+ if ($renderedScriptUrl) {
+ $scriptUrls[] = $renderedScriptUrl;
+ } else {
$newScripts[] = $registeredScript;
}
}
- if ($newScripts) {
- $scriptUrls[] = $this->_compileFiles($newScripts);
+ $newLocalScripts = $this->_determineLocalScriptFiles($newScripts);
+ $newRemoteScripts = array_diff($newScripts, $newLocalScripts);
+ if ($newLocalScripts) {
+ $scriptUrls[] = $this->_compileFiles($newLocalScripts);
}
+ $scriptUrls = array_values(
+ array_unique(array_merge($scriptUrls, $newRemoteScripts))
+ );
return $scriptUrls;
} else {
return parent::getScriptUrls();