PageRenderTime 41ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/engine/classes/Elgg/Amd/Config.php

https://github.com/mrclay/Elgg-leaf
PHP | 259 lines | 114 code | 34 blank | 111 comment | 17 complexity | f2aeb8e020f8f400502c97a2c2a81369 MD5 | raw file
  1. <?php
  2. namespace Elgg\Amd;
  3. use Elgg\Exceptions\InvalidParameterException;
  4. /**
  5. * Control configuration of RequireJS
  6. *
  7. * @internal
  8. */
  9. class Config {
  10. private $baseUrl = '';
  11. private $paths = [];
  12. private $shim = [];
  13. private $dependencies = [];
  14. /**
  15. * @var \Elgg\PluginHooksService
  16. */
  17. private $hooks;
  18. /**
  19. * Constructor
  20. *
  21. * @param \Elgg\PluginHooksService $hooks The hooks service
  22. */
  23. public function __construct(\Elgg\PluginHooksService $hooks) {
  24. $this->hooks = $hooks;
  25. }
  26. /**
  27. * Set the base URL for the site
  28. *
  29. * @param string $url URL
  30. * @return void
  31. */
  32. public function setBaseUrl($url) {
  33. $this->baseUrl = $url;
  34. }
  35. /**
  36. * Add a path mapping for a module. If a path is already defined, sets
  37. * current path as preferred.
  38. *
  39. * @param string $name Module name
  40. * @param string $path Full URL of the module
  41. * @return void
  42. */
  43. public function addPath($name, $path) {
  44. if (preg_match("/\.js$/", $path)) {
  45. $path = preg_replace("/\.js$/", '', $path);
  46. }
  47. if (!isset($this->paths[$name])) {
  48. $this->paths[$name] = [];
  49. }
  50. array_unshift($this->paths[$name], $path);
  51. }
  52. /**
  53. * Remove a path for a module
  54. *
  55. * @param string $name Module name
  56. * @param mixed $path The path to remove. If null, removes all paths (default).
  57. * @return void
  58. */
  59. public function removePath($name, $path = null) {
  60. if (!$path) {
  61. unset($this->paths[$name]);
  62. } else {
  63. if (preg_match("/\.js$/", $path)) {
  64. $path = preg_replace("/\.js$/", '', $path);
  65. }
  66. $key = array_search($path, $this->paths[$name]);
  67. unset($this->paths[$name][$key]);
  68. if (empty($this->paths[$name])) {
  69. unset($this->paths[$name]);
  70. }
  71. }
  72. }
  73. /**
  74. * Configures a shimmed module
  75. *
  76. * @param string $name Module name
  77. * @param array $config Configuration for the module
  78. * - deps: array Dependencies
  79. * - exports: string Name of the shimmed module to export
  80. *
  81. * @return void
  82. * @throws InvalidParameterException
  83. */
  84. public function addShim($name, array $config) {
  85. $deps = elgg_extract('deps', $config, []);
  86. $exports = elgg_extract('exports', $config);
  87. if (empty($deps) && empty($exports)) {
  88. throw new InvalidParameterException("Shimmed modules must have deps or exports");
  89. }
  90. $this->shim[$name] = [];
  91. if (!empty($deps)) {
  92. $this->shim[$name]['deps'] = $deps;
  93. }
  94. if (!empty($exports)) {
  95. $this->shim[$name]['exports'] = $exports;
  96. }
  97. }
  98. /**
  99. * Is this shim defined
  100. *
  101. * @param string $name The name of the shim
  102. * @return bool
  103. */
  104. public function hasShim($name) {
  105. return isset($this->shim[$name]);
  106. }
  107. /**
  108. * Unregister the shim config for a module
  109. *
  110. * @param string $name Module name
  111. * @return void
  112. */
  113. public function removeShim($name) {
  114. unset($this->shim[$name]);
  115. }
  116. /**
  117. * Add a dependency
  118. *
  119. * @param string $name Name of the dependency
  120. * @return void
  121. */
  122. public function addDependency($name) {
  123. $this->dependencies[$name] = true;
  124. }
  125. /**
  126. * Removes a dependency
  127. *
  128. * @param string $name Name of the dependency
  129. * @return void
  130. */
  131. public function removeDependency($name) {
  132. unset($this->dependencies[$name]);
  133. }
  134. /**
  135. * Get registered dependencies
  136. *
  137. * @return array
  138. */
  139. public function getDependencies() {
  140. return array_keys($this->dependencies);
  141. }
  142. /**
  143. * Is this dependency registered
  144. *
  145. * @param string $name Module name
  146. * @return bool
  147. */
  148. public function hasDependency($name) {
  149. return isset($this->dependencies[$name]);
  150. }
  151. /**
  152. * Adds a standard AMD or shimmed module to the config.
  153. *
  154. * @param string $name The name of the module
  155. * @param array $config Configuration for the module
  156. * - url: string The full URL for the module if not resolvable from baseUrl
  157. * - deps: array Shimmed module's dependencies
  158. * - exports: string Name of the shimmed module to export
  159. *
  160. * @return void
  161. */
  162. public function addModule($name, array $config = []) {
  163. $url = elgg_extract('url', $config);
  164. $deps = elgg_extract('deps', $config, []);
  165. $exports = elgg_extract('exports', $config);
  166. if (!empty($url)) {
  167. $this->addPath($name, $url);
  168. }
  169. // this is a shimmed module
  170. // some jQuery modules don't need to export anything when shimmed,
  171. // so check for deps too
  172. if (!empty($deps) || !empty($exports)) {
  173. $this->addShim($name, $config);
  174. } else {
  175. $this->addDependency($name);
  176. }
  177. }
  178. /**
  179. * Removes all config for a module
  180. *
  181. * @param string $name The module name
  182. * @return bool
  183. */
  184. public function removeModule($name) {
  185. $this->removeDependency($name);
  186. $this->removeShim($name);
  187. $this->removePath($name);
  188. }
  189. /**
  190. * Is module configured?
  191. *
  192. * @param string $name Module name
  193. * @return boolean
  194. */
  195. public function hasModule($name) {
  196. if (in_array($name, $this->getDependencies())) {
  197. return true;
  198. }
  199. if (isset($this->shim[$name])) {
  200. return true;
  201. }
  202. if (isset($this->paths[$name])) {
  203. return true;
  204. }
  205. return false;
  206. }
  207. /**
  208. * Get the configuration of AMD
  209. *
  210. * @return array
  211. */
  212. public function getConfig() {
  213. $defaults = [
  214. 'baseUrl' => $this->baseUrl,
  215. 'paths' => $this->paths,
  216. 'shim' => $this->shim,
  217. 'deps' => $this->getDependencies(),
  218. 'waitSeconds' => 20,
  219. ];
  220. $params = [
  221. 'defaults' => $defaults
  222. ];
  223. return $this->hooks->trigger('config', 'amd', $params, $defaults);
  224. }
  225. }