Elgg  Version 2.3
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 
96  public function addShim($name, array $config) {
97  $deps = elgg_extract('deps', $config, array());
98  $exports = elgg_extract('exports', $config);
99 
100  if (empty($deps) && empty($exports)) {
101  throw new \InvalidParameterException("Shimmed modules must have deps or exports");
102  }
103 
104  $this->shim[$name] = array();
105 
106  if (!empty($deps)) {
107  $this->shim[$name]['deps'] = $deps;
108  }
109 
110  if (!empty($exports)) {
111  $this->shim[$name]['exports'] = $exports;
112  }
113  }
114 
121  public function hasShim($name) {
122  return isset($this->shim[$name]);
123  }
124 
131  public function removeShim($name) {
132  unset($this->shim[$name]);
133  }
134 
141  public function addDependency($name) {
142  $this->dependencies[$name] = true;
143  }
144 
151  public function removeDependency($name) {
152  unset($this->dependencies[$name]);
153  }
154 
160  public function getDependencies() {
161  return array_keys($this->dependencies);
162  }
163 
170  public function hasDependency($name) {
171  return isset($this->dependencies[$name]);
172  }
173 
185  public function addModule($name, array $config = array()) {
186  $url = elgg_extract('url', $config);
187  $deps = elgg_extract('deps', $config, array());
188  $exports = elgg_extract('exports', $config);
189 
190  if (!empty($url)) {
191  $this->addPath($name, $url);
192  }
193 
194  // this is a shimmed module
195  // some jQuery modules don't need to export anything when shimmed,
196  // so check for deps too
197  if (!empty($deps) || !empty($exports)) {
198  $this->addShim($name, $config);
199  } else {
200  $this->addDependency($name);
201  }
202  }
203 
210  public function removeModule($name) {
211  $this->removeDependency($name);
212  $this->removeShim($name);
213  $this->removePath($name);
214  }
215 
222  public function hasModule($name) {
223  if (in_array($name, $this->getDependencies())) {
224  return true;
225  }
226 
227  if (isset($this->shim[$name])) {
228  return true;
229  }
230 
231  if (isset($this->paths[$name])) {
232  return true;
233  }
234 
235  return false;
236  }
237 
243  public function getConfig() {
244  $defaults = array(
245  'baseUrl' => $this->baseUrl,
246  'paths' => $this->paths,
247  'shim' => $this->shim,
248  'deps' => $this->getDependencies(),
249  'waitSeconds' => 20,
250  );
251 
252  $params = array(
253  'defaults' => $defaults
254  );
255 
256  return $this->hooks->trigger('config', 'amd', $params, $defaults);
257  }
258 }
removeShim($name)
Unregister the shim config for a module.
Definition: Config.php:131
addDependency($name)
Add a dependency.
Definition: Config.php:141
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:185
__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
$path
Definition: details.php:88
removePath($name, $path=null)
Remove a path for a module.
Definition: Config.php:69
getDependencies()
Get registered dependencies.
Definition: Config.php:160
$url
Definition: exceptions.php:24
addShim($name, array $config)
Configures a shimmed module.
Definition: Config.php:96
$params
Definition: login.php:72
Save menu items.
$key
Definition: summary.php:34
getConfig()
Get the configuration of AMD.
Definition: Config.php:243
hasShim($name)
Is this shim defined.
Definition: Config.php:121
$deps
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:1375
hasModule($name)
Is module configured?
Definition: Config.php:222
hasDependency($name)
Is this dependency registered.
Definition: Config.php:170
removeModule($name)
Removes all config for a module.
Definition: Config.php:210
removeDependency($name)
Removes a dependency.
Definition: Config.php:151