Skip to content

Commit

Permalink
Adding fetch command
Browse files Browse the repository at this point in the history
  • Loading branch information
lstrojny committed Nov 10, 2013
1 parent 29d97db commit c1fea65
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 68 deletions.
1 change: 1 addition & 0 deletions bin/uaparser.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@
$application->add(new UpdateCommand(realpath(__DIR__ . '/../resources')));
$application->add(new ParserCommand());
$application->add(new LogfileCommand());
$application->add(new FetchCommand());

$application->run();
39 changes: 39 additions & 0 deletions src/UAParser/Command/FetchCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
/**
* ua-parser
*
* Copyright (c) 2011-2012 Dave Olsen, http://dmolsen.com
*
* Released under the MIT license
*/
namespace UAParser\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Filesystem\Filesystem;
use UAParser\Util\Fetcher;

class FetchCommand extends Command
{
protected function configure()
{
$this
->setName('ua-parser:update')
->setDescription('Fetches an updated YAML file for ua-parser and overwrites the current JSON file.')
->addArgument(
'file',
InputArgument::REQUIRED,
'regexes.yaml output file'
)
;
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$fs = new Filesystem();
$fetcher = new Fetcher();
$fs->dumpFile($input->getArgument('file'), $fetcher->fetch());
}
}
10 changes: 4 additions & 6 deletions src/UAParser/Command/UpdateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use UAParser\Util\Converter;
use UAParser\Util\Updater;
use UAParser\Util\Fetcher;

class UpdateCommand extends Command
{
Expand Down Expand Up @@ -42,11 +42,9 @@ protected function configure()

protected function execute(InputInterface $input, OutputInterface $output)
{
$this->getUpdater()->update($input->getOption('no-backup'));
}
$fetcher = new Fetcher();
$converter = new Converter($this->resourceDirectory);

private function getUpdater()
{
return new Updater(new Converter($this->resourceDirectory));
$converter->convertString($fetcher->fetch(), $input->getOption('no-backup'));
}
}
2 changes: 1 addition & 1 deletion src/UAParser/Util/Converter.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Converter
private $fs;

/**
* @param strin $destination
* @param string $destination
* @param Filesystem $fs
*/
public function __construct($destination, Filesystem $fs = null)
Expand Down
17 changes: 3 additions & 14 deletions src/UAParser/Util/Updater.php → src/UAParser/Util/Fetcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,9 @@
*/
namespace UAParser\Util;

class Updater
class Fetcher
{
/** @var Converter */
private $converter;

public function __construct(Converter $converter)
{
$this->converter = $converter;
}

public function update($backupBeforeOverride = true)
public function fetch()
{
$context = stream_context_create(
array(
Expand All @@ -32,9 +24,6 @@ public function update($backupBeforeOverride = true)
)
);

$this->converter->convertString(
file_get_contents('https://raw.github.com/tobie/ua-parser/master/regexes.yaml', null, $context),
$backupBeforeOverride
);
return file_get_contents('https://raw.github.com/tobie/ua-parser/master/regexes.yaml', null, $context);
}
}
32 changes: 32 additions & 0 deletions tests/UAParser/Tests/Util/FetcherTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
/**
* ua-parser
*
* Copyright (c) 2011-2012 Dave Olsen, http://dmolsen.com
*
* Released under the MIT license
*/
namespace UAParser\Tests\Util;

use PHPUnit_Framework_TestCase as AbstractTestCase;
use UAParser\Util\Fetcher;

/**
* @group online
*/
class FetcherTest extends AbstractTestCase
{

/** @var Fetcher */
private $fetcher;

public function setUp()
{
$this->fetcher = new Fetcher();
}

public function testFetch()
{
$this->assertInternalType('string', $this->fetcher->fetch());
}
}
2 changes: 1 addition & 1 deletion tests/UAParser/Tests/Util/Logfile/AbstractReaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function testTestRealLine()
public function testReadEmptyLine()
{
$this->setExpectedException(
'UAParser\Exception\ParserException',
'UAParser\Exception\ReaderException',
'Cannot extract user agent string from line "invalid"'
);
$this->reader->read('invalid');
Expand Down
46 changes: 0 additions & 46 deletions tests/UAParser/Tests/Util/UpdaterTest.php

This file was deleted.

0 comments on commit c1fea65

Please sign in to comment.