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:
<?php
include_once(__DIR__.'/../Base.php');
include_once(__DIR__.'/Replacer.php');
class Packager_Php_Scripts_Completer extends Packager_Php_Base
{
protected function processPhpCode () {
foreach ($this->files->php as & $fileInfo) {
$this->processPatternAndStringReplacements($fileInfo);
$fileInfo->content = Packager_Php_Scripts_Replacer::ProcessReplacements($fileInfo, $this->cfg);
if ($this->cfg->minifyPhp) {
$fileInfo->content = $this->shrinkPhpCode($fileInfo->content);
}
$fileInfo->content = str_replace("\r\n", "\n", $fileInfo->content);
self::_removeOpenAndClosePhpTags($fileInfo);
if ($fileInfo->containsNamespace !== Packager_Php::NAMESPACE_NONE) {
$this->anyPhpContainsNamespace = TRUE;
}
}
if ($this->anyPhpContainsNamespace) {
foreach ($this->files->php as & $fileInfo) {
if ($fileInfo->containsNamespace === Packager_Php::NAMESPACE_NAMED_SEMICOLONS) {
$fileInfo->content = Packager_Php_Scripts_Replacer::ProcessNamespaces($fileInfo, $this->cfg);
}
}
}
}
protected function completeWrapperCode () {
$wrapperFileName = $this->cfg->phpFsMode . '.php';
$wrapperFullPath = __DIR__ . '/../Wrappers/' . $wrapperFileName;
$this->wrapperCode = file_get_contents($wrapperFullPath);
$this->wrapperCode = str_replace(
"____" . self::$wrapperClassName . "::FS_MODE____",
$this->cfg->phpFsMode,
$this->wrapperCode
);
$this->wrapperCode = str_replace(
"'____" . self::$wrapperClassName . "::\$_minifiedPhp____'",
$this->cfg->minifyPhp ? 'TRUE' : 'FALSE',
$this->wrapperCode
);
if ($this->cfg->phpFsMode != Packager_Php::FS_MODE_STRICT_HDD) {
$this->_processWrapperCodeByReplacementsStatistics();
}
if ($this->cfg->minifyPhp) {
$this->wrapperCode = $this->shrinkPhpCode($this->wrapperCode);
}
$this->wrapperCode = str_replace("\r\n", "\n", $this->wrapperCode);
$this->wrapperCode = trim(mb_substr($this->wrapperCode, strlen('<'.'?php')));
}
private static function _removeOpenAndClosePhpTags (& $fileInfo) {
$fileInfo->content = trim($fileInfo->content);
if (mb_strpos($fileInfo->content, '<' . '?php') === 0) {
$fileInfo->content = mb_substr($fileInfo->content, 5);
} else if (mb_strpos($fileInfo->content, '<' . '?') === 0) {
$fileInfo->content = mb_substr($fileInfo->content, 2);
}
$contentLength = mb_strlen($fileInfo->content);
if (mb_strrpos($fileInfo->content, '?' . '>') === $contentLength - 2) {
$fileInfo->content = mb_substr($fileInfo->content, 0, $contentLength - 2);
}
$fileInfo->content = trim($fileInfo->content);
}
protected function processPatternAndStringReplacements (& $fileInfo) {
foreach ($this->cfg->patternReplacements as $pattern => $replacement) {
if (is_numeric($pattern)) {
$patternLocal = $replacement;
while (preg_match($patternLocal, $fileInfo->content)) {
$fileInfo->content = preg_replace($patternLocal, '', $fileInfo->content);
}
} else {
while (preg_match($pattern, $fileInfo->content)) {
$fileInfo->content = preg_replace($pattern, $replacement, $fileInfo->content);
}
}
}
foreach ($this->cfg->stringReplacements as $from => $to) {
$fileInfo->content = str_replace($from, $to, $fileInfo->content);
}
}
private function _processWrapperCodeByReplacementsStatistics () {
self::$phpReplacementsStatistics = Packager_Php_Scripts_Replacer::GetReplacementsStatistics();
$this->_processWrapperCodeRemovePublicElements();
$this->_processWrapperCodeRemovePrivateElements();
}
private function _processWrapperCodeRemovePublicElements () {
foreach (array('require_once', 'include_once', 'require', 'include') as $statement) {
if (!(
isset(self::$phpReplacementsStatistics[$statement]) &&
self::$phpReplacementsStatistics[$statement] > 0
)) {
$this->_removeWrapperPhpFunctionEquivalent($statement);
}
}
foreach (self::$wrapperReplacements[T_STRING] as $phpFunction => $wrapperEquivalent) {
if (!(
isset(self::$phpReplacementsStatistics[$phpFunction]) &&
self::$phpReplacementsStatistics[$phpFunction] > 0
)) {
$this->_removeWrapperPhpFunctionEquivalent($phpFunction);
}
}
}
private function _processWrapperCodeRemovePrivateElements () {
$privateElementsRemoved = array();
foreach (self::$wrapperInternalElementsDependencies as $internalElement => $dependecies) {
$keepPrivateElementInWrapper = FALSE;
foreach (self::$phpReplacementsStatistics as $phpFunction => $replacedCount) {
if ($replacedCount > 0) {
if (mb_strpos($dependecies, ",$phpFunction,") !== FALSE) {
$keepPrivateElementInWrapper = TRUE;
}
}
}
if (!$keepPrivateElementInWrapper) {
$this->_removeWrapperPhpFunctionEquivalent($internalElement);
$privateElementsRemoved[$internalElement] = 1;
}
}
$initMethodRemoved = FALSE;
if (isset($privateElementsRemoved['NormalizePath'])) {
$this->_removeWrapperPhpFunctionEquivalent('Init');
$this->_removeWrapperPhpFunctionEquivalent('Init');
$initMethodRemoved = TRUE;
}
if (count($this->files->static) === 0 && $initMethodRemoved) {
$this->_removeWrapperPhpFunctionEquivalent('fields');
}
}
private function _removeWrapperPhpFunctionEquivalent ($originalPhpFunctionName) {
$commentTemplate = __CLASS__ . '::{startEnd}({functionName})';
$startStr = str_replace(array('{startEnd}', '{functionName}'), array('start', $originalPhpFunctionName), $commentTemplate);
$endStr = str_replace(array('{startEnd}', '{functionName}'), array('end', $originalPhpFunctionName), $commentTemplate);
$startPos = mb_strpos($this->wrapperCode, $startStr);
$endPos = mb_strpos($this->wrapperCode, $endStr, $startPos + mb_strlen($startStr));
if ($startPos === FALSE || $endPos === FALSE) return;
$this->wrapperCode = mb_substr(
$this->wrapperCode,
0,
$startPos
) . "REMOVED: " . $originalPhpFunctionName . mb_substr(
$this->wrapperCode,
$endPos + mb_strlen($endStr)
);
}
}