rg\broker\customizations\ZipArchive::addDir PHP Method

addDir() public method

Add directories recursively
public addDir ( String $path )
$path String
    public function addDir($path)
    {
        $archiveDirName = str_replace($this->archiveBaseDir, "", $path);
        if (!empty($archiveDirName)) {
            $this->addEmptyDir(ltrim($archiveDirName, "/"));
        }
        $nodes = glob($path . '/*');
        foreach ($nodes as $node) {
            if (is_dir($node)) {
                foreach ($this->excludeDirectories as $dir) {
                    if (strpos($node, $dir) !== false) {
                        continue 2;
                    }
                }
                $this->addDir($node);
            } else {
                if (is_file($node)) {
                    $archiveFileName = str_replace($this->archiveBaseDir . "/", "", $node);
                    $this->addFile($node, $archiveFileName);
                }
            }
        }
    }

Usage Example

Example #1
0
 /**
  * @param string $cacheDir
  * @param string $targetDir
  * @param \Composer\Package\PackageInterface $package
  * @param \Symfony\Component\Console\Output\OutputInterface $output
  * @param \Composer\Util\ProcessExecutor $process
  * @return string
  * @throws \Exception
  */
 protected function createZipFile($cacheDir, $targetDir, \Composer\Package\PackageInterface $package, OutputInterface $output, \Composer\Util\ProcessExecutor $process)
 {
     $zipfileName = str_replace('/', '_', strtolower($package->getPrettyName()));
     if ($package->getDistType() === 'pear') {
         $rootPath = $cacheDir;
         $zipPath = escapeshellarg($package->getPrettyName());
     } else {
         if ($package->getTargetDir()) {
             $rootPath = $cacheDir . '/' . $package->getPrettyName() . '/' . $package->getTargetDir();
             $zipPath = '.';
         } else {
             $rootPath = $cacheDir . '/' . $package->getPrettyName();
             $zipPath = '.';
         }
     }
     $output->writeln('  - ' . $zipfileName . '.zip');
     if (!class_exists('ZipArchive')) {
         $command = 'cd ' . escapeshellarg($rootPath) . ' && zip -9 -r ' . escapeshellarg($targetDir . '/dists/' . $zipfileName . '.zip') . ' ' . $zipPath;
         $result = $process->execute($command);
         if ($result) {
             throw new \Exception('could not create dist package for ' . $package->getName());
         }
     } else {
         $zipFile = $targetDir . '/dists/' . $zipfileName . '.zip';
         $zipArchive = new ZipArchive();
         $zipArchive->addExcludeDirectory('.svn');
         $zipArchive->setArchiveBaseDir($rootPath);
         if (($result = $zipArchive->open($zipFile, ZipArchive::OVERWRITE)) === TRUE) {
             $zipArchive->addDir($rootPath);
             $zipArchive->close();
         } else {
             throw new \Exception('could not create dist package for ' . $package->getName() . ' error code: ' . $result);
         }
     }
     return $zipfileName;
 }