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: 557: 558: 559: 560: 561: 562: 563: 564: 565: 566: 567: 568: 569: 570: 571: 572: 573: 574: 575: 576: 577: 578:
<?php
namespace MvcCore\Ext\Views\Helpers;
class CssHelper extends Assets {
protected static $instance = NULL;
public static $MinifyCallable = ['\Minify_CSS', 'minify'];
private static $_allowedMediaTypes = ['all','aural','braille','handheld','projection','print','screen','tty','tv',];
protected static $linksGroupContainer = [];
public function Css ($groupName = self::GROUP_NAME_DEFAULT) {
$this->actualGroupName = $groupName;
$this->_getLinksGroupContainer($groupName);
return $this;
}
public function Contains ($path = '', $media = 'all', $doNotMinify = FALSE) {
$result = FALSE;
$linksGroup = & $this->_getLinksGroupContainer($this->actualGroupName);
foreach ($linksGroup as & $item) {
if ($item->path == $path) {
if ($item->media == $media && $item->doNotMinify == $doNotMinify) {
$result = TRUE;
break;
}
}
}
return $result;
}
public function Remove ($path = '', $media = 'all', $doNotMinify = FALSE) {
$result = FALSE;
$linksGroup = & $this->_getLinksGroupContainer($this->actualGroupName);
foreach ($linksGroup as $index => & $item) {
if ($item->path == $path) {
if ($item->media == $media && $item->doNotMinify == $doNotMinify) {
$result = TRUE;
$ctrlActionKey = $this->getCtrlActionKey();
unset(self::$linksGroupContainer[$ctrlActionKey][$this->actualGroupName][$index]);
break;
}
}
}
return $result;
}
public function AppendRendered($path = '', $media = 'all', $doNotMinify = FALSE) {
return $this->Append($path, $media, TRUE, $doNotMinify);
}
public function PrependRendered($path = '', $media = 'all', $doNotMinify = FALSE) {
return $this->Prepend($path, $media, TRUE, $doNotMinify);
}
public function OffsetSetRendered($index = 0, $path = '', $media = 'all', $doNotMinify = FALSE) {
return $this->OffsetSet($index, $path, $media, TRUE, $doNotMinify);
}
public function Append($path = '', $media = 'all', $renderPhpTags = FALSE, $doNotMinify = FALSE) {
$item = $this->_completeItem($path, $media, $renderPhpTags, $doNotMinify);
$currentGroupRecords = & $this->_getLinksGroupContainer($this->actualGroupName);
array_push($currentGroupRecords, $item);
return $this;
}
public function Prepend($path = '', $media = 'all', $renderPhpTags = FALSE, $doNotMinify = FALSE) {
$item = $this->_completeItem($path, $media, $renderPhpTags, $doNotMinify);
$currentGroupRecords = & $this->_getLinksGroupContainer($this->actualGroupName);
array_unshift($currentGroupRecords, $item);
return $this;
}
public function OffsetSet ($index = 0, $path = '', $media = 'all', $renderPhpTags = FALSE, $doNotMinify = FALSE) {
$item = $this->_completeItem($path, $media, $renderPhpTags, $doNotMinify);
$currentGroupRecords = & $this->_getLinksGroupContainer($this->actualGroupName);
$newItems = [];
$added = FALSE;
foreach ($currentGroupRecords as $key => $groupItem) {
if ($key == $index) {
$newItems[] = $item;
$added = TRUE;
}
$newItems[] = $groupItem;
}
if (!$added) $newItems[] = $item;
self::$linksGroupContainer[$this->getCtrlActionKey()][$this->actualGroupName] = $newItems;
return $this;
}
private function _completeItem ($path, $media, $render, $doNotMinify) {
if (self::$fileChecking) {
if (!$path) $this->exception('Path to *.css can\'t be an empty string.');
if (!in_array($media, self::$_allowedMediaTypes, TRUE)) $this->exception('Media could be only values: ' . implode(', ', self::$_allowedMediaTypes) . '.');
$duplication = $this->_isDuplicateStylesheet($path);
if ($duplication) $this->warning("Style sheet '$path' is already added in css group: '$duplication'.");
}
return (object) [
'path' => $path,
'media' => $media,
'render' => $render,
'doNotMinify' => $doNotMinify,
];
}
private function _isDuplicateStylesheet ($path) {
$result = '';
$currentRecords = self::$linksGroupContainer[$this->getCtrlActionKey()];
foreach ($currentRecords as $groupName => $groupItems) {
foreach ($groupItems as $item) {
if ($item->path == $path) {
$result = $groupName;
break;
}
}
}
return $result;
}
public function Render ($indent = 0) {
$currentGroupRecords = & $this->_getLinksGroupContainer($this->actualGroupName);
if (count($currentGroupRecords) === 0) return '';
$minify = (bool)self::$globalOptions['cssMinify'];
$joinTogether = (bool)self::$globalOptions['cssJoin'];
if ($joinTogether) {
$result = $this->_renderItemsTogether(
$this->actualGroupName,
$currentGroupRecords,
$indent,
$minify
);
} else {
$result = $this->_renderItemsSeparated(
$this->actualGroupName,
$currentGroupRecords,
$indent,
$minify
);
}
return $result;
}
private function & _getLinksGroupContainer ($name = '') {
$ctrlActionKey = $this->getCtrlActionKey();
if (!isset(self::$linksGroupContainer[$ctrlActionKey])) {
self::$linksGroupContainer[$ctrlActionKey] = [];
}
if (!isset(self::$linksGroupContainer[$ctrlActionKey][$name])) {
self::$linksGroupContainer[$ctrlActionKey][$name] = [];
}
return self::$linksGroupContainer[$ctrlActionKey][$name];
}
private function _minify (& $css, $path) {
$result = '';
$errorMsg = "Unable to minify style sheet ('{$path}').";
if (!is_callable(static::$MinifyCallable)) {
$this->exception(
"Configured callable object for CSS minification doesn't exist. "
.'Use: https://github.com/mrclay/minify -> /min/lib/Minify/CSS.php'
);
}
try {
$result = call_user_func(static::$MinifyCallable, $css);
} 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[] = '<!-- css group begin: ' . $actualGroupName . ' -->';
foreach ($itemsToRenderSeparately as & $itemsToRender) {
$resultItems[] = $this->_renderItemsTogetherAsGroup($itemsToRender, $minify);
}
foreach ($itemsToRenderMinimized as & $itemsToRender) {
$resultItems[] = $this->_renderItemsTogetherAsGroup($itemsToRender, $minify);
}
if (self::$fileRendering) $resultItems[] = '<!-- css group end: ' . $actualGroupName . ' -->';
return $indentStr . implode(PHP_EOL . $indentStr, $resultItems);
}
private function _renderItemsTogetherAsGroup ($itemsToRender = [], $minify = FALSE) {
$filesGroupInfo = [];
foreach ($itemsToRender as $item) {
if (self::$fileChecking) {
$fullPath = $this->getAppRoot() . $item->path;
if (!file_exists($fullPath)) {
$this->exception("File not found in CSS view rendering process ('$fullPath').");
}
$filesGroupInfo[] = $item->path . '?_' . self::getFileImprint($fullPath);
} else {
$filesGroupInfo[] = $item->path;
}
}
$tmpFileFullPath = $this->getTmpFileFullPathByPartFilesInfo($filesGroupInfo, $minify, 'css');
if (self::$fileRendering) {
if (!file_exists($tmpFileFullPath)) {
$resultContent = '';
foreach ($itemsToRender as & $item) {
$srcFileFullPath = $this->getAppRoot() . $item->path;
if ($item->render) {
$fileContent = $this->_renderFile($srcFileFullPath);
} else if ($minify) {
$fileContent = file_get_contents($srcFileFullPath);
}
$fileContent = $this->_convertStylesheetPathsFromRelatives2TmpAbsolutes(
$fileContent, $item->path
);
if ($minify) $fileContent = $this->_minify($fileContent, $item->path);
$resultContent .= PHP_EOL . "/* " . $item->path . " */" . PHP_EOL . $fileContent . PHP_EOL;
}
$this->saveFileContent($tmpFileFullPath, $resultContent);
$this->log("Css files group rendered ('$tmpFileFullPath').", 'debug');
}
}
$firstItem = array_merge((array) $itemsToRender[0], []);
$pathToTmp = substr($tmpFileFullPath, strlen($this->getAppRoot()));
$firstItem['href'] = $this->CssJsFileUrl($pathToTmp);
return $this->_renderItemSeparated((object) $firstItem);
}
private function _renderFile ($absolutePath) {
ob_start();
try {
include($absolutePath);
} catch (\Exception $e) {
$this->exceptionHandler($e);
} catch (\Throwable $e) {
$this->exceptionHandler($e);
}
return ob_get_clean();
}
private function _convertStylesheetPathsFromRelatives2TmpAbsolutes (& $fullPathContent, $href) {
$lastHrefSlashPos = mb_strrpos($href, '/');
if ($lastHrefSlashPos === FALSE) return $fullPathContent;
$stylesheetDirectoryRelative = mb_substr($href, 0, $lastHrefSlashPos + 1);
$position = 0;
while ($position < mb_strlen($fullPathContent)) {
$doubleDotsPos = mb_strpos($fullPathContent, '../', $position);
if ($doubleDotsPos === FALSE) break;
$lastUrlBeginStrPos = mb_strrpos(mb_substr($fullPathContent, 0, $doubleDotsPos), 'url(');
if ($lastUrlBeginStrPos === FALSE) {
$position = $doubleDotsPos + 3;
continue;
}
$beginOfUrlBlockChars = mb_substr($fullPathContent, $lastUrlBeginStrPos + 4, $doubleDotsPos - ($lastUrlBeginStrPos + 4));
$beginOfUrlBlockChars = preg_replace("#[\./ \"'_\-]#", "", $beginOfUrlBlockChars);
if (mb_strlen($beginOfUrlBlockChars) > 0) {
$position = $lastUrlBeginStrPos + 4;
continue;
}
$firstUrlEndStrPos = mb_strpos($fullPathContent, ')', $doubleDotsPos);
if ($firstUrlEndStrPos === FALSE) {
$position = $doubleDotsPos + 3;
continue;
}
$endOfUrlBlockChars = mb_substr($fullPathContent, $doubleDotsPos + 3, $firstUrlEndStrPos - ($doubleDotsPos + 3));
$endOfUrlBlockChars = preg_replace("#[a-zA-Z\./ \"'_\-\?\&\#]#", "", $endOfUrlBlockChars);
if (mb_strlen($endOfUrlBlockChars) > 0) {
$position = $firstUrlEndStrPos + 1;
continue;
}
$lastUrlBeginStrPos += 4;
$urlSubStr = mb_substr($fullPathContent, $lastUrlBeginStrPos, $firstUrlEndStrPos - $lastUrlBeginStrPos);
$firstStr = mb_substr($urlSubStr, 0, 1);
$lastStr = mb_substr($urlSubStr, mb_strlen($urlSubStr) - 1, 1);
if ($firstStr === '"' && $lastStr === '"') {
$urlSubStr = mb_substr($urlSubStr, 1, mb_strlen($urlSubStr) - 2);
$quote = '"';
} else if ($firstStr === "'" && $lastStr === "'") {
$urlSubStr = mb_substr($urlSubStr, 1, mb_strlen($urlSubStr) - 2);
$quote = "'";
} else {
$quote = '"';
}
$trimmedUrlSubStr = ltrim($urlSubStr, './');
$trimmedPartLength = mb_strlen($urlSubStr) - mb_strlen($trimmedUrlSubStr);
$trimmedPart = trim(mb_substr($urlSubStr, 0, $trimmedPartLength), '/');
$subjectRestPath = trim(mb_substr($urlSubStr, $trimmedPartLength), '/');
$urlFullBasePath = str_replace('\\', '/', realpath($this->getAppRoot() . $stylesheetDirectoryRelative . $trimmedPart));
$urlFullPath = $urlFullBasePath . '/' . $subjectRestPath;
$webPath = mb_substr($urlFullPath, mb_strlen($this->getAppRoot()));
$webPath = $this->CssJsFileUrl($webPath);
$fullPathContent = mb_substr($fullPathContent, 0, $lastUrlBeginStrPos)
. $quote . $webPath . $quote
. mb_substr($fullPathContent, $firstUrlEndStrPos);
$position = $lastUrlBeginStrPos + mb_strlen($webPath) + 3;
}
return str_replace('__RELATIVE_BASE_PATH__', '../..', $fullPathContent);
}
private function _renderFileToTmpAndGetNewHref ($item, $minify = FALSE) {
$path = $item->path;
$tmpFileName = '/rendered_css_' . 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) {
if ($item->render) {
$fileContent = $this->_renderFile($srcFileFullPath);
} else if ($minify) {
$fileContent = file_get_contents($srcFileFullPath);
}
$fileContent = $this->_convertStylesheetPathsFromRelatives2TmpAbsolutes($fileContent, $path);
if ($minify) $fileContent = $this->_minify($fileContent, $item->path);
$this->saveFileContent($tmpFileFullPath, $fileContent);
$this->log("Css file rendered ('$tmpFileFullPath').", 'debug');
}
}
}
$tmpPath = substr($tmpFileFullPath, strlen($this->getAppRoot()));
return $tmpPath;
}
private function _renderItemSeparated (\stdClass $item) {
$result = '<link rel="stylesheet"';
if ($item->media !== 'all') $result .= ' media="' . $item->media . '"';
if (!$item->render && self::$fileChecking) {
$fullPath = $this->getAppRoot() . $item->path;
if (!file_exists($fullPath)) {
$this->log("File not found in CSS view rendering process ('$fullPath').", 'error');
}
}
$result .= ' href="' . $item->href . '" />';
return $result;
}
private function _renderItemsSeparated ($actualGroupName = '', $items = [], $indent = 0, $minify = FALSE) {
$indentStr = $this->getIndentString($indent);
$resultItems = [];
if (self::$fileRendering) $resultItems[] = '<!-- css group begin: ' . $actualGroupName . ' -->';
$appCompilation = \MvcCore\Application::GetInstance()->GetCompiled();
foreach ($items as $item) {
if ($item->render || ($minify && !$item->doNotMinify)) {
$item->href = $this->CssJsFileUrl($this->_renderFileToTmpAndGetNewHref($item, $minify));
} else {
$item->href = $this->CssJsFileUrl($item->path);
}
if (!$appCompilation) {
$item->href = $this->addFileModificationImprintToHrefUrl($item->href, $item->path);
}
$resultItems[] = $this->_renderItemSeparated($item);
}
if (self::$fileRendering) $resultItems[] = '<!-- css group end: ' . $actualGroupName . ' -->';
return $indentStr . implode(PHP_EOL . $indentStr, $resultItems);
}
}