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:
<?php
include_once(__DIR__.'/Scripts/Dependencies.php');
class Packager_Php_Completer extends Packager_Php_Scripts_Dependencies
{
private static $_universalStoringType = 'base64';
private static $_extensionsAndStoringTypes = array();
private static $_fileTypesForWhiteSpaceTrim = array(
'css','htc','js','txt','svg','ini','htm','html','phtml','xml',
);
protected function autoloadJob ($params = array()) {
$this->completePhpFilesDependenciesByAutoloadDeclaration($params['file']);
}
protected function mainJob ($params = array()) {
$this->completeAllFiles();
$this->completePhpFilesDependencies();
$this->completePhpFilesOrder();
$this->_completePhpAndStaticFiles();
$this->processPhpCode();
$this->_completeResult();
$this->_saveResult();
$this->notify();
}
private function _completePhpAndStaticFiles () {
foreach ($this->filesPhpOrder as $fullPath) {
if (isset($this->files->all[$fullPath])) {
$fileInfo = $this->files->all[$fullPath];
$this->files->php[$fullPath] = $fileInfo;
unset($this->files->all[$fullPath]);
}
}
if ($this->cfg->phpFsMode == Packager_Php::FS_MODE_STRICT_HDD) {
$this->files->static = array();
} else {
$fullPaths = array_keys($this->files->all);
for ($i = 0, $l = count($fullPaths); $i < $l; $i += 1) {
$fullPath = $fullPaths[$i];
$this->files->static[$fullPath] = $this->files->all[$fullPath];
unset($this->files->all[$fullPath]);
}
}
$this->files->all = array();
$this->filesPhpOrder = array();
}
private function _completeResult () {
self::_setUpExtensionsAndStoringTypes();
$this->_completeResultPhpCodeAndScriptFilesRecords();
if ($this->cfg->phpFsMode != Packager_Php::FS_MODE_STRICT_HDD) {
$this->_completeResultStaticFilesRecords();
}
$this->completeWrapperCode();
$this->_completeWholeResult();
}
private static function _setUpExtensionsAndStoringTypes () {
foreach (self::$fileTypesStoringTypes as $storingType => $fileTypes) {
foreach ($fileTypes as $fileType) {
self::$_extensionsAndStoringTypes[$fileType] = $storingType;
}
}
}
private static function _getStoringTypeByExtension ($extension) {
$extension = strtolower($extension);
$storingType = self::$_universalStoringType;
if (isset(self::$_extensionsAndStoringTypes[$extension])) {
$storingType = self::$_extensionsAndStoringTypes[$extension];
}
return $storingType;
}
private function _completeResultPhpCodeAndScriptFilesRecords () {
$fullPaths = array_keys($this->files->php);
$linesCounter = 0;
$this->globalNamespaceOpened = $this->anyPhpContainsNamespace;
for ($i = 0, $l = count($fullPaths); $i < $l; $i += 1) {
$fullPath = $fullPaths[$i];
$fileInfo = $this->files->php[$fullPath];
$relPath = $fileInfo->relPath;
$filemtime = $fileInfo->filemtime;
$filesize = $fileInfo->filesize;
$linesCount = substr_count($fileInfo->content, "\n") + 1;
$newLineGlue = ($this->result == '') ? '' : "\n";
if ($this->anyPhpContainsNamespace) {
if (
$fileInfo->containsNamespace === Packager_Php::NAMESPACE_NONE &&
!$this->globalNamespaceOpened
) {
$this->result .= $newLineGlue . "namespace{\n";
$newLineGlue = '';
$linesCounter += 1;
$this->globalNamespaceOpened = TRUE;
} else if (
$fileInfo->containsNamespace !== Packager_Php::NAMESPACE_NONE &&
$this->globalNamespaceOpened
) {
$this->result .= $newLineGlue . "}\n";
$newLineGlue = '';
$linesCounter += 1;
$this->globalNamespaceOpened = FALSE;
}
}
$this->result .= $newLineGlue . $fileInfo->content;
$this->resultFilesInfo .= "\n" . "'$relPath'=>array('index'=>-1,'mtime'=>$filemtime,'size'=>$filesize,'lines'=>array($linesCounter,$linesCount)),";
$this->files->php[$fullPath] = $relPath;
$linesCounter += $linesCount;
}
if ($this->anyPhpContainsNamespace && $this->globalNamespaceOpened) {
$this->result .= "\n}";
}
$this->files->php = array_values($this->files->php);
}
private function _completeResultStaticFilesRecords () {
$fullPaths = array_keys($this->files->static);
for ($i = 0, $l = count($fullPaths); $i < $l; $i += 1) {
$fullPath = $fullPaths[$i];
$fileInfo = $this->files->static[$fullPath];
$relPath = $fileInfo->relPath;
$filemtime = $fileInfo->filemtime;
$filesize = $fileInfo->filesize;
$filesize = strlen($fileInfo->content);
$storingType = self::_getStoringTypeByExtension($fileInfo->extension);
$this->_processStaticFileContent($fileInfo, $storingType);
$glue = ($this->resultFilesInfo == '') ? '' : "\n";
$this->resultFilesInfo .= $glue . "'$relPath'=>array('index'=>$i,'mtime'=>$filemtime,'size'=>$filesize,'store'=>'$storingType'),";
$glue = ($this->resultFilesContents == '') ? '' : "\n";
$this->resultFilesContents .=
$glue . self::$wrapperClassName . "::\$Contents[$i]="
. $this->_packStaticFileContent($fileInfo, $storingType);
$this->files->static[$fullPath] = $relPath . ($fileInfo->utf8bomRemoved ? ' (UTF8 BOM REMOVED)' : '');
}
$this->files->static = array_values($this->files->static);
}
private function _processStaticFileContent (& $fileInfo, $storingType) {
if (in_array($fileInfo->extension, self::$_fileTypesForWhiteSpaceTrim)) {
$fileInfo->content = trim($fileInfo->content);
}
if (in_array($fileInfo->extension, static::$templatesExtensions)) {
$this->processPatternAndStringReplacements($fileInfo);
$fileInfo->content = Packager_Php_Scripts_Replacer::ProcessReplacements($fileInfo, $this->cfg);
if ($this->cfg->minifyPhp) {
$fileInfo->content = $this->shrinkPhpCode($fileInfo->content);
}
if ($this->cfg->minifyTemplates) {
@include_once('vendor/autoload.php');
$fileInfo->content = Minify_HTML::minify($fileInfo->content);
}
}
}
private function _completeWholeResult () {
$this->wrapperCode = str_replace(
"'____" . self::$wrapperClassName . "::\$Info____'",
$this->resultFilesInfo . "\n",
$this->wrapperCode
);
$baseCode = '<'.'?php'
. ($this->anyPhpContainsNamespace ? "\nnamespace{" : "")
. "\n" . 'error_reporting('.$this->cfg->errorReportingLevel.');'
. "\n" . $this->wrapperCode
. "\n" . $this->resultFilesContents
. "\n";
$baseCodeLinesCount = substr_count($baseCode, "\n") + 1;
$baseCode = str_replace(
"'____" . self::$wrapperClassName . "::\$_baseLinesCount____'",
$baseCodeLinesCount,
$baseCode
);
$this->result = $baseCode . $this->result;
unset($baseCode);
$this->resultFilesInfo = '';
$this->resultFilesContents = '';
}
private function _packStaticFileContent (& $fileInfo, $storingType) {
if ($storingType == 'gzip') {
$gzipStr = gzencode($fileInfo->content, 6);
return "<<<'" . self::$wrapperStringDeclarator . "GZIP'"
. "\n" . $gzipStr
. "\n" . self::$wrapperStringDeclarator . 'GZIP;';
} else if ($storingType == 'binary') {
return "<<<'" . self::$wrapperStringDeclarator . "BIN'"
. "\n" . $fileInfo->content
. "\n" . self::$wrapperStringDeclarator . 'BIN;';
} else if ($storingType == 'base64') {
return "'".base64_encode($fileInfo->content)."';";
} else if ($storingType == 'template') {
return "function(){ ?>"
. "\n" . $fileInfo->content
. "\n" . '<?php return 1;};';
} else if ($storingType == 'text') {
$fileInfo->content = str_replace("\r\n", "\n", $fileInfo->content);
return "<<<'" . self::$wrapperStringDeclarator . "TEXT'"
. "\n" . $fileInfo->content
. "\n" . self::$wrapperStringDeclarator . 'TEXT;';
}
}
private function _saveResult () {
$releaseFile = $this->cfg->releaseFile;
unlink($releaseFile);
file_put_contents($releaseFile, $this->result);
}
protected function notify ($title = 'Successfully packed') {
$scriptsCount = count($this->files->php);
$staticsCount = count($this->files->static);
$totalCount = $scriptsCount + $staticsCount;
if (php_sapi_name() == 'cli') {
$content = "Total included files: $totalCount\n\n"
. "\nIncluded PHP files ($scriptsCount):\n\n"
. implode("\n", $this->files->php)
. "\n\n\nIncluded static files ($staticsCount):\n\n"
. implode("\n", $this->files->static);
if (count($this->unsafeOrderDetection)) {
$content .= "\n\n\nDeclaration order for files below was not detected certainly\n"
. "If there will occur any exceptions by running result, complete order for these files manualy.\n\n"
. implode("\n", $this->unsafeOrderDetection);
}
$content .= "\n\n\nDONE";
$this->sendResult(
$title,
$content
);
} else {
$content = "<div>Total included files: $totalCount</div>"
. "<h2>Included PHP files ($scriptsCount):</h2>"
. '<div class="files">'
. implode('<br />', $this->files->php)
. "</div>"
. "<h2>Included static files ($staticsCount):</h2>"
. '<div class="files">'
. implode('<br />', $this->files->static)
. "</div>";
if (count($this->unsafeOrderDetection)) {
$content .= "<h2>Declaration order for files below was not detected certainly</h2>"
. "<p>If there will occur any exceptions by running result, complete order for these files manualy.</p>"
. '<div class="files">'
. implode('<br />', $this->unsafeOrderDetection)
. "</div>";
}
$content .= '</div><h2>DONE</h2>';
$this->sendResult(
$title,
$content,
'success'
);
}
}
}