ExternalFiles.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. namespace Elgg\Assets;
  3. /**
  4. * WARNING: API IN FLUX. DO NOT USE DIRECTLY.
  5. *
  6. * @access private
  7. *
  8. * @package Elgg.Core
  9. * @subpackage Assets
  10. * @since 1.10.0
  11. */
  12. class ExternalFiles {
  13. /**
  14. * Global Elgg configuration
  15. *
  16. * @var \stdClass
  17. */
  18. private $CONFIG;
  19. /**
  20. * Constructor
  21. *
  22. * @param \stdClass $config Predefined configuration in format used by global $CONFIG variable
  23. */
  24. public function __construct(\stdClass $config = null) {
  25. if (!($config instanceof \stdClass)) {
  26. $config = new \stdClass();
  27. }
  28. $this->CONFIG = $config;
  29. }
  30. /**
  31. * Core registration function for external files
  32. *
  33. * @param string $type Type of external resource (js or css)
  34. * @param string $name Identifier used as key
  35. * @param string $url URL
  36. * @param string $location Location in the page to include the file
  37. * @param int $priority Loading priority of the file
  38. *
  39. * @return bool
  40. */
  41. public function register($type, $name, $url, $location, $priority = 500) {
  42. if (empty($name) || empty($url)) {
  43. return false;
  44. }
  45. $url = elgg_format_url($url);
  46. $url = elgg_normalize_url($url);
  47. $this->bootstrap($type);
  48. $name = trim(strtolower($name));
  49. // normalize bogus priorities, but allow empty, null, and false to be defaults.
  50. if (!is_numeric($priority)) {
  51. $priority = 500;
  52. }
  53. // no negative priorities right now.
  54. $priority = max((int)$priority, 0);
  55. $item = elgg_extract($name, $this->CONFIG->externals_map[$type]);
  56. if ($item) {
  57. // updating a registered item
  58. // don't update loaded because it could already be set
  59. $item->url = $url;
  60. $item->location = $location;
  61. // if loaded before registered, that means it hasn't been added to the list yet
  62. if ($this->CONFIG->externals[$type]->contains($item)) {
  63. $priority = $this->CONFIG->externals[$type]->move($item, $priority);
  64. } else {
  65. $priority = $this->CONFIG->externals[$type]->add($item, $priority);
  66. }
  67. } else {
  68. $item = new \stdClass();
  69. $item->loaded = false;
  70. $item->url = $url;
  71. $item->location = $location;
  72. $priority = $this->CONFIG->externals[$type]->add($item, $priority);
  73. }
  74. $this->CONFIG->externals_map[$type][$name] = $item;
  75. return $priority !== false;
  76. }
  77. /**
  78. * Unregister an external file
  79. *
  80. * @param string $type Type of file: js or css
  81. * @param string $name The identifier of the file
  82. *
  83. * @return bool
  84. */
  85. public function unregister($type, $name) {
  86. $this->bootstrap($type);
  87. $name = trim(strtolower($name));
  88. $item = elgg_extract($name, $this->CONFIG->externals_map[$type]);
  89. if ($item) {
  90. unset($this->CONFIG->externals_map[$type][$name]);
  91. return $this->CONFIG->externals[$type]->remove($item);
  92. }
  93. return false;
  94. }
  95. /**
  96. * Load an external resource for use on this page
  97. *
  98. * @param string $type Type of file: js or css
  99. * @param string $name The identifier for the file
  100. *
  101. * @return void
  102. */
  103. public function load($type, $name) {
  104. $this->bootstrap($type);
  105. $name = trim(strtolower($name));
  106. $item = elgg_extract($name, $this->CONFIG->externals_map[$type]);
  107. if ($item) {
  108. // update a registered item
  109. $item->loaded = true;
  110. } else {
  111. $item = new \stdClass();
  112. $item->loaded = true;
  113. $item->url = '';
  114. $item->location = '';
  115. $this->CONFIG->externals[$type]->add($item);
  116. $this->CONFIG->externals_map[$type][$name] = $item;
  117. }
  118. }
  119. /**
  120. * Get external resource descriptors
  121. *
  122. * @param string $type Type of file: js or css
  123. * @param string $location Page location
  124. *
  125. * @return array
  126. */
  127. public function getLoadedFiles($type, $location) {
  128. if (
  129. isset($this->CONFIG->externals)
  130. && isset($this->CONFIG->externals[$type])
  131. && $this->CONFIG->externals[$type] instanceof \ElggPriorityList
  132. ) {
  133. $items = $this->CONFIG->externals[$type]->getElements();
  134. $items = array_filter($items, function($v) use ($location) {
  135. return $v->loaded == true && $v->location == $location;
  136. });
  137. if ($items) {
  138. array_walk($items, function(&$v, $k){
  139. $v = $v->url;
  140. });
  141. }
  142. return $items;
  143. }
  144. return array();
  145. }
  146. /**
  147. * Bootstraps the externals data structure in $CONFIG.
  148. *
  149. * @param string $type The type of external, js or css.
  150. * @return null
  151. */
  152. protected function bootstrap($type) {
  153. if (!isset($this->CONFIG->externals)) {
  154. $this->CONFIG->externals = array();
  155. }
  156. if (!isset($this->CONFIG->externals[$type]) || !$this->CONFIG->externals[$type] instanceof \ElggPriorityList) {
  157. $this->CONFIG->externals[$type] = new \ElggPriorityList();
  158. }
  159. if (!isset($this->CONFIG->externals_map)) {
  160. $this->CONFIG->externals_map = array();
  161. }
  162. if (!isset($this->CONFIG->externals_map[$type])) {
  163. $this->CONFIG->externals_map[$type] = array();
  164. }
  165. }
  166. }