Zend_Loader-ClassMapAutoloader.xml 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.loader.class-map-autoloader">
  4. <title>The ClassMapAutoloader</title>
  5. <sect2 id="zend.loader.class-map-autoloader.intro">
  6. <title>Overview</title>
  7. <para>
  8. The <classname>ClassMapAutoloader</classname> is designed with performance in mind. The
  9. idea behind it is simple: when asked to load a class, see if it's in the map, and, if
  10. so, load the file associated with the class in the map. This avoids unnecessary
  11. filesystem operations, and can also ensure the autoloader "plays nice" with opcode
  12. caches and PHP's realpath cache.
  13. </para>
  14. <para>
  15. In order to use the <classname>ClassMapAutoloader</classname>, you first need class
  16. maps. Zend Framework also provides a tool for generating these class maps; you can find it in
  17. <filename>bin/classmap_generator.php</filename> of the distribution. Full documentation
  18. of this too is provided in <xref linkend="zend.loader.classmap-generator"/>.
  19. </para>
  20. </sect2>
  21. <sect2 id="zend.loader.class-map-autoloader.quick-start">
  22. <title>Quick Start</title>
  23. <para>
  24. The first step is to generate a class map file. You may run this over any directory
  25. containing source code anywhere underneath it.
  26. </para>
  27. <programlisting language="sh"><![CDATA[
  28. php classmap_generator.php Some/Directory/
  29. ]]></programlisting>
  30. <para>
  31. This will create a file named <filename>Some/Directory/autoload_classmap.php</filename>, which
  32. is a PHP file returning an associative array that represents the class map.
  33. </para>
  34. <para>
  35. Within your code, you will now instantiate the
  36. <classname>ClassMapAutoloader</classname>, and provide it the location of the map.
  37. </para>
  38. <programlisting language="php"><![CDATA[
  39. // This example assumes ZF is on your include_path.
  40. // You could also load the autoloader class from a path relative to the
  41. // current script, or via an absolute path.
  42. require_once 'Zend/Loader/ClassMapAutoloader.php';
  43. $loader = new Zend_Loader_ClassMapAutoloader();
  44. // Register the class map:
  45. $loader->registerAutoloadMap('Some/Directory/autoload_classmap.php');
  46. // Register with spl_autoload:
  47. $loader->register();
  48. ]]></programlisting>
  49. <para>
  50. At this point, you may now use any classes referenced in your class map.
  51. </para>
  52. </sect2>
  53. <sect2 id="zend.loader.class-map-autoloader.options">
  54. <title>Configuration Options</title>
  55. <para>
  56. The <classname>ClassMapAutoloader</classname> defines the following options.
  57. </para>
  58. <variablelist>
  59. <title>ClassMapAutoloader Options</title>
  60. <varlistentry>
  61. <term>$options</term>
  62. <listitem>
  63. <para>
  64. The <classname>ClassMapAutoloader</classname> expects an array of options,
  65. where each option is either a filename referencing a class map, or an
  66. associative array of class name/filename pairs.
  67. </para>
  68. <para>
  69. As an example:
  70. </para>
  71. <programlisting language="php"><![CDATA[
  72. // Configuration defining both a file-based class map, and an array map
  73. $config = array(
  74. __DIR__ . '/library/autoload_classmap.php', // file-based class map
  75. array( // array class map
  76. 'Application_Bootstrap' => __DIR__ . '/application/Bootstrap.php',
  77. 'Test_Bootstrap' => __DIR__ . '/tests/Bootstrap.php',
  78. ),
  79. );
  80. ]]></programlisting>
  81. </listitem>
  82. </varlistentry>
  83. </variablelist>
  84. </sect2>
  85. <sect2 id="zend.loader.class-map-autoloader.methods">
  86. <title>Available Methods</title>
  87. <refentry id="zend.loader.class-map-autoloader.methods.constructor">
  88. <refnamediv>
  89. <refname>__construct</refname>
  90. <refpurpose>Initialize and configure the object</refpurpose>
  91. </refnamediv>
  92. <refsynopsisdiv>
  93. <methodsynopsis>
  94. <methodname>__construct</methodname>
  95. <methodparam>
  96. <funcparams>$options = null</funcparams>
  97. </methodparam>
  98. </methodsynopsis>
  99. </refsynopsisdiv>
  100. <refsection>
  101. <title>Constructor</title>
  102. <para>
  103. Used during instantiation of the object. Optionally, pass options, which may be
  104. either an array or <interfacename>Traversable</interfacename> object; this
  105. argument will be passed to <link linkend="zend.loader.class-map-autoloader.methods.set-options">setOptions()</link>.
  106. </para>
  107. </refsection>
  108. </refentry>
  109. <refentry id="zend.loader.class-map-autoloader.methods.set-options">
  110. <refnamediv>
  111. <refname>setOptions</refname>
  112. <refpurpose>Configure the autoloader</refpurpose>
  113. </refnamediv>
  114. <refsynopsisdiv>
  115. <methodsynopsis>
  116. <methodname>setOptions</methodname>
  117. <methodparam>
  118. <funcparams>$options</funcparams>
  119. </methodparam>
  120. </methodsynopsis>
  121. </refsynopsisdiv>
  122. <refsection>
  123. <title>setOptions()</title>
  124. <para>
  125. Configures the state of the autoloader, including registering class maps.
  126. Expects an array or <interfacename>Traversable</interfacename> object; the
  127. argument will be passed to <link linkend="zend.loader.class-map-autoloader.methods.register-autoload-maps">registerAutoloadMaps()</link>.
  128. </para>
  129. </refsection>
  130. </refentry>
  131. <refentry id="zend.loader.class-map-autoloader.methods.register-autoload-map">
  132. <refnamediv>
  133. <refname>registerAutoloadMap</refname>
  134. <refpurpose>Register a class map</refpurpose>
  135. </refnamediv>
  136. <refsynopsisdiv>
  137. <methodsynopsis>
  138. <methodname>registerAutoloadMap</methodname>
  139. <methodparam>
  140. <funcparams>$map</funcparams>
  141. </methodparam>
  142. </methodsynopsis>
  143. </refsynopsisdiv>
  144. <refsection>
  145. <title>registerAutoloadMap()</title>
  146. <para>
  147. Registers a class map with the autoloader. <varname>$map</varname> may be either
  148. a string referencing a PHP script that returns a class map, or an array defining
  149. a class map.
  150. </para>
  151. <para>
  152. More than one class map may be registered; each will be merged with the
  153. previous, meaning it's possible for a later class map to overwrite entries from
  154. a previously registered map.
  155. </para>
  156. </refsection>
  157. </refentry>
  158. <refentry id="zend.loader.class-map-autoloader.methods.register-autoload-maps">
  159. <refnamediv>
  160. <refname>registerAutoloadMaps</refname>
  161. <refpurpose>Register multiple class maps at once</refpurpose>
  162. </refnamediv>
  163. <refsynopsisdiv>
  164. <methodsynopsis>
  165. <methodname>registerAutoloadMaps</methodname>
  166. <methodparam>
  167. <funcparams>$maps</funcparams>
  168. </methodparam>
  169. </methodsynopsis>
  170. </refsynopsisdiv>
  171. <refsection>
  172. <title>registerAutoloadMaps()</title>
  173. <para>
  174. Register multiple class maps with the autoloader. Expects either an array or
  175. <interfacename>Traversable</interfacename> object; it then iterates over the
  176. argument and passes each value to <link linkend="zend.loader.class-map-autoloader.methods.register-autoload-map">registerAutoloadMap()</link>.
  177. </para>
  178. </refsection>
  179. </refentry>
  180. <refentry id="zend.loader.class-map-autoloader.methods.get-autoload-map">
  181. <refnamediv>
  182. <refname>getAutoloadMap</refname>
  183. <refpurpose>Retrieve the current class map</refpurpose>
  184. </refnamediv>
  185. <refsynopsisdiv>
  186. <methodsynopsis>
  187. <methodname>getAutoloadMap</methodname>
  188. </methodsynopsis>
  189. </refsynopsisdiv>
  190. <refsection>
  191. <title>getAutoloadMap()</title>
  192. <para>
  193. Retrieves the state of the current class map; the return value is simply an
  194. array.
  195. </para>
  196. </refsection>
  197. </refentry>
  198. <refentry id="zend.loader.class-map-autoloader.methods.autoload">
  199. <refnamediv>
  200. <refname>autoload</refname>
  201. <refpurpose>Attempt to load a class.</refpurpose>
  202. </refnamediv>
  203. <refsynopsisdiv>
  204. <methodsynopsis>
  205. <methodname>autoload</methodname>
  206. <methodparam>
  207. <funcparams>$class</funcparams>
  208. </methodparam>
  209. </methodsynopsis>
  210. </refsynopsisdiv>
  211. <refsection>
  212. <title>autoload()</title>
  213. <para>
  214. Attempts to load the class specified. Returns a boolean
  215. <constant>false</constant> on failure, or a string indicating the class loaded
  216. on success.
  217. </para>
  218. </refsection>
  219. </refentry>
  220. <refentry id="zend.loader.class-map-autoloader.methods.register">
  221. <refnamediv>
  222. <refname>register</refname>
  223. <refpurpose>Register with spl_autoload.</refpurpose>
  224. </refnamediv>
  225. <refsynopsisdiv>
  226. <methodsynopsis>
  227. <methodname>register</methodname>
  228. </methodsynopsis>
  229. </refsynopsisdiv>
  230. <refsection>
  231. <title>register()</title>
  232. <para>
  233. Registers the <methodname>autoload()</methodname> method of the current instance
  234. with <function>spl_autoload_register()</function>.
  235. </para>
  236. </refsection>
  237. </refentry>
  238. </sect2>
  239. <sect2 id="zend.loader.class-map-autoloader.examples">
  240. <title>Examples</title>
  241. <example id="zend.loader.class-map-autoloader.examples.configuration">
  242. <title>Using configuration to seed ClassMapAutoloader</title>
  243. <para>
  244. Often, you will want to configure your <classname>ClassMapAutoloader</classname>.
  245. These values may come from a configuration file, a cache (such as ShMem or
  246. memcached), or a simple PHP array. The following is an example of a PHP array that
  247. could be used to configure the autoloader:
  248. </para>
  249. <programlisting language="php"><![CDATA[
  250. // Configuration defining both a file-based class map, and an array map
  251. $config = array(
  252. APPLICATION_PATH . '/../library/autoload_classmap.php', // file-based class map
  253. array( // array class map
  254. 'Application_Bootstrap' => APPLICATION_PATH . '/Bootstrap.php',
  255. 'Test_Bootstrap' => APPLICATION_PATH . '/../tests/Bootstrap.php',
  256. ),
  257. );
  258. ]]></programlisting>
  259. <para>
  260. An eqivalent INI style configuration might look like this:
  261. </para>
  262. <programlisting language="ini"><![CDATA[
  263. classmap.library = APPLICATION_PATH "/../library/autoload_classmap.php"
  264. classmap.resources.Application_Bootstrap = APPLICATION_PATH "/Bootstrap.php"
  265. classmap.resources.Test_Bootstrap = APPLICATION_PATH "/../tests/Bootstrap.php"
  266. ]]></programlisting>
  267. <para>
  268. Once you have your configuration, you can pass it either to the constructor of the
  269. <classname>ClassMapAutoloader</classname>, to its
  270. <methodname>setOptions()</methodname> method, or to
  271. <methodname>registerAutoloadMaps()</methodname>.
  272. </para>
  273. <programlisting language="php"><![CDATA[
  274. /* The following are all equivalent */
  275. // To the constructor:
  276. $loader = new Zend_Loader_ClassMapAutoloader($config);
  277. // To setOptions():
  278. $loader = new Zend_Loader_ClassMapAutoloader();
  279. $loader->setOptions($config);
  280. // To registerAutoloadMaps():
  281. $loader = new Zend_Loader_ClassMapAutoloader();
  282. $loader->registerAutoloadMaps($config);
  283. ]]></programlisting>
  284. </example>
  285. </sect2>
  286. </sect1>