defaultCacheFolder = $defaultCacheFolder; parent::__construct(); } /** * @throws InvalidArgumentException */ protected function configure(): void { $this ->setName('browscap:parse') ->setDescription('Parses a user agent string and dumps the results.') ->addArgument( 'user-agent', InputArgument::REQUIRED, 'User agent string to analyze', null ) ->addOption( 'cache', 'c', InputOption::VALUE_OPTIONAL, 'Where the cache files are located', $this->defaultCacheFolder ); } /** * @throws InvalidArgumentException * @throws \InvalidArgumentException */ protected function execute(InputInterface $input, OutputInterface $output): int { $logger = LoggerHelper::createDefaultLogger($output); $cacheOption = $input->getOption('cache'); assert(is_string($cacheOption)); $adapter = new LocalFilesystemAdapter($cacheOption); $filesystem = new Filesystem($adapter); $cache = new SimpleCache( new Flysystem($filesystem) ); $browscap = new Browscap($cache, $logger); $uaArgument = $input->getArgument('user-agent'); assert(is_string($uaArgument)); try { $result = $browscap->getBrowser($uaArgument); } catch (Exception $e) { $logger->debug($e); return self::PARSER_ERROR; } try { $output->writeln(json_encode($result, JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR)); } catch (JsonException $e) { $logger->error($e); return self::PARSER_ERROR; } return self::SUCCESS; } }