|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
5
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
6
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
7
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
8
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
9
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
10
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
11
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
12
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
13
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
14
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
15
|
|
|
* |
|
16
|
|
|
* This software consists of voluntary contributions made by many individuals |
|
17
|
|
|
* and is licensed under the MIT license. |
|
18
|
|
|
*/ |
|
19
|
|
|
namespace LearnZF2Themes; |
|
20
|
|
|
|
|
21
|
|
|
use LearnZF2Themes\Service\ReloadService; |
|
22
|
|
|
use Zend\EventManager\EventInterface; |
|
23
|
|
|
use Zend\ModuleManager\Feature\AutoloaderProviderInterface; |
|
24
|
|
|
use Zend\ModuleManager\Feature\BootstrapListenerInterface; |
|
25
|
|
|
use Zend\ModuleManager\Feature\ConfigProviderInterface; |
|
26
|
|
|
|
|
27
|
|
|
class Module implements AutoloaderProviderInterface, BootstrapListenerInterface, ConfigProviderInterface |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* @var \Zend\getServiceManager\ServiceManager |
|
31
|
|
|
*/ |
|
32
|
|
|
private $service = null; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Listen to the bootstrap event. |
|
36
|
|
|
* |
|
37
|
|
|
* @param EventInterface $event |
|
38
|
|
|
*/ |
|
39
|
|
|
public function onBootstrap(EventInterface $event) |
|
40
|
|
|
{ |
|
41
|
|
|
$app = $event->getApplication(); |
|
|
|
|
|
|
42
|
|
|
$this->service = $app->getServiceManager(); |
|
43
|
|
|
$eventManager = $app->getEventManager(); |
|
44
|
|
|
$sharedEventManager = $eventManager->getSharedManager(); |
|
45
|
|
|
|
|
46
|
|
|
$eventManager->attach('render', [$this, 'loadTheme'], 100); |
|
47
|
|
|
$sharedEventManager->attach(ReloadService::class, 'reload', [$this, 'reloadConfig'], 100); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Listen for theme change and override Config. |
|
52
|
|
|
*/ |
|
53
|
|
|
public function reloadConfig() |
|
54
|
|
|
{ |
|
55
|
|
|
$request = $this->service->get('Request'); |
|
56
|
|
|
|
|
57
|
|
|
$config = $this->service->get('Config'); |
|
58
|
|
|
$this->service->setAllowOverride(true); |
|
59
|
|
|
$config['theme']['name'] = $request->getPost()['themeName']; |
|
60
|
|
|
$this->service->setService('Config', $config); |
|
61
|
|
|
$this->service->setAllowOverride(false); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Setup theme. |
|
66
|
|
|
*/ |
|
67
|
|
|
public function loadTheme() |
|
68
|
|
|
{ |
|
69
|
|
|
return $this->service->get('initThemes'); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @return array|\Traversable |
|
74
|
|
|
*/ |
|
75
|
|
|
public function getConfig() |
|
76
|
|
|
{ |
|
77
|
|
|
return include __DIR__.'/config/module.config.php'; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function getAutoloaderConfig() |
|
81
|
|
|
{ |
|
82
|
|
|
return [ |
|
83
|
|
|
'Zend\Loader\StandardAutoloader' => [ |
|
84
|
|
|
'namespaces' => [ |
|
85
|
|
|
__NAMESPACE__ => __DIR__.'/src/'.__NAMESPACE__, |
|
86
|
|
|
], |
|
87
|
|
|
], |
|
88
|
|
|
]; |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
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: