dumpFile() public method

Atomically dumps content into a file.
public dumpFile ( string $filename, string $content, integer $mode = 438 )
$filename string The file to be written to.
$content string The data to write into the file.
$mode integer The file mode (octal). If null, file permissions are not modified Deprecated since version 2.3.12, to be removed in 3.0.
 /**
  * fetches a remote file and stores it into a local folder
  *
  * @param string $file       The name of the file where to store the remote content
  * @param string $remoteFile The code for the remote file to load
  *
  * @throws \BrowscapPHP\Exception\FetcherException
  * @throws \BrowscapPHP\Helper\Exception
  */
 public function fetch($file, $remoteFile = IniLoader::PHP_INI)
 {
     if (null === ($cachedVersion = $this->checkUpdate())) {
         // no newer version available
         return;
     }
     $this->getLogger()->debug('started fetching remote file');
     $uri = (new IniLoader())->setRemoteFilename($remoteFile)->getRemoteIniUrl();
     /** @var \Psr\Http\Message\ResponseInterface $response */
     $response = $this->getClient()->get($uri, ['connect_timeout' => $this->connectTimeout]);
     if ($response->getStatusCode() !== 200) {
         throw new FetcherException('an error occured while fetching remote data from URI ' . $uri . ': StatusCode was ' . $response->getStatusCode());
     }
     try {
         $content = $response->getBody()->getContents();
     } catch (\Exception $e) {
         throw new FetcherException('an error occured while fetching remote data', 0, $e);
     }
     if (empty($content)) {
         $error = error_get_last();
         throw FetcherException::httpError($uri, $error['message']);
     }
     $this->getLogger()->debug('finished fetching remote file');
     $this->getLogger()->debug('started storing remote file into local file');
     $content = $this->sanitizeContent($content);
     $converter = new Converter($this->getLogger(), $this->getCache());
     $iniVersion = $converter->getIniVersion($content);
     if ($iniVersion > $cachedVersion) {
         $fs = new Filesystem();
         $fs->dumpFile($file, $content);
     }
     $this->getLogger()->debug('finished storing remote file into local file');
 }
Example #2
0
 /**
  * fetches a remote file and stores it into a local folder
  *
  * @param string $file       The name of the file where to store the remote content
  * @param string $remoteFile The code for the remote file to load
  *
  * @throws \BrowscapPHP\Exception\FetcherException
  */
 public function fetch($file, $remoteFile = IniLoader::PHP_INI)
 {
     $this->getLoader()->setRemoteFilename($remoteFile)->setOptions($this->options)->setLogger($this->getLogger());
     $this->getLogger()->debug('started fetching remote file');
     $content = $this->getLoader()->load();
     if (false === $content) {
         $error = error_get_last();
         throw FetcherException::httpError($this->getLoader()->getRemoteIniUrl(), $error['message']);
     }
     $this->getLogger()->debug('finished fetching remote file');
     $this->getLogger()->debug('started storing remote file into local file');
     $content = $this->sanitizeContent($content);
     $fs = new Filesystem();
     $fs->dumpFile($file, $content);
     $this->getLogger()->debug('finished storing remote file into local file');
 }
Example #3
0
 /**
  * fetches a remote file and stores it into a local folder
  *
  * @param string $file       The name of the file where to store the remote content
  * @param string $remoteFile The code for the remote file to load
  *
  * @throws \BrowscapPHP\Exception\FetcherException
  * @throws \BrowscapPHP\Helper\Exception
  */
 public function fetch($file, $remoteFile = IniLoader::PHP_INI)
 {
     if (null === ($cachedVersion = $this->checkUpdate($remoteFile))) {
         // no newer version available
         return;
     }
     $this->getLoader()->setRemoteFilename($remoteFile)->setOptions($this->options)->setLogger($this->getLogger());
     $this->getLogger()->debug('started fetching remote file');
     try {
         $content = $this->getLoader()->load();
     } catch (Helper\Exception $e) {
         throw new FetcherException('an error occured while fetching remote data', 0, $e);
     }
     if (false === $content) {
         $error = error_get_last();
         throw FetcherException::httpError($this->getLoader()->getRemoteIniUrl(), $error['message']);
     }
     $this->getLogger()->debug('finished fetching remote file');
     $this->getLogger()->debug('started storing remote file into local file');
     $content = $this->sanitizeContent($content);
     $converter = new Converter($this->getLogger(), $this->getCache());
     $iniVersion = $converter->getIniVersion($content);
     if ($iniVersion > $cachedVersion) {
         $fs = new Filesystem();
         $fs->dumpFile($file, $content);
     }
     $this->getLogger()->debug('finished storing remote file into local file');
 }
Example #4
0
 /**
  * @param InputInterface  $input
  * @param OutputInterface $output
  *
  * @throws \UnexpectedValueException
  * @throws \BrowscapPHP\Exception\InvalidArgumentException
  * @return int|null|void
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     if (!$input->getOption('log-file') && !$input->getOption('log-dir')) {
         throw InvalidArgumentException::oneOfCommandArguments('log-file', 'log-dir');
     }
     $loggerHelper = new LoggerHelper();
     $logger = $loggerHelper->create($input->getOption('debug'));
     $browscap = new Browscap();
     $loader = new IniLoader();
     $collection = ReaderFactory::factory();
     $fs = new Filesystem();
     $browscap->setLogger($logger)->setCache($this->getCache($input));
     /** @var $file \Symfony\Component\Finder\SplFileInfo */
     foreach ($this->getFiles($input) as $file) {
         $this->uas = [];
         $path = $this->getPath($file);
         $this->countOk = 0;
         $this->countNok = 0;
         $logger->info('Analyzing file "' . $file->getPathname() . '"');
         $lines = file($path);
         if (empty($lines)) {
             $logger->info('Skipping empty file "' . $file->getPathname() . '"');
             continue;
         }
         $this->totalCount = count($lines);
         foreach ($lines as $line) {
             $this->handleLine($output, $collection, $browscap, $line);
         }
         $this->outputProgress($output, '', true);
         arsort($this->uas, SORT_NUMERIC);
         try {
             $fs->dumpFile($input->getArgument('output') . '/output.txt', implode(PHP_EOL, array_unique($this->undefinedClients)));
         } catch (IOException $e) {
             // do nothing
         }
         try {
             $fs->dumpFile($input->getArgument('output') . '/output-with-amount.txt', $this->createAmountContent());
         } catch (IOException $e) {
             // do nothing
         }
         try {
             $fs->dumpFile($input->getArgument('output') . '/output-with-amount-and-type.txt', $this->createAmountTypeContent());
         } catch (IOException $e) {
             // do nothing
         }
     }
     $outputFile = $input->getArgument('output') . '/output.txt';
     try {
         $fs->dumpFile($outputFile, implode(PHP_EOL, array_unique($this->undefinedClients)));
     } catch (IOException $e) {
         throw new \UnexpectedValueException('writing to file "' . $outputFile . '" failed', 0, $e);
     }
     try {
         $fs->dumpFile($input->getArgument('output') . '/output-with-amount.txt', $this->createAmountContent());
     } catch (IOException $e) {
         // do nothing
     }
     try {
         $fs->dumpFile($input->getArgument('output') . '/output-with-amount-and-type.txt', $this->createAmountTypeContent());
     } catch (IOException $e) {
         // do nothing
     }
 }