Zend_Loader-Autoloader-Resource.xml 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.loader.autoloader-resource">
  4. <title>Resource Autoloaders</title>
  5. <para>
  6. Resource autoloaders are intended to manage namespaced library code that
  7. follow Zend Framework coding standard guidelines, but which do not have
  8. a 1:1 mapping between the class name and the directory structure. Their
  9. primary purpose is to facilitate autoloading application resource code,
  10. such as application-specific models, forms, and <acronym>ACL</acronym>s.
  11. </para>
  12. <para>
  13. Resource autoloaders register with the <link
  14. linkend="zend.loader.autoloader">autoloader</link> on instantiation,
  15. with the namespace to which they are associated. This allows you to
  16. easily namespace code in specific directories, and still reap the
  17. benefits of autoloading.
  18. </para>
  19. <sect2 id="zend.loader.autoloader-resource.usage">
  20. <title>Resource autoloader usage</title>
  21. <para>
  22. Let's consider the following directory structure:
  23. </para>
  24. <programlisting language="text"><![CDATA[
  25. path/to/some/directory/
  26. acls/
  27. Site.php
  28. forms/
  29. Login.php
  30. models/
  31. User.php
  32. ]]></programlisting>
  33. <para>
  34. Within this directory, all code is prefixed with the namespace
  35. "My_". Within the "acls" subdirectory, the component prefix "Acl_"
  36. is added, giving a final class name of "My_Acl_Site". Similarly, the
  37. "forms" subdirectory maps to "Form_", giving "My_Form_Login". The
  38. "models" subdirectory maps to "Model_", giving "My_Model_User".
  39. </para>
  40. <para>
  41. You can use a resource autoloader to autoload these classes. To
  42. instantiate the resource autoloader, you are required to pass at the
  43. minimum the base path and namespace for the resources it will be
  44. responsible for:
  45. </para>
  46. <programlisting language="php"><![CDATA[
  47. $resourceLoader = new Zend_Loader_Autoloader_Resource(array(
  48. 'basePath' => 'path/to/some/directory',
  49. 'namespace' => 'My',
  50. ));
  51. ]]></programlisting>
  52. <note>
  53. <title>Base namespace</title>
  54. <para>
  55. In <classname>Zend_Loader_Autoloader</classname>, you are expected to
  56. provide the trailing underscore ("_") in your namespace if your
  57. autoloader will use it to match the namespace.
  58. <classname>Zend_Loader_Autoloader_Resource</classname> makes the
  59. assumption that all code you are autoloading will use an
  60. underscore separator between namespaces, components, and
  61. classes. As a result, you do not need to use the trailing
  62. underscore when registering a resource autoloader.
  63. </para>
  64. </note>
  65. <para>
  66. Now that we have setup the base resource autoloader, we can add some
  67. components to it to autoload. This is done using the
  68. <methodname>addResourceType()</methodname> method, which accepts three
  69. arguments: a resource "type", used internally as a reference name;
  70. the subdirectory path underneath the base path in which these
  71. resources live; and the component namespace to append to the base
  72. namespace. As an example, let's add each of our resource types.
  73. </para>
  74. <programlisting language="php"><![CDATA[
  75. $resourceLoader->addResourceType('acl', 'acls/', 'Acl')
  76. ->addResourceType('form', 'forms/', 'Form')
  77. ->addResourceType('model', 'models/', 'Model');
  78. ]]></programlisting>
  79. <para>
  80. Alternately, you could pass these as an array to
  81. <methodname>addResourceTypes()</methodname>; the following is equivalent to the
  82. above:
  83. </para>
  84. <programlisting language="php"><![CDATA[
  85. $resourceLoader->addResourceTypes(array(
  86. 'acl' => array(
  87. 'path' => 'acls/',
  88. 'namespace' => 'Acl',
  89. ),
  90. 'form' => array(
  91. 'path' => 'forms/',
  92. 'namespace' => 'Form',
  93. ),
  94. 'model' => array(
  95. 'path' => 'models/',
  96. 'namespace' => 'Model',
  97. ),
  98. ));
  99. ]]></programlisting>
  100. <para>
  101. Finally, you can specify all of this when instantiating the object,
  102. by simply specifying a "resourceTypes" key in the options passed and
  103. a structure like that above:
  104. </para>
  105. <programlisting language="php"><![CDATA[
  106. $resourceLoader = new Zend_Loader_Autoloader_Resource(array(
  107. 'basePath' => 'path/to/some/directory',
  108. 'namespace' => 'My',
  109. 'resourceTypes' => array(
  110. 'acl' => array(
  111. 'path' => 'acls/',
  112. 'namespace' => 'Acl',
  113. ),
  114. 'form' => array(
  115. 'path' => 'forms/',
  116. 'namespace' => 'Form',
  117. ),
  118. 'model' => array(
  119. 'path' => 'models/',
  120. 'namespace' => 'Model',
  121. ),
  122. ),
  123. ));
  124. ]]></programlisting>
  125. </sect2>
  126. <sect2 id="zend.loader.autoloader-resource.module">
  127. <title>The Module Resource Autoloader</title>
  128. <para>
  129. Zend Framework ships with a concrete implementation of
  130. <classname>Zend_Loader_Autoloader_Resource</classname> that contains resource
  131. type mappings that cover the default recommended directory structure
  132. for Zend Framework <acronym>MVC</acronym> applications. This loader,
  133. <classname>Zend_Application_Module_Autoloader</classname>, comes with the
  134. following mappings:
  135. </para>
  136. <programlisting language="text"><![CDATA[
  137. forms/ => Form
  138. models/ => Model
  139. DbTable/ => Model_DbTable
  140. mappers/ => Model_Mapper
  141. plugins/ => Plugin
  142. services/ => Service
  143. views/
  144. helpers => View_Helper
  145. filters => View_Filter
  146. ]]></programlisting>
  147. <para>
  148. As an example, if you have a module with the prefix of "Blog_", and
  149. attempted to instantiate the class "Blog_Form_Entry", it would look
  150. in the resource directory's "forms/" subdirectory for a file named
  151. "Entry.php".
  152. </para>
  153. <para>
  154. When using module bootstraps with <classname>Zend_Application</classname>, an
  155. instance of <classname>Zend_Application_Module_Autoloader</classname> will be
  156. created by default for each discrete module, allowing you to
  157. autoload module resources.
  158. </para>
  159. </sect2>
  160. <sect2 id="zend.loader.autoloader-resource.factory">
  161. <title>Using Resource Autoloaders as Object Factories</title>
  162. <para></para>
  163. <!-- @todo -->
  164. </sect2>
  165. <sect2 id="zend.loader.autoloader-resource.reference">
  166. <title>Resource Autoloader Reference</title>
  167. <para></para>
  168. <!-- @todo -->
  169. </sect2>
  170. <!-- @todo
  171. Write section on using load() functionality
  172. Potentially add functionality to load() to allow passing arguments
  173. Show how to use overloading to retrieve class instances
  174. Write reference section
  175. -->
  176. </sect1>