Elgg  Version 2.2
 All Classes Namespaces Files Functions Variables Pages
EntityPreloader.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg;
4 
7 
14 
20 
26 
33  public function __construct(EntityCache $entity_cache, EntityTable $entity_table) {
34  $this->_callable_cache_checker = function ($guid) use ($entity_cache) {
35  return $entity_cache->get($guid);
36  };
37  $this->_callable_entity_loader = function ($options) use ($entity_table) {
38  return $entity_table->getEntities($options);
39  };
40  }
41 
51  public function preload($objects, array $guid_properties) {
52  $guids = $this->getGuidsToLoad($objects, $guid_properties);
53  // If only 1 to load, not worth the overhead of elgg_get_entities(),
54  // get_entity() will handle it later.
55  if (count($guids) > 1) {
56  call_user_func($this->_callable_entity_loader, array(
57  'guids' => $guids,
58  ));
59  }
60  }
61 
71  protected function getGuidsToLoad($objects, array $guid_properties) {
72  if (!is_array($objects) || count($objects) < 2) {
73  return array();
74  }
75  $preload_guids = array();
76  foreach ($objects as $object) {
77  if (is_object($object)) {
78  foreach ($guid_properties as $property) {
79  if (empty($object->{$property})) {
80  continue;
81  }
82  $guid = $object->{$property};
83  if ($guid && !call_user_func($this->_callable_cache_checker, $guid)) {
84  $preload_guids[] = $guid;
85  }
86  }
87  }
88  }
89  return array_unique($preload_guids);
90  }
91 }
$object
These two snippets demonstrates triggering an event and how to register for that event.
Definition: trigger.php:7
$guid
Removes an admin notice.
$options
Elgg admin footer.
Definition: footer.php:6
__construct(EntityCache $entity_cache, EntityTable $entity_table)
Constructor.
Volatile cache for entities.
Definition: EntityCache.php:12
getGuidsToLoad($objects, array $guid_properties)
Get GUIDs that need to be loaded.
getEntities(array $options=array())
Returns an array of entities with optional filtering.
$guids
get($guid)
Retrieve a entity from the cache.
Definition: EntityCache.php:57
preload($objects, array $guid_properties)
Preload entities based on the given objects.
Preload entities based on properties of fetched objects.