Web Commons
Current file: /home/antoine/zavaee/Web Commons/WebCommons/environment.php
Legend: executed not executed dead code

  Coverage
  Classes Functions / Methods Lines
Total
100.00%100.00%
100.00% 3 / 3
100.00%100.00%
100.00% 4 / 4
100.00%100.00%
100.00% 12 / 12
 
AbstractContext
100.00%100.00%
100.00% 1 / 1
100.00%100.00%
100.00% 1 / 1
100.00%100.00%
100.00% 9 / 9
 public function __get($attr)
100.00%100.00%
100.00% 1 / 1
100.00%100.00%
100.00% 9 / 9
HTTPRequest
100.00%100.00%
100.00% 1 / 1
100.00%100.00%
100.00% 3 / 3
100.00%100.00%
100.00% 3 / 3
 public function method()
100.00%100.00%
100.00% 1 / 1
100.00%100.00%
100.00% 1 / 1
 public function post($key, $default=Null)
100.00%100.00%
100.00% 1 / 1
100.00%100.00%
100.00% 1 / 1
 public function get($key, $default=Null)
100.00%100.00%
100.00% 1 / 1
100.00%100.00%
100.00% 1 / 1
ContextException
100.00%100.00%
100.00% 1 / 1
  
   
100.00%100.00%
100.00% 0 / 0


       1                 : <?php                                                                           
       2                 : /**                                                                             
       3                 :  * This file groups classes pertaining to execution environment.                
       4                 :  *                                                                               
       5                 :  * @author Antoine d'Otreppe de Bouvette <a.dotreppe@aspyct.org>                
       6                 :  * @license http://www.opensource.org/licenses/mit-license.php                  
       7                 :  * @version 0.1dev                                                              
       8                 :  */                                                                             
       9                 :                                                                                 
      10                 : /**                                                                             
      11                 :  * Offers read-only properties related to application context.                  
      12                 :  *                                                                               
      13                 :  * This class offers read-only properties named after the existing createXxx    
      14                 :  * methods. A "createConfig" method would offer a "config" attribute.           
      15                 :  * These "createXxx" methods will be called only once, so that each represent   
      16                 :  * a kind of singleton.                                                         
      17                 :  * This is useful for providing a global access to main application resources   
      18                 :  * like configuration, request objects and such.                                
      19                 :  * Some methods are provided as a default behaviour.                            
      20                 :  *                                                                               
      21                 :  * @since 0.1                                                                   
      22                 :  */                                                                             
      23                 : abstract class AbstractContext {                                                
      24                 :     /**                                                                         
      25                 :      * Contains the previously created properties.                              
      26                 :      *                                                                           
      27                 :      * @var array                                                               
      28                 :      */                                                                         
      29                 :     private $props = array();                                                   
      30                 :                                                                                     
      31                 :     /**                                                                         
      32                 :      * Returns the object/scalar/array/whatever created by the method           
      33                 :      * "createXxx" where "Xxx" = ucfirst($attr). If no such method exists, a    
      34                 :      * ContextException is thrown.                                              
      35                 :      *                                                                           
      36                 :      * @param string $attr                                                      
      37                 :      * @return mixed                                                            
      38                 :      * @since 0.1                                                               
      39                 :      */                                                                         
      40                 :     public function __get($attr) {                                              
      41               3 :         if (!array_key_exists($attr, $this->props)) {                           
      42               3 :             $method = array($this, 'create' . ucfirst($attr));                  
      43                 :                                                                                             
      44               3 :             if (is_callable($method)) {                                         
      45               2 :                 $this->props[$attr] = call_user_func($method);                  
      46               2 :             }                                                                   
      47                 :             else {                                                              
      48               1 :                 throw new ContextException(                                     
      49               1 :                     'No attribute "' . $attr . '" in context.');                
      50                 :             }                                                                   
      51               2 :         }                                                                       
      52                 :                                                                                         
      53               2 :         return $this->props[$attr];                                             
      54                 :     }                                                                           
      55                 : }                                                                               
      56                 :                                                                                 
      57                 : /**                                                                             
      58                 :  * Provides access to request parameters.                                       
      59                 :  * @since 0.1                                                                   
      60                 :  */                                                                             
      61                 : interface Request {                                                             
      62                 :     /**                                                                         
      63                 :      * Returns the request method, uppercased.                                  
      64                 :      * @return string                                                           
      65                 :      * @since 0.1                                                               
      66                 :      */                                                                         
      67                 :     function method();                                                          
      68                 :                                                                                     
      69                 :     /**                                                                         
      70                 :      * Returns the GET parameter corresponding to $key, or $default if this key 
      71                 :      * is not defined.                                                          
      72                 :      *                                                                           
      73                 :      * @param string $key                                                       
      74                 :      * @param mixed $default                                                    
      75                 :      * @return mixed                                                            
      76                 :      * @since 0.1                                                               
      77                 :      */                                                                         
      78                 :     function get($key, $default=Null);                                          
      79                 :                                                                                     
      80                 :     /**                                                                         
      81                 :      * Returns the POST parameter corresponding to $key, or $default if this key
      82                 :      * is not defined.                                                          
      83                 :      *                                                                           
      84                 :      * @param string $key                                                       
      85                 :      * @param mixed $default                                                    
      86                 :      * @return mixed                                                            
      87                 :      * @since 0.1                                                               
      88                 :      */                                                                         
      89                 :     function post($key, $default=Null);                                         
      90                 : }                                                                               
      91                 :                                                                                 
      92                 : /**                                                                             
      93                 :  * A request that fits a standard web environment.                              
      94                 :  * See {@link Request} for method details.                                      
      95                 :  *                                                                               
      96                 :  * @since 0.1                                                                   
      97                 :  */                                                                             
      98                 : class HTTPRequest implements Request {                                          
      99                 :     /**                                                                         
     100                 :      * (non-PHPdoc)                                                             
     101                 :      * @see WebCommons/Request::method()                                        
     102                 :      */                                                                         
     103                 :     public function method() {                                                  
     104               1 :         return strtoupper($_SERVER['REQUEST_METHOD']);                          
     105                 :     }                                                                           
     106                 :                                                                                     
     107                 :     /**                                                                         
     108                 :      * (non-PHPdoc)                                                             
     109                 :      * @see WebCommons/Request::post()                                          
     110                 :      */                                                                         
     111                 :     public function post($key, $default=Null) {                                 
     112               2 :         return array_get_default($_POST, $key, $default);                       
     113                 :     }                                                                           
     114                 :                                                                                     
     115                 :     /**                                                                         
     116                 :      * (non-PHPdoc)                                                             
     117                 :      * @see WebCommons/Request::get()                                           
     118                 :      */                                                                         
     119                 :     public function get($key, $default=Null) {                                  
     120               2 :         return array_get_default($_GET, $key, $default);                        
     121                 :     }                                                                           
     122                 : }                                                                               
     123                 :                                                                                 
     124                 : /**                                                                             
     125                 :  * Exception thrown when a requested attribute is missing in a Context object.  
     126                 :  *                                                                               
     127                 :  * @since 0.1                                                                   
     128                 :  */                                                                             
     129                 : class ContextException extends Exception {}                                     

Generated by PHPUnit 3.4.5 and Xdebug 2.1.0 at Fri Jul 16 0:48:38 CEST 2010.