This repository has been archived by the owner on Mar 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Module.php
192 lines (151 loc) · 6.8 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/ZendSkeletonApplication for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace DelamatreZend;
use Monolog\Handler\LogglyHandler;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use Zend\Mvc\ModuleRouteListener;
use Zend\Mvc\MvcEvent;
class Module
{
public function onBootstrap(MvcEvent $e)
{
//get the event and and service manager instances
$eventManager = $e->getApplication()->getEventManager();
$serviceManager = $e->getApplication()->getServiceManager();
$config = $serviceManager->get('Config');
//set default timezone from config
if(isset($config['myapp']['timezone'])){
date_default_timezone_set($config['myapp']['timezone']);
}
//set error reporting from config
if(isset($config['myapp']['environment']['error_reporting'])){
error_reporting($config['myapp']['environment']['error_reporting']);
}
//set display errors from config
if(isset($config['myapp']['environment']['display_errors'])){
ini_set('display_errors', $config['myapp']['environment']['display_errors']);
}
//set display exceptions from config
if(isset($config['myapp']['environment']['display_exceptions'])){
ini_set('display_exception', $config['myapp']['environment']['display_exceptions']);
}
//log exceptions
$eventManager->attach('dispatch.error', function($e){
$exception = $e->getResult()->exception;
if ($exception) {
$serviceManager = $e->getApplication()->getServiceManager();
$config = $serviceManager->get('Config');
//if logging exceptions
if(isset($config['myapp']['environment']['log_exceptions'])
&& $config['myapp']['environment']['log_exceptions']){
//create logger
$log = new Logger('exceptions');
//log to file
if(isset($config['myapp']['environment']['log_file'])
&& $config['myapp']['environment']['log_file']){
$formatter = new \Monolog\Formatter\LineFormatter();
$formatter->includeStacktraces(true);
$streamHandler = new StreamHandler($config['myapp']['environment']['log_file'], Logger::ERROR);
$streamHandler->setFormatter($formatter);
$log->pushHandler($streamHandler);
}
//log to loggly
if(isset($config['myapp']['environment']['log_loggly'])
&& $config['myapp']['environment']['log_loggly']){
$log->pushHandler(new LogglyHandler($config['myapp']['environment']['log_loggly'], Logger::ERROR));
}
$log->error($exception);
}
}
});
//change the layout based on the controller
//fix-me: convert this to a configuration file
$eventManager->attach(MvcEvent::EVENT_DISPATCH, function($e) {
$controller = $e->getTarget();
if($e->getRequest()->isXmlHttpRequest()) {
$controller->layout('layout/ajax');
}
});
//attach the event manager to the module route listener so that we can change the layout based on route paramaters
$moduleRouteListener = new ModuleRouteListener();
$moduleRouteListener->attach($eventManager);
//get the router, request and matched route
$router = $serviceManager->get('router');
$request = $serviceManager->get('request');
$matchedRoute = $router->match($request);
//we are going to inject some variables into the view model so that we can use them later
if($matchedRoute){
$params = $matchedRoute->getParams();
$controller = $params['controller'];
$action = $params['action'];
if(isset($params['__NAMESPACE__'])){
$module_array = explode('\\', $params['__NAMESPACE__']);
$module = $module_array[0];
}else{
$module = $controller;
}
$route = $matchedRoute->getMatchedRouteName();
//force canonical for blog-posts
if($route=='blog-post'){
unset($params['category']);
}
if(!in_array($route,array('doctrine_cli'))){
$url = $serviceManager->get('ViewHelperManager')->get('url')->__invoke($route,$params, array('force_canonical' => true));
}
//$url = $this->url($route, array('id' => 123), array('force_canonical' => true);
$e->getViewModel()->setVariables(
array(
'CURRENT_MODULE_NAME' => $module,
'CURRENT_CONTROLLER_NAME' => $controller,
'CURRENT_ACTION_NAME' => $action,
'CURRENT_ROUTE_NAME' => $route,
'CANONICAL_URL' => $url,
)
);
}
}
public function getConfig()
{
$config = array();
//split the module config into multiple files
$configFiles = array(
__DIR__ . '/config/module.config.php',
__DIR__ . '/config/assetic.global.php',
__DIR__ . '/config/doctrine.global.php',
__DIR__ . '/config/ext.elfinder.global.php',
__DIR__ . '/config/ext.getresponse.global.php',
__DIR__ . '/config/ext.google.global.php',
__DIR__ . '/config/ext.maxmind.global.php',
__DIR__ . '/config/ext.mashape.global.php',
__DIR__ . '/config/ext.phantomjs.global.php',
__DIR__ . '/config/ext.salesforce.global.php',
__DIR__ . '/config/ext.typekit.global.php',
__DIR__ . '/config/myapp.global.php',
__DIR__ . '/config/navigation.global.php',
__DIR__ . '/config/session.global.php',
__DIR__ . '/config/user.global.php',
);
// Merge all module config options
foreach($configFiles as $configFile) {
$config = \Zend\Stdlib\ArrayUtils::merge($config, include $configFile);
}
return $config;
}
public function getAutoloaderConfig()
{
return array(
'Zend\Loader\StandardAutoloader' => array(
'namespaces' => array(
__NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
),
),
);
}
}