Elgg  Version 1.9
Config.php
Go to the documentation of this file.
1 <?php
2 
12  private $baseUrl = '';
13  private $paths = array();
14  private $shim = array();
15  private $dependencies = array();
16 
23  public function setBaseUrl($url) {
24  $this->baseUrl = $url;
25  }
26 
35  public function addPath($name, $path) {
36  if (preg_match("/\.js$/", $path)) {
37  $path = preg_replace("/\.js$/", '', $path);
38  }
39 
40  if (!isset($this->paths[$name])) {
41  $this->paths[$name] = array();
42  }
43 
44  array_unshift($this->paths[$name], $path);
45  }
46 
54  public function removePath($name, $path = null) {
55  if (!$path) {
56  unset($this->paths[$name]);
57  }
58 
59  $key = array_search($path, $this->paths[$name]);
60  unset($this->paths[$name][$key]);
61  }
62 
72  public function addShim($name, array $config) {
73  $deps = elgg_extract('deps', $config, array());
74  $exports = elgg_extract('exports', $config);
75 
76  if (empty($deps) && empty($exports)) {
77  throw new InvalidParameterException("Shimmed modules must have deps or exports");
78  }
79 
80  $this->shim[$name] = array();
81 
82  if (!empty($deps)) {
83  $this->shim[$name]['deps'] = $deps;
84  }
85 
86  if (!empty($exports)) {
87  $this->shim[$name]['exports'] = $exports;
88  }
89  }
90 
97  public function hasShim($name) {
98  return isset($this->shim[$name]);
99  }
100 
107  public function removeShim($name) {
108  unset($this->shim[$name]);
109  }
110 
117  public function addDependency($name) {
118  $this->dependencies[$name] = true;
119  }
120 
127  public function removeDependency($name) {
128  unset($this->dependencies[$name]);
129  }
130 
136  public function getDependencies() {
137  return array_keys($this->dependencies);
138  }
139 
146  public function hasDependency($name) {
147  return isset($this->dependencies[$name]);
148  }
149 
161  public function addModule($name, array $config = array()) {
162  $url = elgg_extract('url', $config);
163  $deps = elgg_extract('deps', $config, array());
164  $exports = elgg_extract('exports', $config);
165 
166  if (!empty($url)) {
167  $this->addPath($name, $url);
168  }
169 
170  // this is a shimmed module
171  // some jQuery modules don't need to export anything when shimmed,
172  // so check for deps too
173  if (!empty($deps) || !empty($exports)) {
174  $this->addShim($name, $config);
175  } else {
176  $this->addDependency($name);
177  }
178  }
179 
186  public function removeModule($name) {
187  _elgg_services()->amdConfig->removeDependency($name);
188  _elgg_services()->amdConfig->removeShim($name);
189  _elgg_services()->amdConfig->removePath($name);
190  }
191 
198  public function hasModule($name) {
199  if (in_array($name, $this->getDependencies())) {
200  return true;
201  }
202 
203  if (isset($this->shims[$name])) {
204  return true;
205  }
206 
207  if (isset($this->paths[$name])) {
208  return true;
209  }
210 
211  return false;
212  }
213 
219  public function getConfig() {
220  return array(
221  'baseUrl' => $this->baseUrl,
222  'paths' => $this->paths,
223  'shim' => $this->shim,
224  'deps' => $this->getDependencies(),
225  );
226  }
227 }
hasDependency($name)
Is this dependency registered.
Definition: Config.php:146
addDependency($name)
Add a dependency.
Definition: Config.php:117
hasShim($name)
Is this shim defined.
Definition: Config.php:97
if($guid==elgg_get_logged_in_user_guid()) $name
Definition: delete.php:21
addPath($name, $path)
Add a path mapping for a module.
Definition: Config.php:35
removeShim($name)
Unregister the shim config for a module.
Definition: Config.php:107
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:1464
$url
Definition: exceptions.php:24
removeDependency($name)
Removes a dependency.
Definition: Config.php:127
$key
Definition: summary.php:34
_elgg_services()
Definition: autoloader.php:14
hasModule($name)
Is module configured?
Definition: Config.php:198
$deps
addShim($name, array $config)
Configures a shimmed module.
Definition: Config.php:72
getConfig()
Get the configuration of AMD.
Definition: Config.php:219
addModule($name, array $config=array())
Adds a standard AMD or shimmed module to the config.
Definition: Config.php:161
removePath($name, $path=null)
Remove a path for a module.
Definition: Config.php:54
getDependencies()
Get registered dependencies.
Definition: Config.php:136
removeModule($name)
Removes all config for a module.
Definition: Config.php:186
setBaseUrl($url)
Set the base URL for the site.
Definition: Config.php:23
$path
Definition: invalid.php:17