Elgg  Version 1.11
Config.php
Go to the documentation of this file.
1 <?php
2 namespace Elgg\Amd;
3 
12 class Config {
13  private $baseUrl = '';
14  private $paths = array();
15  private $shim = array();
16  private $dependencies = array();
17 
21  private $hooks;
22 
28  public function __construct(\Elgg\PluginHooksService $hooks) {
29  $this->hooks = $hooks;
30  }
31 
38  public function setBaseUrl($url) {
39  $this->baseUrl = $url;
40  }
41 
50  public function addPath($name, $path) {
51  if (preg_match("/\.js$/", $path)) {
52  $path = preg_replace("/\.js$/", '', $path);
53  }
54 
55  if (!isset($this->paths[$name])) {
56  $this->paths[$name] = array();
57  }
58 
59  array_unshift($this->paths[$name], $path);
60  }
61 
69  public function removePath($name, $path = null) {
70  if (!$path) {
71  unset($this->paths[$name]);
72  } else {
73  if (preg_match("/\.js$/", $path)) {
74  $path = preg_replace("/\.js$/", '', $path);
75  }
76 
77  $key = array_search($path, $this->paths[$name]);
78  unset($this->paths[$name][$key]);
79 
80  if (empty($this->paths[$name])) {
81  unset($this->paths[$name]);
82  }
83  }
84  }
85 
95  public function addShim($name, array $config) {
96  $deps = elgg_extract('deps', $config, array());
97  $exports = elgg_extract('exports', $config);
98 
99  if (empty($deps) && empty($exports)) {
100  throw new \InvalidParameterException("Shimmed modules must have deps or exports");
101  }
102 
103  $this->shim[$name] = array();
104 
105  if (!empty($deps)) {
106  $this->shim[$name]['deps'] = $deps;
107  }
108 
109  if (!empty($exports)) {
110  $this->shim[$name]['exports'] = $exports;
111  }
112  }
113 
120  public function hasShim($name) {
121  return isset($this->shim[$name]);
122  }
123 
130  public function removeShim($name) {
131  unset($this->shim[$name]);
132  }
133 
140  public function addDependency($name) {
141  $this->dependencies[$name] = true;
142  }
143 
150  public function removeDependency($name) {
151  unset($this->dependencies[$name]);
152  }
153 
159  public function getDependencies() {
160  return array_keys($this->dependencies);
161  }
162 
169  public function hasDependency($name) {
170  return isset($this->dependencies[$name]);
171  }
172 
184  public function addModule($name, array $config = array()) {
185  $url = elgg_extract('url', $config);
186  $deps = elgg_extract('deps', $config, array());
187  $exports = elgg_extract('exports', $config);
188 
189  if (!empty($url)) {
190  $this->addPath($name, $url);
191  }
192 
193  // this is a shimmed module
194  // some jQuery modules don't need to export anything when shimmed,
195  // so check for deps too
196  if (!empty($deps) || !empty($exports)) {
197  $this->addShim($name, $config);
198  } else {
199  $this->addDependency($name);
200  }
201  }
202 
209  public function removeModule($name) {
210  $this->removeDependency($name);
211  $this->removeShim($name);
212  $this->removePath($name);
213  }
214 
221  public function hasModule($name) {
222  if (in_array($name, $this->getDependencies())) {
223  return true;
224  }
225 
226  if (isset($this->shim[$name])) {
227  return true;
228  }
229 
230  if (isset($this->paths[$name])) {
231  return true;
232  }
233 
234  return false;
235  }
236 
242  public function getConfig() {
243  $defaults = array(
244  'baseUrl' => $this->baseUrl,
245  'paths' => $this->paths,
246  'shim' => $this->shim,
247  'deps' => $this->getDependencies(),
248  'waitSeconds' => 20,
249  );
250 
251  $params = array(
252  'defaults' => $defaults
253  );
254 
255  return $this->hooks->trigger('config', 'amd', $params, $defaults);
256  }
257 }
removeShim($name)
Unregister the shim config for a module.
Definition: Config.php:130
addDependency($name)
Add a dependency.
Definition: Config.php:140
if($guid==elgg_get_logged_in_user_guid()) $name
Definition: delete.php:21
addModule($name, array $config=array())
Adds a standard AMD or shimmed module to the config.
Definition: Config.php:184
__construct(\Elgg\PluginHooksService $hooks)
Constructor.
Definition: Config.php:28
$defaults
addPath($name, $path)
Add a path mapping for a module.
Definition: Config.php:50
setBaseUrl($url)
Set the base URL for the site.
Definition: Config.php:38
removePath($name, $path=null)
Remove a path for a module.
Definition: Config.php:69
elgg_extract($key, array $array, $default=null, $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
Definition: elgglib.php:1246
getDependencies()
Get registered dependencies.
Definition: Config.php:159
$url
Definition: exceptions.php:24
addShim($name, array $config)
Configures a shimmed module.
Definition: Config.php:95
$params
Definition: login.php:72
Save menu items.
$key
Definition: summary.php:34
getConfig()
Get the configuration of AMD.
Definition: Config.php:242
hasShim($name)
Is this shim defined.
Definition: Config.php:120
$deps
hasModule($name)
Is module configured?
Definition: Config.php:221
hasDependency($name)
Is this dependency registered.
Definition: Config.php:169
removeModule($name)
Removes all config for a module.
Definition: Config.php:209
$path
Definition: invalid.php:17
removeDependency($name)
Removes a dependency.
Definition: Config.php:150