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: 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: 
<?php

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

class Packager_Common_StaticCopies extends Packager_Common_Base
{
    protected $staticallyCopiedFiles = [];

    private static $_isWindows = NULL;
    private static $_winDirsLinkItems = [];

    protected function cleanReleaseDir () {
        $releaseDir = $this->cfg->releaseDir;
        if (!is_dir($releaseDir))
            mkdir($releaseDir, 0777);
        $this->cfg->sourcesDir = str_replace('\\', '/', realpath($this->cfg->sourcesDir));
        $releaseDir = str_replace('\\', '/', realpath($releaseDir));
        $this->cfg->releaseDir = $releaseDir;
        clearstatcache();
        $rdi = new \RecursiveDirectoryIterator($releaseDir);
        $rii = new \RecursiveIteratorIterator($rdi);
        $dirsFullPaths = [];
        foreach ($rii as $item) {
            /** @var $item \SplFileInfo */
            $path = str_replace('\\', '/', $item->getPath());
            $fileName = $item->getFilename();
            $parentDir = $fileName == '..';
            if ($parentDir) continue;
            $isDir = $fileName == '.';
            $baseDir = $isDir && $releaseDir === $path;
            if ($baseDir) continue;
            if ($isDir) {
                $dirsFullPaths[$path] = TRUE;
                continue;
            }
            $fullPath = $path . '/' . $fileName;
            unlink($fullPath);
            clearstatcache(TRUE, $fullPath);
            if (file_exists($fullPath)) {
                $this->sendResult(
                    "Unable to remove a file to clean whole release directory first:", 
                    "Full path to the file:<br /><br />'$fullPath'", 
                    'error'
                );
            }
        }
        $dirsFullPaths = array_keys($dirsFullPaths);
        rsort($dirsFullPaths);
        foreach ($dirsFullPaths as $dirFullPath) {
            rmdir($dirFullPath);
            clearstatcache(TRUE, $dirFullPath);
            if (file_exists($dirFullPath)) {
                $this->sendResult(
                    "Unable to remove a directory to clean whole release directory first:", 
                    "Full path to the directory:<br /><br />'$dirFullPath'", 
                    'error'
                );
            }
        }
        sort($this->staticallyCopiedFiles);
    }
    protected function copyStaticFilesAndFolders () {
        if (!$this->cfg->staticCopies) return;
        try {
            $sourcesDir = $this->cfg->sourcesDir;
            $releaseDir = $this->cfg->releaseDir;
            $sourcesDirLength = mb_strlen($sourcesDir);
            foreach ($this->cfg->staticCopies as $key => $value) {
                $source = $sourcesDir . (is_numeric($key) ? $value : $key);
                $destination = $releaseDir . $value;
                $i = 0;
                while ($i < 5) {
                    $sourceSpl = new \SplFileInfo($source);
                    if (self::_isLink($sourceSpl)) {
                        $source = self::_readLink($sourceSpl);
                    } else {
                        break;
                    }
                    $i++;
                }
                if (is_dir($source)) {
                    //var_dump(['dir', $source, $destination]);
                    $this->_copyDirectoryRecursively($source, $destination);
                } else if (is_file($source)) {
                    //var_dump(['file', $source, $destination]);
                    $this->_copyFile($source, $destination, mb_substr($source, $sourcesDirLength));
                }
            }
        } catch (\Exception $e) {
            $this->sendResult(
                "Unable to copy a file/directory into release directory:", 
                $e->getMessage(), 'error'
            );
        }
    }
    private function _copyDirectoryRecursively ($sourceDirFullPath, $destinationBaseDirFullPath) {
        $rdi = new \RecursiveDirectoryIterator(
            $sourceDirFullPath,
            \FilesystemIterator::FOLLOW_SYMLINKS
        );
        $rii = new \RecursiveIteratorIterator($rdi);
        $dirsFullPaths = [];
        $filesFullPaths = [];
        foreach ($rii as $item) {
            /** @var $item \SplFileInfo */
            $path = str_replace('\\', '/', $item->getPath());
            $isDir = $item->isDir();
            $baseDir = $isDir && $sourceDirFullPath === $path;
            if ($baseDir) continue;
            if ($isDir) {
                $dirsFullPaths[$path] = TRUE;
            } else {
                $filesFullPaths[$path . '/' . $item->getFilename()] = TRUE;
            }
        }
        $dirsFullPaths = array_keys($dirsFullPaths);
        $filesFullPaths = array_keys($filesFullPaths);
        sort($dirsFullPaths);
        rsort($filesFullPaths);
        $sourceDirFullPathLength = mb_strlen($sourceDirFullPath);
        $sourceBaseDirFullPathLength = mb_strlen($this->cfg->sourcesDir);
        foreach ($dirsFullPaths as $dirsFullPath) {
            $sourceRelativePath = mb_substr($dirsFullPath, $sourceDirFullPathLength);
            $destinationDirFullPath = $destinationBaseDirFullPath . $sourceRelativePath;
            //var_dump(['dir-dir', $dirsFullPath, $destinationDirFullPath]);
            mkdir($destinationDirFullPath, 640, TRUE);
        }
        foreach ($filesFullPaths as $fileFullPath) {
            $sourceRelativePath = mb_substr($fileFullPath, $sourceDirFullPathLength);
            $destinationFileFullPath = $destinationBaseDirFullPath . $sourceRelativePath;
            //var_dump(['dir-file', $filesFullPath, $destinationFileFullPath]);
            $this->_copyFile($fileFullPath, $destinationFileFullPath, mb_substr($fileFullPath, $sourceBaseDirFullPathLength));
        }
    }
    private function _copyFile ($sourceFileFullPath, $destinationFileFullPath, $sourceFileRelPath) {
        $destinationDirFullPath = dirname($destinationFileFullPath);
        if (!is_dir($destinationDirFullPath))
            mkdir($destinationDirFullPath, 640, TRUE);
        $success = copy($sourceFileFullPath, $destinationFileFullPath);
        if ($success) {
            $this->staticallyCopiedFiles[] = $sourceFileRelPath;
        } else {
            $this->sendResult(
                "Unable to copy a file into release directory:", 
                "Source file full path:<br /><br />'$sourceFileFullPath'<br /><br /><br />"
                ."Destination file full path:<br /><br />'$destinationFileFullPath'", 
                'error'
            );
        }
    }
    private static function _isLink (\SplFileInfo $spl) {
        $splFileName = $spl->getFilename();
        if (self::_isWindows()) {
            $dirPath = str_replace('\\', '/', $spl->getPath());
            $linkItems = self::_getItemDirWinSymLinks($dirPath);
            return isset($linkItems[$splFileName]);
        } else {
            return is_link($spl->getPath() . '/' . $splFileName);
        }
    }
    private static function _readLink (\SplFileInfo $spl) { 
        $splFileName = $spl->getFilename();
        if (self::_isWindows()) {
            $dirPath = str_replace('\\', '/', $spl->getPath());
            $linkItems = self::_getItemDirWinSymLinks($dirPath);
            return $linkItems[$splFileName];
        } else {
            return readlink($spl->getPath() . '/' . $splFileName);
        }
    }
    private static function _isWindows () {
        if (self::$_isWindows === NULL) {
            if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
                self::$_isWindows = TRUE;
            } else {
                self::$_isWindows = FALSE;
            }
        }
        return self::$_isWindows;
    }
    private static function _getItemDirWinSymLinks ($dirPath) {
        if (isset(self::$_winDirsLinkItems[$dirPath])) {
            $linkItems = self::$_winDirsLinkItems[$dirPath];
        } else {
            $linkItems = [];
            $sysOut = self::_system('dir /A:L', $dirPath);
            if ($sysOut !== FALSE) {
                $sysOutLines = explode(PHP_EOL, $sysOut);
                foreach ($sysOutLines as $sysOutLine) {
                    if (mb_strlen($sysOutLine) === 0) continue;
                    $firstChar = mb_substr($sysOutLine, 0, 1);
                    if ($firstChar === ' ') continue;
                    $fileNameWithTarget = mb_substr($sysOutLine, 36);
                    $fileNameWithTargetLength = mb_strlen($fileNameWithTarget);
                    $fileNameEndPos = mb_strrpos($fileNameWithTarget, ' [');
                    if ($fileNameEndPos === FALSE) {
                        $fileNameEndPos = mb_strlen($fileNameWithTarget);
                    } else {
                        $fileNameEndPos += 2;
                    }
                    $fileName = mb_substr($fileNameWithTarget, 0, $fileNameEndPos - 2);
                    $target = str_replace('\\', '/', 
                        mb_substr(
                            $fileNameWithTarget, 
                            $fileNameEndPos, 
                            $fileNameWithTargetLength - $fileNameEndPos - 1
                        )
                    );
                    $linkItems[$fileName] = $target;
                }
            }
            self::$_winDirsLinkItems[$dirPath] = $linkItems;
        }
        return $linkItems;
    }
    private static function _system ($cmd, $dirPath = NULL) {
        if (!function_exists('system')) return FALSE;
        $dirPathPresented = $dirPath !== NULL && mb_strlen($dirPath) > 0;
        $cwd = '';
        if ($dirPathPresented) {
            $cwd = getcwd();
            chdir($dirPath);
        }
        ob_start();
        system($cmd);
        $sysOut = ob_get_clean();
        if ($dirPathPresented) chdir($cwd);
        return $sysOut;
    }
}
Packager API Documentation API documentation generated by ApiGen