Elgg  Version 6.3
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  public function __construct(protected EntityCache $entity_cache) {
23  }
24 
34  public function preload(array $objects, array $guid_properties): void {
35  $guids = $this->getGuidsToLoad($objects, $guid_properties);
36 
37  // If only 1 to load, not worth the overhead of elgg_get_entities(),
38  // get_entity() will handle it later.
39  if (count($guids) > 1) {
40  Entities::find([
41  'guids' => $guids,
42  'limit' => self::MAX_PRELOAD,
43  'order_by' => false,
44  ]);
45  }
46  }
47 
58  protected function getGuidsToLoad(array $objects, array $guid_properties): array {
59  if (count($objects) < 2) {
60  return [];
61  }
62 
63  $preload_guids = [];
64  foreach ($objects as $object) {
65  if (!is_object($object)) {
66  continue;
67  }
68 
69  foreach ($guid_properties as $property) {
70  if (empty($object->{$property})) {
71  continue;
72  }
73 
74  $guid = (int) $object->{$property};
75  if ($guid && is_null($this->entity_cache->load($guid))) {
76  $preload_guids[] = $guid;
77  }
78  }
79  }
80 
81  return array_unique($preload_guids);
82  }
83 }
$guid
Reset an ElggUpgrade.
Definition: reset.php:6
$guids
Activates all specified installed and inactive plugins.
Definition: activate_all.php:9
Volatile cache for entities.
Definition: EntityCache.php:12
Entities repository contains methods for fetching entities from database or performing calculations o...
Definition: Entities.php:19
Preload entities based on properties of fetched objects.
__construct(protected EntityCache $entity_cache)
Constructor.
getGuidsToLoad(array $objects, array $guid_properties)
Get GUIDs that need to be loaded.
preload(array $objects, array $guid_properties)
Preload entities based on the given objects.
if($email instanceof \Elgg\Email) $object
Definition: body.php:24
if(parse_url(elgg_get_site_url(), PHP_URL_PATH) !=='/') if(file_exists(elgg_get_root_path() . 'robots.txt'))
Set robots.txt.
Definition: robots.php:10