Config.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. <?php
  2. namespace Elgg\Amd;
  3. /**
  4. * Control configuration of RequireJS
  5. *
  6. * @package Elgg.Core
  7. * @subpackage JavaScript
  8. *
  9. * @access private
  10. */
  11. class Config {
  12. private $baseUrl = '';
  13. private $paths = array();
  14. private $shim = array();
  15. private $dependencies = array();
  16. /**
  17. * @var \Elgg\PluginHooksService
  18. */
  19. private $hooks;
  20. /**
  21. * Constructor
  22. *
  23. * @param \Elgg\PluginHooksService $hooks The hooks service
  24. */
  25. public function __construct(\Elgg\PluginHooksService $hooks) {
  26. $this->hooks = $hooks;
  27. }
  28. /**
  29. * Set the base URL for the site
  30. *
  31. * @param string $url URL
  32. * @return void
  33. */
  34. public function setBaseUrl($url) {
  35. $this->baseUrl = $url;
  36. }
  37. /**
  38. * Add a path mapping for a module. If a path is already defined, sets
  39. * current path as preferred.
  40. *
  41. * @param string $name Module name
  42. * @param string $path Full URL of the module
  43. * @return void
  44. */
  45. public function addPath($name, $path) {
  46. if (preg_match("/\.js$/", $path)) {
  47. $path = preg_replace("/\.js$/", '', $path);
  48. }
  49. if (!isset($this->paths[$name])) {
  50. $this->paths[$name] = array();
  51. }
  52. array_unshift($this->paths[$name], $path);
  53. }
  54. /**
  55. * Remove a path for a module
  56. *
  57. * @param string $name Module name
  58. * @param mixed $path The path to remove. If null, removes all paths (default).
  59. * @return void
  60. */
  61. public function removePath($name, $path = null) {
  62. if (!$path) {
  63. unset($this->paths[$name]);
  64. } else {
  65. if (preg_match("/\.js$/", $path)) {
  66. $path = preg_replace("/\.js$/", '', $path);
  67. }
  68. $key = array_search($path, $this->paths[$name]);
  69. unset($this->paths[$name][$key]);
  70. if (empty($this->paths[$name])) {
  71. unset($this->paths[$name]);
  72. }
  73. }
  74. }
  75. /**
  76. * Configures a shimmed module
  77. *
  78. * @param string $name Module name
  79. * @param array $config Configuration for the module
  80. * deps: array Dependencies
  81. * exports: string Name of the shimmed module to export
  82. * @return void
  83. */
  84. public function addShim($name, array $config) {
  85. $deps = elgg_extract('deps', $config, array());
  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] = array();
  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 = array()) {
  163. $url = elgg_extract('url', $config);
  164. $deps = elgg_extract('deps', $config, array());
  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 = array(
  214. 'baseUrl' => $this->baseUrl,
  215. 'paths' => $this->paths,
  216. 'shim' => $this->shim,
  217. 'deps' => $this->getDependencies(),
  218. 'waitSeconds' => 20,
  219. );
  220. $params = array(
  221. 'defaults' => $defaults
  222. );
  223. return $this->hooks->trigger('config', 'amd', $params, $defaults);
  224. }
  225. }