Elgg  Version 5.1
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 
75  protected function getGuidsToLoad($objects, array $guid_properties) {
76  if (!is_array($objects) || count($objects) < 2) {
77  return [];
78  }
79 
80  $preload_guids = [];
81  foreach ($objects as $object) {
82  if (is_object($object)) {
83  foreach ($guid_properties as $property) {
84  if (empty($object->{$property})) {
85  continue;
86  }
87 
88  $guid = $object->{$property};
89  if ($guid && !call_user_func($this->_callable_cache_checker, $guid)) {
90  $preload_guids[] = $guid;
91  }
92  }
93  }
94  }
95 
96  return array_unique($preload_guids);
97  }
98 }
getFromCache(int $guid)
Get an entity from the in-memory or memcache caches.
$options
Elgg admin footer.
Definition: footer.php:6
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
if($email instanceof\Elgg\Email) $object
Definition: body.php:24
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.
Entity table database service.
Definition: EntityTable.php:26
$guid
Reset an ElggUpgrade.
Definition: reset.php:6