示例#1
0
 /**
  * Copies a file using filter chains.
  * 
  * @param PhingFile $sourceFile
  * @param PhingFile $destFile
  * @param boolean $overwrite
  * @param boolean $preserveLastModified
  * @param array $filterChains 
  * @param Project $project
  * @return void
  */
 function copyFile(PhingFile $sourceFile, PhingFile $destFile, $overwrite = false, $preserveLastModified = true, &$filterChains = null, Project $project)
 {
     if ($overwrite || !$destFile->exists() || $destFile->lastModified() < $sourceFile->lastModified()) {
         if ($destFile->exists() && $destFile->isFile()) {
             $destFile->delete();
         }
         // ensure that parent dir of dest file exists!
         $parent = $destFile->getParentFile();
         if ($parent !== null && !$parent->exists()) {
             $parent->mkdirs();
         }
         if (is_array($filterChains) && !empty($filterChains)) {
             $in = self::getChainedReader(new BufferedReader(new FileReader($sourceFile)), $filterChains, $project);
             $out = new BufferedWriter(new FileWriter($destFile));
             // New read() methods returns a big buffer.
             while (-1 !== ($buffer = $in->read())) {
                 // -1 indicates EOF
                 $out->write($buffer);
             }
             if ($in !== null) {
                 $in->close();
             }
             if ($out !== null) {
                 $out->close();
             }
         } else {
             // simple copy (no filtering)
             $sourceFile->copyTo($destFile);
         }
         if ($preserveLastModified) {
             $destFile->setLastModified($sourceFile->lastModified());
         }
     }
 }
示例#2
0
 /**
  * @throws BuildException
  */
 public function main()
 {
     $this->checkPreconditions();
     try {
         $this->log('Building package: ' . $this->destinationFile->__toString(), Project::MSG_INFO);
         /*
          * Delete old package, if exists.
          */
         if ($this->destinationFile->exists()) {
             /*
              * TODO Check operation for errors...
              */
             $this->destinationFile->delete();
         }
         $phar = $this->buildPhar();
         $phar->startBuffering();
         $baseDirectory = realpath($this->baseDirectory->getPath());
         foreach ($this->filesets as $fileset) {
             foreach ($fileset as $realFileName) {
                 /*
                  * Calculate local file name.
                  */
                 $localFileName = $realFileName;
                 if (0 === strpos($realFileName, $baseDirectory)) {
                     $localFileName = substr($realFileName, strlen($baseDirectory));
                 }
                 $this->log('Adding ' . $realFileName . ' as ' . $localFileName . ' to package', Project::MSG_VERBOSE);
                 $phar->addFile($realFileName, $localFileName);
             }
         }
         $phar->stopBuffering();
     } catch (Exception $e) {
         throw new BuildException('Problem creating package: ' . $e->getMessage(), $e, $this->getLocation());
     }
 }
示例#3
0
 /**
  * @throws BuildException
  */
 public function main()
 {
     $this->checkPreconditions();
     try {
         $this->log('Building package: ' . $this->destinationFile->__toString(), Project::MSG_INFO);
         /*
          * Delete old package, if exists.
          */
         if ($this->destinationFile->exists()) {
             /*
              * TODO Check operation for errors...
              */
             $this->destinationFile->delete();
         }
         $phar = $this->buildPhar();
         $phar->startBuffering();
         $baseDirectory = realpath($this->baseDirectory->getPath());
         foreach ($this->filesets as $fileset) {
             $this->log('Adding specified files in ' . $fileset->getDir($this->project) . ' to package', Project::MSG_VERBOSE);
             $phar->buildFromIterator($fileset, $baseDirectory);
         }
         $phar->stopBuffering();
         /*
          * File compression, if needed.
          */
         if (Phar::NONE != $this->compression) {
             $phar->compressFiles($this->compression);
         }
     } catch (Exception $e) {
         throw new BuildException('Problem creating package: ' . $e->getMessage(), $e, $this->getLocation());
     }
 }
 /**
  * @throws BuildException
  */
 public function main()
 {
     $this->checkPreconditions();
     try {
         $this->log('Building archive: ' . $this->destinationFile->__toString(), Project::MSG_INFO);
         /**
          * Delete old archive, if exists.
          */
         if ($this->destinationFile->exists()) {
             $isDeleted = $this->destinationFile->delete();
             if (!$isDeleted) {
                 $this->log("Could not delete destination file {$this->destinationFile}", Project::MSG_WARN);
             }
         }
         $pharData = new PharData($this->baseDirectory->getPath() . '/' . $this->destinationFile->getName());
         foreach ($this->filesets as $fileset) {
             $this->log('Adding specified files in ' . $fileset->getDir($this->project) . ' to archive', Project::MSG_VERBOSE);
             $pharData->buildFromIterator($fileset->getIterator(), $fileset->getDir($this->project));
         }
         if ($this->compression !== PHAR::NONE && $pharData->canCompress($this->compression)) {
             try {
                 $pharData->compress($this->compression);
             } catch (UnexpectedValueException $uve) {
                 $pharData->compressFiles($this->compression);
             }
             unset($pharData);
         }
     } catch (Exception $e) {
         throw new BuildException('Problem creating archive: ' . $e->getMessage(), $e, $this->getLocation());
     }
 }
 public function testCorrectModeSet()
 {
     $this->executeTarget("test");
     $dir = new PhingFile(PHING_TEST_BASE . "/etc/regression/745/testdir");
     $mode = $dir->getMode() & 511;
     $this->assertEquals($mode, 511);
     $dir->delete(true);
 }
 /**
  * @return Phar
  */
 private function initPhar()
 {
     /**
      * Delete old package, if exists.
      */
     if ($this->destinationFile->exists()) {
         $this->destinationFile->delete();
     }
     $phar = $this->buildPhar();
     return $phar;
 }
示例#7
0
 /**
  * @throws BuildException
  */
 public function main()
 {
     $this->checkPreconditions();
     try {
         $this->log('Building package: ' . $this->destinationFile->__toString(), Project::MSG_INFO);
         /**
          * Delete old package, if exists.
          */
         if ($this->destinationFile->exists()) {
             /**
              * TODO Check operation for errors...
              */
             $this->destinationFile->delete();
         }
         $phar = $this->buildPhar();
         $phar->startBuffering();
         $baseDirectory = realpath($this->baseDirectory->getPath());
         foreach ($this->filesets as $fileset) {
             $this->log('Adding specified files in ' . $fileset->getDir($this->project) . ' to package', Project::MSG_VERBOSE);
             $phar->buildFromIterator($fileset, $baseDirectory);
         }
         $phar->stopBuffering();
         /**
          * File compression, if needed.
          */
         if (Phar::NONE != $this->compression) {
             $phar->compressFiles($this->compression);
         }
         if ($this->signatureAlgorithm == Phar::OPENSSL) {
             // Load up the contents of the key
             $keyContents = file_get_contents($this->key);
             // Attempt to load the given key as a PKCS#12 Cert Store first.
             if (openssl_pkcs12_read($keyContents, $certs, $this->keyPassword)) {
                 $private = openssl_pkey_get_private($certs['pkey']);
             } else {
                 // Fall back to a regular PEM-encoded private key.
                 // Setup an OpenSSL resource using the private key
                 // and tell the Phar to sign it using that key.
                 $private = openssl_pkey_get_private($keyContents, $this->keyPassword);
             }
             openssl_pkey_export($private, $pkey);
             $phar->setSignatureAlgorithm(Phar::OPENSSL, $pkey);
             // Get the details so we can get the public key and write that out
             // alongside the phar.
             $details = openssl_pkey_get_details($private);
             file_put_contents($this->destinationFile . '.pubkey', $details['key']);
         } else {
             $phar->setSignatureAlgorithm($this->signatureAlgorithm);
         }
     } catch (Exception $e) {
         throw new BuildException('Problem creating package: ' . $e->getMessage(), $e, $this->getLocation());
     }
 }
 /**
  * Copies a file using filter chains.
  *
  * @param  PhingFile $sourceFile
  * @param  PhingFile $destFile
  * @param  boolean   $overwrite
  * @param  boolean   $preserveLastModified
  * @param  array     $filterChains
  * @param  Project   $project
  * @param  integer   $mode
  * @param bool       $preservePermissions
  *
  * @throws Exception
  * @throws IOException
  * @return void
  */
 public function copyFile(PhingFile $sourceFile, PhingFile $destFile, $overwrite = false, $preserveLastModified = true, &$filterChains = null, Project $project, $mode = 0755, $preservePermissions = true)
 {
     if ($overwrite || !$destFile->exists() || $destFile->lastModified() < $sourceFile->lastModified()) {
         if ($destFile->exists() && $destFile->isFile()) {
             $destFile->delete();
         }
         // ensure that parent dir of dest file exists!
         $parent = $destFile->getParentFile();
         if ($parent !== null && !$parent->exists()) {
             // Setting source directory permissions to target
             // (On permissions preservation, the target directory permissions
             // will be inherited from the source directory, otherwise the 'mode'
             // will be used)
             $dirMode = $preservePermissions ? $sourceFile->getParentFile()->getMode() : $mode;
             $parent->mkdirs($dirMode);
         }
         if (is_array($filterChains) && !empty($filterChains)) {
             $in = self::getChainedReader(new BufferedReader(new FileReader($sourceFile)), $filterChains, $project);
             $out = new BufferedWriter(new FileWriter($destFile));
             // New read() methods returns a big buffer.
             while (-1 !== ($buffer = $in->read())) {
                 // -1 indicates EOF
                 $out->write($buffer);
             }
             if ($in !== null) {
                 $in->close();
             }
             if ($out !== null) {
                 $out->close();
             }
             // Set/Copy the permissions on the target
             if ($preservePermissions === true) {
                 $destFile->setMode($sourceFile->getMode());
             }
         } else {
             // simple copy (no filtering)
             $sourceFile->copyTo($destFile);
             // By default, PHP::Copy also copies the file permissions. Therefore,
             // re-setting the mode with the "user file-creation mask" information.
             if ($preservePermissions === false) {
                 $destFile->setMode(FileUtils::getDefaultFileCreationMask(false, true));
             }
         }
         if ($preserveLastModified && !$destFile->isLink()) {
             $destFile->setLastModified($sourceFile->lastModified());
         }
     }
 }
示例#9
0
 private function output($is_css, $data, $js)
 {
     if (!$is_css) {
         $folder = '/js/';
     } else {
         $folder = '/css/';
     }
     //Build path to output file
     $out = new PhingFile($this->assetDir . $folder . $js->file);
     //Check whether to overwrite files
     if ($out->exists() && !$js->overwrite) {
         throw new BuildException("Trying to write to " . $js->file . " but the overwrite flag has not been set to TRUE", $this->location);
     } else {
         if ($out->exists()) {
             $out->delete();
         }
     }
     //Write compressed JS to file
     file_put_contents($out->getAbsolutePath(), $data);
 }
示例#10
0
 /**
  * remove an array of files in a directory, and a list of subdirectories
  * which will only be deleted if 'includeEmpty' is true
  * @param PhingFile $d directory to work from
  * @param array &$files array of files to delete; can be of zero length
  * @param array &$dirs array of directories to delete; can of zero length
  */
 private function removeFiles(PhingFile $d, &$files, &$dirs)
 {
     if (count($files) > 0) {
         $this->log("Deleting " . count($files) . " files from " . $d->__toString());
         for ($j = 0, $_j = count($files); $j < $_j; $j++) {
             $f = new PhingFile($d, $files[$j]);
             $this->log("Deleting " . $f->getAbsolutePath(), $this->verbosity);
             try {
                 $f->delete();
             } catch (Exception $e) {
                 $message = "Unable to delete file " . $f->__toString() . ": " . $e->getMessage();
                 if ($this->failonerror) {
                     throw new BuildException($message);
                 } else {
                     $this->log($message, $this->quiet ? Project::MSG_VERBOSE : Project::MSG_WARN);
                 }
             }
         }
     }
     if (count($dirs) > 0 && $this->includeEmpty) {
         $dirCount = 0;
         for ($j = count($dirs) - 1; $j >= 0; --$j) {
             $dir = new PhingFile($d, $dirs[$j]);
             $dirFiles = $dir->listDir();
             if ($dirFiles === null || count($dirFiles) === 0) {
                 $this->log("Deleting " . $dir->__toString(), $this->verbosity);
                 try {
                     $dir->delete();
                     $dirCount++;
                 } catch (Exception $e) {
                     $message = "Unable to delete directory " . $dir->__toString();
                     if ($this->failonerror) {
                         throw new BuildException($message);
                     } else {
                         $this->log($message, $this->quiet ? Project::MSG_VERBOSE : Project::MSG_WARN);
                     }
                 }
             }
         }
         if ($dirCount > 0) {
             $this->log("Deleted {$dirCount} director" . ($dirCount == 1 ? "y" : "ies") . " from " . $d->__toString());
         }
     }
 }
示例#11
0
 /**
  * Attempts to rename a file from a source to a destination.
  * If overwrite is set to true, this method overwrites existing file
  * even if the destination file is newer.
  * Otherwise, the source f
  * ile is renamed only if the destination file #
  * is older than it.
  */
 private function renameFile(PhingFile $sourceFile, PhingFile $destFile, $overwrite)
 {
     $renamed = true;
     // ensure that parent dir of dest file exists!
     $parent = $destFile->getParentFile();
     if ($parent !== null) {
         if (!$parent->exists()) {
             $parent->mkdirs();
         }
     }
     if ($destFile->exists()) {
         try {
             $destFile->delete();
         } catch (Exception $e) {
             throw new BuildException("Unable to remove existing file " . $destFile->__toString() . ": " . $e->getMessage());
         }
     }
     $renamed = $sourceFile->renameTo($destFile);
     return $renamed;
 }