1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ZfSnapGeoip; |
4
|
|
|
|
5
|
|
|
use Zend\Console\Adapter\AdapterInterface; |
6
|
|
|
use Zend\EventManager\EventInterface; |
7
|
|
|
use Zend\ModuleManager\Feature\AutoloaderProviderInterface as Autoloader; |
8
|
|
|
use Zend\ModuleManager\Feature\BootstrapListenerInterface as BootstrapListener; |
9
|
|
|
use Zend\ModuleManager\Feature\ConfigProviderInterface as Config; |
10
|
|
|
use Zend\ModuleManager\Feature\ConsoleUsageProviderInterface as ConsoleUsage; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Geoip module |
14
|
|
|
* |
15
|
|
|
* @author Witold Wasiczko <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
class Module implements ConsoleUsage, Config, Autoloader, BootstrapListener |
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
const CONSOLE_GEOIP_DOWNLOAD = 'geoip download [--no-clobber] [-q]'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @return array |
24
|
|
|
*/ |
25
|
|
|
public function getConfig() |
26
|
|
|
{ |
27
|
|
|
return include __DIR__ . '/../../config/module.config.php'; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @return array |
32
|
|
|
*/ |
33
|
|
|
public function getAutoloaderConfig() |
34
|
|
|
{ |
35
|
|
|
return [ |
36
|
|
|
'Zend\Loader\StandardAutoloader' => [ |
37
|
|
|
'namespaces' => [ |
38
|
|
|
__NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__, |
39
|
|
|
], |
40
|
|
|
], |
41
|
|
|
]; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param AdapterInterface $console |
46
|
|
|
* @return array |
47
|
|
|
* |
48
|
|
|
* @SuppressWarnings(PHPMD.UnusedFormalParameter) |
49
|
|
|
*/ |
50
|
|
|
public function getConsoleUsage(AdapterInterface $console) |
51
|
|
|
{ |
52
|
|
|
return ['Manage GeoIP database', |
53
|
|
|
self::CONSOLE_GEOIP_DOWNLOAD => 'Downloads the newest GeoIP db', |
54
|
|
|
['--no-clobber', 'Don\'t overwrite an existing db file'], |
55
|
|
|
['-q', 'Turn off output'], |
56
|
|
|
]; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param \Zend\Mvc\MvcEvent $event |
61
|
|
|
*/ |
62
|
|
|
public function onBootstrap(EventInterface $event) |
63
|
|
|
{ |
64
|
|
|
$serviceManager = $event->getApplication()->getServiceManager(); |
|
|
|
|
65
|
|
|
$config = $serviceManager->get('config'); |
66
|
|
|
|
67
|
|
|
require_once $config['maxmind']['timezone_function_path']; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: