Elgg  Version 1.10
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 
24  public function setBaseUrl($url) {
25  $this->baseUrl = $url;
26  }
27 
36  public function addPath($name, $path) {
37  if (preg_match("/\.js$/", $path)) {
38  $path = preg_replace("/\.js$/", '', $path);
39  }
40 
41  if (!isset($this->paths[$name])) {
42  $this->paths[$name] = array();
43  }
44 
45  array_unshift($this->paths[$name], $path);
46  }
47 
55  public function removePath($name, $path = null) {
56  if (!$path) {
57  unset($this->paths[$name]);
58  } else {
59  if (preg_match("/\.js$/", $path)) {
60  $path = preg_replace("/\.js$/", '', $path);
61  }
62 
63  $key = array_search($path, $this->paths[$name]);
64  unset($this->paths[$name][$key]);
65 
66  if (empty($this->paths[$name])) {
67  unset($this->paths[$name]);
68  }
69  }
70  }
71 
81  public function addShim($name, array $config) {
82  $deps = elgg_extract('deps', $config, array());
83  $exports = elgg_extract('exports', $config);
84 
85  if (empty($deps) && empty($exports)) {
86  throw new \InvalidParameterException("Shimmed modules must have deps or exports");
87  }
88 
89  $this->shim[$name] = array();
90 
91  if (!empty($deps)) {
92  $this->shim[$name]['deps'] = $deps;
93  }
94 
95  if (!empty($exports)) {
96  $this->shim[$name]['exports'] = $exports;
97  }
98  }
99 
106  public function hasShim($name) {
107  return isset($this->shim[$name]);
108  }
109 
116  public function removeShim($name) {
117  unset($this->shim[$name]);
118  }
119 
126  public function addDependency($name) {
127  $this->dependencies[$name] = true;
128  }
129 
136  public function removeDependency($name) {
137  unset($this->dependencies[$name]);
138  }
139 
145  public function getDependencies() {
146  return array_keys($this->dependencies);
147  }
148 
155  public function hasDependency($name) {
156  return isset($this->dependencies[$name]);
157  }
158 
170  public function addModule($name, array $config = array()) {
171  $url = elgg_extract('url', $config);
172  $deps = elgg_extract('deps', $config, array());
173  $exports = elgg_extract('exports', $config);
174 
175  if (!empty($url)) {
176  $this->addPath($name, $url);
177  }
178 
179  // this is a shimmed module
180  // some jQuery modules don't need to export anything when shimmed,
181  // so check for deps too
182  if (!empty($deps) || !empty($exports)) {
183  $this->addShim($name, $config);
184  } else {
185  $this->addDependency($name);
186  }
187  }
188 
195  public function removeModule($name) {
196  $this->removeDependency($name);
197  $this->removeShim($name);
198  $this->removePath($name);
199  }
200 
207  public function hasModule($name) {
208  if (in_array($name, $this->getDependencies())) {
209  return true;
210  }
211 
212  if (isset($this->shim[$name])) {
213  return true;
214  }
215 
216  if (isset($this->paths[$name])) {
217  return true;
218  }
219 
220  return false;
221  }
222 
228  public function getConfig() {
229  return array(
230  'baseUrl' => $this->baseUrl,
231  'paths' => $this->paths,
232  'shim' => $this->shim,
233  'deps' => $this->getDependencies(),
234  );
235  }
236 }
removeShim($name)
Unregister the shim config for a module.
Definition: Config.php:116
addDependency($name)
Add a dependency.
Definition: Config.php:126
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:170
addPath($name, $path)
Add a path mapping for a module.
Definition: Config.php:36
setBaseUrl($url)
Set the base URL for the site.
Definition: Config.php:24
removePath($name, $path=null)
Remove a path for a module.
Definition: Config.php:55
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:1349
getDependencies()
Get registered dependencies.
Definition: Config.php:145
$url
Definition: exceptions.php:24
addShim($name, array $config)
Configures a shimmed module.
Definition: Config.php:81
$key
Definition: summary.php:34
getConfig()
Get the configuration of AMD.
Definition: Config.php:228
hasShim($name)
Is this shim defined.
Definition: Config.php:106
$deps
hasModule($name)
Is module configured?
Definition: Config.php:207
hasDependency($name)
Is this dependency registered.
Definition: Config.php:155
removeModule($name)
Removes all config for a module.
Definition: Config.php:195
$path
Definition: invalid.php:17
removeDependency($name)
Removes a dependency.
Definition: Config.php:136