Elgg  Version 3.0
ExternalFiles.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Assets;
4 
6 
14 
18  protected $externals = [];
19 
23  protected $externals_map = [];
24 
36  public function register($type, $name, $url, $location, $priority = 500) {
37  if (empty($name) || empty($url)) {
38  return false;
39  }
40 
41  $url = elgg_normalize_url($url);
42 
43  $this->setupType($type);
44 
45  $name = trim(strtolower($name));
46 
47  // normalize bogus priorities, but allow empty, null, and false to be defaults.
48  if (!is_numeric($priority)) {
49  $priority = 500;
50  }
51 
52  // no negative priorities right now.
53  $priority = max((int) $priority, 0);
54 
55  $item = elgg_extract($name, $this->externals_map[$type]);
56 
57  if ($item) {
58  // updating a registered item
59  // don't update loaded because it could already be set
60  $item->url = $url;
61  $item->location = $location;
62 
63  // if loaded before registered, that means it hasn't been added to the list yet
64  if ($this->externals[$type]->contains($item)) {
65  $priority = $this->externals[$type]->move($item, $priority);
66  } else {
67  $priority = $this->externals[$type]->add($item, $priority);
68  }
69  } else {
70  $item = (object) [
71  'loaded' => false,
72  'url' => $url,
73  'location' => $location,
74  ];
75  $priority = $this->externals[$type]->add($item, $priority);
76  }
77 
78  $this->externals_map[$type][$name] = $item;
79 
80  return $priority !== false;
81  }
82 
91  public function unregister($type, $name) {
92  $this->setupType($type);
93 
94  $name = trim(strtolower($name));
95  $item = elgg_extract($name, $this->externals_map[$type]);
96 
97  if ($item) {
98  unset($this->externals_map[$type][$name]);
99  return $this->externals[$type]->remove($item);
100  }
101 
102  return false;
103  }
104 
113  public function getFile($type, $name) {
114  $this->setupType($type);
115 
116  $name = trim(strtolower($name));
117  if (!isset($this->externals_map[$type][$name])) {
118  return null;
119  }
120 
121  $item = $this->externals_map[$type][$name];
122  $priority = $this->externals[$type]->getPriority($item);
123 
124  // don't allow internal properties to be altered
125  $clone = clone $item;
126  $clone->priority = $priority;
127 
128  return $clone;
129  }
130 
139  public function load($type, $name) {
140  $this->setupType($type);
141 
142  $name = trim(strtolower($name));
143 
144  $item = elgg_extract($name, $this->externals_map[$type]);
145 
146  if ($item) {
147  // update a registered item
148  $item->loaded = true;
149  } else {
150  $item = (object) [
151  'loaded' => true,
152  'url' => '',
153  'location' => '',
154  ];
155  if (elgg_view_exists($name)) {
156  $item->url = elgg_get_simplecache_url($name);
157  $item->location = ($type == 'js') ? 'foot' : 'head';
158  }
159 
160  $this->externals[$type]->add($item);
161  $this->externals_map[$type][$name] = $item;
162  }
163  }
164 
173  public function getLoadedFiles($type, $location) {
174  if (!isset($this->externals[$type])) {
175  return [];
176  }
177 
178  $items = $this->externals[$type]->getElements();
179 
180  $items = array_filter($items, function($v) use ($location) {
181  return $v->loaded == true && $v->location == $location;
182  });
183  if (!empty($items)) {
184  array_walk($items, function(&$v, $k){
185  $v = $v->url;
186  });
187  }
188  return $items;
189  }
190 
199  public function getRegisteredFiles($type, $location) {
200  if (!isset($this->externals[$type])) {
201  return [];
202  }
203 
204  $ret = [];
205  $items = $this->externals[$type]->getElements();
206  $items = array_filter($items, function($v) use ($location) {
207  return ($v->location == $location);
208  });
209 
210  foreach ($items as $item) {
211  $ret[] = clone $item;
212  }
213 
214  return $ret;
215  }
216 
222  public function reset() {
223  $this->externals = [];
224  $this->externals_map = [];
225  }
226 
233  protected function setupType($type) {
234  if (!isset($this->externals[$type])) {
235  $this->externals[$type] = new \ElggPriorityList();
236  }
237 
238  if (!isset($this->externals_map[$type])) {
239  $this->externals_map[$type] = [];
240  }
241  }
242 }
elgg_view_exists($view, $viewtype= '', $recurse=true)
Returns whether the specified view exists.
Definition: views.php:205
if(!$user||!$user->canDelete()) $name
Definition: delete.php:22
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 contains
Definition: LICENSE.txt:210
elgg_normalize_url($url)
Definition: output.php:186
if(!$items) $item
Definition: delete.php:13
elgg_get_simplecache_url($view, $subview= '')
Get the URL for the cached view.
Definition: cache.php:141
getRegisteredFiles($type, $location)
Get registered file objects.
$items
Definition: delete.php:8
$type
Definition: delete.php:21
setupType($type)
Bootstraps the externals data structure.
reset()
Unregister all files.
elgg ElggPriorityList
Priority lists allow you to create an indexed list that can be iterated through in a specific order...
getFile($type, $name)
Get metadata for a registered file.
$url
Definition: default.php:33
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:1131
$location
Definition: default.php:42
getLoadedFiles($type, $location)
Get external resource descriptors.
load($type, $name)
Load an external resource for use on this page.
WARNING: API IN FLUX.
$priority
unregister($type, $name)
Unregister an external file.