eZ Publish  [4.0]
ezexecution.php
Go to the documentation of this file.
00001 <?php
00002 //
00003 // Definition of eZExecution class
00004 //
00005 // Created on: <29-Nov-2002 11:24:42 amos>
00006 //
00007 // ## BEGIN COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00008 // SOFTWARE NAME: eZ Publish
00009 // SOFTWARE RELEASE: 4.0.x
00010 // COPYRIGHT NOTICE: Copyright (C) 1999-2008 eZ Systems AS
00011 // SOFTWARE LICENSE: GNU General Public License v2.0
00012 // NOTICE: >
00013 //   This program is free software; you can redistribute it and/or
00014 //   modify it under the terms of version 2.0  of the GNU General
00015 //   Public License as published by the Free Software Foundation.
00016 //
00017 //   This program is distributed in the hope that it will be useful,
00018 //   but WITHOUT ANY WARRANTY; without even the implied warranty of
00019 //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020 //   GNU General Public License for more details.
00021 //
00022 //   You should have received a copy of version 2.0 of the GNU General
00023 //   Public License along with this program; if not, write to the Free
00024 //   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00025 //   MA 02110-1301, USA.
00026 //
00027 //
00028 // ## END COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00029 //
00030 
00031 /*! \file ezexecution.php
00032 */
00033 
00034 /*!
00035   \class eZExecution ezexecution.php
00036   \brief Handles proper script execution, fatal error detection and handling.
00037 
00038   By registering a fatal error handler it's possible for the PHP script to
00039   catch fatal errors, such as "Call to a member function on a non-object".
00040 
00041   By registering a cleanup handler it's possible to make sure the script can
00042   end properly.
00043 */
00044 
00045 class eZExecution
00046 {
00047     /*!
00048      Sets the clean exit flag to on,
00049      this notifies the exit handler that everything finished properly.
00050     */
00051     static function setCleanExit()
00052     {
00053         $GLOBALS['eZExecutionCleanExit'] = true;
00054     }
00055 
00056     /*!
00057      Calls the cleanup handlers to make sure that the script is ready to exit.
00058     */
00059     static function cleanup()
00060     {
00061         $handlers = eZExecution::cleanupHandlers();
00062         foreach ( $handlers as $handler )
00063         {
00064             if ( function_exists( $handler ) )
00065                 $handler();
00066         }
00067     }
00068 
00069     /*!
00070      Adds a cleanup handler to the end of the list,
00071      \a $handler must contain the name of the function to call.
00072      The function is called at the end of the script execution to
00073      do some cleanups.
00074     */
00075     static function addCleanupHandler( $handler )
00076     {
00077         $handlers =& eZExecution::cleanupHandlers();
00078         $handlers[] = $handler;
00079     }
00080 
00081     /*!
00082      \return An array with cleanup handlers.
00083     */
00084     static function &cleanupHandlers()
00085     {
00086         $handlers =& $GLOBALS['eZExecutionCleanupHandlers'];
00087         if ( !isset( $handlers ) )
00088             $handlers = array();
00089         return $handlers;
00090     }
00091 
00092     /*!
00093      Adds a fatal error handler to the end of the list,
00094      \a $handler must contain the name of the function to call.
00095      The handler will be called whenever a fatal error occurs,
00096      which usually happens when the script did not finish.
00097     */
00098     static function addFatalErrorHandler( $handler )
00099     {
00100         $handlers =& eZExecution::fatalErrorHandlers();
00101         $handlers[] = $handler;
00102     }
00103 
00104     /*!
00105      \return An array with fatal error handlers.
00106     */
00107     static function &fatalErrorHandlers()
00108     {
00109         $handlers =& $GLOBALS['eZExecutionFatalErrorHandlers'];
00110         if ( !isset( $handlers ) )
00111             $handlers = array();
00112         return $handlers;
00113     }
00114 
00115     /*!
00116      \return true if the request finished properly.
00117     */
00118     static function isCleanExit()
00119     {
00120         return $GLOBALS['eZExecutionCleanExit'];
00121     }
00122 
00123     /*!
00124      Sets the clean exit flag and exits the page.
00125      Use this if you want premature exits instead of the \c exit function.
00126     */
00127     static function cleanExit()
00128     {
00129         eZExecution::cleanup();
00130         eZExecution::setCleanExit();
00131         exit;
00132     }
00133 
00134 }
00135 
00136 
00137 /*!
00138  Exit handler which called after the script is done, if it detects
00139  that eZ Publish did not exit cleanly it will issue an error message
00140  and display the debug.
00141 */
00142 function eZExecutionUncleanShutdownHandler()
00143 {
00144     // Need to change the current directory, since this information is lost
00145     // when the callbackfunction is called. eZDocumentRoot is set in index.php.
00146     if ( isset( $GLOBALS['eZDocumentRoot'] ) )
00147     {
00148         $documentRoot = $GLOBALS['eZDocumentRoot'];
00149         chdir( $documentRoot );
00150     }
00151 
00152     if ( eZExecution::isCleanExit() )
00153         return;
00154     eZExecution::cleanup();
00155     $handlers =& eZExecution::fatalErrorHandlers();
00156     foreach ( $handlers as $handler )
00157     {
00158         if ( function_exists( $handler ) )
00159             $handler();
00160     }
00161 }
00162 
00163 register_shutdown_function( 'eZExecutionUncleanShutdownHandler' );
00164 
00165 $GLOBALS['eZExecutionCleanExit'] = false;
00166 
00167 ?>