-
Notifications
You must be signed in to change notification settings - Fork 0
/
Module.php
121 lines (108 loc) · 3.36 KB
/
Module.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php
/**
* CRUD actions module for Doctrine entities
*
* PHP Version 5.4.13
*
* @author Anis Safine <[email protected]>
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace DoctrineCRUD;
use Zend\Mvc\MvcEvent;
/**
* CRUD actions module for Doctrine entities
*
* @author Anis Safine <[email protected]>
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Module
{
/**
* Returns the application configuration
*
* @return array
*/
public function getConfig()
{
return require __DIR__ . '/config/module.config.php';
}
/**
* Returns the autoloader configuration
*
* @return array
*/
public function getAutoloaderConfig()
{
return array(
'Zend\Loader\StandardAutoloader' => array(
'namespaces' => array(
__NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__
),
),
);
}
/**
* Bootstraps the module
*
* Basically, it's here to attach our listeners on important ZF2 events.
*
* @return void
*/
public function onBootstrap(MvcEvent $e)
{
$e->getApplication()->getEventManager()->attach(MvcEvent::EVENT_RENDER, array($this, 'setDefaultTemplate'));
}
/**
* Sets a default view template
*
* In case a custom template has not been defined for the requested action, this method sets a default template
* provided by this module.
* This method is triggered by the Event Listener on render.
*
* @param \Zend\View\MvcEvent $e View event (triggerer)
*
* @eturn void
*/
public function setDefaultTemplate(MvcEvent $e)
{
// Ensure a route matched
if ($e->getRouteMatch() === null) {
return;
}
// Resolve the name of matched controller
$matchedController = $e->getRouteMatch()->getParam('controller');
foreach ($e->getApplication()->getConfig()['controllers'] as $controllers) {
if (array_key_exists($matchedController, $controllers)) {
$matchedController = $controllers[$matchedController];
break;
}
}
// Ensure matched controller is part of that module
if (!is_subclass_of(new $matchedController(), __NAMESPACE__ . '\Controller\CRUDController')) {
return;
}
// Ensure matched action is a CRUD action
$matchedAction = $e->getRouteMatch()->getParam('action');
if (!in_array($matchedAction, array('create', 'read', 'update', 'delete'))) {
return;
}
// Get the content view (if it exists)
$contentView = null;
foreach ($e->getViewModel()->getChildren() as $view) {
if ($view->captureTo() === 'content') {
$contentView = $view;
break;
}
}
if ($contentView === null) {
return;
}
// Check if the template can be resolved for that view
$sm = $e->getApplication()->getServiceManager();
if ($sm->get('ViewResolver')->resolve($contentView->getTemplate(), $sm->get('ViewRenderer')) !== false) {
return;
}
// If not, set a default template
$contentView->setTemplate('doctrine-crud/crud/'.$matchedAction.'/'.$matchedAction);
}
}