|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace AssetManager; |
|
4
|
|
|
|
|
5
|
|
|
use AssetManager\Core\Service\AssetManager; |
|
6
|
|
|
use Zend\Console\Adapter\AdapterInterface; |
|
7
|
|
|
use Zend\EventManager\EventInterface; |
|
8
|
|
|
use Zend\Loader\AutoloaderFactory; |
|
9
|
|
|
use Zend\Loader\StandardAutoloader; |
|
10
|
|
|
use Zend\ModuleManager\Feature\AutoloaderProviderInterface; |
|
11
|
|
|
use Zend\ModuleManager\Feature\BootstrapListenerInterface; |
|
12
|
|
|
use Zend\ModuleManager\Feature\ConfigProviderInterface; |
|
13
|
|
|
use Zend\Mvc\MvcEvent; |
|
14
|
|
|
use Zend\Psr7Bridge\Psr7Response; |
|
15
|
|
|
use Zend\Psr7Bridge\Psr7ServerRequest; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Module class |
|
19
|
|
|
* |
|
20
|
|
|
* @category AssetManager |
|
21
|
|
|
* @package AssetManager |
|
22
|
|
|
*/ |
|
23
|
|
|
class Module implements |
|
24
|
|
|
AutoloaderProviderInterface, |
|
25
|
|
|
ConfigProviderInterface, |
|
26
|
|
|
BootstrapListenerInterface |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* {@inheritDoc} |
|
30
|
|
|
*/ |
|
31
|
1 |
|
public function getAutoloaderConfig() |
|
32
|
|
|
{ |
|
33
|
|
|
return array( |
|
34
|
1 |
|
AutoloaderFactory::STANDARD_AUTOLOADER => array( |
|
35
|
1 |
|
StandardAutoloader::LOAD_NS => array( |
|
36
|
|
|
__NAMESPACE__ => __DIR__, |
|
37
|
|
|
), |
|
38
|
|
|
), |
|
39
|
|
|
); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* {@inheritDoc} |
|
44
|
|
|
*/ |
|
45
|
1 |
|
public function getConfig() |
|
46
|
|
|
{ |
|
47
|
1 |
|
return include __DIR__ . '/../config/module.config.php'; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Callback method for dispatch and dispatch.error events. |
|
52
|
|
|
* |
|
53
|
|
|
* @param MvcEvent $event |
|
54
|
|
|
* @return \Zend\Http\Response|null |
|
55
|
|
|
*/ |
|
56
|
4 |
|
public function onDispatch(MvcEvent $event) |
|
57
|
|
|
{ |
|
58
|
|
|
/* @var $zendResponse \Zend\Http\Response */ |
|
59
|
4 |
|
$zendResponse = $event->getResponse(); |
|
60
|
4 |
|
if (!method_exists($zendResponse, 'getStatusCode') || $zendResponse->getStatusCode() !== 404) { |
|
61
|
2 |
|
return null; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
2 |
|
$response = Psr7Response::fromZend($zendResponse); |
|
65
|
2 |
|
$request = Psr7ServerRequest::fromZend($event->getRequest()); |
|
|
|
|
|
|
66
|
2 |
|
$serviceManager = $event->getApplication()->getServiceManager(); |
|
67
|
|
|
|
|
68
|
|
|
/** @var AssetManager $assetManager */ |
|
69
|
2 |
|
$assetManager = $serviceManager->get(AssetManager::class); |
|
70
|
|
|
|
|
71
|
2 |
|
if (!$assetManager->resolvesToAsset($request)) { |
|
72
|
1 |
|
return null; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
1 |
|
$zendResponse = Psr7Response::toZend($assetManager->setAssetOnResponse($response)); |
|
76
|
1 |
|
$zendResponse->setStatusCode(200); |
|
77
|
|
|
|
|
78
|
1 |
|
return $zendResponse; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* {@inheritDoc} |
|
83
|
|
|
*/ |
|
84
|
1 |
|
public function onBootstrap(EventInterface $event) |
|
85
|
|
|
{ |
|
86
|
|
|
// Attach for dispatch, and dispatch.error (with low priority to make sure statusCode gets set) |
|
87
|
|
|
/* @var $eventManager \Zend\EventManager\EventManagerInterface */ |
|
88
|
1 |
|
$eventManager = $event->getTarget()->getEventManager(); |
|
89
|
1 |
|
$callback = array($this, 'onDispatch'); |
|
90
|
1 |
|
$priority = -9999999; |
|
91
|
1 |
|
$eventManager->attach(MvcEvent::EVENT_DISPATCH, $callback, $priority); |
|
92
|
1 |
|
$eventManager->attach(MvcEvent::EVENT_DISPATCH_ERROR, $callback, $priority); |
|
93
|
1 |
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @param \Zend\Console\Adapter\AdapterInterface $console |
|
97
|
|
|
* @return array |
|
98
|
|
|
* |
|
99
|
|
|
* @SuppressWarnings(PHPMD.UnusedFormalParameter) |
|
100
|
|
|
*/ |
|
101
|
|
|
public function getConsoleUsage(AdapterInterface $console) |
|
102
|
|
|
{ |
|
103
|
|
|
return array( |
|
104
|
|
|
'Warmup', |
|
105
|
|
|
'assetmanager warmup [--purge] [--verbose|-v]' => 'Warm AssetManager up', |
|
106
|
|
|
array('--purge', '(optional) forces cache flushing'), |
|
107
|
|
|
array('--verbose | -v', '(optional) verbose mode'), |
|
108
|
|
|
); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.