Elgg  Version 5.1
Config.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Amd;
4 
6 
12 class Config {
13 
14  private $baseUrl = '';
15 
16  private $paths = [];
17 
18  private $shim = [];
19 
20  private $dependencies = [];
21 
25  protected $events;
26 
32  public function __construct(\Elgg\EventsService $events) {
33  $this->events = $events;
34  }
35 
43  public function setBaseUrl($url) {
44  $this->baseUrl = $url;
45  }
46 
56  public function addPath(string $name, string $path): void {
57  if (preg_match('/\.js$/', $path)) {
58  $path = preg_replace('/\.js$/', '', $path);
59  }
60 
61  if (!isset($this->paths[$name])) {
62  $this->paths[$name] = [];
63  }
64 
65  array_unshift($this->paths[$name], $path);
66  }
67 
76  public function removePath($name, $path = null) {
77  if (!$path) {
78  unset($this->paths[$name]);
79  } else {
80  if (preg_match('/\.js$/', $path)) {
81  $path = preg_replace('/\.js$/', '', $path);
82  }
83 
84  $key = array_search($path, $this->paths[$name]);
85  unset($this->paths[$name][$key]);
86 
87  if (empty($this->paths[$name])) {
88  unset($this->paths[$name]);
89  }
90  }
91  }
92 
104  public function addShim(string $name, array $config): void {
105  $deps = elgg_extract('deps', $config, []);
106  $exports = elgg_extract('exports', $config);
107 
108  if (empty($deps) && empty($exports)) {
109  throw new InvalidArgumentException('Shimmed modules must have deps or exports');
110  }
111 
112  $this->shim[$name] = [];
113 
114  if (!empty($deps)) {
115  $this->shim[$name]['deps'] = $deps;
116  }
117 
118  if (!empty($exports)) {
119  $this->shim[$name]['exports'] = $exports;
120  }
121  }
122 
130  public function hasShim($name) {
131  return isset($this->shim[$name]);
132  }
133 
141  public function removeShim($name) {
142  unset($this->shim[$name]);
143  }
144 
152  public function addDependency(string $name): void {
153  $this->dependencies[$name] = true;
154  }
155 
163  public function removeDependency(string $name): void {
164  unset($this->dependencies[$name]);
165  }
166 
172  public function getDependencies() {
173  return array_keys($this->dependencies);
174  }
175 
183  public function hasDependency($name) {
184  return isset($this->dependencies[$name]);
185  }
186 
198  public function addModule($name, array $config = []) {
199  $url = elgg_extract('url', $config);
200  $deps = elgg_extract('deps', $config, []);
201  $exports = elgg_extract('exports', $config);
202 
203  if (!empty($url)) {
204  $this->addPath($name, $url);
205  }
206 
207  // this is a shimmed module
208  // some jQuery modules don't need to export anything when shimmed,
209  // so check for deps too
210  if (!empty($deps) || !empty($exports)) {
211  $this->addShim($name, $config);
212  } else {
213  $this->addDependency($name);
214  }
215  }
216 
224  public function removeModule($name) {
225  $this->removeDependency($name);
226  $this->removeShim($name);
227  $this->removePath($name);
228  }
229 
237  public function hasModule($name) {
238  if (in_array($name, $this->getDependencies())) {
239  return true;
240  }
241 
242  if (isset($this->shim[$name])) {
243  return true;
244  }
245 
246  if (isset($this->paths[$name])) {
247  return true;
248  }
249 
250  return false;
251  }
252 
258  public function getConfig() {
259  $defaults = [
260  'baseUrl' => $this->baseUrl,
261  'paths' => $this->paths,
262  'shim' => $this->shim,
263  'deps' => $this->getDependencies(),
264  'waitSeconds' => 20,
265  ];
266 
267  $params = [
268  'defaults' => $defaults
269  ];
270 
271  return $this->events->triggerResults('config', 'amd', $params, $defaults);
272  }
273 }
removeShim($name)
Unregister the shim config for a module.
Definition: Config.php:141
addShim(string $name, array $config)
Configures a shimmed module.
Definition: Config.php:104
Exception thrown if an argument is not of the expected type.
$params
Saves global plugin settings.
Definition: save.php:13
addPath(string $name, string $path)
Add a path mapping for a module.
Definition: Config.php:56
$defaults
Generic entity header upload helper.
Definition: header.php:6
if(!$user||!$user->canDelete()) $name
Definition: delete.php:22
if(!empty($children)) elseif($item->getData('show_with_empty_children')===false) $deps
Definition: item.php:52
setBaseUrl($url)
Set the base URL for the site.
Definition: Config.php:43
c Accompany it with the information you received as to the offer to distribute corresponding source complete source code means all the source code for all modules it plus any associated interface definition plus the scripts used to control compilation and installation of the executable as a special the source code distributed need not include anything that is normally and so on of the operating system on which the executable unless that component itself accompanies the executable If distribution of executable or object code is made by offering access to copy from a designated then offering equivalent access to copy the source code from the same place counts as distribution of the source even though third parties are not compelled to copy the source along with the object code You may not or distribute the Program except as expressly provided under this License Any attempt otherwise to sublicense or distribute the Program is void
Definition: LICENSE.txt:215
Events service.
removePath($name, $path=null)
Remove a path for a module.
Definition: Config.php:76
getDependencies()
Get registered dependencies.
Definition: Config.php:172
elgg_extract($key, $array, $default=null, bool $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
Definition: elgglib.php:254
$config
Advanced site settings, debugging section.
Definition: debugging.php:6
$path
Definition: details.php:70
__construct(\Elgg\EventsService $events)
Constructor.
Definition: Config.php:32
addModule($name, array $config=[])
Adds a standard AMD or shimmed module to the config.
Definition: Config.php:198
getConfig()
Get the configuration of AMD.
Definition: Config.php:258
addDependency(string $name)
Add a dependency.
Definition: Config.php:152
Control configuration of RequireJS.
Definition: Config.php:12
hasShim($name)
Is this shim defined.
Definition: Config.php:130
if($container instanceof ElggGroup &&$container->guid!=elgg_get_page_owner_guid()) $key
Definition: summary.php:44
hasModule($name)
Is module configured?
Definition: Config.php:237
hasDependency($name)
Is this dependency registered.
Definition: Config.php:183
foreach($plugin_guids as $guid) if(empty($deactivated_plugins)) $url
Definition: deactivate.php:39
removeModule($name)
Removes all config for a module.
Definition: Config.php:224
removeDependency(string $name)
Removes a dependency.
Definition: Config.php:163