Elgg  Version master
EntityPreloader.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg;
4 
7 
14 
15  const MAX_PRELOAD = 256;
16 
22 
28 
34  public function __construct(EntityCache $entity_cache) {
35  $this->_callable_cache_checker = function ($guid) use ($entity_cache) {
36  return $entity_cache->load($guid);
37  };
38  $this->_callable_entity_loader = function ($options) {
39  return Entities::find($options);
40  };
41  }
42 
52  public function preload($objects, array $guid_properties) {
53  $guids = $this->getGuidsToLoad($objects, $guid_properties);
54 
55  // If only 1 to load, not worth the overhead of elgg_get_entities(),
56  // get_entity() will handle it later.
57  if (count($guids) > 1) {
58  call_user_func($this->_callable_entity_loader, [
59  'guids' => $guids,
60  'limit' => self::MAX_PRELOAD,
61  'order_by' => false,
62  ]);
63  }
64  }
65 
76  protected function getGuidsToLoad($objects, array $guid_properties) {
77  if (!is_array($objects) || count($objects) < 2) {
78  return [];
79  }
80 
81  $preload_guids = [];
82  foreach ($objects as $object) {
83  if (is_object($object)) {
84  foreach ($guid_properties as $property) {
85  if (empty($object->{$property})) {
86  continue;
87  }
88 
89  $guid = $object->{$property};
90  if ($guid && !call_user_func($this->_callable_cache_checker, $guid)) {
91  $preload_guids[] = $guid;
92  }
93  }
94  }
95  }
96 
97  return array_unique($preload_guids);
98  }
99 }
__construct(EntityCache $entity_cache)
Constructor.
if($who_can_change_language=== 'nobody') elseif($who_can_change_language=== 'admin_only'&&!elgg_is_admin_logged_in()) $options
Definition: language.php:20
Volatile cache for entities.
Definition: EntityCache.php:10
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
load(int $guid)
Retrieve a entity from the cache.
Definition: EntityCache.php:27
preload($objects, array $guid_properties)
Preload entities based on the given objects.
Preload entities based on properties of fetched objects.
$guid
Reset an ElggUpgrade.
Definition: reset.php:6