Example #1
0
 /**
  * Decode a compressed inventory file
  */
 public function decodeInventoryAction()
 {
     $input = $this->getRequest()->getParam('input_file');
     if (!is_file($input) or !is_readable($input)) {
         $model = new \Zend\Mvc\Console\View\ViewModel();
         $model->setErrorLevel(10);
         $model->setResult("Input file does not exist or is not readable.\n");
         return $model;
     }
     try {
         return $this->_inventoryDecode->filter(\Library\FileObject::fileGetContents($input));
     } catch (\InvalidArgumentException $e) {
         $model = new \Zend\Mvc\Console\View\ViewModel();
         $model->setErrorLevel(11);
         $model->setResult($e->getMessage() . "\n");
         return $model;
     }
 }
 /**
  * Upload a file
  *
  * @param string $fileName File name
  * @return \Zend\Http\Response Server response
  */
 public function uploadFile($fileName)
 {
     return $this->uploadData(\Library\FileObject::fileGetContents($fileName));
 }
Example #3
0
 /**
  * Write Package content
  *
  * @param array $data Package data
  * @param string $file Source file
  * @param bool $deleteSource Delete source file
  * @return integer Number of fragments
  */
 public function writeContent($data, $file, $deleteSource)
 {
     $id = $data['Id'];
     $baseName = $this->getPath($id) . "/{$id}-";
     $fileSize = @$data['Size'];
     $maxFragmentSize = @$data['MaxFragmentSize'] * 1024;
     // Kilobytes => Bytes
     if (!$data['FileLocation']) {
         // No file
         $numFragments = 0;
     } elseif ($fileSize == 0 or $maxFragmentSize == 0 or $fileSize <= $maxFragmentSize) {
         // Don't split, just copy/move/rename the file
         if ($deleteSource) {
             \Library\FileObject::rename($file, $baseName . '1');
         } else {
             \Library\FileObject::copy($file, $baseName . '1');
         }
         $numFragments = 1;
     } else {
         // Split file into fragments of nearly identical size no bigger than $maxFragmentSize.
         $fragmentSize = ceil($fileSize / ceil($fileSize / $maxFragmentSize));
         // Determine number of fragments by files actually written
         $numFragments = 0;
         $input = new \Library\FileObject($file, 'rb');
         while ($fragment = $input->fread($fragmentSize)) {
             $numFragments++;
             \Library\FileObject::filePutContents($baseName . $numFragments, $fragment);
         }
         unset($input);
         // Close file before eventually trying to delete it
         if ($deleteSource) {
             \Library\FileObject::unlink($file);
         }
     }
     return $numFragments;
 }
     if (in_array('--force', $_SERVER['argv'])) {
         $update = true;
     } else {
         // Read existing POT file into $oldPot
         $oldPot = \Library\FileObject::fileGetContentsAsArray($potFileName, FILE_IGNORE_NEW_LINES);
         // Skip to first message string (strip header and first empty line)
         $startPos = array_search('', $oldPot, true);
         if ($startPos === false) {
             print "WARNING: File {$potFileName} as unexpected content. Skipping.\n";
             continue;
         }
         $oldPot = array_slice($oldPot, $startPos + 1);
         $update = $newPot != $oldPot;
     }
     if ($update) {
         \Library\FileObject::FilePutContents($potFileName, sprintf($template, $module, implode("\n", $newPot)));
         print "Changes written to {$potFileName}.\n";
     } else {
         print "No changes detected for {$potFileName}.\n";
     }
 }
 // STAGE 2: Update .po files if necessary
 print "Updating .po files for {$module} module...";
 foreach (new \GlobIterator("{$translationPath}/*.po", \GlobIterator::CURRENT_AS_PATHNAME) as $poFileName) {
     $cmd = array('msgmerge', '--quiet', '--update', '--backup=off', '--sort-by-file', escapeshellarg($poFileName), escapeshellarg($potFileName));
     $cmd = implode(' ', $cmd);
     exec($cmd, $output, $result);
     if ($result) {
         print "ERROR: msgmerge returned with error code {$result}.\n";
         print "Command line was:\n\n";
         print "{$cmd}\n\n";
Example #5
0
 public function testRmdirErrorNotEmpty()
 {
     $dir = vfsStream::newDirectory('test')->at($this->_root);
     $dirname = $dir->url();
     $filename = vfsStream::newFile('test.txt')->at($dir)->url();
     try {
         FileObject::rmdir($dirname);
         $this->fail('Expected exception was not thrown');
     } catch (\RuntimeException $e) {
         $this->assertEquals("Error removing directory '{$dirname}'", $e->getMessage());
         $this->assertFileExists($filename);
     }
 }
Example #6
0
 /**
  * Write XML content to file
  *
  * This is a reimplementation of \DomDocument::save() with improved error
  * handling. An exception is thrown on error, and no file remains on disk.
  *
  * @param string $filename
  * @param integer $options
  * @return integer number of bytes written
  * @throws \RuntimeException if a write error occurs
  */
 public function save($filename, $options = 0)
 {
     $xml = $this->saveXml(null, $options);
     try {
         \Library\FileObject::filePutContents($filename, $xml);
     } catch (\Exception $e) {
         if (is_file($filename)) {
             unlink($filename);
         }
         throw $e;
     }
     return strlen($xml);
 }
 /**
  * Create a platform-specific archive if the source file is not already an
  * archive of the correct type
  *
  * This is currently only supported for the 'windows' platform which expects
  * a ZIP archive. If the Zip extension is not available, the source file is
  * assumed to be a ZIP archive and a warning is generated.
  *
  * The return value is the path to the archive file - either the source file
  * or a generated archive.
  *
  * @param array $data Package data
  * @param string $path Path where a new archive will be created
  * @param bool $deleteSource Delete source file after successfully creating an archive
  * @return string Path to archive file
  * @throws RuntimeException if an error occurs
  */
 public function autoArchive($data, $path, $deleteSource)
 {
     $source = $data['FileLocation'];
     if (!$source) {
         return $source;
     }
     $archiveManager = $this->_serviceManager->get('Library\\ArchiveManager');
     switch ($data['Platform']) {
         case 'windows':
             $type = \Library\ArchiveManager::ZIP;
             break;
         default:
             // other platforms not implemented yet
             return $source;
     }
     if (!$archiveManager->isSupported($type)) {
         trigger_error("Support for archive type '{$type}' not available. Assuming archive.", E_USER_NOTICE);
         return $source;
     }
     if ($archiveManager->isArchive($type, $source)) {
         // Already an archive of reqired type. Do nothing.
         return $source;
     }
     try {
         $filename = "{$path}/archive";
         $archive = $archiveManager->createArchive($type, $filename);
         $archiveManager->addFile($archive, $source, $data['FileName']);
         $archiveManager->closeArchive($archive);
         if ($deleteSource) {
             \Library\FileObject::unlink($source);
         }
     } catch (\Exception $e) {
         if (isset($archive)) {
             $archiveManager->closeArchive($archive, true);
             \Library\FileObject::unlink($filename);
         }
         throw new RuntimeException($e->getMessage(), (int) $e->getCode(), $e);
     }
     return $filename;
 }