Elgg  Version 3.0
Config.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Amd;
4 
13 class Config {
14  private $baseUrl = '';
15  private $paths = [];
16  private $shim = [];
17  private $dependencies = [];
18 
22  private $hooks;
23 
29  public function __construct(\Elgg\PluginHooksService $hooks) {
30  $this->hooks = $hooks;
31  }
32 
39  public function setBaseUrl($url) {
40  $this->baseUrl = $url;
41  }
42 
51  public function addPath($name, $path) {
52  if (preg_match("/\.js$/", $path)) {
53  $path = preg_replace("/\.js$/", '', $path);
54  }
55 
56  if (!isset($this->paths[$name])) {
57  $this->paths[$name] = [];
58  }
59 
60  array_unshift($this->paths[$name], $path);
61  }
62 
70  public function removePath($name, $path = null) {
71  if (!$path) {
72  unset($this->paths[$name]);
73  } else {
74  if (preg_match("/\.js$/", $path)) {
75  $path = preg_replace("/\.js$/", '', $path);
76  }
77 
78  $key = array_search($path, $this->paths[$name]);
79  unset($this->paths[$name][$key]);
80 
81  if (empty($this->paths[$name])) {
82  unset($this->paths[$name]);
83  }
84  }
85  }
86 
97  public function addShim($name, array $config) {
98  $deps = elgg_extract('deps', $config, []);
99  $exports = elgg_extract('exports', $config);
100 
101  if (empty($deps) && empty($exports)) {
102  throw new \InvalidParameterException("Shimmed modules must have deps or exports");
103  }
104 
105  $this->shim[$name] = [];
106 
107  if (!empty($deps)) {
108  $this->shim[$name]['deps'] = $deps;
109  }
110 
111  if (!empty($exports)) {
112  $this->shim[$name]['exports'] = $exports;
113  }
114  }
115 
122  public function hasShim($name) {
123  return isset($this->shim[$name]);
124  }
125 
132  public function removeShim($name) {
133  unset($this->shim[$name]);
134  }
135 
142  public function addDependency($name) {
143  $this->dependencies[$name] = true;
144  }
145 
152  public function removeDependency($name) {
153  unset($this->dependencies[$name]);
154  }
155 
161  public function getDependencies() {
162  return array_keys($this->dependencies);
163  }
164 
171  public function hasDependency($name) {
172  return isset($this->dependencies[$name]);
173  }
174 
186  public function addModule($name, array $config = []) {
187  $url = elgg_extract('url', $config);
188  $deps = elgg_extract('deps', $config, []);
189  $exports = elgg_extract('exports', $config);
190 
191  if (!empty($url)) {
192  $this->addPath($name, $url);
193  }
194 
195  // this is a shimmed module
196  // some jQuery modules don't need to export anything when shimmed,
197  // so check for deps too
198  if (!empty($deps) || !empty($exports)) {
199  $this->addShim($name, $config);
200  } else {
201  $this->addDependency($name);
202  }
203  }
204 
211  public function removeModule($name) {
212  $this->removeDependency($name);
213  $this->removeShim($name);
214  $this->removePath($name);
215  }
216 
223  public function hasModule($name) {
224  if (in_array($name, $this->getDependencies())) {
225  return true;
226  }
227 
228  if (isset($this->shim[$name])) {
229  return true;
230  }
231 
232  if (isset($this->paths[$name])) {
233  return true;
234  }
235 
236  return false;
237  }
238 
244  public function getConfig() {
245  $defaults = [
246  'baseUrl' => $this->baseUrl,
247  'paths' => $this->paths,
248  'shim' => $this->shim,
249  'deps' => $this->getDependencies(),
250  'waitSeconds' => 20,
251  ];
252 
253  $params = [
254  'defaults' => $defaults
255  ];
256 
257  return $this->hooks->trigger('config', 'amd', $params, $defaults);
258  }
259 }
removeShim($name)
Unregister the shim config for a module.
Definition: Config.php:132
if(!$user||!$user->canDelete()) $name
Definition: delete.php:22
addDependency($name)
Add a dependency.
Definition: Config.php:142
$params
Saves global plugin settings.
Definition: save.php:13
__construct(\Elgg\PluginHooksService $hooks)
Constructor.
Definition: Config.php:29
$defaults
addPath($name, $path)
Add a path mapping for a module.
Definition: Config.php:51
setBaseUrl($url)
Set the base URL for the site.
Definition: Config.php:39
$path
Definition: details.php:89
removePath($name, $path=null)
Remove a path for a module.
Definition: Config.php:70
getDependencies()
Get registered dependencies.
Definition: Config.php:161
addShim($name, array $config)
Configures a shimmed module.
Definition: Config.php:97
$config
Advanced site settings, debugging section.
Definition: debugging.php:6
Configuration exception.
addModule($name, array $config=[])
Adds a standard AMD or shimmed module to the config.
Definition: Config.php:186
getConfig()
Get the configuration of AMD.
Definition: Config.php:244
$url
Definition: default.php:33
hasShim($name)
Is this shim defined.
Definition: Config.php:122
elgg_extract($key, $array, $default=null, $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
Definition: elgglib.php:1131
if(!$item instanceof ElggMenuItem) $deps
Definition: item_deps.php:17
if($container instanceof ElggGroup &&$container->guid!=elgg_get_page_owner_guid()) $key
Definition: summary.php:55
hasModule($name)
Is module configured?
Definition: Config.php:223
hasDependency($name)
Is this dependency registered.
Definition: Config.php:171
removeModule($name)
Removes all config for a module.
Definition: Config.php:211
removeDependency($name)
Removes a dependency.
Definition: Config.php:152