Elgg  Version 1.11
ExternalFiles.php
Go to the documentation of this file.
1 <?php
2 namespace Elgg\Assets;
3 
4 
20  private $CONFIG;
21 
27  public function __construct(\stdClass $config = null) {
28  if (!($config instanceof \stdClass)) {
29  $config = new \stdClass();
30  }
31  $this->CONFIG = $config;
32  }
33 
45  public function register($type, $name, $url, $location, $priority = 500) {
46 
47 
48  if (empty($name) || empty($url)) {
49  return false;
50  }
51 
52  $url = elgg_format_url($url);
53  $url = elgg_normalize_url($url);
54 
55  $this->bootstrap($type);
56 
57  $name = trim(strtolower($name));
58 
59  // normalize bogus priorities, but allow empty, null, and false to be defaults.
60  if (!is_numeric($priority)) {
61  $priority = 500;
62  }
63 
64  // no negative priorities right now.
65  $priority = max((int)$priority, 0);
66 
67  $item = elgg_extract($name, $this->CONFIG->externals_map[$type]);
68 
69  if ($item) {
70  // updating a registered item
71  // don't update loaded because it could already be set
72  $item->url = $url;
73  $item->location = $location;
74 
75  // if loaded before registered, that means it hasn't been added to the list yet
76  if ($this->CONFIG->externals[$type]->contains($item)) {
77  $priority = $this->CONFIG->externals[$type]->move($item, $priority);
78  } else {
79  $priority = $this->CONFIG->externals[$type]->add($item, $priority);
80  }
81  } else {
82  $item = new \stdClass();
83  $item->loaded = false;
84  $item->url = $url;
85  $item->location = $location;
86 
87  $priority = $this->CONFIG->externals[$type]->add($item, $priority);
88  }
89 
90  $this->CONFIG->externals_map[$type][$name] = $item;
91 
92  return $priority !== false;
93  }
94 
103  public function unregister($type, $name) {
104 
105  $this->bootstrap($type);
106 
107  $name = trim(strtolower($name));
108  $item = elgg_extract($name, $this->CONFIG->externals_map[$type]);
109 
110  if ($item) {
111  unset($this->CONFIG->externals_map[$type][$name]);
112  return $this->CONFIG->externals[$type]->remove($item);
113  }
114 
115  return false;
116  }
117 
126  public function load($type, $name) {
127 
128 
129  $this->bootstrap($type);
130 
131  $name = trim(strtolower($name));
132 
133  $item = elgg_extract($name, $this->CONFIG->externals_map[$type]);
134 
135  if ($item) {
136  // update a registered item
137  $item->loaded = true;
138  } else {
139  $item = new \stdClass();
140  $item->loaded = true;
141  $item->url = '';
142  $item->location = '';
143 
144  $this->CONFIG->externals[$type]->add($item);
145  $this->CONFIG->externals_map[$type][$name] = $item;
146  }
147  }
148 
157  public function getLoadedFiles($type, $location) {
158 
159 
160  if (
161  isset($this->CONFIG->externals)
162  && isset($this->CONFIG->externals[$type])
163  && $this->CONFIG->externals[$type] instanceof \ElggPriorityList
164  ) {
165  $items = $this->CONFIG->externals[$type]->getElements();
166 
167  $items = array_filter($items, function($v) use ($location) {
168  return $v->loaded == true && $v->location == $location;
169  });
170  if ($items) {
171  array_walk($items, function(&$v, $k){
172  $v = $v->url;
173  });
174  }
175  return $items;
176  }
177  return array();
178  }
179 
186  protected function bootstrap($type) {
187 
188  if (!isset($this->CONFIG->externals)) {
189  $this->CONFIG->externals = array();
190  }
191 
192  if (!isset($this->CONFIG->externals[$type]) || !$this->CONFIG->externals[$type] instanceof \ElggPriorityList) {
193  $this->CONFIG->externals[$type] = new \ElggPriorityList();
194  }
195 
196  if (!isset($this->CONFIG->externals_map)) {
197  $this->CONFIG->externals_map = array();
198  }
199 
200  if (!isset($this->CONFIG->externals_map[$type])) {
201  $this->CONFIG->externals_map[$type] = array();
202  }
203  }
204 }
__construct(\stdClass $config=null)
Constructor.
elgg_normalize_url($url)
Definition: output.php:311
if($guid==elgg_get_logged_in_user_guid()) $name
Definition: delete.php:21
bootstrap($type)
Bootstraps the externals data structure in $CONFIG.
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:1246
$url
Definition: exceptions.php:24
elgg ElggPriorityList
Priority lists allow you to create an indexed list that can be iterated through in a specific order...
$item
Definition: item.php:12
$type
Definition: add.php:8
$items
Definition: list.php:11
elgg_format_url($url)
Handles formatting of ampersands in urls.
Definition: output.php:105
getLoadedFiles($type, $location)
Get external resource descriptors.
load($type, $name)
Load an external resource for use on this page.
$priority
unregister($type, $name)
Unregister an external file.