~cdparra/gelee/trunk

« back to all changes in this revision

Viewing changes to webui/ecosystem/workspace/ecosystem/uwa-server-original/server/lib/Zend/Controller/Plugin/ErrorHandler.php

  • Committer: parra
  • Date: 2010-03-15 02:39:02 UTC
  • Revision ID: svn-v4:ac5bba68-f036-4e09-846e-8f32731cc928:trunk/gelee:1433
merged gelee at svn

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/**
 
3
 * Zend Framework
 
4
 *
 
5
 * LICENSE
 
6
 *
 
7
 * This source file is subject to the new BSD license that is bundled
 
8
 * with this package in the file LICENSE.txt.
 
9
 * It is also available through the world-wide-web at this URL:
 
10
 * http://framework.zend.com/license/new-bsd
 
11
 * If you did not receive a copy of the license and are unable to
 
12
 * obtain it through the world-wide-web, please send an email
 
13
 * to license@zend.com so we can send you a copy immediately.
 
14
 *
 
15
 * @category   Zend
 
16
 * @package    Zend_Controller
 
17
 * @subpackage Plugins
 
18
 * @copyright  Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
 
19
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 
20
 */
 
21
 
 
22
/** Zend_Controller_Plugin_Abstract */
 
23
require_once 'Zend/Controller/Plugin/Abstract.php';
 
24
 
 
25
/**
 
26
 * Handle exceptions that bubble up based on missing controllers, actions, or
 
27
 * application errors, and forward to an error handler.
 
28
 *
 
29
 * @uses       Zend_Controller_Plugin_Abstract
 
30
 * @category   Zend
 
31
 * @package    Zend_Controller
 
32
 * @subpackage Plugins
 
33
 * @copyright  Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
 
34
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 
35
 * @version    $Id: ErrorHandler.php 8064 2008-02-16 10:58:39Z thomas $
 
36
 */
 
37
class Zend_Controller_Plugin_ErrorHandler extends Zend_Controller_Plugin_Abstract
 
38
{
 
39
    /**
 
40
     * Const - No controller exception; controller does not exist
 
41
     */
 
42
    const EXCEPTION_NO_CONTROLLER = 'EXCEPTION_NO_CONTROLLER';
 
43
 
 
44
    /**
 
45
     * Const - No action exception; controller exists, but action does not
 
46
     */
 
47
    const EXCEPTION_NO_ACTION = 'EXCEPTION_NO_ACTION';
 
48
 
 
49
    /**
 
50
     * Const - Other Exception; exceptions thrown by application controllers
 
51
     */
 
52
    const EXCEPTION_OTHER = 'EXCEPTION_OTHER';
 
53
 
 
54
    /**
 
55
     * Module to use for errors; defaults to default module in dispatcher
 
56
     * @var string
 
57
     */
 
58
    protected $_errorModule;
 
59
 
 
60
    /**
 
61
     * Controller to use for errors; defaults to 'error'
 
62
     * @var string
 
63
     */
 
64
    protected $_errorController = 'error';
 
65
 
 
66
    /**
 
67
     * Action to use for errors; defaults to 'error'
 
68
     * @var string
 
69
     */
 
70
    protected $_errorAction = 'error';
 
71
 
 
72
    /**
 
73
     * Flag; are we already inside the error handler loop?
 
74
     * @var bool
 
75
     */
 
76
    protected $_isInsideErrorHandlerLoop = false;
 
77
 
 
78
    /**
 
79
     * Exception count logged at first invocation of plugin
 
80
     * @var int
 
81
     */
 
82
    protected $_exceptionCountAtFirstEncounter = 0;
 
83
 
 
84
    /**
 
85
     * Constructor
 
86
     *
 
87
     * Options may include:
 
88
     * - module
 
89
     * - controller
 
90
     * - action
 
91
     *
 
92
     * @param  Array $options
 
93
     * @return void
 
94
     */
 
95
    public function __construct(Array $options = array())
 
96
    {
 
97
        $this->setErrorHandler($options);
 
98
    }
 
99
 
 
100
    /**
 
101
     * setErrorHandler() - setup the error handling options
 
102
     *
 
103
     * @param  array $options
 
104
     * @return Zend_Controller_Plugin_ErrorHandler
 
105
     */
 
106
    public function setErrorHandler(Array $options = array())
 
107
    {
 
108
        if (isset($options['module'])) {
 
109
            $this->setErrorHandlerModule($options['module']);
 
110
        }
 
111
        if (isset($options['controller'])) {
 
112
            $this->setErrorHandlerController($options['controller']);
 
113
        }
 
114
        if (isset($options['action'])) {
 
115
            $this->setErrorHandlerAction($options['action']);
 
116
        }
 
117
        return $this;
 
118
    }
 
119
 
 
120
    /**
 
121
     * Set the module name for the error handler
 
122
     *
 
123
     * @param  string $module
 
124
     * @return Zend_Controller_Plugin_ErrorHandler
 
125
     */
 
126
    public function setErrorHandlerModule($module)
 
127
    {
 
128
        $this->_errorModule = (string) $module;
 
129
        return $this;
 
130
    }
 
131
 
 
132
    /**
 
133
     * Retrieve the current error handler module
 
134
     *
 
135
     * @return string
 
136
     */
 
137
    public function getErrorHandlerModule()
 
138
    {
 
139
        if (null === $this->_errorModule) {
 
140
            $this->_errorModule = Zend_Controller_Front::getInstance()->getDispatcher()->getDefaultModule();
 
141
        }
 
142
        return $this->_errorModule;
 
143
    }
 
144
 
 
145
    /**
 
146
     * Set the controller name for the error handler
 
147
     *
 
148
     * @param  string $controller
 
149
     * @return Zend_Controller_Plugin_ErrorHandler
 
150
     */
 
151
    public function setErrorHandlerController($controller)
 
152
    {
 
153
        $this->_errorController = (string) $controller;
 
154
        return $this;
 
155
    }
 
156
 
 
157
    /**
 
158
     * Retrieve the current error handler controller
 
159
     *
 
160
     * @return string
 
161
     */
 
162
    public function getErrorHandlerController()
 
163
    {
 
164
        return $this->_errorController;
 
165
    }
 
166
 
 
167
    /**
 
168
     * Set the action name for the error handler
 
169
     *
 
170
     * @param  string $action
 
171
     * @return Zend_Controller_Plugin_ErrorHandler
 
172
     */
 
173
    public function setErrorHandlerAction($action)
 
174
    {
 
175
        $this->_errorAction = (string) $action;
 
176
        return $this;
 
177
    }
 
178
 
 
179
    /**
 
180
     * Retrieve the current error handler action
 
181
     *
 
182
     * @return string
 
183
     */
 
184
    public function getErrorHandlerAction()
 
185
    {
 
186
        return $this->_errorAction;
 
187
    }
 
188
 
 
189
    /**
 
190
     * postDispatch() plugin hook -- check for exceptions and dispatch error
 
191
     * handler if necessary
 
192
     *
 
193
     * If the 'noErrorHandler' front controller flag has been set,
 
194
     * returns early.
 
195
     *
 
196
     * @param  Zend_Controller_Request_Abstract $request
 
197
     * @return void
 
198
     */
 
199
    public function postDispatch(Zend_Controller_Request_Abstract $request)
 
200
    {
 
201
        $frontController = Zend_Controller_Front::getInstance();
 
202
        if ($frontController->getParam('noErrorHandler')) {
 
203
            return;
 
204
        }
 
205
 
 
206
        $response = $this->getResponse();
 
207
 
 
208
        if ($this->_isInsideErrorHandlerLoop) {
 
209
            $exceptions = $response->getException();
 
210
            if (count($exceptions) > $this->_exceptionCountAtFirstEncounter) {
 
211
                // Exception thrown by error handler; tell the front controller to throw it
 
212
                $frontController->throwExceptions(true);
 
213
                throw array_pop($exceptions);
 
214
            }
 
215
        }
 
216
 
 
217
        // check for an exception AND allow the error handler controller the option to forward
 
218
        if (($response->isException()) && (!$this->_isInsideErrorHandlerLoop)) {
 
219
            $this->_isInsideErrorHandlerLoop = true;
 
220
 
 
221
            // Get exception information
 
222
            $error            = new ArrayObject(array(), ArrayObject::ARRAY_AS_PROPS);
 
223
            $exceptions       = $response->getException();
 
224
            $exception        = $exceptions[0];
 
225
            $exceptionType    = get_class($exception);
 
226
            $error->exception = $exception;
 
227
            switch ($exceptionType) {
 
228
                case 'Zend_Controller_Dispatcher_Exception':
 
229
                    $error->type = self::EXCEPTION_NO_CONTROLLER;
 
230
                    break;
 
231
                case 'Zend_Controller_Action_Exception':
 
232
                    if (404 == $exception->getCode()) {
 
233
                        $error->type = self::EXCEPTION_NO_ACTION;
 
234
                    } else {
 
235
                        $error->type = self::EXCEPTION_OTHER;
 
236
                    }
 
237
                    break;
 
238
                default:
 
239
                    $error->type = self::EXCEPTION_OTHER;
 
240
                    break;
 
241
            }
 
242
 
 
243
            // Keep a copy of the original request
 
244
            $error->request = clone $request;
 
245
 
 
246
            // get a count of the number of exceptions encountered
 
247
            $this->_exceptionCountAtFirstEncounter = count($exceptions);
 
248
 
 
249
            // Forward to the error handler
 
250
            $request->setParam('error_handler', $error)
 
251
                    ->setModuleName($this->getErrorHandlerModule())
 
252
                    ->setControllerName($this->getErrorHandlerController())
 
253
                    ->setActionName($this->getErrorHandlerAction())
 
254
                    ->setDispatched(false);
 
255
        }
 
256
    }
 
257
}