Esempio n. 1
0
 /**
  * Write data for key into cache
  *
  * @param string $key Identifier for the data
  * @param mixed $data Data to be cached
  * @return bool True if the data was successfully cached, false on failure
  */
 public function write($key, $data)
 {
     if ($data === '' || !$this->_init) {
         return false;
     }
     $key = $this->_key($key);
     if ($this->_setKey($key, true) === false) {
         return false;
     }
     $lineBreak = "\n";
     if ($this->_config['isWindows']) {
         $lineBreak = "\r\n";
     }
     if (!empty($this->_config['serialize'])) {
         if ($this->_config['isWindows']) {
             $data = str_replace('\\', '\\\\\\\\', serialize($data));
         } else {
             $data = serialize($data);
         }
     }
     $duration = $this->_config['duration'];
     $expires = time() + $duration;
     $contents = $expires . $lineBreak . $data . $lineBreak;
     if ($this->_config['lock']) {
         $this->_File->flock(LOCK_EX);
     }
     $this->_File->rewind();
     $success = $this->_File->ftruncate(0) && $this->_File->fwrite($contents) && $this->_File->fflush();
     if ($this->_config['lock']) {
         $this->_File->flock(LOCK_UN);
     }
     $this->_File = null;
     return $success;
 }
Esempio n. 2
0
 public function write($key, $data, $cache_duration)
 {
     if ($data === '' || is_null($data) || !$this->is_init) {
         return false;
     }
     if ($this->_openFile($key, true) === false) {
         return false;
     }
     $line_break = "\n";
     if ($this->settings['is_windows']) {
         $lineBreak = "\r\n";
     }
     if (!empty($this->settings['serialize'])) {
         if ($this->settings['is_windows']) {
             $data = str_replace('\\', '\\\\\\\\', json_encode($data));
         } else {
             $data = json_encode($data);
         }
     }
     $expires = time() + $cache_duration;
     $contents = $expires . $line_break . $data . $line_break;
     if ($this->settings['lock']) {
         $this->file->flock(LOCK_EX);
     }
     $this->file->rewind();
     $success = $this->file->ftruncate(0) && $this->file->fwrite($contents) && $this->file->fflush();
     if ($this->settings['lock']) {
         $this->file->flock(LOCK_UN);
     }
     return $success;
 }
 /**
  * Essentially, an "improved" tempnam().
  *
  * As compared to the core tempnam, this version allows you to add a file
  * extension, returns an SplFileObject and has a short hand for priming it
  * with some content. If content is written, the file is not rewound before
  * returning.
  *
  * @param  string|null    $content
  * @param  string         $extension
  * @return \SplFileObject
  */
 public function createFile($content = null, $extension = 'tmp')
 {
     $filename = $this->generateFilename($extension);
     if (file_exists($filename)) {
         throw FileAlreadyExistsException::create($filename);
     }
     $tempFile = new \SplFileObject($filename, 'x+');
     if ($content !== null) {
         $tempFile->fwrite($content);
         $tempFile->fflush();
     }
     return $tempFile;
 }
Esempio n. 4
0
 /**
  * Saves the current upload stream to designated target.
  *
  * @param {string} Target path to save the contents, cannot be a directory.
  * @param {?bool} True to append to target file, defaults to replace.
  * @return {boolean} True on success, false otherwise.
  */
 public function save($path, $append = false)
 {
     if (is_dir($path)) {
         $path = preg_replace('/\\' . preg_quote(DS) . '?$/', '$1/' . $this->getFilename(), $path);
     }
     $target = new \SplFileObject($path, $append ? 'a' : 'w');
     $this->rewind();
     while (!$this->eof()) {
         $target->fwrite($this->fread(4096));
     }
     $target->fflush();
     $target->rewind();
     return $target;
 }
Esempio n. 5
0
 /**
  * @inheritDoc
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $context = $this->getContainer()->get('router')->getContext();
     $context->setHost($input->getOption('host'));
     $urlCollector = $this->getContainer()->get('shop.url_collector');
     $iterator = $urlCollector->collect();
     if ($file = $input->getOption('output')) {
         $file = new \SplFileObject($file, 'w');
         foreach ($iterator as $item) {
             $file->fwrite($item . PHP_EOL);
         }
         $file->fflush();
     } else {
         foreach ($iterator as $item) {
             $output->writeln($item);
         }
     }
 }
Esempio n. 6
0
 /**
  * Post a file to myMemory
  *
  * Remove the first line from csv ( source and target )
  * and rewrite the csv because MyMemory doesn't want the header line
  *
  * @param            $file
  * @param            $key
  * @param bool|false $name
  *
  * @return Engines_Results_MyMemory_TmxResponse
  */
 public function glossaryImport($file, $key, $name = false)
 {
     try {
         $origFile = new SplFileObject($file, 'r+');
         $origFile->setFlags(SplFileObject::READ_CSV | SplFileObject::SKIP_EMPTY | SplFileObject::READ_AHEAD);
         $tmpFileName = tempnam("/tmp", 'GLOS');
         $newFile = new SplFileObject($tmpFileName, 'r+');
         $newFile->setFlags(SplFileObject::READ_CSV | SplFileObject::SKIP_EMPTY | SplFileObject::READ_AHEAD);
         foreach ($origFile as $line_num => $line) {
             if (count($line) < 2) {
                 throw new RuntimeException("No valid glossary file provided. Field separator could be not valid.");
             }
             if ($line_num == 0) {
                 list($source_lang, $target_lang, ) = $line;
                 //eventually, remove BOM from source language
                 $bom = pack('H*', 'EFBBBF');
                 $source_lang = preg_replace("/^{$bom}/", "", $source_lang);
                 if (!Langs_Languages::isEnabled($source_lang)) {
                     throw new RuntimeException("The source language specified in the glossary is not supported: " . $source_lang);
                 }
                 if (!Langs_Languages::isEnabled($target_lang)) {
                     throw new RuntimeException("The target language specified in the glossary is not supported: " . $target_lang);
                 }
                 if (empty($source_lang) || empty($target_lang)) {
                     throw new RuntimeException("No language definition found in glossary file.");
                 }
                 continue;
             }
             //copy stream to stream
             $newFile->fputcsv($line);
         }
         $newFile->fflush();
         $origFile = null;
         //close the file handle
         $newFile = null;
         //close the file handle
         copy($tmpFileName, $file);
         unlink($tmpFileName);
     } catch (RuntimeException $e) {
         $this->result = new Engines_Results_MyMemory_TmxResponse(array("responseStatus" => 406, "responseData" => null, "responseDetails" => $e->getMessage()));
         return $this->result;
     }
     $postFields = array('glossary' => "@" . realpath($file), 'source_lang' => $source_lang, 'target_lang' => $target_lang, 'name' => $name);
     $postFields['key'] = trim($key);
     $this->call("glossary_import_relative_url", $postFields, true);
     return $this->result;
 }
<?php

/*
 * test a successful flush
*/
$obj = new SplFileObject(dirname(__FILE__) . '/SplFileObject_testinput.csv');
var_dump($obj->fflush());
/*
 * test a unsuccessful flush
*/
//create a basic stream class
class VariableStream
{
    var $position;
    var $varname;
    function stream_open($path, $mode, $options, &$opened_path)
    {
        return true;
    }
    function url_stat()
    {
    }
}
stream_wrapper_register("SPLtest", "VariableStream");
$ftruncate_test = "";
//end creating stream
//open an SplFileObject using the above test stream
$obj = new SplFileObject("SPLtest://ftruncate_test");
var_dump($obj->fflush());
Esempio n. 8
0
 /**
  * Add backup process message
  *
  * @param string $message
  * @param bool|string $error Boolean or ERROR|WARNING|INFO|DEBUG
  *
  * @return Varien_Data_Form_Element_Abstract
  */
 public function addBackupProcessMessage($message, $error = false)
 {
     $message_type = $this->getMessageType($error);
     if ($message_type === self::LOG_LEVEL_WARNING) {
         $this->getSession()->setHasBackupWarnings(true);
     }
     $logLevel = $this->getLogLevel();
     if ($logLevel === self::LOG_LEVEL_OFF) {
         return $this;
     }
     if ($logLevel !== self::LOG_LEVEL_ALL) {
         if ($logLevel === self::LOG_LEVEL_WARNING && $message_type !== self::LOG_LEVEL_ERROR && $message_type !== self::LOG_LEVEL_WARNING) {
             return $this;
         } else {
             if ($logLevel === self::LOG_LEVEL_INFO && $message_type === self::LOG_LEVEL_DEBUG) {
                 return $this;
             }
         }
     }
     $content = $this->getBackupProcessMessage($message, $message_type);
     if (!($logFile = $this->getLogMessageFileName())) {
         Mage::logException(new Exception('Message "' . $content . '" is not written to log file'));
     } else {
         $file = new SplFileObject($logFile, 'a');
         $file->fwrite($content . PHP_EOL);
         $file->fflush();
         $file = null;
     }
     return $this;
 }