소스 검색

ZF-8177: add registerHelper() method to Zend_View

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@19404 44c647ce-9c0f-0410-b52a-842ac1e357ba
matthew 16 년 전
부모
커밋
d38deff956
3개의 변경된 파일115개의 추가작업 그리고 0개의 파일을 삭제
  1. 34 0
      documentation/manual/en/module_specs/Zend_View-Helpers.xml
  2. 33 0
      library/Zend/View/Abstract.php
  3. 48 0
      tests/Zend/ViewTest.php

+ 34 - 0
documentation/manual/en/module_specs/Zend_View-Helpers.xml

@@ -555,6 +555,40 @@ class My_View_Helper_ScriptPath
             not need to define this method, as it is defined for you.
         </para>
     </sect2>
+
+    <sect2 id="zend.view.helpers.registering-concrete">
+        <title>Registering Concrete Helpers</title>
+
+        <para>
+            Sometimes it is convenient to instantiate a view helper, and then register it with the
+            view. As of version 1.10.0, this is now possible using the
+            <methodname>registerHelper()</methodname> method, which expects two arguments: the
+            helper object, and the name by which it will be registered.
+        </para>
+
+        <programlisting language="php"><![CDATA[
+$helper = new My_Helper_Foo();
+// ...do some configuration or dependency injection...
+
+$view->registerHelper($helper, 'foo');
+]]></programlisting>
+
+        <para>
+            If the helper has a <methodname>setView()</methodname> method, the view object will call
+            this and inject itself into the helper on registration.
+        </para>
+
+        <note>
+            <title>Helper name should match a method</title>
+
+            <para>
+                The second argument to <methodname>registerHelper()</methodname> is the name of the
+                helper. A corresponding method name should exist in the helper; otherwise,
+                <classname>Zend_View</classname> will call a non-existent method when invoking the
+                helper, raising a fatal PHP error.
+            </para>
+        </note>
+    </sect2>
 </sect1>
 <!--
 vim:se ts=4 sw=4 et:

+ 33 - 0
library/Zend/View/Abstract.php

@@ -555,6 +555,39 @@ abstract class Zend_View_Abstract implements Zend_View_Interface
     {
         return $this->getPluginLoader('helper')->getPaths();
     }
+    
+    /**
+     * Registers a helper object, bypassing plugin loader
+     *
+     * @param  Zend_View_Helper_Abstract|object $helper
+     * @param  string $name
+     * @return Zend_View_Abstract
+     * @throws Zend_View_Exception
+     */
+    public function registerHelper($helper, $name)
+    {
+        if (!is_object($helper)) {
+            require_once 'Zend/View/Exception.php';
+            throw new Zend_View_Exception('View helper must be an object');
+        }
+
+        if (!$helper instanceof Zend_View_Interface) {
+            if (!method_exists($helper, $name)) {
+                require_once 'Zend/View/Exception.php';
+                throw new Zend_View_Exception(
+                    'View helper must implement Zend_View_Interface or have a method matching the name provided'
+                );
+            }
+        }
+
+        if (method_exists($helper, 'setView')) {
+            $helper->setView($this);
+        }
+
+        $name = ucfirst($name);
+        $this->_helper[$name] = $helper;
+        return $this;
+    }
 
     /**
      * Get a helper by name

+ 48 - 0
tests/Zend/ViewTest.php

@@ -1074,6 +1074,54 @@ class Zend_ViewTest extends PHPUnit_Framework_TestCase
         $paths = $view->getFilterPaths();
         $this->assertTrue(array_key_exists('My_View_', $paths), var_export($paths, 1));
     }
+    
+    /**
+     * @group ZF-8177
+     */
+    public function testRegisterHelperShouldRegisterHelperWithView()
+    {
+    	require_once dirname(__FILE__) . '/View/_stubs/HelperDir1/Stub1.php';
+    	
+    	$view = new Zend_View();
+    	$helper = new Foo_View_Helper_Stub1();
+    	$view->registerHelper($helper, 'stub1');
+    	
+    	$this->assertEquals($view->getHelper('stub1'), $helper);
+    	$this->assertEquals($view->stub1(), 'foo');
+    }
+
+    /**
+     * @group ZF-8177
+     * @expectedException Zend_View_Exception
+     */
+    public function testRegisterHelperShouldThrowExceptionIfNotProvidedAnObject()
+    {
+        $view = new Zend_View();
+        $view->registerHelper('Foo', 'foo');
+    }
+
+    /**
+     * @group ZF-8177
+     * @expectedException Zend_View_Exception
+     */
+    public function testRegisterHelperShouldThrowExceptionIfProvidedANonHelperObject()
+    {
+        $view   = new Zend_View();
+        $helper = new stdClass;
+        $view->registerHelper($helper, 'foo');
+    }
+
+    /**
+     * @group ZF-8177
+     */
+    public function testRegisterHelperShouldRegisterViewObjectWithHelper()
+    {
+    	require_once 'Zend/View/Helper/Doctype.php';
+    	$view = new Zend_View();
+    	$helper = new Zend_View_Helper_Doctype();
+    	$view->registerHelper($helper, 'doctype');
+        $this->assertSame($view, $helper->view);
+    }
 }
 
 /**