2
0

Zend_Loader-AutoloaderFactory.xml 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.loader.autoloader-factory">
  4. <title>The AutoloaderFactory</title>
  5. <sect2 id="zend.loader.autoloader-factory.intro">
  6. <title>Overview</title>
  7. <para>
  8. Starting with version 1.12.0, Zend Framework now offers multiple autoloader strategies.
  9. Often, it will be useful to employ multiple autoloading strategies; as an example, you
  10. may have a class map for your most used classes, but want to use a PSR-0 style
  11. autoloader for 3rd party libraries.
  12. </para>
  13. <para>
  14. While you could potentially manually configure these, it may be more useful to define
  15. the autoloader configuration somewhere and cache it. For these cases, the
  16. <classname>AutoloaderFactory</classname> will be useful.
  17. </para>
  18. </sect2>
  19. <sect2 id="zend.loader.autoloader-factory.quick-start">
  20. <title>Quick Start</title>
  21. <para>
  22. Configuration may be stored as a PHP array, or in some form of configuration file. As an
  23. example, consider the following PHP array:
  24. </para>
  25. <programlisting language="php"><![CDATA[
  26. $config = array(
  27. 'Zend_Loader_ClassMapAutoloader' => array(
  28. 'application' => APPLICATION_PATH . '/autoload_classmap.php',
  29. 'zf' => APPLICATION_PATH . '/../library/Zend/autoload_classmap.php',
  30. ),
  31. 'Zend_Loader_StandardAutoloader' => array(
  32. 'namespaces' => array(
  33. 'Phly\Mustache' => APPLICATION_PATH . '/../library/Phly/Mustache',
  34. 'Doctrine' => APPLICATION_PATH . '/../library/Doctrine',
  35. ),
  36. ),
  37. );
  38. ]]></programlisting>
  39. <para>
  40. An equivalent INI-style configuration might look like the following:
  41. </para>
  42. <programlisting language="ini"><![CDATA[
  43. Zend_Loader_ClassMapAutoloader.application = APPLICATION_PATH "/autoload_classmap.php"
  44. Zend_Loader_ClassMapAutoloader.zf = APPLICATION_PATH "/../library/Zend/autoload_classmap.php"
  45. Zend_Loader_StandardAutoloader.namespaces.Phly\Mustache = APPLICATION_PATH "/../library/Phly/Mustache"
  46. Zend_Loader_StandardAutoloader.namespaces.Doctrine = APPLICATION_PATH "/../library/Doctrine"
  47. ]]></programlisting>
  48. <para>
  49. Once you have your configuration in a PHP array, you simply pass it to the
  50. <classname>AutoloaderFactory</classname>.
  51. </para>
  52. <programlisting language="php"><![CDATA[
  53. // This example assumes ZF is on your include_path.
  54. // You could also load the factory class from a path relative to the
  55. // current script, or via an absolute path.
  56. require_once 'Zend_Loader_AutoloaderFactory.php';
  57. Zend_Loader_AutoloaderFactory::factory($config);
  58. ]]></programlisting>
  59. <para>
  60. The <classname>AutoloaderFactory</classname> will instantiate each autoloader with the
  61. given options, and also call its <methodname>register()</methodname> method to register
  62. it with the SPL autoloader.
  63. </para>
  64. </sect2>
  65. <sect2 id="zend.loader.autoloader-factory.options">
  66. <title>Configuration Options</title>
  67. <variablelist>
  68. <title>AutoloaderFactory Options</title>
  69. <varlistentry>
  70. <term>$options</term>
  71. <listitem>
  72. <para>
  73. The <classname>AutoloaderFactory</classname> expects an associative array or
  74. <interfacename>Traversable</interfacename> object. Keys should be valid
  75. autoloader class names, and the values should be the options that should be
  76. passed to the class constructor.
  77. </para>
  78. <para>
  79. Internally, the <classname>AutoloaderFactory</classname> checks to see if
  80. the autoloader class referenced exists. If not, it will use the <link linkend="zend.loader.standard-autoloader">StandardAutoloader</link> to
  81. attempt to load the class via the <varname>include_path</varname> (or, in
  82. the case of "Zend"-namespaced classes, using the Zend Framework library
  83. path). If the class is not found, or does not implement the
  84. <link linkend="zend.loader.spl-autoloader">SplAutoloader</link> interface,
  85. an exception will be raised.
  86. </para>
  87. </listitem>
  88. </varlistentry>
  89. </variablelist>
  90. </sect2>
  91. <sect2 id="zend.loader.autoloader-factory.methods">
  92. <title>Available Methods</title>
  93. <refentry id="zend.loader.autoloader-factory.methods.factory">
  94. <refnamediv>
  95. <refname>factory</refname>
  96. <refpurpose>Instantiate and register autoloaders</refpurpose>
  97. </refnamediv>
  98. <refsynopsisdiv>
  99. <methodsynopsis>
  100. <methodname>factory</methodname>
  101. <methodparam>
  102. <funcparams>$options</funcparams>
  103. </methodparam>
  104. </methodsynopsis>
  105. </refsynopsisdiv>
  106. <refsection>
  107. <title>factory()</title>
  108. <para>
  109. This method is <emphasis>static</emphasis>, and is used to instantiate
  110. autoloaders and register them with the SPL autoloader. It expects either an
  111. array or <interfacename>Traversable</interfacename> object as denoted in the
  112. <link linkend="zend.loader.autoloader-factory.options">Options section</link>.
  113. </para>
  114. </refsection>
  115. </refentry>
  116. <refentry id="zend.loader.autoloader-factory.methods.get-registered-autoloaders">
  117. <refnamediv>
  118. <refname>getRegisteredAutoloaders</refname>
  119. <refpurpose>Retrieve a list of all autoloaders registered using the factory</refpurpose>
  120. </refnamediv>
  121. <refsynopsisdiv>
  122. <methodsynopsis>
  123. <methodname>getRegisteredAutoloaders</methodname>
  124. </methodsynopsis>
  125. </refsynopsisdiv>
  126. <refsection>
  127. <title>getRegisteredAutoloaders()</title>
  128. <para>
  129. This method is <emphasis>static</emphasis>, and may be used to retrieve a list
  130. of all autoloaders registered via the <methodname>factory()</methodname> method.
  131. It returns simply an array of autoloader instances.
  132. </para>
  133. </refsection>
  134. </refentry>
  135. </sect2>
  136. <sect2 id="zend.loader.autoloader-factory.examples">
  137. <title>Examples</title>
  138. <para>
  139. Please see the <link linkend="zend.loader.autoloader-factory.quick-start">Quick
  140. Start</link> for a detailed example.
  141. </para>
  142. </sect2>
  143. </sect1>