Changeset 7713
- Timestamp:
- 03/02/08 14:28:53 (1 year ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
branches/1.1/lib/plugins/sfCompat10Plugin/lib/helper/AssetHelper.php
r5089 r7713 456 456 $html = ''; 457 457 458 foreach ( array('first', '', 'last') as $position)458 foreach ($response->getPositions() as $position) 459 459 { 460 460 foreach ($response->getJavascripts($position) as $files => $options) … … 505 505 $html = ''; 506 506 507 foreach ( array('first', '', 'last') as $position)507 foreach ($response->getPositions() as $position) 508 508 { 509 509 foreach ($response->getStylesheets($position) as $files => $options) branches/1.1/lib/response/sfWebResponse.class.php
r6988 r7713 30 30 $metas = array(), 31 31 $httpMetas = array(), 32 $positions = array('first', '', 'last'), 32 33 $stylesheets = array(), 33 34 $javascripts = array(), … … 47 48 { 48 49 parent::initialize($dispatcher, $parameters); 50 51 $this->javascripts = array_combine($this->positions, array_fill(0, count($this->positions), array())); 52 $this->stylesheets = array_combine($this->positions, array_fill(0, count($this->positions), array())); 49 53 50 54 $this->statusTexts = array( … … 538 542 539 543 /** 544 * Returns the available position names for stylesheets and javascripts in order. 545 * 546 * @return array An array of position names 547 */ 548 public function getPositions() 549 { 550 return $this->positions; 551 } 552 553 /** 540 554 * Retrieves stylesheets for the current web response. 541 555 * … … 550 564 return $this->stylesheets; 551 565 } 566 567 $this->validatePosition($position); 552 568 553 569 return isset($this->stylesheets[$position]) ? $this->stylesheets[$position] : array(); … … 563 579 public function addStylesheet($css, $position = '', $options = array()) 564 580 { 565 if (!isset($this->stylesheets[$position])) 566 { 567 $this->stylesheets[$position] = array(); 568 } 581 $this->validatePosition($position); 569 582 570 583 $this->stylesheets[$position][$css] = $options; … … 584 597 return $this->javascripts; 585 598 } 599 600 $this->validatePosition($position); 586 601 587 602 return isset($this->javascripts[$position]) ? $this->javascripts[$position] : array(); … … 597 612 public function addJavascript($js, $position = '', $options = array()) 598 613 { 599 if (!isset($this->javascripts[$position])) 600 { 601 $this->javascripts[$position] = array(); 602 } 614 $this->validatePosition($position); 603 615 604 616 $this->javascripts[$position][$js] = $options; … … 697 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; 698 710 } 711 712 /** 713 * Validate a position name. 714 * 715 * @throws InvalidArgumentException if the position is not available 716 */ 717 protected function validatePosition($position) 718 { 719 if (!in_array($position, $this->positions, true)) 720 { 721 throw new InvalidArgumentException(sprintf('The position "%s" does not exist (available positions: %s).', $position, implode(', ', $this->positions))); 722 } 723 } 699 724 } branches/1.1/test/unit/response/sfWebResponseTest.php
r6791 r7713 11 11 require_once(dirname(__FILE__).'/../../bootstrap/unit.php'); 12 12 13 $t = new lime_test(6 6, new lime_output_color());13 $t = new lime_test(68, new lime_output_color()); 14 14 15 15 class myWebResponse extends sfWebResponse … … 194 194 $t->is($stylesheets['bar'], array('media' => 'print'), '->addStylesheet() takes an array of parameters as its third argument'); 195 195 196 try 197 { 198 $response->addStylesheet('last', 'none'); 199 $t->fail('->addStylesheet() throws an InvalidArgumentException if the position is not first, the empty string, or last'); 200 } 201 catch (InvalidArgumentException $e) 202 { 203 $t->pass('->addStylesheet() throws an InvalidArgumentException if the position is not first, the empty string, or last'); 204 } 205 196 206 // ->getStylesheets() 197 207 $t->diag('->getStylesheets()'); … … 212 222 $t->ok(array_key_exists('last_js', $response->getJavascripts('last')), '->addJavascript() takes a position as its second argument'); 213 223 224 try 225 { 226 $response->addJavascript('last_js', 'none'); 227 $t->fail('->addJavascript() throws an InvalidArgumentException if the position is not first, the empty string, or last'); 228 } 229 catch (InvalidArgumentException $e) 230 { 231 $t->pass('->addJavascript() throws an InvalidArgumentException if the position is not first, the empty string, or last'); 232 } 233 214 234 // ->getJavascripts() 215 235 $t->diag('->getJavascripts()');

