コード例 #1
0
ファイル: LogfileCommand.php プロジェクト: mythquan/ua-parser
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     if (!$input->getOption('log-file') && !$input->getOption('log-dir')) {
         throw InvalidArgumentException::oneOfCommandArguments('log-file', 'log-dir');
     }
     $parser = Parser::create();
     $undefinedClients = array();
     /** @var $file SplFileInfo */
     foreach ($this->getFiles($input) as $file) {
         $path = $this->getPath($file);
         $lines = file($path);
         if (empty($lines)) {
             $output->writeln(sprintf('Skipping empty file "%s"', $file->getPathname()));
             $output->writeln('');
             continue;
         }
         $firstLine = reset($lines);
         $reader = AbstractReader::factory($firstLine);
         if (!$reader) {
             $output->writeln(sprintf('Could not find reader for file "%s"', $file->getPathname()));
             $output->writeln('');
             continue;
         }
         $output->writeln('');
         $output->writeln(sprintf('Analyzing "%s"', $file->getPathname()));
         $count = 1;
         $totalCount = count($lines);
         foreach ($lines as $line) {
             try {
                 $userAgentString = $reader->read($line);
             } catch (ReaderException $e) {
                 $count = $this->outputProgress($output, 'E', $count, $totalCount);
                 continue;
             }
             $client = $parser->parse($userAgentString);
             $result = $this->getResult($client);
             if ($result !== '.') {
                 $undefinedClients[] = json_encode(array($client->toString(), $userAgentString), JSON_UNESCAPED_SLASHES);
             }
             $count = $this->outputProgress($output, $result, $count, $totalCount);
         }
         $this->outputProgress($output, '', $count - 1, $totalCount, true);
         $output->writeln('');
     }
     $undefinedClients = $this->filter($undefinedClients);
     $fs = new Filesystem();
     $fs->dumpFile($input->getArgument('output'), join(PHP_EOL, $undefinedClients));
 }
コード例 #2
0
ファイル: Parser.php プロジェクト: krussll/4oWj37LzO9
 /**
  * Start up the parser by importing the json file to $this->regexes
  *
  * @param string|array $customRegexesFileOrArray
  * @throws FileNotFoundException
  */
 public function __construct($customRegexesFileOrArray = null)
 {
     if (is_string($customRegexesFileOrArray) || $customRegexesFileOrArray === null) {
         $regexesFile = $customRegexesFileOrArray !== null ? $customRegexesFileOrArray : static::getDefaultFile();
         if (file_exists($regexesFile)) {
             $this->regexes = (include $regexesFile);
         } elseif ($customRegexesFileOrArray !== null) {
             throw FileNotFoundException::customRegexFileNotFound($regexesFile);
         } else {
             throw FileNotFoundException::defaultFileNotFound(static::getDefaultFile());
         }
         trigger_error('Using the constructor is deprecated. Use Parser::create(string $file = null) instead', E_USER_DEPRECATED);
     } elseif (is_array($customRegexesFileOrArray)) {
         $this->regexes = $customRegexesFileOrArray;
     } else {
         throw InvalidArgumentException::unexpectedArgument('array', gettype($customRegexesFileOrArray), 0, __METHOD__);
     }
     $this->deviceParser = new DeviceParser($this->regexes);
     $this->operatingSystemParser = new OperatingSystemParser($this->regexes);
     $this->userAgentParser = new UserAgentParser($this->regexes);
 }