Skip to content
This repository
Browse code

Fix how $uses is handled.

$uses now defaults to true.  All falsey values turn some features
off, and most truthy values enable them.

Fixes #2590
  • Loading branch information...
commit 1ddcde7c864801625d5254c920a83c0ee0d4c3c8 1 parent 6599422
Mark Story authored
95  lib/Cake/Controller/Controller.php
@@ -75,13 +75,20 @@ class Controller extends Object implements CakeEventListener {
75 75
  *
76 76
  * Example: `public $uses = array('Product', 'Post', 'Comment');`
77 77
  *
78  
- * Can be set to array() to use no models.  Can be set to false to
79  
- * use no models and prevent the merging of $uses with AppController
  78
+ * Can be set to several values to express different options:
  79
+ *
  80
+ * - `true` Use the default inflected model name.
  81
+ * - `array()` Use only models defined in the parent class.
  82
+ * - `false` Use no models at all, do not merge with parent class either.
  83
+ * - `array('Post', 'Comment')` Use only the Post and Comment models. Models
  84
+ *   Will also be merged with the parent class.
  85
+ *
  86
+ * The default value is `true`.
80 87
  *
81 88
  * @var mixed A single name as a string or a list of names as an array.
82 89
  * @link http://book.cakephp.org/2.0/en/controllers.html#components-helpers-and-uses
83 90
  */
84  
-	public $uses = false;
  91
+	public $uses = true;
85 92
 
86 93
 /**
87 94
  * An array containing the names of helpers this controller uses. The array elements should
@@ -530,12 +537,16 @@ protected function _getScaffold(CakeRequest $request) {
530 537
 	}
531 538
 
532 539
 /**
533  
- * Merge components, helpers, and uses vars from Controller::$_mergeParent and PluginAppController.
  540
+ * Merge components, helpers, and uses vars from 
  541
+ * Controller::$_mergeParent and PluginAppController.
534 542
  *
535 543
  * @return void
536 544
  */
537 545
 	protected function _mergeControllerVars() {
538 546
 		$pluginController = $pluginDot = null;
  547
+		$mergeParent = is_subclass_of($this, $this->_mergeParent);
  548
+		$pluginVars = array();
  549
+		$appVars = array();
539 550
 
540 551
 		if (!empty($this->plugin)) {
541 552
 			$pluginController = $this->plugin . 'AppController';
@@ -545,40 +556,59 @@ protected function _mergeControllerVars() {
545 556
 			$pluginDot = $this->plugin . '.';
546 557
 		}
547 558
 
548  
-		if ($pluginController && $this->plugin != null) {
  559
+		if ($pluginController) {
549 560
 			$merge = array('components', 'helpers');
550  
-			$appVars = get_class_vars($pluginController);
551  
-			if (
552  
-				($this->uses !== null || $this->uses !== false) &&
553  
-				is_array($this->uses) && !empty($appVars['uses'])
554  
-			) {
555  
-				$this->uses = array_merge($this->uses, array_diff($appVars['uses'], $this->uses));
556  
-			}
557 561
 			$this->_mergeVars($merge, $pluginController);
558 562
 		}
559 563
 
560  
-		if (is_subclass_of($this, $this->_mergeParent) || !empty($pluginController)) {
  564
+		if ($mergeParent || !empty($pluginController)) {
561 565
 			$appVars = get_class_vars($this->_mergeParent);
562 566
 			$uses = $appVars['uses'];
563 567
 			$merge = array('components', 'helpers');
564  
-
565  
-			if ($uses == $this->uses && !empty($this->uses)) {
566  
-				if (!in_array($pluginDot . $this->modelClass, $this->uses)) {
567  
-					array_unshift($this->uses, $pluginDot . $this->modelClass);
568  
-				} elseif ($this->uses[0] !== $pluginDot . $this->modelClass) {
569  
-					$this->uses = array_flip($this->uses);
570  
-					unset($this->uses[$pluginDot . $this->modelClass]);
571  
-					$this->uses = array_flip($this->uses);
572  
-					array_unshift($this->uses, $pluginDot . $this->modelClass);
573  
-				}
574  
-			} elseif (
575  
-				($this->uses !== null || $this->uses !== false) &&
576  
-				is_array($this->uses) && !empty($appVars['uses'])
577  
-			) {
578  
-				$this->uses = array_merge($this->uses, array_diff($appVars['uses'], $this->uses));
579  
-			}
580 568
 			$this->_mergeVars($merge, $this->_mergeParent, true);
581 569
 		}
  570
+
  571
+		if ($this->uses === null) {
  572
+			$this->uses = false;
  573
+		}
  574
+		if ($this->uses === true) {
  575
+			$this->uses = array($pluginDot . $this->modelClass);
  576
+		}
  577
+		if (isset($appVars['uses']) && $appVars['uses'] === $this->uses) {
  578
+			array_unshift($this->uses, $pluginDot . $this->modelClass);
  579
+		}
  580
+		if ($pluginController) {
  581
+			$pluginVars = get_class_vars($pluginController);
  582
+		}
  583
+		if ($this->uses !== false) {
  584
+			$this->_mergeUses($pluginVars);
  585
+			$this->_mergeUses($appVars);
  586
+		} else {
  587
+			$this->uses = array();
  588
+			$this->modelClass = '';
  589
+		}
  590
+	}
  591
+
  592
+/**
  593
+ * Helper method for merging the $uses property together.
  594
+ *
  595
+ * Merges the elements not already in $this->uses into
  596
+ * $this->uses.
  597
+ *
  598
+ * @param mixed $merge The data to merge in.
  599
+ * @return void
  600
+ */
  601
+	protected function _mergeUses($merge) {
  602
+		if (!isset($merge['uses'])) {
  603
+			return;
  604
+		}
  605
+		if ($merge['uses'] === true) {
  606
+			return;
  607
+		}
  608
+		$this->uses = array_merge(
  609
+			$this->uses,
  610
+			array_diff($merge['uses'], $this->uses)
  611
+		);
582 612
 	}
583 613
 
584 614
 /**
@@ -698,7 +728,7 @@ public function loadModel($modelClass = null, $id = null) {
698 728
 			$modelClass = $this->modelClass;
699 729
 		}
700 730
 
701  
-		$this->uses = ($this->uses) ? $this->uses : array();
  731
+		$this->uses = ($this->uses) ? (array)$this->uses : array();
702 732
 		if (!in_array($modelClass, $this->uses)) {
703 733
 			$this->uses[] = $modelClass;
704 734
 		}
@@ -903,15 +933,12 @@ public function render($view = null, $layout = null) {
903 933
 			return $this->response;
904 934
 		}
905 935
 
906  
-		if (!empty($this->uses)) {
  936
+		if (!empty($this->uses) && is_array($this->uses)) {
907 937
 			foreach ($this->uses as $model) {
908 938
 				list($plugin, $className) = pluginSplit($model);
909 939
 				$this->request->params['models'][$className] = compact('plugin', 'className');
910 940
 			}
911 941
 		}
912  
-		if (!empty($this->modelClass) && ($this->uses === false || $this->uses === array())) {
913  
-			$this->request->params['models'][$this->modelClass] = array('plugin' => $this->plugin, 'className' => $this->modelClass);
914  
-		}
915 942
 
916 943
 		$viewClass = $this->viewClass;
917 944
 		if ($this->viewClass != 'View') {
8  lib/Cake/Test/Case/Controller/ControllerTest.php
... ...
@@ -1,9 +1,5 @@
1 1
 <?php
2 2
 /**
3  
- * ControllerTest file
4  
- *
5  
- * PHP 5
6  
- *
7 3
  * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
8 4
  * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
9 5
  *
@@ -385,7 +381,7 @@ class AnotherTestController extends ControllerTestAppController {
385 381
  *
386 382
  * @var array
387 383
  */
388  
-	public $uses = null;
  384
+	public $uses = false;
389 385
 
390 386
 /**
391 387
  * merge parent
@@ -917,7 +913,7 @@ public function testMergeVars() {
917 913
 
918 914
 
919 915
 		$this->assertTrue(in_array('ControllerPost', $appVars['uses']));
920  
-		$this->assertNull($testVars['uses']);
  916
+		$this->assertFalse($testVars['uses']);
921 917
 
922 918
 		$this->assertFalse(property_exists($TestController, 'ControllerPost'));
923 919
 

0 notes on commit 1ddcde7

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