PageRenderTime 43ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/wire/core/Module.php

https://bitbucket.org/webbear/processwire-base-installation
PHP | 247 lines | 10 code | 20 blank | 217 comment | 0 complexity | 35e32601b4d26da5e02d37c1f1372101 MD5 | raw file
  1. <?php
  2. /**
  3. * ProcessWire Module Interface
  4. *
  5. * Provides the base interfaces required by modules.
  6. *
  7. * ProcessWire 2.x
  8. * Copyright (C) 2011 by Ryan Cramer
  9. * Licensed under GNU/GPL v2, see LICENSE.TXT
  10. *
  11. * http://www.processwire.com
  12. * http://www.ryancramer.com
  13. *
  14. */
  15. /**
  16. * ConfigurableModule is an interface that indicates the module is configurable by providing
  17. * a getModuleConfigInputfields method and config properties may be get/set directly like:
  18. *
  19. * $val = $obj->property, and $obj->property = $val
  20. *
  21. * When you use this as an interface, you MUST also use 'Module' as an interface,
  22. * i.e. "class Something implements Module, ConfigurableModule"
  23. *
  24. * Hint: Make your ConfigurableModule classes inherit from WireData, which already has
  25. * the get/set required methods.
  26. *
  27. * You may optionally specify a handler method for configuration data: setConfigData().
  28. * See commented function reference in the interface below.
  29. *
  30. */
  31. interface ConfigurableModule {
  32. /**
  33. * Return an InputfieldsWrapper of Inputfields used to configure the class
  34. *
  35. * @param array $data Array of config values indexed by field name
  36. * @return InputfieldsWrapper
  37. *
  38. */
  39. public static function getModuleConfigInputfields(array $data);
  40. public function __get($key);
  41. public function __set($key, $value);
  42. /**
  43. * An optional method you may include in your ConfigurableModule to have ProcessWire
  44. * send the configuration data to it rather than populating the properties individually.
  45. *
  46. * @param array $data Array of data in $key => $value format.
  47. *
  48. public function setConfigData(array $data);
  49. *
  50. */
  51. }
  52. /**
  53. * Base interface for all ProcessWire Modules
  54. *
  55. */
  56. interface Module {
  57. /**
  58. * Return an array of module information
  59. *
  60. * Example:
  61. *
  62. * public static function getModuleInfo() {
  63. * return array(
  64. * 'title' => 'Your Module's Title',
  65. * 'version' => 100,
  66. * 'author' => 'Ryan Cramer',
  67. * 'summary' => 'Description of what this module does and who made it.',
  68. * 'href' => 'http://www.domain.com/info/about/this/module/',
  69. * 'singular' => false,
  70. * 'autoload' => false,
  71. * 'requires' => array('HelloWorld', 'LazyCron'),
  72. * 'installs' => array('Module1', 'Module2', 'Module3'),
  73. * );
  74. * }
  75. *
  76. *
  77. * The retured array is associative with the following fields. Fields bulleted with a "+" are required and
  78. * fields bulleted with a "-" are optional, and fields bulleted with a "*" may be optional (see details):
  79. *
  80. * + title: The module's title.
  81. * + version: an integer that indicates the version number, 101 = 1.0.1
  82. * + summary: a summary of the module (1 sentence to 1 paragraph reommended)
  83. * - href: URL to more information about the module.
  84. * - requires: array of module class names that are required by this module in order to install.
  85. * If just one module is required, then it can also be a string with just the module name.
  86. * - installs: array of module class names that this module will handle install/uninstall.
  87. * This causes PW's dependency checker to ignore them and it is assumed your module will handle them (optional).
  88. * If your module does not handle them, PW will automatically install/uninstall them immediately after your module.
  89. * Like requires, this may be a string if there's only one and must be an array if multiple.
  90. * * singular: is only one instance of this module allowed? return boolean true or false.
  91. * If specified, this overrides the isSingular() method, if that method exists in your class.
  92. * See the information for the isSingular() method for more about the 'singular' property.
  93. * If you don't provide an isSingular() method, then you should provide this property here.
  94. * * autoload: should this module load automatically at application start? return boolean true or false.
  95. * If specified, this overrides the isAutoload() method, if that method exists in your class.
  96. * See the information for the isAutoload() method for more about the 'autoload' property.
  97. * If you don't provide an isAutoload() method, then you should provide this property here.
  98. * - permanent: boolean, for core only. True only if the module is permanent and uninstallable.
  99. * This is true only of PW core modules, so leave this out elsewhere.
  100. *
  101. * @return array
  102. *
  103. */
  104. public static function getModuleInfo();
  105. /**
  106. * Method to initialize the module.
  107. *
  108. * While the method is required, if you don't need it, then just leave the implementation blank.
  109. *
  110. * This is called after ProcessWire's API is fully ready for use and hooks. It is called at the end of the
  111. * bootstrap process. This is before PW has started retrieving or rendering a page. If you need to have the
  112. * API ready with the $page ready as well, then see the ready() method below this one.
  113. *
  114. */
  115. public function init();
  116. /**
  117. * Method called when API is fully ready and the $page is determined and set, but before a page is rendered.
  118. *
  119. * Optional and only called if it exists in the module.
  120. *
  121. public function ready();
  122. */
  123. /**
  124. * Returns the class name of this Module instance
  125. *
  126. * If your Module descends from Wire, or any of it's derivatives (as would usually be the case),
  127. * then you don't need to implement this method as it's already present.
  128. *
  129. * If you do want to implement this, then it can be just: "return get_class($this);"
  130. *
  131. * ProcessWire may sometimes substitute placeholders for a module that need to respond with the
  132. * non-placeholder classname. PHP's get_class won't work, which is why this method is used instead.
  133. * This method is only required for PW internal classes. As a result, implementation is optional for others.
  134. *
  135. public function className();
  136. */
  137. /**
  138. * Perform any installation procedures specific to this module, if needed.
  139. *
  140. * The Modules class calls this install method right after performing the install.
  141. *
  142. * If this method throws an exception, PW will catch it, remove it from the installed module list, and
  143. * report that the module installation failed. You may specify details about why with the exception, i.e.
  144. * throw new WireException("Can't install because of ...");
  145. *
  146. * This method is OPTIONAL, which is why it's commented out below.
  147. *
  148. public function ___install();
  149. */
  150. /**
  151. * Perform any uninstall procedures specific to this module, if needed.
  152. *
  153. * It calls this uninstall method right before completing the uninstall.
  154. *
  155. * This method is OPTIONAL, which is why it's commented out below.
  156. *
  157. public function ___uninstall();
  158. */
  159. /**
  160. * Is this module intended to be only a single instance?
  161. *
  162. * This method is OPTIONAL and therefore commented out in this interface. If ommitted, it is assumed that
  163. * you are providing this inforamtion in the getModuleInfo() method, documented above.
  164. *
  165. * A module that returns TRUE is referred to as a "singular" module, because there will never be any more
  166. * than a single instance of the module running.
  167. *
  168. * Why you'd use this method over the property in the getModuleInfo() method:
  169. * Using this method is useful if you have multiple descending classes that you want to provide a default
  170. * value for, rather than having each descending class specify it individually in the getModuleInfo().
  171. * For example, Fieldtype or Inputfield modules.
  172. *
  173. *
  174. * Return TRUE if this module is a single reusable instance, returning the same instance on every call from Modules.
  175. * Return FALSE if this module should return a new instance on every call from Modules.
  176. *
  177. * - Singular modules will have their instance active for the entire request after instantiated.
  178. * - Non-singular modules return a new instance on every $modules->get("YourModule") call.
  179. * - Modules that attach hooks are usually singular.
  180. * - Modules that may have multiple instances (like Inputfields) should NOT be singular.
  181. *
  182. * If you are having trouble deciding whether to make your module singular or not, be sure to read the documentation
  183. * below for the isAutoload() method, because if your module is 'autoload' then it's probably also 'singular'.
  184. *
  185. * @return bool
  186. *
  187. * This method may be optional, which is why it's commented out below.
  188. *
  189. public function isSingular();
  190. */
  191. /**
  192. * Is this module automatically loaded at runtime?
  193. *
  194. * This method is OPTIONAL and therefore commented out in this interface. If ommitted, it is assumed that
  195. * you are providing this information in the getModuleInfo() method.
  196. *
  197. * A module that returns TRUE is referred to as an "autoload" module, because it automatically loads as
  198. * part of ProcessWire's boot process. Autoload modules load before PW attempts to handle the web request.
  199. *
  200. * Return TRUE if this module is automatically loaded [and it's init() method called] at runtime.
  201. * Return FALSE if this module must be requested [via the $modules->get('ModuleName') method] before it is loaded.
  202. *
  203. * Modules that are intended to attach hooks in the application typically should be autoloaded because
  204. * they listen in to classes rather than have classes call upon them. If they weren't autoloaded, then
  205. * they might never get to attach their hooks.
  206. *
  207. * Modules that shouldn't be autoloaded are those that may or may not be needed at runtime, for example
  208. * Fieldtypes and Inputfields.
  209. *
  210. * As a side note, I can't think of any reason why a non-singular module would ever be autoload. The fact that
  211. * an autoload module is automatically loaded as part of PW's boot process implies it's probably going to be the
  212. * only instance running. So if you've decided to make your module 'autoload', then is safe to assume you've
  213. * also decided your module will also be singular (if that helps anyone reading this).
  214. *
  215. * Why you'd use this method over the property in the getModuleInfo() method:
  216. * Using this method is useful if you have multiple descending classes that you want to provide a default
  217. * value for, rather than having each descending class specify it individually in the getModuleInfo().
  218. * For example, Fieldtype or Inputfield modules.
  219. *
  220. *
  221. * @return bool
  222. *
  223. * This method may be optional, which is why it's commented out below.
  224. *
  225. public function isAutoload();
  226. */
  227. }