فهرست منبع

Added setOptions to _Dojo_View_Helper_Dojo_Container #ZF-7938

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@18617 44c647ce-9c0f-0410-b52a-842ac1e357ba
freak 16 سال پیش
والد
کامیت
a625d68007
2فایلهای تغییر یافته به همراه129 افزوده شده و 10 حذف شده
  1. 76 10
      library/Zend/Dojo/View/Helper/Dojo/Container.php
  2. 53 0
      tests/Zend/Dojo/View/Helper/DojoTest.php

+ 76 - 10
library/Zend/Dojo/View/Helper/Dojo/Container.php

@@ -201,23 +201,88 @@ class Zend_Dojo_View_Helper_Dojo_Container
     {
         return $this->_enabled;
     }
+    
+    /**
+     * Add options for the Dojo Container to use
+     * 
+     * @param array|Zend_Config Array or Zend_Config object with options to use
+     * @return Zend_Dojo_View_Helper_Dojo_Container
+     */
+    public function setOptions($options)
+    {
+        if($options instanceof Zend_Config) {
+            $options = $options->toArray();
+        }
+
+        foreach($options as $key => $value) {
+            $key = strtolower($key);
+            switch($key) {
+                case 'requiremodules':
+                    $this->requireModule($value);
+                    break;
+                case 'modulepaths':
+                    foreach($value as $module => $path) {
+                        $this->registerModulePath($module, $path);
+                    }
+                    break;
+                case 'layers':
+                    $value = (array) $value;
+                    foreach($value as $layer) {
+                        $this->addLayer($layer);
+                    }
+                    break;
+                case 'cdnbase':
+                    $this->setCdnBase($value);
+                    break;
+                case 'cdnversion':
+                    $this->setCdnVersion($value);
+                    break;
+                case 'cdndojopath':
+                    $this->setCdnDojoPath($value);
+                    break;
+                case 'localpath':
+                    $this->setLocalPath($value);
+                    break;
+                case 'djconfig':
+                    $this->setDjConfig($value);
+                    break;
+                case 'stylesheetmodules':
+                    $value = (array) $value;
+                    foreach($value as $module) {
+                        $this->addStylesheetModule($module);
+                    }
+                    break;
+                case 'stylesheets':
+                    $value = (array) $value;
+                    foreach($value as $stylesheet) {
+                        $this->addStylesheet($stylesheet);
+                    }
+                    break;
+                case 'registerdojostylesheet':
+                    $this->registerDojoStylesheet($value);
+                    break;
+            }
+        }
+        
+        return $this;
+    }
 
     /**
-     * Specify a module to require
+     * Specify one or multiple modules to require
      *
-     * @param  string $module
+     * @param  string|array $modules
      * @return Zend_Dojo_View_Helper_Dojo_Container
      */
-    public function requireModule($module)
+    public function requireModule($modules)
     {
-        if (!is_string($module) && !is_array($module)) {
+        if (!is_string($modules) && !is_array($modules)) {
             require_once 'Zend/Dojo/View/Exception.php';
             throw new Zend_Dojo_View_Exception('Invalid module name specified; must be a string or an array of strings');
         }
 
-        $module = (array) $module;
+        $modules = (array) $modules;
 
-        foreach ($module as $mod) {
+        foreach ($modules as $mod) {
             if (!preg_match('/^[a-z][a-z0-9._-]+$/i', $mod)) {
                 require_once 'Zend/Dojo/View/Exception.php';
                 throw new Zend_Dojo_View_Exception(sprintf('Module name specified, "%s", contains invalid characters', (string) $mod));
@@ -244,7 +309,8 @@ class Zend_Dojo_View_Helper_Dojo_Container
     /**
      * Register a module path
      *
-     * @param  string $path
+     * @param  string $module The module to register a path for
+     * @param  string $path The path to register for the module
      * @return Zend_Dojo_View_Helper_Dojo_Container
      */
     public function registerModulePath($module, $path)
@@ -279,6 +345,7 @@ class Zend_Dojo_View_Helper_Dojo_Container
         if (!in_array($path, $this->_layers)) {
             $this->_layers[] = $path;
         }
+        
         return $this;
     }
 
@@ -497,10 +564,9 @@ class Zend_Dojo_View_Helper_Dojo_Container
             require_once 'Zend/Dojo/View/Exception.php';
             throw new Zend_Dojo_View_Exception('Invalid stylesheet module specified');
         }
-        if (in_array($module, $this->_stylesheetModules)) {
-            return $this;
+        if (!in_array($module, $this->_stylesheetModules)) {
+            $this->_stylesheetModules[] = $module;
         }
-        $this->_stylesheetModules[] = $module;
         return $this;
     }
 

+ 53 - 0
tests/Zend/Dojo/View/Helper/DojoTest.php

@@ -848,6 +848,59 @@ function() {
         $test = $this->helper->__toString();
         $this->assertRegexp('/zendDijits.*?(zend\.custom)/s', $test, 'Generated markup: ' . $test);
     }
+    
+    public function testDojoViewHelperContainerAddOptionsPassesOnAllStringOptions() {
+        $helper = $this->helper;
+        $options = array(
+            'requireModules' => 'ZfTestRequiredModule',
+            'laYers' => '_added_layer_',
+            'cdnBase' => 'ZF-RLZ',
+            'cdnVersion' => '1.9.5',
+            'cdnDojoPath' => '_cdn_dojo_path_',
+            'localPath' => '/srv/ZF/dojo/',
+            'stylesheetmodules' => 'test.stylesheet.module',
+            'stylesheets' => 'someStyleSheet',
+            'registerdojostylesheet' => true
+        );
+        
+        $helper->setOptions($options);
+        
+        $this->assertEquals(array('ZfTestRequiredModule'), $helper->getModules());
+        $this->assertEquals(array('_added_layer_'), $helper->getLayers());
+        $this->assertEquals('ZF-RLZ', $helper->getCdnBase());
+        $this->assertEquals('1.9.5', $helper->getCdnVersion());
+        $this->assertEquals('_cdn_dojo_path_', $helper->getCdnDojoPath());
+        $this->assertEquals('/srv/ZF/dojo/', $helper->getLocalPath());
+        $this->assertEquals(array('test.stylesheet.module'), $helper->getStyleSheetModules());
+        $this->assertEquals(array('someStyleSheet'), $helper->getStylesheets());
+        $this->assertTrue($helper->registerDojoStylesheet());
+    }
+    
+    public function testDojoViewHelperContainerAddOptionsPassesOnAllArrayOptions() {
+        $helper = $this->helper;
+        $modulePaths = array('module1' => 'path1', 'module2' => 'path2');
+        $layers = array('layer_two','layer_three');
+        $djConfig = array('foo1' => 'bar1', 'foo2' => 'bar2');
+        $stylesheetMods = array('test.one.style', 'test.two.style');
+        $stylesheets = array('style1', 'style2');
+        $options = array(
+            'modulePaths'   => $modulePaths,
+            'layers'        => $layers,
+            'djConfig'      => $djConfig,
+            'styleShEEtModules' => $stylesheetMods,
+            'stylesheets'   => $stylesheets,
+            'registerdojostylesheet' => false
+        );
+        
+        $helper->setOptions($options);
+
+        $this->assertEquals($modulePaths, $helper->getModulePaths());
+        $this->assertEquals($layers, $helper->getLayers());
+        $this->assertEquals($djConfig, $helper->getDjConfig());
+        $this->assertEquals($stylesheetMods, $helper->getStyleSheetModules());
+        $this->assertEquals($stylesheets, $helper->getStylesheets());
+        $this->assertFalse($helper->registerDojoStylesheet());
+    }
 
     public function setupDojo()
     {