Elgg  Version master
ESMService.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Javascript;
4 
7 
14 class ESMService {
15 
16  protected array $imports = [];
17 
18  protected array $runtime_modules = [];
19 
26  public function __construct(
27  protected ViewsService $views,
28  protected SimpleCache $cache
29  ) {
30  }
31 
37  public function getImportMapData(): array {
38  $modules = $this->views->getESModules();
39  $imports = [];
40  if (!empty($modules)) {
41  foreach ($modules as $name) {
42  $short_name = str_replace('.mjs', '', $name);
43  $imports[$short_name] = $this->cache->getUrl($name);
44  }
45  }
46 
47  $imports = array_merge($imports, $this->runtime_modules);
48 
49  return ['imports' => $imports];
50  }
51 
60  public function register(string $name, string $href): void {
61  $this->runtime_modules[$name] = $href;
62  }
63 
71  public function import(string $name): void {
72  $this->imports[$name] = true;
73  }
74 
80  public function getImports(): array {
81  return array_keys($this->imports);
82  }
83 }
getImports()
Returns all modules that requested to be loaded.
Definition: ESMService.php:80
if(!$user||!$user->canDelete()) $name
Definition: delete.php:22
Keeps track of ES modules.
Definition: ESMService.php:14
__construct(protected ViewsService $views, protected SimpleCache $cache)
Constructor.
Definition: ESMService.php:26
Views service.
getImportMapData()
Returns the importmap data.
Definition: ESMService.php:37
Simple cache service.
Definition: SimpleCache.php:15
$views
Definition: item.php:17