PageRenderTime 49ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/authcache_p13n/authcache_p13n.api.php

https://gitlab.com/Drulenium-bot/authcache
PHP | 323 lines | 106 code | 18 blank | 199 comment | 4 complexity | f45696c4ab3dda3bc224391c1417a415 MD5 | raw file
  1. <?php
  2. /**
  3. * @file
  4. * Documentation for hooks provided by the authcache personalization module.
  5. */
  6. /**
  7. * Declare markup fragments which contain personalized information.
  8. *
  9. * Return an associative array where the key represents the fragment id and the
  10. * value an array with the following keys:
  11. * - fragment': The name of the class used to build the markup. The class must
  12. * implement the AuthcacheP13nFragmentInterface.
  13. * - fragment validator: (Optional) The name of the class used to validate the
  14. * parameters for the fragment renderer. The class must implement the
  15. * AuthcacheP13nFragmentValidator interface. If not given, the 'fragment'
  16. * instance is used if it implements the interface mentioned before.
  17. * - fragment loader: (Optional) The name of the class used to load the
  18. * necessary data. The class must implement the AuthcacheP13nFragmentLoader
  19. * interface. If not given, the 'fragment' instance is used if it implements
  20. * the interface mentioned before.
  21. * - fragment access: (Optional) The name of a class used to check access to
  22. * the data. The class must implement the AuthcacheP13nFragmentAccess
  23. * interface. If not given, the 'fragment' instance is used if it implements
  24. * the interface mentioned before.
  25. * - cache maxage: (Optional) The number of seconds a rendered fragment should
  26. * be cacheable in the users browser or in intermediate cache servers.
  27. * - cache granularity (Optional) A bitmask describing the criteria used to
  28. * distinguish between multiple variants of a fragment. A combination of the
  29. * following contstants can be used:
  30. * - AuthcacheP13nCacheGranularity::PER_USER: Content is different for
  31. * each session.
  32. * - AuthcacheP13nCacheGranularity::PER_PAGE: Content changes when
  33. * fragment is rendered on different pages.
  34. * - bootstrap phase: (Optional) The minimal bootstrap phase necessary to
  35. * render the fragment. One of the DRUPAL_BOOTSTRAP_X constants.
  36. */
  37. function hook_authcache_p13n_fragment() {
  38. return array(
  39. 'form-token' => array(
  40. 'fragment' => array(
  41. '#class' => 'AuthcacheFormTokenFragment',
  42. ),
  43. ),
  44. );
  45. }
  46. /**
  47. * Alter markup fragment definitions.
  48. *
  49. * @see hook_authcache_p13n_fragment()
  50. */
  51. function hook_authcache_p13n_fragment_alter(&$info) {
  52. // Extend the maximal age for the form-token fragment to one week.
  53. $info['form-token']['cache maxage'] = 604800;
  54. }
  55. /**
  56. * Declare fragment assemblies containing personalized information.
  57. *
  58. * Return an associative array where the key represents the fragment assembly
  59. * id and the value an array with the following keys:
  60. *
  61. * - partial: An associative array with the following key-value pairs:
  62. * - renderer: The name of the class used to build the markup. The class must
  63. * implement the AuthcacheP13nFragmentInterface.
  64. * - validator: (Optional) The name of the class used to validate the
  65. * parameters for the fragment renderer. The class must implement the
  66. * AuthcacheP13nFragmentValidator interface. If not given, the 'renderer'
  67. * instance is used if it implements the interface mentioned before.
  68. * - loader: (Optional) The name of the class used to load the
  69. * necessary data. The class must implement the AuthcacheP13nFragmentLoader
  70. * interface. If not given, the 'renderer' instance is used if it implements
  71. * the interface mentioned before.
  72. * - access: (Optional) The name of a class used to check access to
  73. * the data. The class must implement the AuthcacheP13nFragmentAccess
  74. * interface. If not given, the 'renderer' instance is used if it implements
  75. * the interface mentioned before.
  76. * - cache maxage: (Optional) The number of seconds a rendered fragment should
  77. * be cacheable in the users browser or in intermediate cache servers.
  78. * - cache granularity (Optional) A bitmask describing the criteria used to
  79. * distinguish between multiple variants of a fragment. A combination of the
  80. * following constants can be used:
  81. * - AuthcacheP13nCacheGranularity::PER_USER: Content is different for
  82. * each session.
  83. * - AuthcacheP13nCacheGranularity::PER_PAGE: Content changes when
  84. * fragment is rendered on different pages.
  85. * - bootstrap phase: (Optional) The minimal bootstrap phase necessary to
  86. * render the fragment. One of the DRUPAL_BOOTSTRAP_X constants.
  87. */
  88. function hook_authcache_p13n_assembly() {
  89. return array(
  90. 'flags' => array(
  91. 'admin name' => 'All flags',
  92. 'admin group' => 'Flag',
  93. 'admin description' => 'All flags on a page',
  94. 'cache maxage' => 600,
  95. 'bootstrap phase' => DRUPAL_BOOTSTRAP_FULL,
  96. 'fragment f1' => array(
  97. '#class' => 'AuthcacheFlagFlagFragment',
  98. '#arguments' => array(
  99. 'bookmarks',
  100. ),
  101. '#member_of' => 'partial f1',
  102. '#key' => 'renderer',
  103. ),
  104. 'partial f1' => array(
  105. '#collection' => 'partial f1',
  106. '#member_of' => 'partials',
  107. '#key' => 'f1',
  108. ),
  109. 'fragment f2' => array(
  110. '#class' => 'AuthcacheFlagFlagFragment',
  111. '#arguments' => array(
  112. 'friends',
  113. ),
  114. '#member_of' => 'partial f2',
  115. '#key' => 'renderer',
  116. ),
  117. 'partial f2' => array(
  118. '#collection' => 'partial f2',
  119. '#member_of' => 'partials',
  120. '#key' => 'f2',
  121. ),
  122. ),
  123. );
  124. }
  125. /**
  126. * Modify fragment assemblies declared by other modules.
  127. *
  128. * @see hook_authcache_p13n_assembly()
  129. */
  130. function hook_authcache_p13n_assembly_alter(&$info) {
  131. }
  132. /**
  133. * Declare settings containing personalized information.
  134. *
  135. * Return an associative array where the key represents the setting id and the
  136. * value an array with the following keys:
  137. * - setting': The name of the class used to build the markup. The class must
  138. * implement the AuthcacheP13nSetting interface.
  139. * - setting validator: (Optional) The name of the class used to validate the
  140. * parameters for the setting renderer. The class must implement the
  141. * AuthcacheP13nSettingValidator interface. If not given, the 'setting'
  142. * instance is used if it implements the interface mentioned before.
  143. * - setting loader: (Optional) The name of the class used to load the
  144. * necessary data. The class must implement the AuthcacheP13nSettingLoader
  145. * interface. If not given, the 'setting' instance is used if it implements
  146. * the interface mentioned before.
  147. * - setting access: (Optional) The name of a class used to check access to
  148. * the data. The class must implement the AuthcacheP13nSettingAccess
  149. * interface. If not given, the 'setting' instance is used if it implements
  150. * the interface mentioned before.
  151. * - cache maxage: (Optional) The number of seconds a rendered setting should
  152. * be cacheable in the users browser or in intermediate cache servers.
  153. * - cache granularity (Optional) A bitmask describing the criteria used to
  154. * distinguish between multiple variants of a setting. A combination of the
  155. * following contstants can be used:
  156. * - AuthcacheP13nCacheGranularity::PER_USER: Content is different for
  157. * each session.
  158. * - AuthcacheP13nCacheGranularity::PER_PAGE: Content changes when
  159. * setting is rendered on different pages.
  160. * - bootstrap phase: (Optional) The minimal bootstrap phase necessary to
  161. * render the setting. One of the DRUPAL_BOOTSTRAP_X constants.
  162. */
  163. function hook_authcache_p13n_setting() {
  164. return array(
  165. 'authcache-contact' => array(
  166. 'setting' => array(
  167. '#class' => 'AuthcacheContactSetting',
  168. ),
  169. 'setting target' => 'authcacheContact',
  170. 'cache maxage' => 86400,
  171. ),
  172. );
  173. }
  174. /**
  175. * Modify setting declared by other modules.
  176. *
  177. * @see hook_authcache_p13n_setting()
  178. */
  179. function hook_authcache_p13n_setting_alter(&$info) {
  180. }
  181. /**
  182. * Called when the external cache for personalized fragments should be cleared.
  183. */
  184. function hook_authcache_p13n_session_invalidate() {
  185. }
  186. /**
  187. * Declare a client method used to defer retrieval of personalized content.
  188. *
  189. * Return an associative array where the key represents the client method
  190. * (normally equal to the module name) and the value is an associative array
  191. * with the following key-value pairs:
  192. * - title: The human readable description of the client method.
  193. * - enabled: Whether or not this client can be used for content delivered
  194. * during the current request.
  195. */
  196. function hook_authcache_p13n_client() {
  197. return array(
  198. 'authcache_ajax' => array(
  199. 'title' => t('Ajax'),
  200. 'enabled' => !empty($_COOKIE['has_js']),
  201. ),
  202. );
  203. }
  204. /**
  205. * Modify client methods declared by other modules.
  206. *
  207. * @see hook_authcache_p13n_client()
  208. */
  209. function hook_authcache_p13n_client_alter(&$info) {
  210. global $base_root;
  211. // Never use ESI on localhost.
  212. if (stristr($base_root, 'localhost')) {
  213. $info['authcache_esi']['enabled'] = FALSE;
  214. }
  215. }
  216. /**
  217. * Change the order of client methods for a given operation.
  218. *
  219. * @param array &$clients
  220. * Associative array of client records indexed by client-id. Each record is
  221. * an array with key-value pairs. Assign an integer value to the weight key
  222. * to influence the client order.
  223. *
  224. * @param string $type
  225. * One of "fragment", "setting" or "assembly".
  226. *
  227. * @param string $id
  228. * The fragment id (e.g., "form").
  229. *
  230. * @param string $param
  231. * The parameter for the personalization operation.
  232. *
  233. * @see authcache_p13n_client_get_preferred()
  234. */
  235. function hook_authcache_p13n_client_order_alter(&$clients, $type, $id, $param) {
  236. if ($type === 'fragment' && $id === 'form') {
  237. // Prefer esi over ajax for form-token retrieval.
  238. $clients['authcache_esi']['weight'] = -99;
  239. $clients['authcache_ajax']['weight'] = 0;
  240. }
  241. }
  242. /**
  243. * Change the fallback markup inserted when no client is available.
  244. *
  245. * @param string &$markup
  246. * The markup replacing the rendered fragment.
  247. * @param string $method
  248. * The fallback method, e.g., 'hide', 'cancel'
  249. * @param array $context
  250. * An associative array containing the following key-value pairs:
  251. * - type: One of 'fragment', 'setting', 'assembly' or 'partial'.
  252. * - id: The fragment-, setting-, assembly-, partial-identifier.
  253. * - param: The parameter for this instance.
  254. * - clients: An array of weighted client definitions supplied to the
  255. * theming-function.
  256. */
  257. function hook_authcache_p13n_client_fallback_alter(&$markup, $method, $context) {
  258. switch ($method) {
  259. case 'cancel':
  260. authcache_cancel(t('No client for %type %id', array('%type' => $context['type'], '%id' => $context['id'])));
  261. break;
  262. }
  263. }
  264. /**
  265. * Return resource record with default request blueprint.
  266. *
  267. * @see authcache_p13n_authcache_p13n_base_request()
  268. */
  269. function hook_authcache_p13n_base_request() {
  270. }
  271. /**
  272. * Modify resource record for default request.
  273. *
  274. * @see hook_authcache_p13n_base_request()
  275. */
  276. function hook_authcache_p13n_base_request_alter(&$resource) {
  277. }
  278. /**
  279. * Return AuthcacheP13nRequestHandler instances.
  280. *
  281. * @see authcache_p13n_authcache_p13n_request()
  282. */
  283. function hook_authcache_p13n_request() {
  284. }
  285. /**
  286. * Modify request-array.
  287. *
  288. * @see hook_authcache_p13n_request()
  289. */
  290. function hook_authcache_p13n_request_alter(&$resources) {
  291. }
  292. /**
  293. * Define additional object factory resource processors.
  294. *
  295. * @see AuthcacheP13nObjectFactory
  296. */
  297. function hook_authcache_p13n_resource_processors() {
  298. }
  299. /**
  300. * Alter resource processor definitions.
  301. *
  302. * @see hook_authcache_p13n_resource_processors()
  303. */
  304. function hook_authcache_p13n_resource_processors_alter(&$processors) {
  305. }