Zend_Loader-ClassMapAutoloader.xml 13 KB

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