Development

Changeset 7714

You must first sign up to be able to contribute.

Changeset 7714

Show
Ignore:
Timestamp:
03/02/08 14:51:08 (1 year ago)
Author:
fabien
Message:

converted response parameter holder object to an option array (and fixed sfDebug output for the sfResponse object)

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/1.1/lib/debug/sfDebug.class.php

    r5342 r7714  
    9595  public static function requestAsArray($request) 
    9696  { 
    97     if ($request) 
     97    if (!$request) 
    9898    { 
    99       $values = array( 
    100         'parameterHolder' => self::flattenParameterHolder($request->getParameterHolder()), 
    101         'attributeHolder' => self::flattenParameterHolder($request->getAttributeHolder()), 
    102       ); 
    103     } 
    104     else 
    105     { 
    106       $values = array('parameterHolder' => array(), 'attributeHolder' => array()); 
     99      return array(); 
    107100    } 
    108101 
    109     return $values; 
     102    return array( 
     103      'parameterHolder' => self::flattenParameterHolder($request->getParameterHolder()), 
     104      'attributeHolder' => self::flattenParameterHolder($request->getAttributeHolder()), 
     105    ); 
    110106  } 
    111107 
     
    119115  public static function responseAsArray($response) 
    120116  { 
    121     if ($response) 
     117    if (!$response) 
    122118    { 
    123       $values = array( 
    124         'cookies'         => array(), 
    125         'httpHeaders'     => array(), 
    126         'parameterHolder' => self::flattenParameterHolder($response->getParameterHolder()), 
    127       ); 
    128       if (method_exists($response, 'getHttpHeaders')) 
    129       { 
    130         foreach ($response->getHttpHeaders() as $key => $value) 
    131         { 
    132           $values['httpHeaders'][$key] = $value; 
    133         } 
    134       } 
    135  
    136       if (method_exists($response, 'getCookies')) 
    137       { 
    138         $cookies = array(); 
    139         foreach ($response->getCookies() as $key => $value) 
    140         { 
    141           $values['cookies'][$key] = $value; 
    142         } 
    143       } 
    144     } 
    145     else 
    146     { 
    147       $values = array('cookies' => array(), 'httpHeaders' => array(), 'parameterHolder' => array()); 
     119      return array(); 
    148120    } 
    149121 
    150     return $values; 
     122    return array( 
     123      'options'     => $response->getOptions(), 
     124      'cookies'     => method_exists($response, 'getCookies')     ? $response->getCookies() : array(), 
     125      'httpHeaders' => method_exists($response, 'getHttpHeaders') ? $response->getHttpHeaders() : array(), 
     126      'javascripts' => method_exists($response, 'getJavascripts') ? $response->getJavascripts('ALL') : array(), 
     127      'stylesheets' => method_exists($response, 'getStylesheets') ? $response->getStylesheets('ALL') : array(), 
     128      'metas'       => method_exists($response, 'getMetas')       ? $response->getMetas() : array(), 
     129      'httpMetas'   => method_exists($response, 'getHttpMetas')   ? $response->getHttpMetas() : array(), 
     130    ); 
    151131  } 
    152132 
  • branches/1.1/lib/response/sfResponse.class.php

    r7215 r7714  
    2121{ 
    2222  protected 
    23     $parameterHolder = null
    24     $dispatcher      = null, 
    25     $content         = ''; 
     23    $options    = array()
     24    $dispatcher = null, 
     25    $content    = ''; 
    2626 
    2727  /** 
     
    3030   * @see initialize() 
    3131   */ 
    32   public function __construct(sfEventDispatcher $dispatcher, $parameters = array()) 
     32  public function __construct(sfEventDispatcher $dispatcher, $options = array()) 
    3333  { 
    34     $this->initialize($dispatcher, $parameters); 
     34    $this->initialize($dispatcher, $options); 
    3535  } 
    3636 
     
    3939   * 
    4040   * @param  sfEventDispatcher  A sfEventDispatcher instance 
    41    * @param  array              An array of parameter
     41   * @param  array              An array of option
    4242   * 
    4343   * @return Boolean            true, if initialization completes successfully, otherwise false 
     
    4545   * @throws <b>sfInitializationException</b> If an error occurs while initializing this sfResponse 
    4646   */ 
    47   public function initialize(sfEventDispatcher $dispatcher, $parameters = array()) 
     47  public function initialize(sfEventDispatcher $dispatcher, $options = array()) 
    4848  { 
    4949    $this->dispatcher = $dispatcher; 
     50    $this->options = $options; 
    5051 
    51     $this->parameterHolder = new sfParameterHolder(); 
    52     $this->parameterHolder->add($parameters); 
     52    if (!isset($this->options['logging'])) 
     53    { 
     54      $this->options['logging'] = false; 
     55    } 
    5356  } 
    5457 
     
    8184    $content = $event->getReturnValue(); 
    8285 
    83     if ($this->getParameter('logging')
     86    if ($this->options['logging']
    8487    { 
    8588      $this->dispatcher->notify(new sfEvent($this, 'application.log', array(sprintf('Send content (%s o)', strlen($content))))); 
     
    97100  } 
    98101 
    99   /** 
    100    * Retrieves the parameters from the current response. 
    101    * 
    102    * @return sfParameterHolder List of parameters 
    103    */ 
    104   public function getParameterHolder() 
     102  public function getOptions() 
    105103  { 
    106     return $this->parameterHolder; 
    107   } 
    108  
    109   /** 
    110    * Retrieves a parameter from the current response. 
    111    * 
    112    * @param string A parameter name 
    113    * @param string A default paramter value 
    114    * 
    115    * @return mixed A parameter value 
    116    */ 
    117   public function getParameter($name, $default = null) 
    118   { 
    119     return $this->parameterHolder->get($name, $default); 
    120   } 
    121  
    122   /** 
    123    * Indicates whether or not a parameter exist for the current response. 
    124    * 
    125    * @param string A parameter name 
    126    * 
    127    * @return boolean true, if the parameter exists otherwise false 
    128    */ 
    129   public function hasParameter($name) 
    130   { 
    131     return $this->parameterHolder->has($name); 
    132   } 
    133  
    134   /** 
    135    * Sets a parameter for the current response. 
    136    * 
    137    * @param string A parameter name 
    138    * @param string The parameter value to be set 
    139    */ 
    140   public function setParameter($name, $value) 
    141   { 
    142     $this->parameterHolder->set($name, $value); 
     104    return $this->options; 
    143105  } 
    144106 
     
    171133  public function serialize() 
    172134  { 
    173     return serialize(array($this->content, $this->parameterHolder)); 
     135    return serialize($this->content); 
    174136  } 
    175137 
     
    183145    $this->initialize(sfContext::hasInstance() ? sfContext::getInstance()->getEventDispatcher() : new sfEventDispatcher()); 
    184146 
    185     list($this->content, $this->parameterHolder) = $data; 
     147    $this->content = $data; 
    186148  } 
    187149} 
  • branches/1.1/lib/response/sfWebResponse.class.php

    r7713 r7714  
    3939   * 
    4040   * @param  sfEventDispatcher  A sfEventDispatcher instance 
    41    * @param  array              An array of parameter
     41   * @param  array              An array of option
    4242   * 
    4343   * @return Boolean            true, if initialization completes successfully, otherwise false 
     
    4545   * @throws <b>sfInitializationException</b> If an error occurs while initializing this sfResponse 
    4646   */ 
    47   public function initialize(sfEventDispatcher $dispatcher, $parameters = array()) 
    48   { 
    49     parent::initialize($dispatcher, $parameters); 
     47  public function initialize(sfEventDispatcher $dispatcher, $options = array()) 
     48  { 
     49    parent::initialize($dispatcher, $options); 
    5050 
    5151    $this->javascripts = array_combine($this->positions, array_fill(0, count($this->positions), array())); 
    5252    $this->stylesheets = array_combine($this->positions, array_fill(0, count($this->positions), array())); 
     53 
     54    if (!isset($this->options['charset'])) 
     55    { 
     56      $this->options['charset'] = 'utf-8'; 
     57    } 
    5358 
    5459    $this->statusTexts = array( 
     
    253258    if (false === stripos($value, 'charset') && (0 === stripos($value, 'text/') || strlen($value) - 3 === strripos($value, 'xml'))) 
    254259    { 
    255       $value .= '; charset='.$this->getParameter('charset')
     260      $value .= '; charset='.$this->options['charset']
    256261    } 
    257262 
     
    266271  public function getContentType() 
    267272  { 
    268     return $this->getHttpHeader('Content-Type', 'text/html; charset='.$this->getParameter('charset')); 
     273    return $this->getHttpHeader('Content-Type', 'text/html; charset='.$this->options['charset']); 
    269274  } 
    270275 
     
    284289    header($status); 
    285290 
    286     if ($this->getParameter('logging')
     291    if ($this->options['logging']
    287292    { 
    288293      $this->dispatcher->notify(new sfEvent($this, 'application.log', array(sprintf('Send status "%s"', $status)))); 
     
    294299      header($name.': '.$value); 
    295300 
    296       if ($value != '' && $this->getParameter('logging')
     301      if ($value != '' && $this->options['logging']
    297302      { 
    298303        $this->dispatcher->notify(new sfEvent($this, 'application.log', array(sprintf('Send header "%s": "%s"', $name, $value)))); 
     
    312317      } 
    313318 
    314       if ($this->getParameter('logging')
     319      if ($this->options['logging']
    315320      { 
    316321        $this->dispatcher->notify(new sfEvent($this, 'application.log', array(sprintf('Send cookie "%s": "%s"', $cookie['name'], $cookie['value'])))); 
     
    378383    else 
    379384    { 
    380       throw new sfParameterException('The second getDate() method parameter must be one of: rfc1123, rfc1036 or asctime.'); 
     385      throw new InvalidArgumentException('The second getDate() method parameter must be one of: rfc1123, rfc1036 or asctime.'); 
    381386    } 
    382387  } 
     
    510515    if ($escape) 
    511516    { 
    512       $value = htmlentities($value, ENT_QUOTES, $this->getParameter('charset')); 
     517      $value = htmlentities($value, ENT_QUOTES, $this->options['charset']); 
    513518    } 
    514519 
     
    679684  public function mergeProperties(sfWebResponse $response) 
    680685  { 
    681     $this->parameterHolder = clone $response->getParameterHolder(); 
    682     $this->headers         = $response->getHttpHeaders(); 
    683     $this->metas           = $response->getMetas(); 
    684     $this->httpMetas       = $response->getHttpMetas(); 
    685     $this->stylesheets     = $response->getStylesheets('ALL'); 
    686     $this->javascripts     = $response->getJavascripts('ALL'); 
    687     $this->slots           = $response->getSlots(); 
     686    $this->options      = $response->getOptions(); 
     687    $this->headers      = $response->getHttpHeaders(); 
     688    $this->metas        = $response->getMetas(); 
     689    $this->httpMetas    = $response->getHttpMetas(); 
     690    $this->stylesheets  = $response->getStylesheets('ALL'); 
     691    $this->javascripts  = $response->getJavascripts('ALL'); 
     692    $this->slots        = $response->getSlots(); 
    688693  } 
    689694 
     
    695700  public function serialize() 
    696701  { 
    697     return serialize(array($this->content, $this->statusCode, $this->statusText, $this->parameterHolder, $this->cookies, $this->headerOnly, $this->headers, $this->metas, $this->httpMetas, $this->stylesheets, $this->javascripts, $this->slots)); 
     702    return serialize(array($this->content, $this->statusCode, $this->statusText, $this->options, $this->cookies, $this->headerOnly, $this->headers, $this->metas, $this->httpMetas, $this->stylesheets, $this->javascripts, $this->slots)); 
    698703  } 
    699704 
     
    707712    $this->initialize(sfContext::hasInstance() ? sfContext::getInstance()->getEventDispatcher() : new sfEventDispatcher()); 
    708713 
    709     list($this->content, $this->statusCode, $this->statusText, $this->parameterHolder, $this->cookies, $this->headerOnly, $this->headers, $this->metas, $this->httpMetas, $this->stylesheets, $this->javascripts, $this->slots) = $data; 
     714    list($this->content, $this->statusCode, $this->statusText, $this->options, $this->cookies, $this->headerOnly, $this->headers, $this->metas, $this->httpMetas, $this->stylesheets, $this->javascripts, $this->slots) = $data; 
    710715  } 
    711716 
  • branches/1.1/test/unit/response/sfResponseTest.php

    r5016 r7714  
    2121} 
    2222 
    23 $t = new lime_test(17, new lime_output_color()); 
     23$t = new lime_test(7, new lime_output_color()); 
    2424 
    2525$dispatcher = new sfEventDispatcher(); 
     
    2828$t->diag('->initialize()'); 
    2929$response = new myResponse($dispatcher, array('foo' => 'bar')); 
    30 $t->is($response->getParameter('foo'), 'bar', '->initialize() takes an array of parameters as its second argument'); 
     30$options = $response->getOptions(); 
     31$t->is($options['foo'], 'bar', '->initialize() takes an array of options as its second argument'); 
    3132 
    3233// ->getContent() ->setContent() 
     
    4748$t->ok(new myResponse($dispatcher) instanceof Serializable, 'sfResponse implements the Serializable interface'); 
    4849 
    49 // parameter holder proxy 
    50 require_once($_test_dir.'/unit/sfParameterHolderTest.class.php'); 
    51 $response = new myResponse($dispatcher); 
    52 $pht = new sfParameterHolderProxyTest($t); 
    53 $pht->launchTests($response, 'parameter'); 
    54  
    5550// new methods via sfEventDispatcher 
    5651require_once($_test_dir.'/unit/sfEventDispatcherTest.class.php'); 
  • branches/1.1/test/unit/response/sfWebResponseTest.php

    r7713 r7714  
    9898$t->diag('->getContentType() ->setContentType()'); 
    9999 
    100 $response->setParameter('charset', 'UTF-8'); 
    101  
    102 $t->is($response->getContentType(), 'text/html; charset=UTF-8', '->getContentType() returns a sensible default value'); 
     100$t->is($response->getContentType(), 'text/html; charset=utf-8', '->getContentType() returns a sensible default value'); 
    103101 
    104102$response->setContentType('text/xml'); 
    105 $t->is($response->getContentType(), 'text/xml; charset=UTF-8', '->setContentType() adds a charset if none is given'); 
     103$t->is($response->getContentType(), 'text/xml; charset=utf-8', '->setContentType() adds a charset if none is given'); 
    106104 
    107105$response->setContentType('application/vnd.mozilla.xul+xml'); 
    108 $t->is($response->getContentType(), 'application/vnd.mozilla.xul+xml; charset=UTF-8', '->setContentType() adds a charset if none is given'); 
     106$t->is($response->getContentType(), 'application/vnd.mozilla.xul+xml; charset=utf-8', '->setContentType() adds a charset if none is given'); 
    109107 
    110108$response->setContentType('image/jpg'); 

The Sensio Labs Network

Since 1998, Sensio Labs has been promoting the Open-Source software movement by providing quality web application development, training, consulting, and supporting several large Open-Source projects.