-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathzray.php
340 lines (278 loc) · 12.3 KB
/
zray.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
<?php
/*********************************
Zend Framework 1 Z-Ray Extension
Version: 1.00
**********************************/
namespace ZF1;
use Traversable,
Closure,
ReflectionProperty;
class ZF1 {
private $isExceptionSaved = false;
public function storeDispatcherExit($context, &$storage) {
$Zend_Controller_Dispatcher_Standard = $context["this"];
$request = $context["functionArgs"][0];
$action = $Zend_Controller_Dispatcher_Standard->getActionMethod($request);
$className = $this->getControllerName($Zend_Controller_Dispatcher_Standard, $request);
$storage['request'][] = array ( 'action' => $action,
'controller' => $className,
'moduleClassName' => $this->getModuleClassName($Zend_Controller_Dispatcher_Standard, $className));
}
public function storeFrontDispatchExit($context, &$storage) {
$Zend_Controller_Front = $context["this"];
if (method_exists($Zend_Controller_Front, 'getPlugins')) {
$plugins = $Zend_Controller_Front->getPlugins();
foreach ($plugins as $plugin) {
$storage['plugin'][get_class($plugin)] = $this->makeArraySerializable($plugin);
}
}
}
public function storeViewExit($context, &$storage) {
$storage['view'][] = $context["functionArgs"];
}
public function storeViewHelperExit($context, &$storage) {
$name = $context["functionArgs"][0];
$args = $context["functionArgs"][1];
$Zend_View_Abstract = $context["this"];
$helper = $Zend_View_Abstract->getHelper($name);
$reflect = new \ReflectionClass($helper);
$properties = array();
foreach ($reflect->getProperties(\ReflectionProperty::IS_PUBLIC | \ReflectionProperty::IS_PROTECTED | \ReflectionProperty::IS_PRIVATE) as $prop) {
$prop->setAccessible(true);
$value = $prop->getValue($helper);
if (is_object($value)) {
$properties[$prop->getName()] = get_class($value);
} else {
$properties[$prop->getName()] = $value;
}
}
$helpersArgs = array();
foreach ($args as $arg) {
if (is_object($arg)) {
$reflect = new \ReflectionClass($arg);
$properties = array();
foreach ($reflect->getProperties(\ReflectionProperty::IS_PUBLIC | \ReflectionProperty::IS_PROTECTED | \ReflectionProperty::IS_PRIVATE) as $prop) {
$prop->setAccessible(true);
$value = $prop->getValue($arg);
if (is_object($value)) {
$properties[$prop->getName()] = get_class($value);
} else {
$properties[$prop->getName()] = $value;
}
}
$helpersArgs[get_class($arg)] = $properties;
} else {
$helpersArgs[] = $arg;
}
}
$storage['viewHelpers'][$name] = array( 'args' => $helpersArgs,
'helperObject' => $properties);
}
public function storeHandleErrorExit($context, &$storage) {
$Zend_Controller_Plugin_ErrorHandler = $context["this"];
$this->getException($Zend_Controller_Plugin_ErrorHandler, $storage);
}
public function storeRouterRewriteRequestExit($context, &$storage) {
if(! $context["exceptionThrown"]) {
$storage['requestObject'][] = $context['returnValue'];
}
}
////////////// PRIVATES ///////////////////
private function getException($Zend_Controller_Plugin_ErrorHandler, &$storage) {
$response = $Zend_Controller_Plugin_ErrorHandler->getResponse();
$reflection = new \ReflectionProperty('Zend_Controller_Plugin_ErrorHandler', '_isInsideErrorHandlerLoop');
$reflection->setAccessible(true);
$_isInsideErrorHandlerLoop = $reflection->getValue($Zend_Controller_Plugin_ErrorHandler);
// check for an exception AND allow the error handler controller the option to forward
if ($response->isException() && !$this->isExceptionSaved) {
// Get exception information
$error = new \ArrayObject(array(), \ArrayObject::ARRAY_AS_PROPS);
$exceptions = $response->getException();
$exception = $exceptions[0];
$exceptionType = get_class($exception);
$error->exception = $exception;
switch ($exceptionType) {
case 'Zend_Controller_Router_Exception':
if (404 == $exception->getCode()) {
$error->type = $Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ROUTE;
} else {
$error->type = $Zend_Controller_Plugin_ErrorHandler::EXCEPTION_OTHER;
}
break;
case 'Zend_Controller_Dispatcher_Exception':
$error->type = $Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER;
break;
case 'Zend_Controller_Action_Exception':
if (404 == $exception->getCode()) {
$error->type = $Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION;
} else {
$error->type = $Zend_Controller_Plugin_ErrorHandler::EXCEPTION_OTHER;
}
break;
default:
$error->type = $Zend_Controller_Plugin_ErrorHandler::EXCEPTION_OTHER;
break;
}
$this->isExceptionSaved = true;
$storage['ErrorHandler'][] = array ( 'exceptionType' => $exceptionType,
'error' => $error,
'exception' => $exception);
}
}
private function getControllerName($Zend_Controller_Dispatcher_Standard, $request) {
/**
* Get controller class
*/
if (!$Zend_Controller_Dispatcher_Standard->isDispatchable($request)) {
$controller = $request->getControllerName();
if (!$Zend_Controller_Dispatcher_Standard->getParam('useDefaultControllerAlways') && !empty($controller)) {
return "";
}
$className = $Zend_Controller_Dispatcher_Standard->getDefaultControllerClass($request);
} else {
$className = $Zend_Controller_Dispatcher_Standard->getControllerClass($request);
if (!$className) {
$className = $Zend_Controller_Dispatcher_Standard->getDefaultControllerClass($request);
}
}
return $className;
}
private function getModuleClassName($Zend_Controller_Dispatcher_Standard, $className) {
$moduleClassName = $className;
$reflection = new \ReflectionProperty('Zend_Controller_Dispatcher_Standard', '_curModule');
$reflection->setAccessible(true);
$_curModule = $reflection->getValue($Zend_Controller_Dispatcher_Standard);
$reflection = new \ReflectionProperty('Zend_Controller_Dispatcher_Standard', '_defaultModule');
$reflection->setAccessible(true);
$_defaultModule = $reflection->getValue($Zend_Controller_Dispatcher_Standard);
if (($_defaultModule != $_curModule)
|| $Zend_Controller_Dispatcher_Standard->getParam('prefixDefaultModule'))
{
$moduleClassName = $Zend_Controller_Dispatcher_Standard->formatClassName($_curModule, $className);
}
return $moduleClassName;
}
/**
* Replaces the un-serializable items in an array with stubs
*
* @param array|\Traversable $data
*
* @return array
*/
private function makeArraySerializable($data) {
$serializable = array();
try {
if (is_a($data, 'Zend_Layout_Controller_Plugin_Layout')) {
$serializable[get_class($data)] = new ZendLayoutControllerPluginLayoutStub();
return array('stubbed to prevent memory exhaustion',new ZendLayoutControllerPluginLayoutStub());
}
foreach ($this->iteratorToArray($data) as $key => $value) {
if ($value instanceof Traversable || is_array($value)) {
$serializable[$key] = $this->makeArraySerializable($value);
continue;
}
if ($value instanceof Closure) {
$serializable[$key] = new ClosureStub();
continue;
}
$serializable[$key] = $value;
}
} catch (\InvalidArgumentException $e) {
return $serializable;
}
return $serializable;
}
/**
* Use reflection to iterate over an object.
*
* @param $iterator
* @param bool $recursive
* @return array
*/
private function objectToArray($iterator, $recursive = true) {
$array = array();
$reflect = new \ReflectionClass($iterator);
foreach ($reflect->getProperties(\ReflectionProperty::IS_PUBLIC | \ReflectionProperty::IS_PROTECTED | \ReflectionProperty::IS_PRIVATE) as $key=>$prop) {
$prop->setAccessible(true);
$value = $prop->getValue($iterator);
if (is_a($value, 'Closure')) {
$array[$prop->getName().' ('.get_class($value).')'] = new ClosureStub();
continue;
}
if (is_object($value)) {
$array[$prop->getName().' ('.get_class($value).')'] = $this->iteratorToArray($value, $recursive);
continue;
}
$array[$prop->getName()] = $value;
}
return $array;
}
/**
* Copied from ZF2/zray.php fix (https://github.com/zend-server-extensions/Z-Ray-ZF2/issues/6#issuecomment-91606537)
* and modified to add object iteration.
*
* @param $iterator
* @param bool $recursive
* @return array
*/
private function iteratorToArray($iterator, $recursive = true) {
if (!is_array($iterator) && !$iterator instanceof Traversable) {
/**
* Use reflection for objects that can't be iterated.
*/
if (is_object($iterator)) {
return $this->objectToArray($iterator,$recursive);
}
throw new \InvalidArgumentException(__METHOD__ . ' expects an array or Traversable object');
}
if (!$recursive) {
if (is_array($iterator)) {
return $iterator;
}
return iterator_to_array($iterator);
}
if (method_exists($iterator, 'toArray')) {
return $iterator->toArray();
}
$array = array();
foreach ($iterator as $key => $value) {
if (is_scalar($value)) {
$array [$key] = $value;
continue;
}
if ($value instanceof Traversable) {
$array [$key] = $this->iteratorToArray($value, $recursive);
continue;
}
if (is_array($value)) {
$array [$key] = $this->iteratorToArray($value, $recursive);
continue;
}
$array [$key] = $value;
}
return $array;
}
}
/**
* Empty class that represents an {@see \Closure} object
*/
class ClosureStub {
}
/**
* Empty class that represents an {@see \Zend_Layout_Controller_Plugin_Layout} object
*/
class ZendLayoutControllerPluginLayoutStub {
}
// Allocate ZRayExtension for namespace "zf1"
$zre = new \ZRayExtension("zf1");
$zf1Storage = new ZF1();
$zre->setMetadata(array(
'logo' => __DIR__ . DIRECTORY_SEPARATOR . 'logo.png',
));
$zre->setEnabledAfter('Zend_Controller_Front::dispatch');
$zre->traceFunction("Zend_Controller_Dispatcher_Standard::dispatch", function(){}, array($zf1Storage, 'storeDispatcherExit'));
$zre->traceFunction("Zend_Controller_Front::dispatch", function(){}, array($zf1Storage, 'storeFrontDispatchExit'));
$zre->traceFunction("Zend_View::_run", function(){}, array($zf1Storage, 'storeViewExit'));
$zre->traceFunction("Zend_View_Abstract::__call", function(){}, array($zf1Storage, 'storeViewHelperExit'));
$zre->traceFunction("Zend_Controller_Plugin_ErrorHandler::_handleError", function(){}, array($zf1Storage, 'storeHandleErrorExit'));
$zre->traceFunction("Zend_Controller_Router_Rewrite::route", function(){} , array($zf1Storage, 'storeRouterRewriteRequestExit'));