diff --git a/.travis.yml b/.travis.yml index 04c26d8..e506a43 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,6 +3,8 @@ language: php php: - 5.3 - 5.4 + - 5.5 + - 5.6 before_script: - wget http://getcomposer.org/composer.phar diff --git a/Module.php b/Module.php index 6a55aa6..109118f 100644 --- a/Module.php +++ b/Module.php @@ -1,65 +1,3 @@ - */ - -namespace ZfSnapGeoip; - -use Zend\Console\Adapter\AdapterInterface; -use Zend\EventManager\EventInterface; -use Zend\ModuleManager\Feature\ConsoleUsageProviderInterface as ConsoleUsage; -use Zend\ModuleManager\Feature\ConfigProviderInterface as Config; -use Zend\ModuleManager\Feature\AutoloaderProviderInterface as Autoloader; -use Zend\ModuleManager\Feature\BootstrapListenerInterface as BootstrapListener; - -class Module implements ConsoleUsage, Config, Autoloader, BootstrapListener -{ - const CONSOLE_GEOIP_DOWNLOAD = 'geoip download [--no-clobber] [-q]'; - - public function getConfig() - { - return include __DIR__ . '/config/module.config.php'; - } - - public function getAutoloaderConfig() - { - return array( - 'Zend\Loader\StandardAutoloader' => array( - 'namespaces' => array( - __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__, - ), - ), - ); - } - - /** - * @param \Zend\Console\Adapter\AdapterInterface $console - * @return array - * - * @SuppressWarnings(PHPMD.UnusedFormalParameter) - */ - public function getConsoleUsage(AdapterInterface $console) - { - return array( - 'Manage GeoIP database', - self::CONSOLE_GEOIP_DOWNLOAD => 'Downloads the newest GeoIP db', - array('--no-clobber', 'Don\'t overwrite an existing db file'), - array('-q', 'Turn off output'), - ); - } - - /** - * @param \Zend\EventManager\EventInterface $e - */ - public function onBootstrap(EventInterface $e) - { - $serviceManager = $e->getParam('application')->getServiceManager(); - $config = $serviceManager->get('config'); - - require_once $config['maxmind']['timezone_function_path']; - } - -} \ No newline at end of file +require __DIR__ . '/src/ZfSnapGeoip/Module.php'; diff --git a/README.md b/README.md index 029f429..234fdfc 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ ZfSnapGeoip [![Build Status](https://travis-ci.org/snapshotpl/ZfSnapGeoip.png?br Maxmind Geoip module for Zend Framework 2 -Version 2.3.0 Created by Witold Wasiczko +Version 2.4.0 Created by Witold Wasiczko Usage ----- @@ -63,8 +63,8 @@ Module supports `\Zend\EventManager`. Class | Event name | Description | Params --- | --- | --- | --- -ZfSnapGeoip\Controller\ConsoleController | downloadAction.exists | If no-clobber is enabled and file exists | path -ZfSnapGeoip\Controller\ConsoleController | downloadAction.pre | Before unzip file | path (to gzip file) +ZfSnapGeoip\Controller\ConsoleController | downloadAction.exists | If no-clobber is enabled and file exists | path (to dat file) +ZfSnapGeoip\Controller\ConsoleController | downloadAction.pre | Before unzip file | path (to dat file), response (gziped response object) ZfSnapGeoip\Controller\ConsoleController | downloadAction.post | After unzip file | path (to dat file) ZfSnapGeoip\Service\Geoip | getIp | After read IP | ip (ip address) ZfSnapGeoip\Service\Geoip | getRecord | After created record | record (instance of ZfSnapGeoip\Entity\RecordInterface) diff --git a/config/module.config.php b/config/module.config.php index 1944817..b2d154c 100644 --- a/config/module.config.php +++ b/config/module.config.php @@ -1,50 +1,50 @@ array( + 'maxmind' => array( 'database' => array( - 'source' => 'http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz', + 'source' => 'http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz', 'destination' => __DIR__ . '/../data/', - 'filename' => 'GeoLiteCity.dat', - 'flag' => GEOIP_STANDARD, - 'regionvars' => __DIR__ . '/../../../geoip/geoip/src/geoipregionvars.php', + 'filename' => 'GeoLiteCity.dat', + 'flag' => GEOIP_STANDARD, + 'regionvars' => __DIR__ . '/../../../geoip/geoip/src/geoipregionvars.php', ), 'timezone_function_path' => __DIR__ . '/../../../geoip/geoip/src/timezone.php', ), 'service_manager' => array( 'invokables' => array( - 'geoip' => 'ZfSnapGeoip\Service\Geoip', - 'geoip_record' => 'ZfSnapGeoip\Entity\Record', + '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( + 'factories' => array( 'ZfSnapGeoip\DatabaseConfig' => 'ZfSnapGeoip\DatabaseConfigFactory', 'ZfSnapGeoip\HttpClient' => 'ZfSnapGeoip\HttpClientFactory', ), - 'shared' => array( + 'shared' => array( 'geoip_record' => false, ), ), - 'view_helpers' => array( + 'view_helpers' => array( 'invokables' => array( 'geoip' => 'ZfSnapGeoip\View\Helper\Geoip', ), ), - 'controllers' => array( + 'controllers' => array( 'factories' => array( 'ZfSnapGeoip\Controller\Console' => 'ZfSnapGeoip\Controller\ConsoleControllerFactory', ), ), - 'console' => array( + 'console' => array( 'router' => array( 'routes' => array( 'geoip-download' => array( 'options' => array( - 'route' => ZfSnapGeoip\Module::CONSOLE_GEOIP_DOWNLOAD, + 'route' => ZfSnapGeoip\Module::CONSOLE_GEOIP_DOWNLOAD, 'defaults' => array( 'controller' => 'ZfSnapGeoip\Controller\Console', - 'action' => 'download', + 'action' => 'download', ) ) ) diff --git a/src/ZfSnapGeoip/Composer/ScriptHandler.php b/src/ZfSnapGeoip/Composer/ScriptHandler.php index 95290a6..0090511 100644 --- a/src/ZfSnapGeoip/Composer/ScriptHandler.php +++ b/src/ZfSnapGeoip/Composer/ScriptHandler.php @@ -1,15 +1,14 @@ */ - -namespace ZfSnapGeoip\Composer; - -use Composer\Script\Event; - class ScriptHandler { const ZEND_INDEX_PATH = 'zend-index-path'; @@ -21,12 +20,12 @@ class ScriptHandler */ public static function downloadData(Event $event) { - $defaultOptions = array( + $defaultOptions = array( self::ZEND_INDEX_PATH => 'public/index.php', ); - $newOptions = $event->getComposer()->getPackage()->getExtra(); - $options = array_merge($defaultOptions, $newOptions); - $zendIndexPath = $options[self::ZEND_INDEX_PATH]; + $newOptions = $event->getComposer()->getPackage()->getExtra(); + $options = array_merge($defaultOptions, $newOptions); + $zendIndexPath = $options[self::ZEND_INDEX_PATH]; $geoipDownloadCmd = 'geoip download'; if ($event->getName() === 'post-install-cmd') { @@ -35,4 +34,5 @@ public static function downloadData(Event $event) $cmd = sprintf('php %s %s', $zendIndexPath, $geoipDownloadCmd); system($cmd); } + } diff --git a/src/ZfSnapGeoip/Controller/ConsoleController.php b/src/ZfSnapGeoip/Controller/ConsoleController.php index 75f9234..d3bbb76 100644 --- a/src/ZfSnapGeoip/Controller/ConsoleController.php +++ b/src/ZfSnapGeoip/Controller/ConsoleController.php @@ -1,11 +1,5 @@ - */ - namespace ZfSnapGeoip\Controller; use Zend\Http\Client; @@ -19,6 +13,11 @@ use Zend\Stdlib\RequestInterface; use Zend\Stdlib\ResponseInterface; +/** + * Console Controller + * + * @author Witold Wasiczko + */ class ConsoleController extends AbstractActionController { /** @@ -39,23 +38,24 @@ class ConsoleController extends AbstractActionController protected $config; /** - * @var Zend\Http\Client + * @var Client */ - protected $httpClient; + protected $httpClient; /** * @param Console $console * @param DatabaseConfig $config + * @param Client $httpClient */ public function __construct(Console $console, DatabaseConfig $config, Client $httpClient) { $this->console = $console; - $this->config = $config; + $this->config = $config; $this->setHttpClient($httpClient); } /** - * @param \Zend\Http\Client $httpClient + * @param Client $httpClient */ public function setHttpClient(Client $httpClient) { @@ -79,42 +79,52 @@ public function dispatch(RequestInterface $request, ResponseInterface $response public function downloadAction() { $datFilePath = $this->config->getDatabasePath(); - $events = $this->getEventManager(); + $events = $this->getEventManager(); if ($this->getRequest()->getParam('no-clobber') && is_file($datFilePath)) { $events->trigger(__FUNCTION__ . '.exists', $this, array( 'path' => $datFilePath, )); - $this->writeline('Database already exist. Skipping...', Color::RED); + $this->writeline('Database already exist. Skipping...', Color::LIGHT_RED); return; } - - $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(); + $response = $this->getDbResponse(); if ($response->getStatusCode() !== Response::STATUS_CODE_200) { - $this->writeLine('Error during file download occured', Color::RED); + $this->writeLine('Error during file download occured', Color::LIGHT_RED); return; } $events->trigger(__FUNCTION__ . '.pre', $this, array( 'path' => $datFilePath, + 'response' => $response, )); - $this->writeLine('Download completed', Color::GREEN); - $this->writeLine('Unzip the downloading data...', Color::YELLOW); + $this->writeLine('Download completed', Color::LIGHT_GREEN); + $this->writeLine('Unzip the downloading data...', Color::LIGHT_YELLOW); + file_put_contents($datFilePath, gzdecode($response->getBody())); $events->trigger(__FUNCTION__ . '.post', $this, array( 'path' => $datFilePath, )); - $this->writeLine(sprintf('Unzip completed (%s)', $datFilePath), Color::GREEN); + $this->writeLine(sprintf('Unzip completed (%s)', $datFilePath), Color::LIGHT_GREEN); + } + + /** + * @return Response + */ + public function getDbResponse() + { + $source = $this->config->getSource(); + + $this->writeLine(sprintf('Downloading %s...', $source), Color::LIGHT_YELLOW); + + $this->httpClient->setUri($source); + $this->httpClient->setMethod(Request::METHOD_GET); + + return $this->httpClient->send(); } /** @@ -122,14 +132,30 @@ public function downloadAction() * @param int $color * @param int $bgColor */ - private function writeLine($text, $color = null, $bgColor = null) + public function writeLine($text, $color = null, $bgColor = null) { - if ($this->isQuiet === null) { - $this->isQuiet = $this->getRequest()->getParam('q', false); + if (!$this->isQuietMode()) { + $this->getConsole()->writeLine($text, $color, $bgColor); } - if (!$this->isQuiet) { - $this->console->writeLine($text, $color, $bgColor); + } + + /** + * @return Console + */ + public function getConsole() + { + return $this->console; + } + + /** + * @return bool + */ + public function isQuietMode() + { + if ($this->isQuiet === null) { + $this->isQuiet = (bool) $this->getRequest()->getParam('q', false); } + return $this->isQuiet; } -} +} diff --git a/src/ZfSnapGeoip/Controller/ConsoleControllerFactory.php b/src/ZfSnapGeoip/Controller/ConsoleControllerFactory.php index 8f2bf15..f8573d9 100644 --- a/src/ZfSnapGeoip/Controller/ConsoleControllerFactory.php +++ b/src/ZfSnapGeoip/Controller/ConsoleControllerFactory.php @@ -1,25 +1,27 @@ - */ - namespace ZfSnapGeoip\Controller; use Zend\ServiceManager\FactoryInterface; use Zend\ServiceManager\ServiceLocatorInterface; +/** + * Factory of ConsoleController + * + * @author Witold Wasiczko + */ class ConsoleControllerFactory implements FactoryInterface { + public function createService(ServiceLocatorInterface $serviceLocator) { - $serviceLocator = $serviceLocator->getServiceLocator(); - $console = $serviceLocator->get('Console'); - $config = $serviceLocator->get('ZfSnapGeoip\DatabaseConfig'); - $httpClient = $serviceLocator->get('ZfSnapGeoip\HttpClient'); + $sm = $serviceLocator->getServiceLocator(); + + $console = $sm->get('Console'); + $config = $sm->get('ZfSnapGeoip\DatabaseConfig'); + $httpClient = $sm->get('ZfSnapGeoip\HttpClient'); 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 af5f2fc..f8737af 100644 --- a/src/ZfSnapGeoip/DatabaseConfig.php +++ b/src/ZfSnapGeoip/DatabaseConfig.php @@ -1,13 +1,12 @@ */ - -namespace ZfSnapGeoip; - class DatabaseConfig { protected $data; @@ -51,4 +50,5 @@ public function getDatabasePath() { return $this->getDestination() . $this->getFilename(); } -} \ No newline at end of file + +} diff --git a/src/ZfSnapGeoip/DatabaseConfigFactory.php b/src/ZfSnapGeoip/DatabaseConfigFactory.php index b9c7b38..5098c13 100644 --- a/src/ZfSnapGeoip/DatabaseConfigFactory.php +++ b/src/ZfSnapGeoip/DatabaseConfigFactory.php @@ -1,23 +1,24 @@ - */ - namespace ZfSnapGeoip; use Zend\ServiceManager\FactoryInterface; use Zend\ServiceManager\ServiceLocatorInterface; +/** + * Factory of DatabaseConfig + * + * @author Witold Wasiczko + */ class DatabaseConfigFactory implements FactoryInterface { + public function createService(ServiceLocatorInterface $serviceLocator) { $config = $serviceLocator->get('config'); - $data = $config['maxmind']['database']; + $data = $config['maxmind']['database']; return new DatabaseConfig($data); } -} \ No newline at end of file + +} diff --git a/src/ZfSnapGeoip/Entity/RecordInterface.php b/src/ZfSnapGeoip/Entity/RecordInterface.php index 6327a5b..53eb77d 100644 --- a/src/ZfSnapGeoip/Entity/RecordInterface.php +++ b/src/ZfSnapGeoip/Entity/RecordInterface.php @@ -3,6 +3,8 @@ namespace ZfSnapGeoip\Entity; /** + * RecordInterface + * * @author Witold Wasiczko */ interface RecordInterface diff --git a/src/ZfSnapGeoip/Exception/DomainException.php b/src/ZfSnapGeoip/Exception/DomainException.php index ccac6c7..1b71d38 100644 --- a/src/ZfSnapGeoip/Exception/DomainException.php +++ b/src/ZfSnapGeoip/Exception/DomainException.php @@ -1,13 +1,12 @@ */ - -namespace ZfSnapGeoip\Exception; - class DomainException extends \DomainException { diff --git a/src/ZfSnapGeoip/HttpClientFactory.php b/src/ZfSnapGeoip/HttpClientFactory.php index ac3bf5d..69df351 100644 --- a/src/ZfSnapGeoip/HttpClientFactory.php +++ b/src/ZfSnapGeoip/HttpClientFactory.php @@ -1,18 +1,22 @@ get('ZfSnapGeoip\HttpClient\Adapter'); + $client = new Client(); - $client->setAdapter($serviceLocator->get('ZfSnapGeoip\HttpClient\Adapter')); + $client->setAdapter($adapter); return $client; } -} \ No newline at end of file + +} diff --git a/src/ZfSnapGeoip/IpAwareInterface.php b/src/ZfSnapGeoip/IpAwareInterface.php index 8dcaaba..8725dbf 100644 --- a/src/ZfSnapGeoip/IpAwareInterface.php +++ b/src/ZfSnapGeoip/IpAwareInterface.php @@ -1,15 +1,15 @@ */ - -namespace ZfSnapGeoip; - interface IpAwareInterface { + /** * @return string IP address */ diff --git a/src/ZfSnapGeoip/Module.php b/src/ZfSnapGeoip/Module.php new file mode 100644 index 0000000..4c8bbba --- /dev/null +++ b/src/ZfSnapGeoip/Module.php @@ -0,0 +1,70 @@ + + */ +class Module implements ConsoleUsage, Config, Autoloader, BootstrapListener +{ + const CONSOLE_GEOIP_DOWNLOAD = 'geoip download [--no-clobber] [-q]'; + + /** + * @return array + */ + public function getConfig() + { + return include __DIR__ . '/../../config/module.config.php'; + } + + /** + * @return array + */ + public function getAutoloaderConfig() + { + return array( + 'Zend\Loader\StandardAutoloader' => array( + 'namespaces' => array( + __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__, + ), + ), + ); + } + + /** + * @param AdapterInterface $console + * @return array + * + * @SuppressWarnings(PHPMD.UnusedFormalParameter) + */ + public function getConsoleUsage(AdapterInterface $console) + { + return array( + 'Manage GeoIP database', + self::CONSOLE_GEOIP_DOWNLOAD => 'Downloads the newest GeoIP db', + array('--no-clobber', 'Don\'t overwrite an existing db file'), + array('-q', 'Turn off output'), + ); + } + + /** + * @param EventInterface $e + */ + public function onBootstrap(EventInterface $e) + { + $serviceManager = $e->getParam('application')->getServiceManager(); + $config = $serviceManager->get('config'); + + require_once $config['maxmind']['timezone_function_path']; + } + +} diff --git a/src/ZfSnapGeoip/Service/Geoip.php b/src/ZfSnapGeoip/Service/Geoip.php index 333e327..f71e80b 100644 --- a/src/ZfSnapGeoip/Service/Geoip.php +++ b/src/ZfSnapGeoip/Service/Geoip.php @@ -1,11 +1,5 @@ - */ - namespace ZfSnapGeoip\Service; use ZfSnapGeoip\DatabaseConfig; @@ -20,6 +14,11 @@ use Zend\ServiceManager\ServiceManagerAwareInterface; use geoiprecord as GeoipCoreRecord; +/** + * Geoip Service + * + * @author Witold Wasiczko + */ class Geoip implements ServiceManagerAwareInterface, EventManagerAwareInterface { /** @@ -135,7 +134,7 @@ public function getRecord($ipAdress = null) return $record; } - $data = get_object_vars($geoipRecord); + $data = get_object_vars($geoipRecord); $data['region_name'] = $this->getRegionName($data); $hydrator = $this->serviceManager->get('geoip_hydrator'); @@ -214,7 +213,7 @@ private function getDefaultIp() $request = $this->serviceManager->get('Request'); if ($request instanceof HttpRequest) { - $ipAddress = $request->getServer('REMOTE_ADDR'); + $ipAddress = $request->getServer('REMOTE_ADDR'); $this->defaultIp = $ipAddress; } else { $this->defaultIp = false; @@ -231,12 +230,12 @@ private function getDefaultIp() */ private function getRegionName(array $data = array()) { - $regions = $this->getRegions(); + $regions = $this->getRegions(); $countryCode = isset($data['country_code']) ? $data['country_code'] : null; if (isset($regions[$countryCode])) { $regionCodes = $regions[$countryCode]; - $regionCode = isset($data['region']) ? $data['region'] : null; + $regionCode = isset($data['region']) ? $data['region'] : null; if (isset($regionCodes[$regionCode])) { return $regionCodes[$regionCode]; @@ -274,4 +273,5 @@ public function setEventManager(EventManagerInterface $eventManager) return $this; } -} \ No newline at end of file + +} diff --git a/src/ZfSnapGeoip/View/Helper/Geoip.php b/src/ZfSnapGeoip/View/Helper/Geoip.php index 9d70988..2173ae5 100644 --- a/src/ZfSnapGeoip/View/Helper/Geoip.php +++ b/src/ZfSnapGeoip/View/Helper/Geoip.php @@ -1,21 +1,20 @@ - */ - namespace ZfSnapGeoip\View\Helper; use Zend\View\Helper\AbstractHelper; use Zend\ServiceManager\ServiceLocatorAwareInterface; use Zend\ServiceManager\ServiceLocatorInterface; +/** + * Geoip view helper + * + * @author Witold Wasiczko + */ class Geoip extends AbstractHelper implements ServiceLocatorAwareInterface { /** - * @var ZfSnapGeoip\Service\Geoip + * @var \ZfSnapGeoip\Service\Geoip */ private $geoip; @@ -39,7 +38,7 @@ public function __invoke($ipAddress = null) */ public function __toString() { - return (string)$this->getGeoip()->getRecord(); + return (string) $this->getGeoip()->getRecord(); } private function getGeoip() @@ -59,4 +58,5 @@ public function setServiceLocator(ServiceLocatorInterface $serviceLocator) { $this->serviceLocator = $serviceLocator; } + } diff --git a/tests/ApplicationConfig.php.dist b/tests/ApplicationConfig.php.dist index c94cc66..314248c 100644 --- a/tests/ApplicationConfig.php.dist +++ b/tests/ApplicationConfig.php.dist @@ -11,4 +11,4 @@ return array( 'config/{,*.}tests.php', ), ), -); \ No newline at end of file +); diff --git a/tests/bootstrap.php b/tests/ZfSnapGeoipTest/Bootstrap.php similarity index 79% rename from tests/bootstrap.php rename to tests/ZfSnapGeoipTest/Bootstrap.php index 299ba9f..6a7f8cf 100644 --- a/tests/bootstrap.php +++ b/tests/ZfSnapGeoipTest/Bootstrap.php @@ -1,4 +1,5 @@ getRecordWithTrueIp(); $this->assertEquals('Mountain View', $record->getCity()); - $this->assertEquals('Mountain View', (string)$record); + $this->assertEquals('Mountain View', (string) $record); } public function testCity() @@ -129,9 +128,10 @@ private function getIpAwareInterfaceImplementation($ip) $ipIterface = $this->getMockBuilder('\ZfSnapGeoip\IpAwareInterface')->getMock(); $ipIterface->expects($this->any()) - ->method('getIpAddress') - ->will($this->returnValue($ip)); + ->method('getIpAddress') + ->will($this->returnValue($ip)); return $ipIterface; } + } diff --git a/tests/config/geoip.tests.php b/tests/config/geoip.tests.php index f820f51..4d17ca5 100644 --- a/tests/config/geoip.tests.php +++ b/tests/config/geoip.tests.php @@ -1,9 +1,10 @@ array( 'database' => array( 'destination' => __DIR__ . '/../../data/', - 'regionvars' => __DIR__ .'/../../vendor/geoip/geoip/src/geoipregionvars.php', + 'regionvars' => __DIR__ . '/../../vendor/geoip/geoip/src/geoipregionvars.php', ), ), -); \ No newline at end of file +); diff --git a/tests/phpunit.xml b/tests/phpunit.xml index d3a91d7..d8834ea 100644 --- a/tests/phpunit.xml +++ b/tests/phpunit.xml @@ -1,6 +1,6 @@ - + ./