1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271: 272: 273: 274: 275: 276: 277: 278: 279: 280: 281: 282: 283: 284: 285: 286: 287: 288: 289: 290: 291: 292: 293: 294: 295: 296: 297: 298: 299: 300: 301: 302: 303: 304: 305: 306: 307: 308: 309: 310: 311: 312: 313: 314: 315: 316: 317: 318: 319: 320: 321: 322: 323: 324: 325: 326: 327: 328: 329: 330: 331: 332: 333: 334: 335: 336: 337: 338: 339: 340: 341: 342: 343: 344: 345: 346: 347: 348: 349: 350: 351: 352: 353: 354: 355: 356: 357: 358: 359: 360: 361: 362: 363: 364: 365: 366: 367: 368: 369: 370: 371: 372: 373: 374: 375: 376: 377: 378: 379: 380: 381: 382: 383: 384: 385: 386: 387: 388: 389: 390: 391: 392: 393: 394: 395: 396: 397: 398: 399: 400: 401: 402: 403: 404: 405: 406: 407: 408: 409: 410: 411: 412: 413: 414: 415: 416: 417: 418: 419: 420: 421: 422: 423: 424: 425: 426: 427: 428: 429: 430: 431: 432: 433: 434: 435: 436: 437: 438: 439: 440: 441: 442: 443: 444: 445: 446: 447: 448: 449: 450: 451: 452: 453: 454: 455: 456: 457: 458: 459: 460: 461: 462: 463: 464: 465: 466: 467: 468: 469: 470: 471: 472: 473: 474: 475: 476: 477: 478: 479: 480: 481: 482: 483: 484: 485: 486: 487: 488: 489: 490: 491: 492: 493: 494: 495: 496: 497: 498: 499: 500: 501: 502: 503: 504: 505: 506: 507: 508: 509: 510: 511: 512: 513: 514: 515: 516: 517: 518: 519: 520: 521: 522: 523: 524: 525: 526: 527: 528: 529: 530: 531: 532: 533: 534: 535: 536: 537: 538: 539: 540: 541: 542: 543: 544: 545: 546: 547: 548: 549: 550: 551: 552: 553: 554: 555: 556:
<?php
namespace MvcCore\Ext\Views\Helpers;
class JsHelper extends Assets {
protected static $instance = NULL;
const EXTERNAL_MIN_CACHE_TIME = 86400;
public static $MinifyCallable = ['\JSMin', 'minify'];
protected static $scriptsGroupContainer = [];
public function Js ($groupName = self::GROUP_NAME_DEFAULT) {
$this->actualGroupName = $groupName;
$this->_getScriptsGroupContainer($groupName);
return $this;
}
public function Contains ($path = '', $async = FALSE, $defer = FALSE, $doNotMinify = FALSE) {
$result = FALSE;
$scriptsGroup = & $this->_getScriptsGroupContainer($this->actualGroupName);
foreach ($scriptsGroup as & $item) {
if ($item->path == $path) {
if ($item->async == $async && $item->defer == $defer && $item->doNotMinify == $doNotMinify) {
$result = TRUE;
break;
}
}
}
return $result;
}
public function Remove ($path = '', $async = FALSE, $defer = FALSE, $doNotMinify = FALSE) {
$result = FALSE;
$scriptsGroup = & $this->_getScriptsGroupContainer($this->actualGroupName);
foreach ($scriptsGroup as $index => & $item) {
if ($item->path == $path) {
if ($item->async == $async && $item->defer == $defer && $item->doNotMinify == $doNotMinify) {
$result = TRUE;
$ctrlActionKey = $this->getCtrlActionKey();
unset(self::$scriptsGroupContainer[$ctrlActionKey][$this->actualGroupName][$index]);
break;
}
}
}
return $result;
}
public function AppendExternal ($path = '', $async = FALSE, $defer = FALSE, $doNotMinify = FALSE) {
return $this->Append($path, $async, $defer, $doNotMinify, TRUE);
}
public function PrependExternal ($path = '', $async = FALSE, $defer = FALSE, $doNotMinify = FALSE) {
return $this->Prepend($path, $async, $defer, $doNotMinify, TRUE);
}
public function OffsetExternal ($index = 0, $path = '', $async = FALSE, $defer = FALSE, $doNotMinify = FALSE) {
return $this->Offset($index, $path, $async, $defer, $doNotMinify, TRUE);
}
public function Append ($path = '', $async = FALSE, $defer = FALSE, $doNotMinify = FALSE, $external = FALSE) {
$item = $this->_completeItem($path, $async, $defer, $doNotMinify, $external);
$actialGroupItems = & $this->_getScriptsGroupContainer($this->actualGroupName);
array_push($actialGroupItems, $item);
return $this;
}
public function Prepend ($path = '', $async = FALSE, $defer = FALSE, $doNotMinify = FALSE, $external = FALSE) {
$item = $this->_completeItem($path, $async, $defer, $doNotMinify, $external);
$actualGroupItems = & $this->_getScriptsGroupContainer($this->actualGroupName);
array_unshift($actualGroupItems, $item);
return $this;
}
public function Offset ($index = 0, $path = '', $async = FALSE, $defer = FALSE, $doNotMinify = FALSE, $external = FALSE) {
$item = $this->_completeItem($path, $async, $defer, $doNotMinify, $external);
$actialGroupItems = & $this->_getScriptsGroupContainer($this->actualGroupName);
$newItems = [];
$added = FALSE;
foreach ($actialGroupItems as $key => & $groupItem) {
if ($key == $index) {
$newItems[] = $item;
$added = TRUE;
}
$newItems[] = $groupItem;
}
if (!$added) $newItems[] = $item;
self::$scriptsGroupContainer[$this->getCtrlActionKey()][$this->actualGroupName] = $newItems;
return $this;
}
private function & _getScriptsGroupContainer ($name = '') {
$ctrlActionKey = $this->getCtrlActionKey();
if (!isset(self::$scriptsGroupContainer[$ctrlActionKey])) {
self::$scriptsGroupContainer[$ctrlActionKey] = [];
}
if (!isset(self::$scriptsGroupContainer[$ctrlActionKey][$name])) {
self::$scriptsGroupContainer[$ctrlActionKey][$name] = [];
}
return self::$scriptsGroupContainer[$ctrlActionKey][$name];
}
private function _completeItem ($path, $async, $defer, $doNotMinify, $external) {
if (self::$loggingAndExceptions) {
if (!$path) $this->exception('Path to *.js can\'t be an empty string.');
if ($this->controller->GetEnvironment()->IsDevelopment()) {
$duplication = $this->_isDuplicateScript($path);
if ($duplication) $this->warning("Script '$path' is already added in js group: '$duplication'.");
}
}
return (object) [
'path' => $path,
'async' => $async,
'defer' => $defer,
'doNotMinify' => $doNotMinify,
'external' => $external,
];
}
private function _isDuplicateScript ($path) {
$result = '';
$allGroupItems = & self::$scriptsGroupContainer[$this->getCtrlActionKey()];
foreach ($allGroupItems as $groupName => $groupItems) {
foreach ($groupItems as $item) {
if ($item->path == $path) {
$result = $groupName;
break;
}
}
}
return $result;
}
public function Render ($indent = 0) {
$actualGroupItems = & $this->_getScriptsGroupContainer($this->actualGroupName);
if (count($actualGroupItems) === 0) return '';
$minify = (bool)self::$globalOptions['jsMinify'];
$joinTogether = (bool)self::$globalOptions['jsJoin'];
if ($joinTogether) {
$result = $this->_renderItemsTogether(
$this->actualGroupName,
$actualGroupItems,
$indent,
$minify
);
} else {
$result = $this->_renderItemsSeparated(
$this->actualGroupName,
$actualGroupItems,
$indent,
$minify
);
}
return $result;
}
private function _renderItemsSeparated ($actualGroupName = '', $items = [], $indent = 0, $minify = FALSE) {
$indentStr = $this->getIndentString($indent);
$resultItems = [];
if (self::$fileRendering) $resultItems[] = '<!-- js group begin: ' . $actualGroupName . ' -->';
$appCompilation = \MvcCore\Application::GetInstance()->GetCompiled();
foreach ($items as $item) {
if ($item->external) {
$item->src = $this->CssJsFileUrl($this->_downloadFileToTmpAndGetNewHref($item, $minify));
} else if ($minify && !$item->doNotMinify) {
$item->src = $this->CssJsFileUrl($this->_renderFileToTmpAndGetNewHref($item, $minify));
} else {
$item->src = $this->CssJsFileUrl($item->path);
}
if (!$appCompilation) {
$item->src = $this->addFileModificationImprintToHrefUrl($item->src, $item->path);
}
$resultItems[] = $this->_renderItemSeparated($item);
}
if (self::$fileRendering) $resultItems[] = '<!-- js group end: ' . $actualGroupName . ' -->';
return $indentStr . implode(PHP_EOL . $indentStr, $resultItems);
}
private function _renderFileToTmpAndGetNewHref ($item, $minify = FALSE) {
$path = $item->path;
$tmpFileName = '/rendered_js_' . self::$systemConfigHash . '_' . trim(str_replace('/', '_', $path), "_");
$srcFileFullPath = $this->getAppRoot() . $path;
$tmpFileFullPath = $this->getTmpDir() . $tmpFileName;
if (self::$fileRendering) {
if (file_exists($srcFileFullPath)) {
$srcFileModDate = filemtime($srcFileFullPath);
} else {
$srcFileModDate = 1;
}
if (file_exists($tmpFileFullPath)) {
$tmpFileModDate = filemtime($tmpFileFullPath);
} else {
$tmpFileModDate = 0;
}
if ($srcFileModDate !== FALSE && $tmpFileModDate !== FALSE) {
if ($srcFileModDate > $tmpFileModDate) {
$fileContent = file_get_contents($srcFileFullPath);
if ($minify) {
$fileContent = $this->_minify($fileContent, $path);
}
$this->saveFileContent($tmpFileFullPath, $fileContent);
$this->log("Js file rendered ('$tmpFileFullPath').", 'debug');
}
}
}
$tmpPath = substr($tmpFileFullPath, strlen($this->getAppRoot()));
return $tmpPath;
}
private function _downloadFileToTmpAndGetNewHref ($item, $minify = FALSE) {
$path = $item->path;
$tmpFileFullPath = $this->getTmpDir() . '/external_js_' . md5($path) . '.js';
if (self::$fileRendering) {
if (file_exists($tmpFileFullPath)) {
$cacheFileTime = filemtime($tmpFileFullPath);
} else {
$cacheFileTime = 0;
}
if (time() > $cacheFileTime + self::EXTERNAL_MIN_CACHE_TIME) {
while (TRUE) {
$newPath = $this->_getPossiblyRedirectedPath($path);
if ($newPath === $path) {
break;
} else {
$path = $newPath;
}
}
$fr = fopen($path, 'r');
$fileContent = '';
$bufferLength = 102400;
$buffer = '';
while ($buffer = fread($fr, $bufferLength)) {
$fileContent .= $buffer;
}
fclose($fr);
if ($minify) {
$fileContent = $this->_minify($fileContent, $path);
}
$this->saveFileContent($tmpFileFullPath, $fileContent);
$this->log("External js file downloaded ('$tmpFileFullPath').", 'debug');
}
}
$tmpPath = substr($tmpFileFullPath, strlen($this->getAppRoot()));
return $tmpPath;
}
private function _getPossiblyRedirectedPath ($path = '') {
$fp = fopen($path, 'r');
$metaData = stream_get_meta_data($fp);
foreach ($metaData['wrapper_data'] as $response) {
if (strtolower(substr($response, 0, 10)) == 'location: ') {
$path = substr($response, 10);
}
}
return $path;
}
private function _renderItemSeparated (\stdClass $item) {
$result = '<script type="text/javascript"';
if ($item->async) $result .= ' async="async"';
if ($item->async) $result .= ' defer="defer"';
if (!$item->external && self::$fileChecking) {
$fullPath = $this->getAppRoot() . $item->path;
if (!file_exists($fullPath)) {
$this->log("File not found in CSS view rendering process ('$fullPath').", 'error');
}
}
$result .= ' src="' . $item->src . '"></script>';
return $result;
}
private function _minify (& $js, $path) {
$result = '';
$errorMsg = "Unable to minify javascript ('{$path}').";
if (!is_callable(static::$MinifyCallable)) {
$this->exception(
"Configured callable object for JS minification doesn't exist. "
."Use: https://github.com/mrclay/minify -> /min/lib/JSMin.php"
);
}
try {
$result = call_user_func(static::$MinifyCallable, $js);
} catch (\Exception $e) {
$this->exception($errorMsg);
} catch (\Throwable $e) {
$this->exception($errorMsg);
}
return $result;
}
private function _renderItemsTogether ($actualGroupName = '', $items = [], $indent = 0, $minify = FALSE) {
list($itemsToRenderMinimized, $itemsToRenderSeparately) = $this->filterItemsForNotPossibleMinifiedAndPossibleMinifiedItems($items);
$indentStr = $this->getIndentString($indent);
$resultItems = [];
if (self::$fileRendering) $resultItems[] = '<!-- js group begin: ' . $actualGroupName . ' -->';
foreach ($itemsToRenderSeparately as & $itemsToRender) {
$resultItems[] = $this->_renderItemsTogetherAsGroup($itemsToRender, FALSE);
}
foreach ($itemsToRenderMinimized as & $itemsToRender) {
$resultItems[] = $this->_renderItemsTogetherAsGroup($itemsToRender, $minify);
}
if (self::$fileRendering) $resultItems[] = $indentStr . '<!-- js group end: ' . $actualGroupName . ' -->';
return $indentStr . implode(PHP_EOL, $resultItems);
}
private function _renderItemsTogetherAsGroup ($itemsToRender = [], $minify = FALSE) {
$filesGroupInfo = [];
foreach ($itemsToRender as $item) {
if ($item->external) {
$srcFileFullPath = $this->_downloadFileToTmpAndGetNewHref($item, $minify);
$filesGroupInfo[] = $item->path . '?_' . self::getFileImprint($this->getAppRoot() . $srcFileFullPath);
} else {
if (self::$fileChecking) {
$fullPath = $this->getAppRoot() . $item->path;
if (!file_exists($fullPath)) {
$this->exception("File not found in JS view rendering process ('$fullPath').");
}
$filesGroupInfo[] = $item->path . '?_' . self::getFileImprint($fullPath);
} else {
$filesGroupInfo[] = $item->path;
}
}
}
$tmpFileFullPath = $this->getTmpFileFullPathByPartFilesInfo($filesGroupInfo, $minify, 'js');
if (self::$fileRendering) {
if (!file_exists($tmpFileFullPath)) {
$resultContent = '';
foreach ($itemsToRender as & $item) {
$srcFileFullPath = $this->getAppRoot() . $item->path;
if ($item->external) {
$srcFileFullPath = $this->_downloadFileToTmpAndGetNewHref($item, $minify);
$fileContent = file_get_contents($this->getAppRoot() . $srcFileFullPath);
} else if ($minify) {
$fileContent = file_get_contents($srcFileFullPath);
if ($minify) $fileContent = $this->_minify($fileContent, $item->path);
} else {
$fileContent = file_get_contents($srcFileFullPath);
}
$resultContent .= PHP_EOL . "/* " . $item->path . " */" . PHP_EOL . $fileContent . PHP_EOL;
}
$this->saveFileContent($tmpFileFullPath, $resultContent);
$this->log("Js files group rendered ('$tmpFileFullPath').", 'debug');
}
}
$firstItem = array_merge((array) $itemsToRender[0], []);
$pathToTmp = substr($tmpFileFullPath, strlen($this->getAppRoot()));
$firstItem['src'] = $this->CssJsFileUrl($pathToTmp);
return $this->_renderItemSeparated((object) $firstItem);
}
}