فهرست منبع

ZF-8496: Make application resource autoloading opt-in (but enabled by default in new projects); document the feature

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@19571 44c647ce-9c0f-0410-b52a-842ac1e357ba
matthew 16 سال پیش
والد
کامیت
7216ace5be

+ 44 - 0
documentation/manual/en/module_specs/Zend_Application-CoreFunctionality-Bootstrap_Bootstrap.xml

@@ -14,9 +14,53 @@
         front controller.
     </para>
 
+
     <para>
         In most cases, you will want to extend this class for your bootstrapping
         needs, or simply use this class and provide a list of resource plugins
         to utilize.
     </para>
+
+    <sect3 id="zend.application.core-functionality.bootstrap-bootstrap.autoloading">
+        <title>Enabling Application Autoloading</title>
+
+        <para>
+            Additionally, this bootstrap implementation provides the ability to specify the
+            "namespace" or class prefix for resources located in its tree, which will enable
+            autoloading of various application resources; essentially, it instantiates a <link
+                linkend="zend.loader.autoloader-resource.module">Zend_Application_Module_Autoloader</link>
+            object, providing the requested namespace and the bootstrap's directory as arguments.
+            You may enable this functionality by providing a namespace to the "appnamespace"
+            configuration option. As an INI example:
+        </para>
+
+        <programlisting language="ini"><![CDATA[
+appnamespace = "Application"
+]]></programlisting>
+
+        <para>
+            Or in XML:
+        </para>
+
+        <programlisting language="xml"><![CDATA[
+<appnamespace>Application</appnamespace>
+]]></programlisting>
+
+        <para>
+            By default, <classname>Zend_Tool</classname> will enable this option with the value
+            "Application".
+        </para>
+
+        <para>
+            Alternately, you can simply define the <varname>$_appNamespace</varname> property of your
+            bootstrap class with the appropriate value:
+        </para>
+
+        <programlisting language="php"><![CDATA[
+class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
+{
+    protected $_appNamespace = 'Application';
+}
+]]></programlisting>
+    </sect3>
 </sect2>

+ 13 - 11
library/Zend/Application/Bootstrap/Bootstrap.php

@@ -36,10 +36,10 @@ class Zend_Application_Bootstrap_Bootstrap
     extends Zend_Application_Bootstrap_BootstrapAbstract
 {
     /**
-     * Default application resource namespace
-     * @var string
+     * Application resource namespace
+     * @var false|string
      */
-    protected $_defaultAppNamespace = 'Application';
+    protected $_appNamespace = false;
 
     /**
      * Application resource autoloader
@@ -116,11 +116,13 @@ class Zend_Application_Bootstrap_Bootstrap
      */
     public function getResourceLoader()
     {
-        if (null === $this->_resourceLoader) {
+        if ((null === $this->_resourceLoader)
+            && (false !== ($namespace = $this->getAppNamespace()))
+        ) {
             $r    = new ReflectionClass($this);
             $path = $r->getFileName();
             $this->setResourceLoader(new Zend_Application_Module_Autoloader(array(
-                'namespace' => $this->getDefaultAppNamespace(),
+                'namespace' => $namespace,
                 'basePath'  => dirname($path),
             )));
         }
@@ -128,24 +130,24 @@ class Zend_Application_Bootstrap_Bootstrap
     }
 
     /**
-     * Get defaultAppNamespace
+     * Get application namespace (used for module autoloading)
      *
      * @return string
      */
-    public function getDefaultAppNamespace()
+    public function getAppNamespace()
     {
-        return $this->_defaultAppNamespace;
+        return $this->_appNamespace;
     }
 
     /**
-     * Set defaultAppNamespace
+     * Set application namespace (for module autoloading)
      *
      * @param  string
      * @return Zend_Application_Bootstrap_Bootstrap
      */
-    public function setDefaultAppNamespace($value)
+    public function setAppNamespace($value)
     {
-        $this->_defaultAppNamespace = (string) $value;
+        $this->_appNamespace = (string) $value;
         return $this;
     }
 }

+ 1 - 0
library/Zend/Tool/Project/Context/Zf/ApplicationConfigFile.php

@@ -90,6 +90,7 @@ phpSettings.display_errors = 0
 includePaths.library = APPLICATION_PATH "/../library"
 bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
 bootstrap.class = "Bootstrap"
+appnamespace = "Application"
 resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
 resources.frontController.params.displayExceptions = 0
 resources.log.zendmonitor.writerName = "ZendMonitor"

+ 12 - 5
tests/Zend/Application/Bootstrap/BootstrapTest.php

@@ -132,17 +132,24 @@ class Zend_Application_Bootstrap_BootstrapTest extends PHPUnit_Framework_TestCas
     /**
      * @group ZF-8496
      */
-    public function testBootstrapShouldInitializeModuleAutoloader()
+    public function testBootstrapModuleAutoloaderShouldNotBeInitializedByDefault()
     {
-        $this->assertTrue($this->bootstrap->getResourceLoader() instanceof Zend_Application_Module_Autoloader);
+        $this->assertFalse($this->bootstrap->getResourceLoader() instanceof Zend_Application_Module_Autoloader);
     }
 
     /**
      * @group ZF-8496
      */
-    public function testBootstrapAutoloaderShouldHaveApplicationNamespaceByDefault()
+    public function testBootstrapShouldInitializeModuleAutoloaderWhenNamespaceSpecified()
     {
-        $al = $this->bootstrap->getResourceLoader();
+        $application = new Zend_Application('testing', array(
+            'appnamespace' => 'Application',
+        ));
+        $bootstrap   = new Zend_Application_Bootstrap_Bootstrap(
+            $application
+        );
+        $this->assertTrue($bootstrap->getResourceLoader() instanceof Zend_Application_Module_Autoloader);
+        $al = $bootstrap->getResourceLoader();
         $this->assertEquals('Application', $al->getNamespace());
     }
 
@@ -152,7 +159,7 @@ class Zend_Application_Bootstrap_BootstrapTest extends PHPUnit_Framework_TestCas
     public function testBootstrapAutoloaderNamespaceShouldBeConfigurable()
     {
         $application = new Zend_Application('testing', array(
-            'defaultappnamespace' => 'Default',
+            'appnamespace' => 'Default',
         ));
         $bootstrap   = new Zend_Application_Bootstrap_Bootstrap(
             $application