Browse Source

Merge pull request #4587 from TamiasSibiricus/3.0-widgets-config-file

3.0 widgets list from config file
Mark Story 11 years ago
parent
commit
ea544f72f3

+ 24 - 3
src/View/Widget/WidgetRegistry.php

@@ -18,6 +18,7 @@ use Cake\Core\App;
 use Cake\View\StringTemplate;
 use Cake\View\View;
 use Cake\View\Widget\WidgetInterface;
+use Cake\Core\Configure\Engine\PhpConfig;
 use \ReflectionClass;
 
 /**
@@ -67,17 +68,37 @@ class WidgetRegistry {
  *
  * @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.
+ * @param string|array $widgets See add() method for more information.
  */
-	public function __construct(StringTemplate $templates, View $view, array $widgets = []) {
+	public function __construct(StringTemplate $templates, View $view, $widgets = []) {
 		$this->_templates = $templates;
 		if (!empty($widgets)) {
-			$this->add($widgets);
+			if (is_string($widgets)) {
+				$this->load($widgets);
+			} else {
+				$this->add($widgets);
+			}
 		}
 		$this->add(['_view' => $view]);
 	}
 
 /**
+ * Load a config file containing widgets.
+ *
+ * Widget files should define a `$config` variable containing
+ * all the widgets to load. Loaded widgets will be merged with existing
+ * widgets.
+ *
+ * @param string $file The file to load
+ * @return void
+ */
+	public function load($file) {
+		$loader = new PhpConfig();
+		$widgets = $loader->read($file);
+		$this->add($widgets);
+	}
+
+/**
  * Adds or replaces existing widget instances/configuration with new ones.
  *
  * Widget arrays can either be descriptions or instances. For example:

+ 22 - 0
tests/TestCase/View/Widget/WidgetRegistryTest.php

@@ -18,6 +18,7 @@ use Cake\TestSuite\TestCase;
 use Cake\View\StringTemplate;
 use Cake\View\View;
 use Cake\View\Widget\WidgetRegistry;
+use Cake\Core\Plugin;
 
 /**
  * WidgetRegistry test case
@@ -50,6 +51,27 @@ class WidgetRegistryTestCase extends TestCase {
 	}
 
 /**
+ * Test loading widgets files in the app.
+ *
+ * @return void
+ */
+	public function testLoadInConstructor() {
+		$inputs = new WidgetRegistry($this->templates, $this->view, 'test_widgets');
+		$this->assertInstanceOf('Cake\View\Widget\Label', $inputs->get('text'));
+	}
+
+/**
+ * Test loading templates files from a plugin
+ *
+ * @return void
+ */
+	public function testLoadPluginInConstuctor() {
+		Plugin::load('TestPlugin');
+		$inputs = new WidgetRegistry($this->templates, $this->view, 'TestPlugin.test_widgets');
+		$this->assertInstanceOf('Cake\View\Widget\Label', $inputs->get('text'));
+	}
+
+/**
  * Test adding new widgets.
  *
  * @return void

+ 7 - 0
tests/test_app/Plugin/TestPlugin/config/test_widgets.php

@@ -0,0 +1,7 @@
+<?php
+/**
+ * Widgets list for testing.
+ */
+$config = [
+	'text' => ['Cake\View\Widget\Label'],
+];

+ 7 - 0
tests/test_app/config/test_widgets.php

@@ -0,0 +1,7 @@
+<?php
+/**
+ * Widgets list for testing.
+ */
+$config = [
+	'text' => ['Cake\View\Widget\Label'],
+];