From f5dbfcdf4b818b152833ddbf6b4216a29503d4ef Mon Sep 17 00:00:00 2001 From: Jon Day Date: Thu, 30 Oct 2014 11:24:32 +0000 Subject: [PATCH 1/8] fixing issues with download action after potential changes to geolite max mind server config --- .../Controller/ConsoleController.php | 27 +++++++++++++------ 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/src/ZfSnapGeoip/Controller/ConsoleController.php b/src/ZfSnapGeoip/Controller/ConsoleController.php index bb548dd..6350625 100644 --- a/src/ZfSnapGeoip/Controller/ConsoleController.php +++ b/src/ZfSnapGeoip/Controller/ConsoleController.php @@ -8,6 +8,9 @@ namespace ZfSnapGeoip\Controller; +use Zend\Filter\File\Rename; +use Zend\Http\Client; +use Zend\Http\Response; use ZfSnapGeoip\DatabaseConfig; use Zend\Console\Adapter\AdapterInterface as Console; use Zend\Console\ColorInterface as Color; @@ -72,24 +75,31 @@ public function downloadAction() return; } - $gzFilePath = $this->config->getPackedDatabasePath(); $source = $this->config->getSource(); $this->writeLine(sprintf('Downloading %s...', $source), Color::YELLOW); - if (!copy($source, $gzFilePath)) { + $adapter = new Client\Adapter\Socket(); + + $client = new Client(); + $client->setUri($source); + $client->setAdapter($adapter); + $client->setMethod(strtoupper('GET')); + $response = $client->send(); + + if ($response->getStatusCode() !== Response::STATUS_CODE_200) { $this->writeLine('Error during file download occured', Color::RED); return; + } else { + $events->trigger(__FUNCTION__ . '.pre', $this, array( + 'path' => $datFilePath, + )); + file_put_contents($datFilePath, gzdecode($response->getBody())); } $this->writeLine('Download completed', Color::GREEN); $this->writeLine('Unzip the downloading data...', Color::YELLOW); - $events->trigger(__FUNCTION__ . '.pre', $this, array( - 'path' => $gzFilePath, - )); - - system(sprintf('gunzip -f %s', $gzFilePath)); $events->trigger(__FUNCTION__ . '.post', $this, array( 'path' => $datFilePath, @@ -112,4 +122,5 @@ private function writeLine($text, $color = null, $bgColor = null) $this->console->writeLine($text, $color, $bgColor); } } -} \ No newline at end of file +} + From 3635df70af07493dddba344fa472c46d0484d6dd Mon Sep 17 00:00:00 2001 From: Jon Day Date: Thu, 30 Oct 2014 11:42:48 +0000 Subject: [PATCH 2/8] setting http method using constant rather than str --- src/ZfSnapGeoip/Controller/ConsoleController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ZfSnapGeoip/Controller/ConsoleController.php b/src/ZfSnapGeoip/Controller/ConsoleController.php index 6350625..414103b 100644 --- a/src/ZfSnapGeoip/Controller/ConsoleController.php +++ b/src/ZfSnapGeoip/Controller/ConsoleController.php @@ -84,7 +84,7 @@ public function downloadAction() $client = new Client(); $client->setUri($source); $client->setAdapter($adapter); - $client->setMethod(strtoupper('GET')); + $client->setMethod(Request::METHOD_GET); $response = $client->send(); if ($response->getStatusCode() !== Response::STATUS_CODE_200) { From c580d90e3386f703bd241cd5435636b4087797c9 Mon Sep 17 00:00:00 2001 From: Jon Day Date: Thu, 30 Oct 2014 21:52:19 +0000 Subject: [PATCH 3/8] changes as per pull request comments to ConsoleController, Factory and DatabaseConfig Classes --- .../Controller/ConsoleController.php | 33 ++++++++++++------- .../Controller/ConsoleControllerFactory.php | 5 ++- src/ZfSnapGeoip/DatabaseConfig.php | 5 --- 3 files changed, 25 insertions(+), 18 deletions(-) diff --git a/src/ZfSnapGeoip/Controller/ConsoleController.php b/src/ZfSnapGeoip/Controller/ConsoleController.php index 414103b..bce5e4c 100644 --- a/src/ZfSnapGeoip/Controller/ConsoleController.php +++ b/src/ZfSnapGeoip/Controller/ConsoleController.php @@ -8,7 +8,6 @@ namespace ZfSnapGeoip\Controller; -use Zend\Filter\File\Rename; use Zend\Http\Client; use Zend\Http\Response; use ZfSnapGeoip\DatabaseConfig; @@ -38,6 +37,11 @@ class ConsoleController extends AbstractActionController */ protected $config; + /** + * @var Zend\Http\Client + */ + protected $httpClient; + /** * @param Console $console * @param DatabaseConfig $config @@ -48,6 +52,14 @@ public function __construct(Console $console, DatabaseConfig $config) $this->config = $config; } + /** + * @param \Zend\Http\Client $httpClient + */ + public function setHttpClient(Client $httpClient) + { + $this->httpClient = $httpClient; + } + /** * {@inheritdoc} */ @@ -81,25 +93,22 @@ public function downloadAction() $adapter = new Client\Adapter\Socket(); - $client = new Client(); - $client->setUri($source); - $client->setAdapter($adapter); - $client->setMethod(Request::METHOD_GET); - $response = $client->send(); + $this->httpClient->setUri($source); + $this->httpClient->setMethod(Request::METHOD_GET); + $response = $this->httpClient->send(); if ($response->getStatusCode() !== Response::STATUS_CODE_200) { $this->writeLine('Error during file download occured', Color::RED); return; - } else { - $events->trigger(__FUNCTION__ . '.pre', $this, array( - 'path' => $datFilePath, - )); - file_put_contents($datFilePath, gzdecode($response->getBody())); } + $events->trigger(__FUNCTION__ . '.pre', $this, array( + 'path' => $datFilePath, + )); + $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, diff --git a/src/ZfSnapGeoip/Controller/ConsoleControllerFactory.php b/src/ZfSnapGeoip/Controller/ConsoleControllerFactory.php index 7d13a51..bf88241 100644 --- a/src/ZfSnapGeoip/Controller/ConsoleControllerFactory.php +++ b/src/ZfSnapGeoip/Controller/ConsoleControllerFactory.php @@ -10,6 +10,7 @@ use Zend\ServiceManager\FactoryInterface; use Zend\ServiceManager\ServiceLocatorInterface; +use Zend\Http\Client; class ConsoleControllerFactory implements FactoryInterface { @@ -18,7 +19,9 @@ public function createService(ServiceLocatorInterface $serviceLocator) $serviceLocator = $serviceLocator->getServiceLocator(); $console = $serviceLocator->get('Console'); $config = $serviceLocator->get('ZfSnapGeoip\DatabaseConfig'); + $client = new Client(); + $client->setAdapter(new Curl()); - return new ConsoleController($console, $config); + return new ConsoleController($console, $config, $client); } } \ 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 From b3dc6d4321580c0274582ab36679466729c271c0 Mon Sep 17 00:00:00 2001 From: Jon Day Date: Tue, 4 Nov 2014 09:19:42 +0000 Subject: [PATCH 4/8] injecting http client and providing factory for easily overriding adapter --- config/module.config.php | 1 + .../Controller/ConsoleController.php | 4 +--- .../Controller/ConsoleControllerFactory.php | 6 ++---- src/ZfSnapGeoip/HttpClientFactory.php | 18 ++++++++++++++++++ 4 files changed, 22 insertions(+), 7 deletions(-) create mode 100644 src/ZfSnapGeoip/HttpClientFactory.php diff --git a/config/module.config.php b/config/module.config.php index 748c513..7eaaabf 100644 --- a/config/module.config.php +++ b/config/module.config.php @@ -19,6 +19,7 @@ ), '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 bce5e4c..6703017 100644 --- a/src/ZfSnapGeoip/Controller/ConsoleController.php +++ b/src/ZfSnapGeoip/Controller/ConsoleController.php @@ -90,9 +90,7 @@ public function downloadAction() $source = $this->config->getSource(); $this->writeLine(sprintf('Downloading %s...', $source), Color::YELLOW); - - $adapter = new Client\Adapter\Socket(); - + $this->httpClient->setUri($source); $this->httpClient->setMethod(Request::METHOD_GET); $response = $this->httpClient->send(); diff --git a/src/ZfSnapGeoip/Controller/ConsoleControllerFactory.php b/src/ZfSnapGeoip/Controller/ConsoleControllerFactory.php index bf88241..8f2bf15 100644 --- a/src/ZfSnapGeoip/Controller/ConsoleControllerFactory.php +++ b/src/ZfSnapGeoip/Controller/ConsoleControllerFactory.php @@ -10,7 +10,6 @@ use Zend\ServiceManager\FactoryInterface; use Zend\ServiceManager\ServiceLocatorInterface; -use Zend\Http\Client; class ConsoleControllerFactory implements FactoryInterface { @@ -19,9 +18,8 @@ public function createService(ServiceLocatorInterface $serviceLocator) $serviceLocator = $serviceLocator->getServiceLocator(); $console = $serviceLocator->get('Console'); $config = $serviceLocator->get('ZfSnapGeoip\DatabaseConfig'); - $client = new Client(); - $client->setAdapter(new Curl()); + $httpClient = $serviceLocator->get('ZfSnapGeoip\HttpClient'); - return new ConsoleController($console, $config, $client); + return new ConsoleController($console, $config, $httpClient); } } \ No newline at end of file diff --git a/src/ZfSnapGeoip/HttpClientFactory.php b/src/ZfSnapGeoip/HttpClientFactory.php new file mode 100644 index 0000000..0ea9ca9 --- /dev/null +++ b/src/ZfSnapGeoip/HttpClientFactory.php @@ -0,0 +1,18 @@ +setAdapter(new Curl()); + + return $client; + } +} \ No newline at end of file From 207ec67f0da56b8a0cd7ff10f20569815a58ad37 Mon Sep 17 00:00:00 2001 From: Jon Day Date: Tue, 4 Nov 2014 10:16:40 +0000 Subject: [PATCH 5/8] adding client to constructor --- src/ZfSnapGeoip/Controller/ConsoleController.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/ZfSnapGeoip/Controller/ConsoleController.php b/src/ZfSnapGeoip/Controller/ConsoleController.php index 6703017..b88d6f7 100644 --- a/src/ZfSnapGeoip/Controller/ConsoleController.php +++ b/src/ZfSnapGeoip/Controller/ConsoleController.php @@ -46,10 +46,11 @@ class ConsoleController extends AbstractActionController * @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->httpClient = $httpClient; } /** From 2ddad65abf022d951e46b5a6aa34d51f8abbf917 Mon Sep 17 00:00:00 2001 From: Jon Day Date: Tue, 4 Nov 2014 10:17:47 +0000 Subject: [PATCH 6/8] adding request object to console controller --- src/ZfSnapGeoip/Controller/ConsoleController.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/ZfSnapGeoip/Controller/ConsoleController.php b/src/ZfSnapGeoip/Controller/ConsoleController.php index b88d6f7..cdc8901 100644 --- a/src/ZfSnapGeoip/Controller/ConsoleController.php +++ b/src/ZfSnapGeoip/Controller/ConsoleController.php @@ -9,6 +9,7 @@ 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; From 875cb60f895db0bed04ee065202241690ac0d69a Mon Sep 17 00:00:00 2001 From: Jon Day Date: Tue, 4 Nov 2014 10:20:47 +0000 Subject: [PATCH 7/8] test injecting adapter --- config/module.config.php | 1 + src/ZfSnapGeoip/HttpClientFactory.php | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/config/module.config.php b/config/module.config.php index 7eaaabf..1944817 100644 --- a/config/module.config.php +++ b/config/module.config.php @@ -16,6 +16,7 @@ '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', diff --git a/src/ZfSnapGeoip/HttpClientFactory.php b/src/ZfSnapGeoip/HttpClientFactory.php index 0ea9ca9..ac3bf5d 100644 --- a/src/ZfSnapGeoip/HttpClientFactory.php +++ b/src/ZfSnapGeoip/HttpClientFactory.php @@ -11,7 +11,7 @@ class HttpClientFactory implements FactoryInterface public function createService(ServiceLocatorInterface $serviceLocator) { $client = new Client(); - $client->setAdapter(new Curl()); + $client->setAdapter($serviceLocator->get('ZfSnapGeoip\HttpClient\Adapter')); return $client; } From d664ec10e93e1966169d05d6f5435eb4d0729795 Mon Sep 17 00:00:00 2001 From: Jon Day Date: Tue, 4 Nov 2014 10:31:54 +0000 Subject: [PATCH 8/8] use setter in constructor --- src/ZfSnapGeoip/Controller/ConsoleController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ZfSnapGeoip/Controller/ConsoleController.php b/src/ZfSnapGeoip/Controller/ConsoleController.php index cdc8901..75f9234 100644 --- a/src/ZfSnapGeoip/Controller/ConsoleController.php +++ b/src/ZfSnapGeoip/Controller/ConsoleController.php @@ -51,7 +51,7 @@ public function __construct(Console $console, DatabaseConfig $config, Client $ht { $this->console = $console; $this->config = $config; - $this->httpClient = $httpClient; + $this->setHttpClient($httpClient); } /**