Exponential 6.0.13 Exponential Enterprise Open Source CMS - PHP Class Reference

ezexecution.php
Go to the documentation of this file.
1 <?php
22 class eZExecution
23 {
28 static function setCleanExit( $hasCleanExit = true )
29 {
30 self::$hasCleanExit = $hasCleanExit;
31 }
32
36 static function cleanup()
37 {
38 $handlers = eZExecution::cleanupHandlers();
39 foreach ( $handlers as $handler )
40 {
41 if ( is_callable( $handler ) )
42 call_user_func( $handler );
43 else
44 eZDebug::writeError('Could not call cleanup handler, is it a static public function?', __METHOD__ );
45 }
46 }
47
54 static function addCleanupHandler( $handler )
55 {
56 self::registerShutdownHandler();
57 self::$cleanupHandlers[] = $handler;
58 }
59
63 static function cleanupHandlers()
64 {
65 return self::$cleanupHandlers;
66 }
67
74 static function addFatalErrorHandler( $handler )
75 {
76 self::registerShutdownHandler();
77 self::$fatalErrorHandlers[] = $handler;
78 }
79
83 static function fatalErrorHandlers()
84 {
85 return self::$fatalErrorHandlers;
86 }
87
91 static function isCleanExit()
92 {
93 return self::$hasCleanExit;
94 }
95
100 static function cleanExit()
101 {
102 eZExecution::cleanup();
103 eZExecution::setCleanExit();
104 exit;
105 }
106
112 static function uncleanShutdownHandler()
113 {
114 // Need to change the current directory, since this information is lost
115 // when the callbackfunction is called. eZDocumentRoot is set in ::registerShutdownHandler
116 // Getting the previous current working directory as we might need to get back there (i.e. Symfony web/ directory).
117 $previousCwd = getcwd();
118 if ( self::$eZDocumentRoot !== null )
119 {
120 chdir( self::$eZDocumentRoot );
121 }
122
123 if ( eZExecution::isCleanExit() )
124 {
125 chdir( $previousCwd );
126 return;
127 }
128
129 eZExecution::cleanup();
130 $handlers = eZExecution::fatalErrorHandlers();
131 foreach ( $handlers as $handler )
132 {
133 if ( is_callable( $handler ) )
134 call_user_func( $handler );
135 else
136
137 eZDebug::writeError('Could not call fatal error handler, is it a static public function?', __METHOD__ );
138 }
139
140 chdir( $previousCwd );
141 }
142
146 static public function registerShutdownHandler( $documentRoot = false )
147 {
148 if ( !self::$shutdownHandle )
149 {
150 register_shutdown_function( array('eZExecution', 'uncleanShutdownHandler') );
151 /*
152 see:
153 - http://www.php.net/manual/en/function.session-set-save-handler.php
154 - http://bugs.php.net/bug.php?id=33635
155 - http://bugs.php.net/bug.php?id=33772
156 */
157 register_shutdown_function( array('eZSession', 'stop') );
158 set_exception_handler( array('eZExecution', 'defaultExceptionHandler') );
159 self::$shutdownHandle = true;
160 }
161
162 // Needed by the error handler, since the current directory is lost when
163 // the callback function eZExecution::uncleanShutdownHandler is called.
164 if ( $documentRoot )
165 {
166 self::$eZDocumentRoot = $documentRoot;
167 }
168 else if ( self::$eZDocumentRoot === null )
169 {
170 self::$eZDocumentRoot = getcwd();
171 }
172 }
173
180 static public function defaultExceptionHandler( $e )
181 {
182 if( PHP_SAPI != 'cli' )
183 {
184 header( 'HTTP/1.x 500 Internal Server Error' );
185 header( 'Content-Type: text/html' );
186
187 echo "An unexpected error has occurred. Please contact the webmaster.<br />";
188
189 if( eZDebug::isDebugEnabled() )
190 {
191 echo $e->getMessage() . ' in ' . $e->getFile() . ' on line ' . $e->getLine();
192 }
193 }
194 else
195 {
196 $cli = eZCLI::instance();
197 $cli->error( "An unexpected error has occurred. Please contact the webmaster.");
198
199 if( eZDebug::isDebugEnabled() )
200 {
201 $cli->error( $e->getMessage() . ' in ' . $e->getFile() . ' on line ' . $e->getLine() );
202 }
203 }
204
205 eZLog::write( 'Unexpected error, the message was : ' . $e->getMessage() . ' in ' . $e->getFile() . ' on line ' . $e->getLine(), 'error.log' );
206
207 eZExecution::cleanup();
208 eZExecution::setCleanExit();
209 exit( 1 );
210 }
211
212 static private $eZDocumentRoot = null;
213 static private $hasCleanExit = false;
214 static private $shutdownHandle = false;
215 static private $fatalErrorHandlers = array();
216 static private $cleanupHandlers = array();
217 }
218
219
220 ?>
eZCLI\instance
static instance()
Definition: ezcli.php:717
eZDebug\writeError
static writeError( $string, $label="", $backgroundClass="")
Definition: ezdebug.php:545
eZDebug\isDebugEnabled
static isDebugEnabled()
Definition: ezdebug.php:1027
eZExecution
Handles proper script execution, fatal error detection and handling.
Definition: ezexecution.php:23
eZExecution\$hasCleanExit
static $hasCleanExit
Definition: ezexecution.php:213
eZExecution\addCleanupHandler
static addCleanupHandler( $handler)
Definition: ezexecution.php:54
eZExecution\setCleanExit
static setCleanExit( $hasCleanExit=true)
Definition: ezexecution.php:28
eZExecution\cleanup
static cleanup()
Definition: ezexecution.php:36
eZExecution\defaultExceptionHandler
static defaultExceptionHandler( $e)
Definition: ezexecution.php:180
eZExecution\cleanExit
static cleanExit()
Definition: ezexecution.php:100
eZExecution\fatalErrorHandlers
static fatalErrorHandlers()
Definition: ezexecution.php:83
eZExecution\registerShutdownHandler
static registerShutdownHandler( $documentRoot=false)
Definition: ezexecution.php:146
eZExecution\$fatalErrorHandlers
static $fatalErrorHandlers
Definition: ezexecution.php:215
eZExecution\$cleanupHandlers
static $cleanupHandlers
Definition: ezexecution.php:216
eZExecution\cleanupHandlers
static cleanupHandlers()
Definition: ezexecution.php:63
eZExecution\addFatalErrorHandler
static addFatalErrorHandler( $handler)
Definition: ezexecution.php:74
eZExecution\uncleanShutdownHandler
static uncleanShutdownHandler()
Definition: ezexecution.php:112
eZExecution\isCleanExit
static isCleanExit()
Definition: ezexecution.php:91
eZExecution\$shutdownHandle
static $shutdownHandle
Definition: ezexecution.php:214
eZExecution\$eZDocumentRoot
static $eZDocumentRoot
Definition: ezexecution.php:212
eZLog\write
static write( $message, $logName='common.log', $dir='var/log')
Definition: ezlog.php:29
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.