tutorial.txt 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. eZ Components - Base
  2. ~~~~~~~~~~~~~~~~~~~~
  3. .. contents:: Table of Contents
  4. Introduction
  5. ============
  6. The Base component provides the basic functionality, such as autoloading, that
  7. all eZ Components need to function properly. The Base component needs to be
  8. loaded specifically. Base can also autoload external class repositories from
  9. outside the eZ Components.
  10. Aside from the autoload functionality, the Base component also contains a number of
  11. generic Exception classes that all inherit from the ezcBaseException class.
  12. Installation
  13. ============
  14. The installation and configuration of the eZ Components environment is
  15. described in a separate article. Please refer to the `Components Introduction`_
  16. for instructions on installation and configuration of the eZ Components library
  17. and the Base component.
  18. .. _Components Introduction: ../../../../zetacomponents/documentation/install.html
  19. Usage
  20. =====
  21. Debugging
  22. ---------
  23. By default the ezcBase component's autoload mechanism will not throw an
  24. exception when an autoload class can not be found. In some cases (during
  25. development) it is useful to have an exception with detailed information
  26. about which autoload files were searched for, and in which directories.
  27. ezcBase supports an option that enables this behavior::
  28. <?php
  29. $options = new ezcBaseAutoloadOptions;
  30. $options->debug = true;
  31. ezcBase::setOptions( $options );
  32. ?>
  33. **Warning**: Exceptions are ignored when they are thrown from an autoload()
  34. handler in PHP. In order to see the exception message that is thrown when a
  35. class can not be found, you need to catch the exception *in* the autoload()
  36. handler. Your autoload() function could then look like::
  37. function __autoload( $className )
  38. {
  39. try
  40. {
  41. ezcBase::autoload( $className );
  42. }
  43. catch ( Exception $e )
  44. {
  45. echo $e->getMessage();
  46. }
  47. }
  48. Preloading
  49. ----------
  50. The default autoload policy of the eZ Components is to load every class
  51. file on demand only. It is also possible to load all classes of one
  52. component at the same time, when one of the component's classes is
  53. requested for the first time. You can change this behavior with the
  54. "preload" option that is available through the ezcBaseAutoloadOptions option
  55. class. You can turn preloading on with::
  56. <?php
  57. $options = new ezcBaseAutoloadOptions;
  58. $options->preload = true;
  59. ezcBase::setOptions( $options );
  60. ?>
  61. Please note that preloading will *not* be done for Exception classes.
  62. Adding class repositories located outside eZ Components to autoload system
  63. --------------------------------------------------------------------------
  64. It can be useful to add repositories of user-defined classes to the eZ
  65. Components autoload system. The ezcBase::addClassRepository() method can be
  66. used to perform this task. You need to arrange the desired external classes
  67. in a class repository. That is, make sure that classes and corresponding
  68. \*_autoload.php files are named and placed according to the explanations below.
  69. After they are in the proper structure, you can call addClassRepository() with
  70. the proper parameters before you use the external classes.
  71. External classes will then be loaded by autoload system.
  72. ezcBase::addClassRepository() takes two arguments:
  73. - $basePath is the base path for the whole class repository.
  74. - $autoloadDirPath is the path where autoload files for this repository are found.
  75. The paths in the autoload files are *not* relative to the package directory
  76. as specified by the $basePath argument. In other words, class definition files will
  77. only be searched for in the location $autoloadDirPath.
  78. Consider the following example:
  79. - There is a class repository stored in the directory "./repos".
  80. - Autoload files for this repository are stored in "./repos/autoloads".
  81. - There are two components in this repository: "Me" and "You".
  82. - The "Me" component has the classes "erMyClass1" and "erMyClass2".
  83. - The "You" component has the classes "erYourClass1" and "erYourClass2".
  84. In this case, you need to create the following files in "./repos/autoloads".
  85. Note that the prefix to _autoload.php ("my" and "your") in the filename is the
  86. first part of the classname (excluding the lowercase classname prefix - "er").
  87. Content of my_autoload.php:
  88. .. include:: repos/autoloads/my_autoload.php
  89. :literal:
  90. Content of your_autoload.php:
  91. .. include:: repos/autoloads/your_autoload.php
  92. :literal:
  93. The directory structure for the external repository is then: ::
  94. ./repos/autoloads/my_autoload.php
  95. ./repos/autoloads/your_autoload.php
  96. ./repos/Me/myclass1.php
  97. ./repos/Me/myclass2.php
  98. ./repos/You/yourclass1.php
  99. ./repos/You/yourclass2.php
  100. To use this repository with the autoload mechanism, use the
  101. following code:
  102. .. include:: tutorial_example_01.php
  103. :literal:
  104. The above code will output: ::
  105. Class 'erMyClass2'
  106. Class 'erYourClass1'
  107. Lazy initialization
  108. -------------------
  109. Lazy initialization is a mechanism to load and configure a component, only
  110. when it is really used in your application. This mechanism saves time for
  111. parsing the classes and configuration, when the component is not used at all
  112. during one request. The implementation in ezcBaseInit may be reused by other
  113. applications and components, like the following example will show.
  114. .. include:: tutorial_lazy_initialization.php
  115. :literal:
  116. The example shows a random class implementing the singleton pattern, which may
  117. be some database connection handler, or anything similar in your case. The
  118. getInstance() method shows a typical PHP 5 implementation except the
  119. additional line 14, which checks, if a configuration callback was provided
  120. earlier and configures the newly created instance. If no configuration
  121. callback was provided, nothing will happen. The customKey is used to receive
  122. the right callback from ezcBaseInit and needs to be known by the user, who
  123. wants to define a configuration callback for your class.
  124. In line 32 the class used to configure your instance on creation is defined.
  125. The first parameter is the key used earlier in the getInstance method, to
  126. reference the right class, and the second parameter is the name of your
  127. configuration class.
  128. The configuration class beginning in line 22 just needs to implement the
  129. ezcBaseConfigurationInitializer interface, which defines one
  130. method: configureObject(). This method will be called with the object to
  131. configure as a single parameter. In the example, a new public property on the
  132. customSingleton instance is created, which will be echo'd later to show the
  133. success of the configuration.
  134. The configuration itself will not happen before the actual instance is created
  135. in line 35 performing the static call on customSingleton::getInstance(). The
  136. var_dump() in the following line shows, that the property value is set and
  137. contains the earlier set value (int) 42.
  138. File Operations
  139. ---------------
  140. Finding files recursively
  141. `````````````````````````
  142. This example shows how to use the ezcBaseFile::findRecursive() method:
  143. .. include:: tutorial_example_02.php
  144. :literal:
  145. The code in this example searches for files in the ``/dat/dev/ezcomponents``
  146. directory. It will only include files that match *all* patterns in the
  147. $includeFilters array (the second parameter). Files that match *any* of the
  148. patterns in the $excludeFilters array (the third parameter) will not be returned.
  149. In other words, the code above searches for files in the ``dat/dev/ezcomponents``
  150. directory, which are in the ``src/`` directory and end with ``_autoload.php``,
  151. except for files that are in the ``/autoload/`` directory.
  152. Removing directories recursively
  153. ````````````````````````````````
  154. This example shows how to use the ezcBaseFile::removeRecursive() method:
  155. .. include:: tutorial_example_03.php
  156. :literal:
  157. This code simply removes the directory ``/dat/dev/ezcomponents/trash`` and all
  158. of its files and sub-directories.
  159. **Warning: Use this function with care, as it has the potential to erase
  160. everything that the current user has access to.**
  161. Overloading the callback
  162. ````````````````````````
  163. The ezcBaseFile::findRecursive() method internally uses the
  164. ezcBaseFile::walkRecursive() method to do the actual recursing. The callback
  165. method ezcBaseFile::findRecursiveCallback() is then responsible for collecting
  166. the data. In case you want to do additional things, such as printing progress,
  167. you can either call walkRecursive() yourself with a callback function of your
  168. choice, or overload the ezcBaseFile class and provide a new
  169. findRecursiveCallback() method. The code below uses
  170. ezcBaseFile::walkRecursive() directly in order to display dots for when ever it
  171. finds a new directory:
  172. .. include:: tutorial_example_04.php
  173. :literal:
  174. ..
  175. Local Variables:
  176. mode: rst
  177. fill-column: 79
  178. End:
  179. vim: et syn=rst tw=79