Skip to content

Commit

Permalink
Code formatting, move files to namespaces, fix phpdocs, refactor some…
Browse files Browse the repository at this point in the history
… methods
  • Loading branch information
snapshotpl committed Nov 4, 2014
1 parent adcf9ac commit 4321095
Show file tree
Hide file tree
Showing 21 changed files with 236 additions and 186 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ language: php
php:
- 5.3
- 5.4
- 5.5
- 5.6

before_script:
- wget http://getcomposer.org/composer.phar
Expand Down
64 changes: 1 addition & 63 deletions Module.php
Original file line number Diff line number Diff line change
@@ -1,65 +1,3 @@
<?php

/**
* Geoip module
*
* @author Witold Wasiczko <[email protected]>
*/

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'];
}

}
require __DIR__ . '/src/ZfSnapGeoip/Module.php';
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
-----
Expand Down Expand Up @@ -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)
Expand Down
28 changes: 14 additions & 14 deletions config/module.config.php
Original file line number Diff line number Diff line change
@@ -1,50 +1,50 @@
<?php

return array(
'maxmind' => 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',
)
)
)
Expand Down
18 changes: 9 additions & 9 deletions src/ZfSnapGeoip/Composer/ScriptHandler.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
<?php

namespace ZfSnapGeoip\Composer;

use Composer\Script\Event;

/**
* Composer script handler
*
* @author Witold Wasiczko <[email protected]>
*/

namespace ZfSnapGeoip\Composer;

use Composer\Script\Event;

class ScriptHandler
{
const ZEND_INDEX_PATH = 'zend-index-path';
Expand All @@ -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') {
Expand All @@ -35,4 +34,5 @@ public static function downloadData(Event $event)
$cmd = sprintf('php %s %s', $zendIndexPath, $geoipDownloadCmd);
system($cmd);
}

}
86 changes: 56 additions & 30 deletions src/ZfSnapGeoip/Controller/ConsoleController.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
<?php

/**
* Console Controller
*
* @author Witold Wasiczko <[email protected]>
*/

namespace ZfSnapGeoip\Controller;

use Zend\Http\Client;
Expand All @@ -19,6 +13,11 @@
use Zend\Stdlib\RequestInterface;
use Zend\Stdlib\ResponseInterface;

/**
* Console Controller
*
* @author Witold Wasiczko <[email protected]>
*/
class ConsoleController extends AbstractActionController
{
/**
Expand All @@ -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)
{
Expand All @@ -79,57 +79,83 @@ 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();
}

/**
* @param string $text
* @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;
}
}

}
Loading

0 comments on commit 4321095

Please sign in to comment.