Elgg  Version 3.0
EntityPreloader.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg;
4 
8 
15 
21 
27 
33  public function __construct(EntityTable $entity_table) {
34  $this->_callable_cache_checker = function ($guid) use ($entity_table) {
35  return $entity_table->getFromCache($guid);
36  };
37  $this->_callable_entity_loader = function ($options) {
38  return Entities::find($options);
39  };
40  }
41 
51  public function preload($objects, array $guid_properties) {
52  $guids = $this->getGuidsToLoad($objects, $guid_properties);
53 
54  // If only 1 to load, not worth the overhead of elgg_get_entities(),
55  // get_entity() will handle it later.
56  if (count($guids) > 1) {
57  call_user_func($this->_callable_entity_loader, [
58  'guids' => $guids,
59  'limit' => EntityCache::MAX_SIZE,
60  'order_by' => false,
61  ]);
62  }
63  }
64 
74  protected function getGuidsToLoad($objects, array $guid_properties) {
75  if (!is_array($objects) || count($objects) < 2) {
76  return [];
77  }
78  $preload_guids = [];
79  foreach ($objects as $object) {
80  if (is_object($object)) {
81  foreach ($guid_properties as $property) {
82  if (empty($object->{$property})) {
83  continue;
84  }
85  $guid = $object->{$property};
86  if ($guid && !call_user_func($this->_callable_cache_checker, $guid)) {
87  $preload_guids[] = $guid;
88  }
89  }
90  }
91  }
92  return array_unique($preload_guids);
93  }
94 }
if(!$item instanceof ElggRiverItem) $object
Definition: responses.php:23
$guid
Removes an admin notice.
$options
Elgg admin footer.
Definition: footer.php:6
Configuration exception.
getGuidsToLoad($objects, array $guid_properties)
Get GUIDs that need to be loaded.
$guids
Activates all specified installed and inactive plugins.
Definition: activate_all.php:9
getFromCache($guid)
Get an entity from the in-memory or memcache caches.
preload($objects, array $guid_properties)
Preload entities based on the given objects.
Preload entities based on properties of fetched objects.
__construct(EntityTable $entity_table)
Constructor.
WARNING: API IN FLUX.
Definition: EntityTable.php:38