Permalink
Browse files

Create Zend Monitor Event when exception thrown

  • Loading branch information...
ruslan-g committed Sep 12, 2012
1 parent 2db58cf commit 2f568b231ddacba9bc7b1e4c9f9725607cfef4ee
@@ -83,6 +83,7 @@ class ViewManager implements ListenerAggregateInterface
protected $rendererStrategy;
protected $resolver;
protected $routeNotFoundStrategy;
+ protected $zendMonitorExceptionStrategy;
protected $view;
protected $viewModel;
/**@-*/
@@ -133,18 +134,20 @@ public function onBootstrap($event)
$this->services = $services;
$this->event = $event;
- $routeNotFoundStrategy = $this->getRouteNotFoundStrategy();
- $exceptionStrategy = $this->getExceptionStrategy();
- $mvcRenderingStrategy = $this->getMvcRenderingStrategy();
- $createViewModelListener = new CreateViewModelListener();
- $injectTemplateListener = new InjectTemplateListener();
- $injectViewModelListener = new InjectViewModelListener();
- $sendResponseListener = new SendResponseListener();
+ $routeNotFoundStrategy = $this->getRouteNotFoundStrategy();
+ $exceptionStrategy = $this->getExceptionStrategy();
+ $zendMonitorExceptionStrategy = $this->getZendMonitorExceptionStrategy();
+ $mvcRenderingStrategy = $this->getMvcRenderingStrategy();
+ $createViewModelListener = new CreateViewModelListener();
+ $injectTemplateListener = new InjectTemplateListener();
+ $injectViewModelListener = new InjectViewModelListener();
+ $sendResponseListener = new SendResponseListener();
$this->registerMvcRenderingStrategies($events);
$this->registerViewStrategies();
$events->attach($routeNotFoundStrategy);
+ $events->attach($zendMonitorExceptionStrategy);
$events->attach($exceptionStrategy);
$events->attach(MvcEvent::EVENT_DISPATCH_ERROR, array($injectViewModelListener, 'injectViewModel'), -100);
$events->attach($mvcRenderingStrategy);
@@ -376,6 +379,25 @@ public function getViewModel()
return $this->viewModel;
}
+
+
+ /**
+ * Instantiates and configures the Zend Monitor Exception strategy
+ * @return \Zend\Mvc\View\Http\ZendMonitorExceptionStrategy
+ */
+ public function getZendMonitorExceptionStrategy()
+ {
+ if ($this->zendMonitorExceptionStrategy) {
+ return $this->zendMonitorExceptionStrategy;
+ }
+
+ $this->zendMonitorExceptionStrategy = new ZendMonitorExceptionStrategy();
+
+ $this->services->setService('zendMonitorExceptionStrategy', $this->zendMonitorExceptionStrategy);
+ $this->services->setAlias('Zend\Mvc\View\Http\ZendMonitorExceptionStrategy', 'ZendMonitorExceptionStrategy');
+
+ return $this->zendMonitorExceptionStrategy;
+ }
/**
* Register additional mvc rendering strategies
@@ -0,0 +1,118 @@
+<?php
+/**
+ * Zend Framework (http://framework.zend.com/)
+ *
+ * @link http://github.com/zendframework/zf2 for the canonical source repository
+ * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ * @package Zend_Mvc
+ */
+
+namespace Zend\Mvc\View\Http;
+
+use Zend\EventManager\EventManagerInterface;
+use Zend\EventManager\ListenerAggregateInterface;
+use Zend\Http\Response as HttpResponse;
+use Zend\Mvc\Application;
+use Zend\Mvc\MvcEvent;
+use Zend\Stdlib\ResponseInterface as Response;
+use Zend\View\Model\ViewModel;
+
+/**
+ * @category Zend
+ * @package Zend_Mvc
+ * @subpackage View
+ */
+class ZendMonitorExceptionStrategy implements ListenerAggregateInterface
+{
+ /**
+ * @var \Zend\Stdlib\CallbackHandler[]
+ */
+ protected $listeners = array();
+
+ /**
+ * Attach the aggregate to the specified event manager
+ *
+ * @param EventManagerInterface $events
+ * @return void
+ */
+ public function attach(EventManagerInterface $events)
+ {
+ $this->listeners[] = $events->attach(MvcEvent::EVENT_DISPATCH_ERROR, array($this, 'createCustomEvent'));
+ }
+
+ /**
+ * Detach aggregate listeners from the specified event manager
+ *
+ * @param EventManagerInterface $events
+ * @return void
+ */
+ public function detach(EventManagerInterface $events)
+ {
+ foreach ($this->listeners as $index => $listener) {
+ if ($events->detach($listener)) {
+ unset($this->listeners[$index]);
+ }
+ }
+ }
+
+ /**
+ * Create a custom event in Zend Server
+ *
+ * @param MvcEvent $e
+ * @return void
+ */
+ public function createCustomEvent(MvcEvent $e)
+ {
+ // Do nothing if no error in the event
+
+ $error = $e->getError();
+ if (empty($error)) {
+ return;
+ }
+
+ // Do nothing if the result is a response object
+ $result = $e->getResult();
+ if ($result instanceof Response) {
+ return;
+ }
+
+ switch ($error) {
+ case Application::ERROR_CONTROLLER_NOT_FOUND:
+ case Application::ERROR_CONTROLLER_INVALID:
+ case Application::ERROR_ROUTER_NO_MATCH:
+ // Specifically not handling these
+ return;
+
+ case Application::ERROR_EXCEPTION:
+ default:
+ if (!empty($e)) {
+ $exception = $e->getParam('exception'); /* @var $exception \Exception */
+ $message = 'An error occurred during execution: ' . $exception->getMessage();
+ if ($this->isCustomEventByRuleIsEnabled()) {
+ zend_monitor_custom_event_ex('Zend Framework Exception', $message, 'Zend Framework Exception Rule');
+ } elseif ($this->isCustomEventIsEnabled) {
+ zend_monitor_custom_event('Zend Framework Exception', $message);
+ }
+ }
+ }
+ }
+
+ /**
+ * Is Zend Server Monitor enabled?
+ * @return boolean
+ */
+ protected function isCustomEventByRuleIsEnabled()
+ {
+ return function_exists('zend_monitor_custom_event_ex');
+ }
+
+ /**
+ * Is Zend Server Monitor enabled?
+ * @return boolean
+ */
+ protected function isCustomEventIsEnabled()
+ {
+ return function_exists('zend_monitor_custom_event');
+ }
+}

0 comments on commit 2f568b2

Please sign in to comment.