-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Code formatting, move files to namespaces, fix phpdocs, refactor some…
… methods
- Loading branch information
1 parent
adcf9ac
commit 4321095
Showing
21 changed files
with
236 additions
and
186 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,8 @@ language: php | |
php: | ||
- 5.3 | ||
- 5.4 | ||
- 5.5 | ||
- 5.6 | ||
|
||
before_script: | ||
- wget http://getcomposer.org/composer.phar | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
|
@@ -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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
@@ -19,6 +13,11 @@ | |
use Zend\Stdlib\RequestInterface; | ||
use Zend\Stdlib\ResponseInterface; | ||
|
||
/** | ||
* Console Controller | ||
* | ||
* @author Witold Wasiczko <[email protected]> | ||
*/ | ||
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,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; | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.