Browse Source

Update View & Helpers to use method names consistent with features.

Use helpers() and addHelper() as these methods are more congruent with
other parts of CakePHP.
mark_story 12 years ago
parent
commit
84823ff126

+ 2 - 2
src/View/Helper.php

@@ -150,7 +150,7 @@ class Helper extends Object implements EventListener {
 			$this->settings = Hash::merge($this->settings, $settings);
 		}
 		if (!empty($this->helpers)) {
-			$this->_helperMap = $View->Helpers->normalizeArray($this->helpers);
+			$this->_helperMap = $View->helpers()->normalizeArray($this->helpers);
 		}
 	}
 
@@ -175,7 +175,7 @@ class Helper extends Object implements EventListener {
 	public function __get($name) {
 		if (isset($this->_helperMap[$name]) && !isset($this->{$name})) {
 			$settings = array_merge((array)$this->_helperMap[$name]['settings'], array('enabled' => false));
-			$this->{$name} = $this->_View->loadHelper($this->_helperMap[$name]['class'], $settings);
+			$this->{$name} = $this->_View->addHelper($this->_helperMap[$name]['class'], $settings);
 		}
 		if (isset($this->{$name})) {
 			return $this->{$name};

+ 22 - 12
src/View/View.php

@@ -1,7 +1,5 @@
 <?php
 /**
- * Methods for displaying presentation data in the view.
- *
  * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  *
@@ -67,9 +65,9 @@ class View extends Object {
 /**
  * Helpers collection
  *
- * @var HelperRegistry
+ * @var Cake\View\HelperRegistry
  */
-	public $Helpers;
+	public $_helpers;
 
 /**
  * ViewBlock instance.
@@ -337,7 +335,6 @@ class View extends Object {
 		} else {
 			$this->response = new Response();
 		}
-		$this->Helpers = new HelperRegistry($this);
 		$this->Blocks = new ViewBlock();
 		$this->loadHelpers();
 		parent::__construct();
@@ -774,9 +771,10 @@ class View extends Object {
  * @return mixed
  */
 	public function __get($name) {
-		if (isset($this->Helpers->{$name})) {
-			$this->{$name} = $this->Helpers->{$name};
-			return $this->Helpers->{$name};
+		$registry = $this->helpers();
+		if (isset($registry->{$name})) {
+			$this->{$name} = $registry->{$name};
+			return $registry->{$name};
 		}
 		return $this->{$name};
 	}
@@ -811,10 +809,11 @@ class View extends Object {
  * @return void
  */
 	public function loadHelpers() {
-		$helpers = $this->Helpers->normalizeArray($this->helpers);
+		$registry = $this->helpers();
+		$helpers = $registry->normalizeArray($this->helpers);
 		foreach ($helpers as $properties) {
 			list(, $class) = pluginSplit($properties['class']);
-			$this->{$class} = $this->Helpers->load($properties['class'], $properties['settings']);
+			$this->{$class} = $registry->load($properties['class'], $properties['settings']);
 		}
 	}
 
@@ -882,6 +881,17 @@ class View extends Object {
 	}
 
 /**
+ * Get the helper registry in use by this View class.
+ *
+ * @return \Cake\View\HelperRegistry
+ */
+	public function helpers() {
+		if ($this->_helpers === null) {
+			$this->_helpers = new HelperRegistry($this);
+		}
+		return $this->_helpers;
+	}
+/**
  * Loads a helper. Delegates to the `HelperRegistry::load()` to load the helper
  *
  * @param string $helperName Name of the helper to load.
@@ -889,8 +899,8 @@ class View extends Object {
  * @return Helper a constructed helper object.
  * @see HelperRegistry::load()
  */
-	public function loadHelper($helperName, $settings = array()) {
-		return $this->Helpers->load($helperName, $settings);
+	public function addHelper($helperName, $settings = []) {
+		return $this->helpers()->load($helperName, $settings);
 	}
 
 /**

+ 18 - 7
tests/TestCase/View/ViewTest.php

@@ -1,7 +1,5 @@
 <?php
 /**
- * ViewTest file
- *
  * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  *
@@ -787,9 +785,9 @@ class ViewTest extends TestCase {
  *
  * @return void
  */
-	public function testMagicGet() {
+	public function testMagicGetAndAddHelper() {
 		$View = new View($this->PostsController);
-		$View->loadHelper('Html');
+		$View->addHelper('Html');
 		$this->assertInstanceOf('Cake\View\Helper\HtmlHelper', $View->Html);
 	}
 
@@ -920,7 +918,7 @@ class ViewTest extends TestCase {
 		);
 		$View = new View($this->PostsController);
 		$View->render('index');
-		$this->assertEquals('Valuation', $View->Helpers->TestBeforeAfter->property);
+		$this->assertEquals('Valuation', $View->helpers()->TestBeforeAfter->property);
 	}
 
 /**
@@ -956,7 +954,7 @@ class ViewTest extends TestCase {
 		$result = $View->render('index', false);
 		$this->assertEquals('posts index', $result);
 
-		$attached = $View->Helpers->loaded();
+		$attached = $View->helpers()->loaded();
 		$this->assertEquals(array('Session', 'Html', 'Form', 'Number'), $attached);
 
 		$this->PostsController->helpers = array('Html', 'Form', 'Number', 'TestPlugin.PluggedHelper');
@@ -965,7 +963,7 @@ class ViewTest extends TestCase {
 		$result = $View->render('index', false);
 		$this->assertEquals('posts index', $result);
 
-		$attached = $View->Helpers->loaded();
+		$attached = $View->helpers()->loaded();
 		$expected = array('Html', 'Form', 'Number', 'PluggedHelper');
 		$this->assertEquals($expected, $attached, 'Attached helpers are wrong.');
 	}
@@ -1572,4 +1570,17 @@ TEXT;
 		$result = $this->View->get('title', $default);
 		$this->assertEquals($expected, $result);
 	}
+
+/**
+ * Test the helpers() method.
+ *
+ * @return void
+ */
+	public function testHelpers() {
+		$this->assertInstanceOf('Cake\View\HelperRegistry', $this->View->helpers());
+
+		$result = $this->View->helpers();
+		$this->assertSame($result, $this->View->helpers());
+	}
+
 }

+ 1 - 1
tests/test_app/TestApp/Template/Layout/default.ctp

@@ -1,5 +1,5 @@
 <?php
-$this->loadHelper('Html');
+$this->addHelper('Html');
 
 $cakeDescription = 'CakePHP: the rapid development php framework';
 ?>