PageRenderTime 52ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/docs/manual/en-US/chapters/introduction.md

http://github.com/joomla/joomla-platform
Markdown | 361 lines | 245 code | 116 blank | 0 comment | 0 complexity | 843617f4ea799f49644f971ea3c3bef7 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. # Joomla Platform Manual
  2. ## Introduction
  3. This is the introduction to the Joomla Platform.
  4. ## Folder Structure
  5. The following outlines the purpose of the top-level folder structure of
  6. the Joomla Platform as found in the [GitHub repository](https://github.com/joomla/joomla-platform/ "Joomla Platform Github repository").
  7. Folder | Description
  8. ---------- | --------------------
  9. /build | Contains information relevant for building code style reports about the platform. Output from various automated processes may also end up in this folder.
  10. /docs | Contain developer manuals in Markdown format.
  11. /libraries | Contains all the server-side PHP code used in the Joomla Platform API.
  12. /media | Contains any client-side resources used by the platform.
  13. /tests | Contains all of the unit tests used for quality control.
  14. ## Bootstrapping
  15. Bootstrapping the Joomla Platform is done by including
  16. `/libraries/import.php` in your application. This file will initiliase a
  17. number of constants if they are not already defined by the developer
  18. prior to including the import.php file:
  19. Name | Description
  20. ---------------- | -----------------------
  21. JPATH\_PLATFORM | The path to the Joomla Platform (where `loader.php` or `platform.php` is located, usually in the same folder as `import.php`).
  22. IS\_WIN | A boolean value, true if the platform is Microsoft Windows based.
  23. IS\_MAC | A boolean value, true if the platform is Apple OSX based.
  24. IS\_UNIX | A bollean value, true is the platform is some flavor or Unix, Linux or similar (but not Mac).
  25. The bootstrap file will load the `JPlatform` and `JLoader` classes and
  26. also `JFactory`, `JException` (legacy), `JObject`, `JRequest` (legacy), `JText`
  27. and `JRoute`. Depending on the version, it may also register classes in a
  28. transitional state that do not conform to the auto-loader standards.
  29. ### Minimalistic Approach
  30. If required, the bootstrap file (`import.php`) can be ignored and a custom
  31. solution can be implemented (such as if the core `JFactory` class is to be
  32. overriden).
  33. ```php
  34. // Define the base path.
  35. define('JPATH_PLATFORM', '/path/to/platform/');
  36. // Load the platform version and loader classes.
  37. require_once JPATH_PLATFORM . '/platform.php';
  38. require_once JPATH_PLATFORM . '/loader.php';
  39. // Setup the autoloaders.
  40. JLoader::setup();
  41. // Do custom registration if required.
  42. ```
  43. ### Legacy Platform
  44. The Joomla Platform also supports a legacy tree for API. It includes
  45. many classes that are only used by the Joomla CMS, or classes and
  46. packages that have been upgraded and introduced backward compatibility
  47. issues. Bootstrapping the legacy Joomla Platform is done by including
  48. `/libraries/import.legacy.php`. This instructs the auto-loader to look
  49. for classes in the legacy tree first, and then in the core tree.
  50. ### Using Phar
  51. The Joomla Platform can be packed into a Phar file (a PHP archive). This can allow a developer to ship an application with the Platform in a compressed format so that everything can 'just work' without downloading the Platform separately. In large bespoke projects it also provides a convenient way to update the Joomla Platform with a single file and also ensuring that development and production environments are all set up with the same version of the Platform.
  52. To make a Phar of the Platform first download the Packager Tool from [https://github.com/LouisLandry/packager/downloads](https://github.com/LouisLandry/packager/downloads) and put the `joomla-packager.phar` file in your operating system's executable path. This is actually a standalone application with selected parts of the Joomla Platform included with it (it's one of those applications that uses itself to build itself).
  53. To create the phar of the core Joomla Platform (without the legacy tree) go to the root folder of the Joomla Platform and execute `joomla-packager.phar`. This will build automatically detect the `packager.xml` configuration file and place the phar file in `build/joomla.phar`. You can then include this phar file in your application by using the following code:
  54. ```php
  55. require_once 'path/to/joomla.phar';
  56. ```
  57. After this you can just start using the Platform API as required.
  58. For advanced applications or projects that include their own unit test suite, the Platform and test framework can also be built into a phar by executing the following:
  59. ```bash
  60. $ joomla-packager.phar -f packager.test.phar
  61. ```
  62. ## Platform Version
  63. Platform version information can be found by accessing the `JPlatform`
  64. class.
  65. ### JPlatform
  66. `JPlatform` is a final class that cannot be modified by the developer. It
  67. has a number of public constant pertaining to the platform version and
  68. some static utility methods.
  69. #### Constants
  70. Name | Description
  71. ------------------------------ | ---------------------------
  72. JPlatform::PRODUCT | Joomla Platform
  73. JPlatform::RELEASE | The release number of the platform.
  74. JPlatform::MAINTENANCE | The point maintenance version if applicable.
  75. JPlatform::STATUS | The development status.
  76. JPlatform::BUILD | The build number for the platform, if applicable.
  77. JPlatform::CODE\_NAME | A human readable code name for this version.
  78. JPlatform::RELEASE\_DATE | The official release date for this version.
  79. JPlatform::RELEASE\_TIME | The official release time for this version, if applicable.
  80. JPlatform::RELEASE\_TIME\_ZONE | The timezone for the official release date and time.
  81. JPlatform::COPYRIGHT | The copyright statement.
  82. JPlatform::LINK\_TEXT | An HTML hyperlink to the Joomla Project.
  83. #### Methods
  84. `JPlatform` has three utility methods, one for testing the version and two
  85. for display.
  86. Method | Description
  87. --------------------------------- | ------------------------
  88. JPlatform::isCompatible($version) | Tests if $version is the installed version of the platform.
  89. JPlatform::getShortVersion() | A short textual representation of the platform version.
  90. JPlatform::getLongVersion() | A really verbose representation of the platform version.
  91. ```php
  92. // Tests the required version of the platform.
  93. if (!JPlatform::isCompatible('11.4'))
  94. {
  95. throw new LogicException(sprintf('Platform version %s not compatible.', JPlatform::getShortVersion());
  96. }
  97. ```
  98. ## Class Auto-loading
  99. `JLoader` is the mainstay of the Joomla Platform as it controls auto-loading of classes.
  100. It removes the need for the developer to include files by hand, or by using a fall to the `jimport` function.
  101. Multiple ways of auto loading classes, following different conventions are proposed by JLoader.
  102. ### The Namespace Loader
  103. Since the release 12.3 of the Joomla Platform there is the possibility to auto classes within namespaces.
  104. * A developer can register the full path to a top level (root) namespace where the loader can find classes (within this namespace).
  105. * A developer can override an existing namespace path by replacing it with a new one.
  106. * A developer can register multiple paths to the same namespace.
  107. #### Convention
  108. The convention is to have the namespace names matching the directories names.
  109. For example :
  110. ```php
  111. <?php
  112. namespace Chess\Piece;
  113. class Pawn
  114. {
  115. }
  116. ```
  117. must be found in `BASE_PATH/chess/piece/pawn.php` or in `BASE_PATH/Chess/Piece/Pawn.php`.
  118. For the namespace declaration, it is recommanded to use camel case letters as you will have for a class name.
  119. But as you saw above there are different possibilities for the paths case :
  120. #### Lower Case :
  121. The directory structure is lower case and the namespace can be any case.
  122. It must be used when the path is lower case and the namespace camel case.
  123. Example :
  124. ```php
  125. <?php
  126. namespace Chess\Piece;
  127. class Pawn
  128. {
  129. }
  130. ```
  131. for a class in `BASE_PATH/chess/piece/pawn.php`.
  132. #### Natural Case :
  133. The namespace case matches the path case.
  134. It must be used when you have lower case namespaces and paths or when you have camel case namespaces and paths.
  135. Examples :
  136. ```php
  137. <?php
  138. namespace Chess\Piece;
  139. class Pawn
  140. {
  141. }
  142. ```
  143. for a class in `BASE_PATH/Chess/Pieces/Pawn.php`.
  144. ```php
  145. <?php
  146. namespace chess\piece;
  147. class Pawn
  148. {
  149. }
  150. ```
  151. for a class in `BASE_PATH/chess/pieces/pawn.php`.
  152. #### Mixed Case :
  153. It regroups the two options.
  154. It must be used when you have some lower case and camel case paths and camel case or lower case namespace declarations.
  155. For example, Joomla can stay lower case and your application can have a camel case directory structure.
  156. Both can be auto loaded using the same Mixed Case loader.
  157. #### Usage
  158. #### Setup the Loader
  159. In order to correctly use the namespace auto loader you need to setup it according the case strategy you choosed.
  160. ```php
  161. <?php
  162. // Setup the loader with the Lower Case strategy.
  163. JLoader::setup(JLoader::LOWER_CASE, true);
  164. // Setup the loader with the Natural Case strategy.
  165. JLoader::setup(JLoader::NATURAL_CASE, true);
  166. // Setup the loader with the Mixed Case strategy.
  167. JLoader::setup(JLoader::MIXED_CASE, true);
  168. ```
  169. #### Registering a namespace
  170. You can register a top level namespace by using `JLoader::registerNamespace`.
  171. For example :
  172. ```php
  173. <?php
  174. // The two parameters are case sensitive.
  175. // The first one must match the namespace declaration case.
  176. // The second one must match the path case.
  177. JLoader::registerNamespace('Chess', BASE_PATH . '/chess');
  178. ```
  179. All classes respecting the naming and path convention will be auto loaded.
  180. #### Appending an other path
  181. ```php
  182. <?php
  183. // Adding an other path to the Chess namespace.
  184. JLoader::registerNamespace('Chess', AN_OTHER_PATH . '/chess');
  185. ```
  186. #### Reseting a path
  187. ```php
  188. <?php
  189. // Reseting a path by adding an other one.
  190. JLoader::registerNamespace('Chess', AN_OTHER_PATH . '/chess', true);
  191. ```
  192. ### The Prefix Loader
  193. Since 12.1, there is the ability to register where the auto-loader will
  194. look based on a class prefix (previously only the "J" prefix was
  195. supported, bound to the `/libraries/joomla` folder). This allows for
  196. several scenarios:
  197. * A developer can register the prefix of custom classes, and a root path to allow the auto-loader to find them.
  198. * A developer can register an extra path for an existing prefix (for example, this allows the Joomla CMS to have custom libraries but still using the "J" prefix).
  199. * A developer can register a force override for a prefix. This could be used to completely override the core classes with a custom replacement.
  200. #### Convention
  201. The class name must be in camel case and each segment of the name will represent a folder path
  202. where the last segment of the name is the name of the class file.
  203. If there is only one part to the class name, the auto-loader will look for the file in a folder of the
  204. same name.
  205. Folder names must be in lower case.
  206. Examples :
  207. `PrefixUserModel` should be located in `PATH_TO_PREFIX/user/model.php`.
  208. `PrefixUser` should be located in `PATH_TO_PREFIX/user/user.php`.
  209. There is no limit to the depth to which the auto-loader will search,
  210. providing it forms a valid path based on the camel case natural of the
  211. class name.
  212. Note that while acronyms and names such as HTML, XML and MySQL have a standard presention in text,
  213. such terms should observe camel case rules programmatically ("HTML" becomes "Html", "XML" becomes "Xml"
  214. and so on).
  215. #### Usage
  216. ```php
  217. // Tell the auto-loader to also look in the /libraries/cms folder for "J" prefixed classes.
  218. JLoader::registerPrefix('J', JPATH_PLATFORM . '/cms');
  219. // Tell the auto-loader to look for classes starting with "Foo" in a specific folder.
  220. JLoader::registerPrefix('Foo', '/path/to/custom/packages');
  221. // Tell the auto-loader to reset the "J" prefix and point it to a custom fork of the platform.
  222. JLoader::registerPrefix('J', '/my/platform/fork', true);
  223. ```
  224. ### Registering Classes
  225. New classes, or override classes can be registered using the register
  226. method. This method takes the class name, the path to the class file,
  227. and an option boolean to force an update of the class register.
  228. ```php
  229. // Register an adhoc class.
  230. JLoader::register('AdhocClass', '/the/path/adhoc.php');
  231. // Register a custom class to override as core class.
  232. // This must be done before the core class is loaded.
  233. JLoader::register('JDatabase', '/custom/path/database_driver.php', true);
  234. ```
  235. ### Discovering Classes
  236. Classes in a folder that follow a naming convention, but not one the
  237. auto-loader immediately recognises, can be registered collectively with
  238. `JLoader`'s discover method. The `discover` method looks at the file names
  239. in a folder and registers classes based on those names. Additional
  240. arguments can be used to update the class register and recurse into
  241. sub-folders.
  242. ```php
  243. // Register all files in the /the/path/ folder as classes with a name like: Prefix<Filename>
  244. JLoader::discover('Prefix', '/the/path/');
  245. ```