diff --git a/config/module.config.php b/config/module.config.php index 748c513..1944817 100644 --- a/config/module.config.php +++ b/config/module.config.php @@ -16,9 +16,11 @@ 'geoip' => 'ZfSnapGeoip\Service\Geoip', 'geoip_record' => 'ZfSnapGeoip\Entity\Record', 'geoip_hydrator' => 'Zend\Stdlib\Hydrator\ClassMethods', + 'ZfSnapGeoip\HttpClient\Adapter' => 'Zend\Http\Client\Adapter\Curl', ), 'factories' => array( 'ZfSnapGeoip\DatabaseConfig' => 'ZfSnapGeoip\DatabaseConfigFactory', + 'ZfSnapGeoip\HttpClient' => 'ZfSnapGeoip\HttpClientFactory', ), 'shared' => array( 'geoip_record' => false, diff --git a/src/ZfSnapGeoip/Controller/ConsoleController.php b/src/ZfSnapGeoip/Controller/ConsoleController.php index bb548dd..75f9234 100644 --- a/src/ZfSnapGeoip/Controller/ConsoleController.php +++ b/src/ZfSnapGeoip/Controller/ConsoleController.php @@ -8,6 +8,9 @@ namespace ZfSnapGeoip\Controller; +use Zend\Http\Client; +use Zend\Http\Request; +use Zend\Http\Response; use ZfSnapGeoip\DatabaseConfig; use Zend\Console\Adapter\AdapterInterface as Console; use Zend\Console\ColorInterface as Color; @@ -35,14 +38,28 @@ class ConsoleController extends AbstractActionController */ protected $config; + /** + * @var Zend\Http\Client + */ + protected $httpClient; + /** * @param Console $console * @param DatabaseConfig $config */ - public function __construct(Console $console, DatabaseConfig $config) + public function __construct(Console $console, DatabaseConfig $config, Client $httpClient) { $this->console = $console; $this->config = $config; + $this->setHttpClient($httpClient); + } + + /** + * @param \Zend\Http\Client $httpClient + */ + public function setHttpClient(Client $httpClient) + { + $this->httpClient = $httpClient; } /** @@ -72,24 +89,26 @@ public function downloadAction() return; } - $gzFilePath = $this->config->getPackedDatabasePath(); $source = $this->config->getSource(); $this->writeLine(sprintf('Downloading %s...', $source), Color::YELLOW); + + $this->httpClient->setUri($source); + $this->httpClient->setMethod(Request::METHOD_GET); + $response = $this->httpClient->send(); - if (!copy($source, $gzFilePath)) { + if ($response->getStatusCode() !== Response::STATUS_CODE_200) { $this->writeLine('Error during file download occured', Color::RED); return; } - $this->writeLine('Download completed', Color::GREEN); - $this->writeLine('Unzip the downloading data...', Color::YELLOW); - $events->trigger(__FUNCTION__ . '.pre', $this, array( - 'path' => $gzFilePath, + 'path' => $datFilePath, )); - system(sprintf('gunzip -f %s', $gzFilePath)); + $this->writeLine('Download completed', Color::GREEN); + $this->writeLine('Unzip the downloading data...', Color::YELLOW); + file_put_contents($datFilePath, gzdecode($response->getBody())); $events->trigger(__FUNCTION__ . '.post', $this, array( 'path' => $datFilePath, @@ -112,4 +131,5 @@ private function writeLine($text, $color = null, $bgColor = null) $this->console->writeLine($text, $color, $bgColor); } } -} \ No newline at end of file +} + diff --git a/src/ZfSnapGeoip/Controller/ConsoleControllerFactory.php b/src/ZfSnapGeoip/Controller/ConsoleControllerFactory.php index 7d13a51..8f2bf15 100644 --- a/src/ZfSnapGeoip/Controller/ConsoleControllerFactory.php +++ b/src/ZfSnapGeoip/Controller/ConsoleControllerFactory.php @@ -18,7 +18,8 @@ public function createService(ServiceLocatorInterface $serviceLocator) $serviceLocator = $serviceLocator->getServiceLocator(); $console = $serviceLocator->get('Console'); $config = $serviceLocator->get('ZfSnapGeoip\DatabaseConfig'); + $httpClient = $serviceLocator->get('ZfSnapGeoip\HttpClient'); - return new ConsoleController($console, $config); + return new ConsoleController($console, $config, $httpClient); } } \ No newline at end of file diff --git a/src/ZfSnapGeoip/DatabaseConfig.php b/src/ZfSnapGeoip/DatabaseConfig.php index 294d9ad..af5f2fc 100644 --- a/src/ZfSnapGeoip/DatabaseConfig.php +++ b/src/ZfSnapGeoip/DatabaseConfig.php @@ -51,9 +51,4 @@ public function getDatabasePath() { return $this->getDestination() . $this->getFilename(); } - - public function getPackedDatabasePath() - { - return $this->getDestination() . $this->getSourceBasename(); - } } \ No newline at end of file diff --git a/src/ZfSnapGeoip/HttpClientFactory.php b/src/ZfSnapGeoip/HttpClientFactory.php new file mode 100644 index 0000000..ac3bf5d --- /dev/null +++ b/src/ZfSnapGeoip/HttpClientFactory.php @@ -0,0 +1,18 @@ +setAdapter($serviceLocator->get('ZfSnapGeoip\HttpClient\Adapter')); + + return $client; + } +} \ No newline at end of file