Author: Fabien Potencier (fabien@symfony.com)
Inheritance: extends Symfony\Component\Filesystem\Filesystem
Exemple #1
0
 /**
  * fetches a remote file, parses it and writes the result into the cache
  *
  * if the local stored information are in the same version as the remote data no actions are
  * taken
  *
  * @param string      $remoteFile The code for the remote file to load
  * @param string|null $buildFolder
  * @param int|null    $buildNumber
  *
  * @throws \BrowscapPHP\Exception\FileNotFoundException
  * @throws \BrowscapPHP\Helper\Exception
  * @throws \BrowscapPHP\Exception\FetcherException
  */
 public function update($remoteFile = IniLoader::PHP_INI, $buildFolder = null, $buildNumber = null)
 {
     $this->getLogger()->debug('started fetching remote file');
     $converter = new Converter($this->getLogger(), $this->getCache());
     if (class_exists('\\Browscap\\Browscap')) {
         $resourceFolder = 'vendor/browscap/browscap/resources/';
         if (null === $buildNumber) {
             $buildNumber = (int) file_get_contents('vendor/browscap/browscap/BUILD_NUMBER');
         }
         if (null === $buildFolder) {
             $buildFolder = 'resources';
         }
         $buildFolder .= '/browscap-ua-test-' . $buildNumber;
         $iniFile = $buildFolder . '/full_php_browscap.ini';
         mkdir($buildFolder, 0777, true);
         $writerCollectionFactory = new PhpWriterFactory();
         $writerCollection = $writerCollectionFactory->createCollection($this->getLogger(), $buildFolder);
         $buildGenerator = new BuildGenerator($resourceFolder, $buildFolder);
         $buildGenerator->setLogger($this->getLogger())->setCollectionCreator(new CollectionCreator())->setWriterCollection($writerCollection)->run($buildNumber, false);
         $converter->setVersion($buildNumber)->storeVersion()->convertFile($iniFile);
         $filesystem = new Filesystem();
         $filesystem->remove($buildFolder);
     } else {
         if (null === ($cachedVersion = $this->checkUpdate($remoteFile))) {
             // no newer version available
             return;
         }
         $this->getLoader()->setRemoteFilename($remoteFile)->setOptions($this->options)->setLogger($this->getLogger());
         try {
             $content = $this->getLoader()->load();
         } catch (Helper\Exception $e) {
             throw new FetcherException('an error occured while loading remote data', 0, $e);
         }
         if (false === $content) {
             $internalLoader = $this->getLoader()->getLoader();
             $error = error_get_last();
             throw FetcherException::httpError($internalLoader->getUri(), $error['message']);
         }
         $this->getLogger()->debug('finished fetching remote file');
         $this->storeContent($converter, $content, $cachedVersion);
     }
 }
 /**
  * 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');
 }
 /**
  * @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
     }
 }