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:
<?php
include_once(__DIR__.'/../Common/Base.php');
include_once(__DIR__.'/Scripts/Replacer.php');
class Packager_Php_Base extends Packager_Common_Base
{
const NAMESPACE_NONE = 0;
const NAMESPACE_GLOBAL_CURLY_BRACKETS = 1;
const NAMESPACE_NAMED_CURLY_BRACKETS = 2;
const NAMESPACE_NAMED_SEMICOLONS = 3;
protected static $wrapperClassName = '\Packager_Php_Wrapper';
protected static $wrapperStringDeclarator = 'PACKAGER_';
protected static $wrapperReplacements = array(
T_DIR => NULL,
T_FILE => NULL,
T_REQUIRE_ONCE => array('require_once','%WrapperClass%::RequireOnce'),
T_INCLUDE_ONCE => array('include_once','%WrapperClass%::IncludeOnce'),
T_REQUIRE => array('require', '%WrapperClass%::RequireStandard'),
T_INCLUDE => array('include', '%WrapperClass%::IncludeStandard'),
T_STRING => array(
'DirectoryIterator' => '%WrapperClass%_DirectoryIterator',
'SplFileInfo' => '%WrapperClass%_SplFileInfo',
'is_dir' => '%WrapperClass%::IsDir',
'is_file' => '%WrapperClass%::IsFile',
'readfile' => '%WrapperClass%::Readfile',
'file_get_contents' => '%WrapperClass%::FileGetContents',
'file_exists' => '%WrapperClass%::FileExists',
'filemtime' => '%WrapperClass%::Filemtime',
'filesize' => '%WrapperClass%::Filesize',
'simplexml_load_file' => '%WrapperClass%::SimplexmlLoadFile',
'parse_ini_file' => '%WrapperClass%::ParseIniFile',
'md5_file' => '%WrapperClass%::Md5File',
),
);
protected static $includeFirstDefault = array(
);
protected static $includeLastDefault = array(
'/index.php',
);
protected static $excludePatternsDefault = array(
'^/Libs/startup\.php$',
);
protected static $errorReportingLevelDefault = 5;
protected static $phpReplacementsStatistics = array();
protected static $wrapperInternalElementsDependencies = array(
'NormalizePath' => ',require_once,include_once,require,include,readfile,file_get_contents,parse_ini_file,simplexml_load_file,filemtime,filesize,file_exists,DirectoryIterator,md5_file,is_dir,is_file,',
'Warning' => ',require_once,include_once,require,include,readfile,file_get_contents,parse_ini_file,simplexml_load_file,filemtime,filesize,',
'_getFileContent' => ',require_once,include_once,require,include,readfile,file_get_contents,parse_ini_file,simplexml_load_file,md5_file,',
'_includeFile' => ',require_once,include_once,require,include,',
'_isProtocolPath' => ',readfile,file_get_contents,simplexml_load_file,',
);
protected $filesPhpDependencies = array();
protected $filesPhpOrder = array();
protected $wrapperCode = '';
protected $result = '';
protected $resultFilesInfo = '';
protected $resultFilesContents = '';
protected $unsafeOrderDetection = array();
protected $anyPhpContainsNamespace = FALSE;
protected $globalNamespaceOpened = TRUE;
public function __construct ($cfg = array()) {
parent::__construct($cfg);
}
public static function SetIncludeFirstDefault (array $includeFirstDefault = array()) {
static::$includeFirstDefault = $includeFirstDefault;
}
public static function SetIncludeLastDefault (array $includeLastDefault = array()) {
static::$includeLastDefault = $includeLastDefault;
}
public static function SetExcludePatternsDefault (array $excludePatternsDefault = array()) {
static::$excludePatternsDefault = $excludePatternsDefault;
}
public function Run ($cfg = array()) {
parent::Run($cfg);
$this->_checkAndSetUpCompletePhpConfiguration();
$this->_prepareScriptsReplacer();
$this->files = (object) array(
'all' => array(),
'php' => array(),
'static' => array(),
);
return $this;
}
private function _checkAndSetUpCompletePhpConfiguration () {
foreach ($this->cfg->includeFirst as $key => $relPath) {
$this->cfg->includeFirst[$key] = $this->cfg->sourcesDir . $relPath;
}
foreach (static::$includeFirstDefault as $relPath) {
$absPath = $this->cfg->sourcesDir . $relPath;
if (!in_array($absPath, $this->cfg->includeFirst)) {
array_unshift($this->cfg->includeFirst, $absPath);
}
}
foreach ($this->cfg->includeLast as $key => $relPath) {
$this->cfg->includeLast[$key] = $this->cfg->sourcesDir . $relPath;
}
foreach (static::$includeLastDefault as $relPath) {
$absPath = $this->cfg->sourcesDir . $relPath;
if (!in_array($absPath, $this->cfg->includeLast)) {
$this->cfg->includeLast[] = $absPath;
}
}
foreach (static::$excludePatternsDefault as $pattern) {
if (!in_array($pattern, $this->cfg->excludePatterns)) {
$this->cfg->excludePatterns[] = $pattern;
}
}
if (!isset($this->cfg->errorReportingLevel)) {
$this->cfg->errorReportingLevel = static::$errorReportingLevelDefault;
}
}
private function _prepareScriptsReplacer () {
$phpFunctionsToProcess = array();
if ($this->cfg->phpFsMode != Packager_Php::FS_MODE_STRICT_HDD) {
$defaultCollection = array_merge(
array('require_once', 'include_once', 'require', 'include'),
array_keys(self::$wrapperReplacements[T_STRING])
);
foreach ($defaultCollection as $phpFunctionName) {
$phpFunctionsToProcess[$phpFunctionName] = 1;
}
}
$itemsToReplace = $this->cfg->phpFunctionsToReplace;
$itemsToKeep = $this->cfg->phpFunctionsToKeep;
foreach ($itemsToReplace as $item) {
if (!isset($phpFunctionsToProcess[$item])) $phpFunctionsToProcess[$item] = 1;
}
foreach ($itemsToKeep as $item) {
if ($item == 'include_once' || $item == 'require_once') continue;
if (isset($phpFunctionsToProcess[$item])) unset($phpFunctionsToProcess[$item]);
}
$this->cfg->phpFunctionsToProcess = $phpFunctionsToProcess;
unset($this->cfg->phpFunctionsToReplace, $this->cfg->phpFunctionsToKeep);
self::$wrapperReplacements[T_STRING] = (object) self::$wrapperReplacements[T_STRING];
static::$wrapperReplacements[T_DIR] = function (& $fileInfo) {
$relPathDir = $fileInfo->relPathDir;
return "str_replace('\\\','/',__DIR__).'$relPathDir'";
};
static::$wrapperReplacements[T_FILE] = function (& $fileInfo) {
$relPath = $fileInfo->relPath;
return "str_replace('\\\','/',__DIR__).'$relPath'";
};
Packager_Php_Scripts_Replacer::SetPhpFunctionsToProcess($this->cfg->phpFunctionsToProcess);
Packager_Php_Scripts_Replacer::SetWrapperReplacements(static::$wrapperReplacements);
Packager_Php_Scripts_Replacer::SetWrapperClassName(static::$wrapperClassName);
Packager_Php_Scripts_Replacer::SetPhpFsMode($this->cfg->phpFsMode);
}
}