Overview

Classes

  • Packager_Common_Base
  • Packager_Common_StaticCopies
  • Packager_Phar
  • Packager_Phar_ResultCompleter
  • Packager_Php
  • Packager_Php_Base
  • Packager_Php_Completer
  • Packager_Php_Scripts_Completer
  • Packager_Php_Scripts_Dependencies
  • Packager_Php_Scripts_Order
  • Packager_Php_Scripts_Replacer
  • Packager_Php_Wrapper
  • Packager_Php_Wrapper_DirectoryIterator
  • Packager_Php_Wrapper_SplFileInfo

Exceptions

  • Packager_Php_Scripts_Throwable
  • Overview
  • Class
  • Tree
  • Todo
  • Deprecated
  • Download
  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: 
<?php

include_once(__DIR__.'/../Base.php');
include_once(__DIR__.'/Replacer.php');

class Packager_Php_Scripts_Completer extends Packager_Php_Base
{
    protected function processScriptsPhpCode () {
        $processScriptsPhpReplacements = (
            $this->cfg->phpFsMode !== Packager_Php::FS_MODE_STRICT_HDD &&
            $this->cfg->phpFsMode !== Packager_Php::FS_MODE_PHP_LIBRARY
        );
        foreach ($this->files->php as & $fileInfo) {

            // process pattern and string replacements by config
            $this->processPatternAndStringReplacements($fileInfo);

            // process php code and wrap configured functions
            if ($processScriptsPhpReplacements)
                $fileInfo->content = Packager_Php_Scripts_Replacer::ProcessScriptsReplacements(
                    $fileInfo, $this->cfg
                );

            // minify if necessary
            if ($this->cfg->minifyPhp) {
                $fileInfo->content = $this->shrinkPhpCode($fileInfo->content);
            }
            $fileInfo->content = str_replace("\r\n", "\n", $fileInfo->content);

            // remove open tag - only at file begin (<\?php or <\?)
            // and remove close tag - only at file end (?\>)
            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)) {
                // if there is numeric key - values is always pattern to replace with empty string
                $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 (['require_once', 'include_once', 'require', 'include'] as $statement) {
            if (!(
                isset(self::$phpReplacementsStatistics[$statement]) &&
                self::$phpReplacementsStatistics[$statement] > 0
            )) {
                // remove php function equivalent from wrapper code
                $this->_removeWrapperPhpFunctionEquivalent($statement);
            }
        }
        // go thought all wrapper public elements and decide
        // if there will be for each one public function or not by statistic record
        foreach (self::$wrapperReplacements[T_STRING] as $phpFunction => $wrapperEquivalent) {
            if (!(
                isset(self::$phpReplacementsStatistics[$phpFunction]) &&
                self::$phpReplacementsStatistics[$phpFunction] > 0
            )) {
                // remove php function equivalent from wrapper code
                $this->_removeWrapperPhpFunctionEquivalent($phpFunction);
            }
        }
    }
    private function _processWrapperCodeRemovePrivateElements () {
        // go thought all wrapper private elements and decide
        // if there will necessary by dependencies to keep private element or not
        $privateElementsRemoved = [];
        foreach (self::$wrapperInternalElementsDependencies as $internalElement => $dependecies) {
            $keepPrivateElementInWrapper = FALSE;
            // go thought statistics and try to catch replaced function (with bigger statistic int than 0) in dependencies string
            foreach (self::$phpReplacementsStatistics as $phpFunction => $replacedCount) {
                if ($replacedCount > 0) {
                    // if there is any php function replaced more times than 0 in target code
                    if (mb_strpos($dependecies, ",$phpFunction,") !== FALSE) {
                        // if there is any dependency from private wrapper code function
                        // representing original php function - keep this private code in wrapper
                        $keepPrivateElementInWrapper = TRUE;
                    }
                }
            }
            if (!$keepPrivateElementInWrapper) {
                $this->_removeWrapperPhpFunctionEquivalent($internalElement);
                $privateElementsRemoved[$internalElement] = 1;
            }
        }
        // check if there is still necessary to have sections: fields, Init
        // there is not necessary to call Init() function in wrapper, if there is no
        $initMethodRemoved = FALSE;
        if (isset($privateElementsRemoved['NormalizePath'])) {
            $this->_removeWrapperPhpFunctionEquivalent('Init'); // remove method declaration
            $this->_removeWrapperPhpFunctionEquivalent('Init'); // remove method call
            $initMethodRemoved = TRUE;
        }
        // there is not necessary to have there a field section,
        // if there are no static files to include in result and
        // if there is removed Init() method
        if (count($this->files->static) === 0 && $initMethodRemoved) {
            $this->_removeWrapperPhpFunctionEquivalent('fields');
        }
    }
    private function _removeWrapperPhpFunctionEquivalent ($originalPhpFunctionName) {
        $commentTemplate = get_class() . '::{startEnd}({functionName})';
        $startStr   = str_replace(['{startEnd}', '{functionName}'], ['start', $originalPhpFunctionName], $commentTemplate);
        $endStr     = str_replace(['{startEnd}', '{functionName}'], ['end', $originalPhpFunctionName], $commentTemplate);
        $startPos = mb_strpos($this->wrapperCode, $startStr);

        /*if ($startPos === FALSE) {
            echo "<h1>Comment containing '$startStr' not founded in wrapper code.</h1>";
            echo "<pre>{$this->wrapperCode}</pre>";
            die();
        }*/

        $endPos = mb_strpos($this->wrapperCode, $endStr, $startPos + mb_strlen($startStr));

        /*if ($endPos === FALSE) {
            echo "<h1>Comment containing '$endStr' not founded in wrapper code.</h1>";
            echo "<pre>{$this->wrapperCode}</pre>";
            die();
        }*/
        if ($startPos === FALSE || $endPos === FALSE) return;

        $this->wrapperCode = mb_substr(
            $this->wrapperCode,
            0,
            $startPos
        ) . "REMOVED: " . $originalPhpFunctionName . mb_substr(
            $this->wrapperCode,
            $endPos + mb_strlen($endStr)
        );
    }
}
Packager API Documentation API documentation generated by ApiGen