Browse Source

Add a special `_view` widget.

Widget classes can use `_view` as a dependency to access the current
view class. This makes it much easier for widget objects to add script
files, css files, or render elements if they need to.
mark_story 11 years ago
parent
commit
14d1aab467

+ 1 - 1
src/View/Helper/FormHelper.php

@@ -197,7 +197,7 @@ class FormHelper extends Helper {
 	public function widgetRegistry(WidgetRegistry $instance = null, $widgets = []) {
 		if ($instance === null) {
 			if ($this->_registry === null) {
-				$this->_registry = new WidgetRegistry($this->templater(), $widgets);
+				$this->_registry = new WidgetRegistry($this->templater(), $this->_View, $widgets);
 			}
 			return $this->_registry;
 		}

+ 7 - 2
src/View/Widget/WidgetRegistry.php

@@ -16,6 +16,7 @@ namespace Cake\View\Widget;
 
 use Cake\Core\App;
 use Cake\View\StringTemplate;
+use Cake\View\View;
 use Cake\View\Widget\WidgetInterface;
 use \ReflectionClass;
 
@@ -31,6 +32,8 @@ use \ReflectionClass;
  *
  * Each widget should expect a StringTemplate instance as their first
  * argument. All other dependencies will be included after.
+ *
+ * Widgets can ask for the current view by using the `_view` widget.
  */
 class WidgetRegistry {
 
@@ -62,14 +65,16 @@ class WidgetRegistry {
 /**
  * Constructor
  *
- * @param StringTemplate $templates Templates instance to use.
+ * @param \Cake\View\StringTemplate $templates Templates instance to use.
+ * @param \Cake\View\View $view The view instance to set as a widget.
  * @param array $widgets See add() method for more information.
  */
-	public function __construct(StringTemplate $templates, array $widgets = []) {
+	public function __construct(StringTemplate $templates, View $view, array $widgets = []) {
 		$this->_templates = $templates;
 		if (!empty($widgets)) {
 			$this->add($widgets);
 		}
+		$this->add(['_view' => $view]);
 	}
 
 /**

+ 12 - 10
tests/TestCase/View/Widget/WidgetRegistryTest.php

@@ -16,6 +16,7 @@ namespace Cake\Test\TestCase\View\Widget;
 
 use Cake\TestSuite\TestCase;
 use Cake\View\StringTemplate;
+use Cake\View\View;
 use Cake\View\Widget\WidgetRegistry;
 
 /**
@@ -31,6 +32,7 @@ class WidgetRegistryTestCase extends TestCase {
 	public function setUp() {
 		parent::setUp();
 		$this->templates = new StringTemplate();
+		$this->view = new View();
 	}
 
 /**
@@ -42,7 +44,7 @@ class WidgetRegistryTestCase extends TestCase {
 		$widgets = [
 			'text' => ['Cake\View\Widget\Basic'],
 		];
-		$inputs = new WidgetRegistry($this->templates, $widgets);
+		$inputs = new WidgetRegistry($this->templates, $this->view, $widgets);
 		$result = $inputs->get('text');
 		$this->assertInstanceOf('Cake\View\Widget\Basic', $result);
 	}
@@ -53,7 +55,7 @@ class WidgetRegistryTestCase extends TestCase {
  * @return void
  */
 	public function testAdd() {
-		$inputs = new WidgetRegistry($this->templates);
+		$inputs = new WidgetRegistry($this->templates, $this->view);
 		$result = $inputs->add([
 			'text' => ['Cake\View\Widget\Basic'],
 		]);
@@ -61,7 +63,7 @@ class WidgetRegistryTestCase extends TestCase {
 		$result = $inputs->get('text');
 		$this->assertInstanceOf('Cake\View\Widget\WidgetInterface', $result);
 
-		$inputs = new WidgetRegistry($this->templates);
+		$inputs = new WidgetRegistry($this->templates, $this->view);
 		$result = $inputs->add([
 			'hidden' => 'Cake\View\Widget\Basic',
 		]);
@@ -78,7 +80,7 @@ class WidgetRegistryTestCase extends TestCase {
  * @return void
  */
 	public function testAddInvalidType() {
-		$inputs = new WidgetRegistry($this->templates);
+		$inputs = new WidgetRegistry($this->templates, $this->view);
 		$inputs->add([
 			'text' => new \StdClass()
 		]);
@@ -91,7 +93,7 @@ class WidgetRegistryTestCase extends TestCase {
  * @return void
  */
 	public function testGet() {
-		$inputs = new WidgetRegistry($this->templates);
+		$inputs = new WidgetRegistry($this->templates, $this->view);
 		$inputs->add([
 			'text' => ['Cake\View\Widget\Basic'],
 		]);
@@ -106,7 +108,7 @@ class WidgetRegistryTestCase extends TestCase {
  * @return void
  */
 	public function testGetFallback() {
-		$inputs = new WidgetRegistry($this->templates);
+		$inputs = new WidgetRegistry($this->templates, $this->view);
 		$inputs->add([
 			'_default' => ['Cake\View\Widget\Basic'],
 		]);
@@ -125,7 +127,7 @@ class WidgetRegistryTestCase extends TestCase {
  * @return void
  */
 	public function testGetNoFallbackError() {
-		$inputs = new WidgetRegistry($this->templates);
+		$inputs = new WidgetRegistry($this->templates, $this->view);
 		$inputs->clear();
 		$inputs->get('foo');
 	}
@@ -136,7 +138,7 @@ class WidgetRegistryTestCase extends TestCase {
  * @return void
  */
 	public function testGetResolveDependency() {
-		$inputs = new WidgetRegistry($this->templates);
+		$inputs = new WidgetRegistry($this->templates, $this->view);
 		$inputs->clear();
 		$inputs->add([
 			'label' => ['Cake\View\Widget\Label'],
@@ -154,7 +156,7 @@ class WidgetRegistryTestCase extends TestCase {
  * @return void
  */
 	public function testGetResolveDependencyMissingClass() {
-		$inputs = new WidgetRegistry($this->templates);
+		$inputs = new WidgetRegistry($this->templates, $this->view);
 		$inputs->add(['test' => ['TestApp\View\Derp']]);
 		$inputs->get('test');
 	}
@@ -167,7 +169,7 @@ class WidgetRegistryTestCase extends TestCase {
  * @return void
  */
 	public function testGetResolveDependencyMissingDependency() {
-		$inputs = new WidgetRegistry($this->templates);
+		$inputs = new WidgetRegistry($this->templates, $this->view);
 		$inputs->clear();
 		$inputs->add(['multicheckbox' => ['Cake\View\Widget\MultiCheckbox', 'label']]);
 		$inputs->get('multicheckbox');