Skip to content
This repository
Browse code

ENHANCEMENT: Split arguments passed to SSViewer into underlay and ove…

…rlay arguments to control precedence. Also fixes breakage of SecurityAdmin
  • Loading branch information...
commit 521742aaf88bb2d48d56fa522483375c4dbe137e 1 parent aa14a51
Hamish Friedlander authored April 13, 2012
5  tests/view/SSViewerTest.php
@@ -468,6 +468,11 @@ function testIncludeWithArguments() {
468 468
 			$this->render('<% include SSViewerTestIncludeWithArguments Arg1="A", Arg2=$B %>', new ArrayData(array('B' => 'Bar'))),
469 469
 			'<p>A</p><p>Bar</p>'
470 470
 		);
  471
+
  472
+		$this->assertEquals(
  473
+			$this->render('<% include SSViewerTestIncludeWithArguments Arg1="A" %>', new ArrayData(array('Arg1' => 'Foo', 'Arg2' => 'Bar'))),
  474
+			'<p>A</p><p>Bar</p>'
  475
+		);
471 476
 	}
472 477
 
473 478
 	
42  view/SSViewer.php
@@ -301,9 +301,12 @@ class SSViewer_DataPresenter extends SSViewer_Scope {
301 301
 	private static $globalProperties = null;
302 302
 	private static $iteratorProperties = null;
303 303
 
304  
-	protected $extras;
  304
+	/** @var array|null Overlay variables. Take precedence over anything from the current scope */
  305
+	protected $overlay;
  306
+	/** @var array|null Underlay variables. Concede precedence to overlay variables or anything from the current scope */
  307
+	protected $underlay;
305 308
 
306  
-	function __construct($item, $extras = null){
  309
+	function __construct($item, $overlay = null, $underlay = null){
307 310
 		parent::__construct($item);
308 311
 
309 312
 		// Build up global property providers array only once per request
@@ -320,7 +323,8 @@ function __construct($item, $extras = null){
320 323
 			$this->createCallableArray(self::$iteratorProperties, "TemplateIteratorProvider", "get_template_iterator_variables", true);   //call non-statically
321 324
 		}
322 325
 
323  
-		$this->extras = $extras ? $extras : array();
  326
+		$this->overlay = $overlay ? $overlay : array();
  327
+		$this->underlay = $underlay ? $underlay : array();
324 328
 	}
325 329
 
326 330
 	protected function createCallableArray(&$extraArray, $interfaceToQuery, $variableMethod, $createObject = false) {
@@ -354,16 +358,22 @@ protected function createCallableArray(&$extraArray, $interfaceToQuery, $variabl
354 358
 	}
355 359
 
356 360
 	function getInjectedValue($property, $params, $cast = true) {
357  
-		// Check if the method to-be-called exists on the target object, and if so don't check global objects
358 361
 		$on = $this->itemIterator ? $this->itemIterator->current() : $this->item;
359  
-		if (isset($on->$property) || method_exists($on, $property)) return null;
360 362
 
361 363
 		// Find the source of the value
362 364
 		$source = null;
363 365
 
364 366
 		// Check for a presenter-specific override
365  
-		if (array_key_exists($property, $this->extras)) {
366  
-			$source = array('value' => $this->extras[$property]);
  367
+		if (array_key_exists($property, $this->overlay)) {
  368
+			$source = array('value' => $this->overlay[$property]);
  369
+		}
  370
+		// Check if the method to-be-called exists on the target object - if so, don't check any further injection locations
  371
+		else if (isset($on->$property) || method_exists($on, $property)) {
  372
+			$source = null;
  373
+		}
  374
+		// Check for a presenter-specific override
  375
+		else if (array_key_exists($property, $this->underlay)) {
  376
+			$source = array('value' => $this->underlay[$property]);
367 377
 		}
368 378
 		// Then for iterator-specific overrides
369 379
 		else if (array_key_exists($property, self::$iteratorProperties)) {
@@ -730,10 +740,11 @@ public function getPartialCacheStore() {
730 740
 	 *
731 741
 	 * @param string $cacheFile - The path to the file that contains the template compiled to PHP
732 742
 	 * @param Object $item - The item to use as the root scope for the template
733  
-	 * @param array|null $arguments - Any variables to layer into the root scope
  743
+	 * @param array|null $overlay - Any variables to layer on top of the scope
  744
+	 * @param array|null $underlay - Any variables to layer underneath the scope
734 745
 	 * @return string - The result of executing the template
735 746
 	 */
736  
-	protected function includeGeneratedTemplate($cacheFile, $item, $arguments) {
  747
+	protected function includeGeneratedTemplate($cacheFile, $item, $overlay, $underlay) {
737 748
 		if(isset($_GET['showtemplate']) && $_GET['showtemplate']) {
738 749
 			$lines = file($cacheFile);
739 750
 			echo "<h2>Template: $cacheFile</h2>";
@@ -745,7 +756,7 @@ protected function includeGeneratedTemplate($cacheFile, $item, $arguments) {
745 756
 		}
746 757
 
747 758
 		$cache = $this->getPartialCacheStore();
748  
-		$scope = new SSViewer_DataPresenter($item, $arguments ? $arguments : array());
  759
+		$scope = new SSViewer_DataPresenter($item, $overlay, $underlay);
749 760
 		$val = '';
750 761
 
751 762
 		include($cacheFile);
@@ -801,10 +812,7 @@ public function process($item, $arguments = null) {
801 812
 			if(isset($_GET['debug_profile'])) Profiler::unmark("SSViewer::process - compile", " for $template");
802 813
 		}
803 814
 
804  
-		$templateSpecificGlobals = array('I18NNamespace' => basename($template));
805  
-		$arguments = $arguments ? array_merge($templateSpecificGlobals, $arguments) : $templateSpecificGlobals;
806  
-
807  
-		$subtemplateGlobals = array();
  815
+		$underlay = array('I18NNamespace' => basename($template));
808 816
 
809 817
 		// Makes the rendered sub-templates available on the parent item,
810 818
 		// through $Content and $Layout placeholders.
@@ -813,11 +821,11 @@ public function process($item, $arguments = null) {
813 821
 				$subtemplateViewer = new SSViewer($this->chosenTemplates[$subtemplate]);
814 822
 				$subtemplateViewer->setPartialCacheStore($this->getPartialCacheStore());
815 823
 
816  
-				$subtemplateGlobals[$subtemplate] = $subtemplateViewer->process($item, $arguments);
  824
+				$underlay[$subtemplate] = $subtemplateViewer->process($item, $arguments);
817 825
 			}
818 826
 		}
819 827
 
820  
-		$val = $this->includeGeneratedTemplate($cacheFile, $item, array_merge($subtemplateGlobals, $arguments));
  828
+		$val = $this->includeGeneratedTemplate($cacheFile, $item, $arguments, $underlay);
821 829
 		$output = Requirements::includeInHTML($template, $val);
822 830
 		
823 831
 		array_pop(SSViewer::$topLevel);
@@ -913,7 +921,7 @@ public function process($item, $arguments = null) {
913 921
 		fwrite($fh, $template);
914 922
 		fclose($fh);
915 923
 
916  
-		$val = $this->includeGeneratedTemplate($tmpFile, $item, $arguments);
  924
+		$val = $this->includeGeneratedTemplate($tmpFile, $item, $arguments, null);
917 925
 
918 926
 		unlink($tmpFile);
919 927
 		return $val;

0 notes on commit 521742a

Please sign in to comment.
Something went wrong with that request. Please try again.