Changeset 1094

Show
Ignore:
Timestamp:
11/30/07 00:12:08 (9 months ago)
Author:
mikey
Message:

more thoughts on unified mode handling (enhancement #113)

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/main/php/net/stubbles/lang/stubMode.php

    r1089 r1094  
    1515 * or not. Currently, there are four different modes available: 
    1616 * stubMode::$PROD 
    17  *      - uses exception handler net::stubbles::util::errorhandler::stubProdModeExceptionHandler 
    18  *      - uses default error handler net.stubbles.util.errorhandler.stubDefaultErrorHandler 
     17 *      - uses exception handler net::stubbles::lang::errorhandler::stubProdModeExceptionHandler 
     18 *      - uses default error handler net.stubbles.lang.errorhandler.stubDefaultErrorHandler 
    1919 *      - caching enabled 
    2020 * stubMode::$TEST 
    21  *      - uses exception handler net::stubbles::util::errorhandler::stubDisplayExceptionHandler 
     21 *      - uses exception handler net::stubbles::lang::errorhandler::stubDisplayExceptionHandler 
    2222 *      - no error handler 
    2323 *      - caching enabled 
    2424 * stubMode::$STAGE 
    25  *      - uses exception handler net::stubbles::util::errorhandler::stubDisplayExceptionHandler 
     25 *      - uses exception handler net::stubbles::lang::errorhandler::stubDisplayExceptionHandler 
    2626 *      - no error handler 
    2727 *      - caching disabled 
    2828 * stubMode::$DEV 
    29  *      - uses exception handler net::stubbles::util::errorhandler::stubDisplayExceptionHandler 
     29 *      - uses exception handler net::stubbles::lang::errorhandler::stubDisplayExceptionHandler 
    3030 *      - no error handler 
    3131 *      - caching disabled 
     
    4747{ 
    4848    /** 
     49     * handler method must be called statically 
     50     */ 
     51    const HANDLER_STATIC        = 'static'; 
     52    /** 
     53     * handler has to be an instance 
     54     */ 
     55    const HANDLER_INSTANCE      = 'instance'; 
     56    /** 
    4957     * mode: production 
    5058     * 
     
    94102    public static function __static() 
    95103    { 
     104        // production mode 
    96105        self::$PROD                     = new self('PROD', 0); 
    97         self::$PROD->exceptionHandler   = array('class'  => 'net.stubbles.util.errorhandler.stubProdModeExceptionHandler', 
    98                                                 'method' => 'handle' 
    99                                           ); 
    100         self::$PROD->errorHandler       = array('class'  => 'net.stubbles.util.errorhandler.stubDefaultErrorHandler', 
    101                                                 'method' => 'handle' 
    102                                           ); 
     106        self::$PROD->exceptionHandler   = array('class'  => 'net.stubbles.lang.errorhandler.stubProdModeExceptionHandler', 
     107                                                'method' => 'handle', 
     108                                                'type'   => self::HANDLER_INSTANCE 
     109                                          ); 
     110        self::$PROD->errorHandler       = array('class'  => 'net.stubbles.lang.errorhandler.stubDefaultErrorHandler', 
     111                                                'method' => 'handle', 
     112                                                'type'   => self::HANDLER_INSTANCE 
     113                                          ); 
     114         
     115        // test mode 
    103116        self::$TEST                     = new self('TEST', 1); 
    104         self::$TEST->exceptionHandler   = array('class'  => 'net.stubbles.util.errorhandler.stubDisplayExceptionHandler', 
    105                                                 'method' => 'handle' 
    106                                           ); 
     117        self::$TEST->exceptionHandler   = array('class'  => 'net.stubbles.lang.errorhandler.stubDisplayExceptionHandler', 
     118                                                'method' => 'handle', 
     119                                                'type'   => self::HANDLER_INSTANCE 
     120                                          ); 
     121         
     122        // stage mode 
    107123        self::$STAGE                    = new self('STAGE', 2); 
    108         self::$STAGE->exceptionHandler  = array('class'  => 'net.stubbles.util.errorhandler.stubDisplayExceptionHandler', 
    109                                                 'method' => 'handle' 
     124        self::$STAGE->exceptionHandler  = array('class'  => 'net.stubbles.lang.errorhandler.stubDisplayExceptionHandler', 
     125                                                'method' => 'handle', 
     126                                                'type'   => self::HANDLER_INSTANCE 
    110127                                          ); 
    111128        self::$STAGE->enableCache       = false; 
     129         
     130        // development mode 
    112131        self::$DEV                      = new self('STAGE', 3); 
    113         self::$DEV->exceptionHandler    = array('class'  => 'net.stubbles.util.errorhandler.stubDisplayExceptionHandler', 
    114                                                 'method' => 'handle' 
     132        self::$DEV->exceptionHandler    = array('class'  => 'net.stubbles.lang.errorhandler.stubDisplayExceptionHandler', 
     133                                                'method' => 'handle', 
     134                                                'type'   => self::HANDLER_INSTANCE 
    115135                                          ); 
    116136        self::$DEV->enableCache         = false; 
     
    122142     * To register the new exception handler call registerExceptionHandler(). 
    123143     * 
    124      * @param  string  $fqClassName 
    125      * @param  string  $methodName 
    126      */ 
    127     public function setExceptionHandler($fqClassName, $methodName) 
    128     { 
    129         $this->exceptionHandler = array('class'  => $fqClassName, 
    130                                         'method' => $methodName 
     144     * @param  string|object  $class        name or instance of exception handler class 
     145     * @param  string         $methodName   name of exception handler method 
     146     * @param  string         $type         optional  whether method has to be called statically or via an instance 
     147     */ 
     148    public function setExceptionHandler($class, $methodName, $type = self::HANDLER_INSTANCE) 
     149    { 
     150        $this->exceptionHandler = array('class'  => $class, 
     151                                        'method' => $methodName, 
     152                                        'type'   => $type 
    131153                                  ); 
    132154    } 
     
    143165        } 
    144166 
    145         if (class_exists($this->exceptionHandler['class'], false) === false) { 
    146             stubClassLoader::load($this->exceptionHandler['class']); 
    147         } 
    148          
    149         set_exception_handler(array(stubClassLoader::getNonQualifiedClassName($this->exceptionHandler['class']), $this->exceptionHandler['method'])); 
     167        set_exception_handler($this->getCallback($this->exceptionHandler)); 
    150168        return true; 
    151169    } 
     
    156174     * To register the new error handler call registerErrorHandler(). 
    157175     * 
    158      * @param  string  $fqClassName 
    159      * @param  string  $methodName 
    160      */ 
    161     public function setErrorHandler($fqClassName, $methodName) 
    162     { 
    163         $this->errorHandler = array('class'  => $fqClassName, 
    164                                     'method' => $methodName 
     176     * @param  string|object  $class        name or instance of error handler class 
     177     * @param  string         $methodName   name of error handler method 
     178     * @param  string         $type         optional  whether method has to be called statically or via an instance 
     179     */ 
     180    public function setErrorHandler($class, $methodName, $type = self::HANDLER_INSTANCE) 
     181    { 
     182        $this->errorHandler = array('class'  => $class, 
     183                                    'method' => $methodName, 
     184                                    'type'   => $type 
    165185                              ); 
    166186    } 
     
    176196            return false; 
    177197        } 
    178  
    179         if (class_exists($this->errorHandler['class'], false) === false) { 
    180             stubClassLoader::load($this->errorHandler['class']); 
    181         } 
    182          
    183         set_error_handler(array(stubClassLoader::getNonQualifiedClassName($this->errorHandler['class']), $this->errorHandler['method'])); 
     198         
     199        set_error_handler($this->getCallback($this->errorHandler)); 
    184200        return true; 
    185201    } 
     
    193209    { 
    194210        return $this->cacheEnabled; 
     211    } 
     212 
     213    /** 
     214     * helper method to create the callback from the handler data 
     215     * 
     216     * @param   array     $handler  handler data 
     217     * @return  callback 
     218     */ 
     219    protected function getCallback(array &$handler) 
     220    { 
     221        if (is_string($handler['class']) === true && class_exists($handler['class'], false) === false) { 
     222            stubClassLoader::load($handler['class']); 
     223        } 
     224         
     225        if (self::HANDLER_INSTANCE === $handler['type']) { 
     226            if (is_string($handler['class']) === true) { 
     227                $instance = stubClassLoader::getNonQualifiedClassName($handler['class']); 
     228            } else { 
     229                $instance = $handler['class']; 
     230            } 
     231             
     232             return array($instance, $handler['method']); 
     233        } 
     234         
     235        return array(stubClassLoader::getNonQualifiedClassName($handler['class']), $handler['method']); 
    195236    } 
    196237}