Elgg  Version 2.3
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  'limit' => EntityCache::MAX_SIZE,
59  ));
60  }
61  }
62 
72  protected function getGuidsToLoad($objects, array $guid_properties) {
73  if (!is_array($objects) || count($objects) < 2) {
74  return array();
75  }
76  $preload_guids = array();
77  foreach ($objects as $object) {
78  if (is_object($object)) {
79  foreach ($guid_properties as $property) {
80  if (empty($object->{$property})) {
81  continue;
82  }
83  $guid = $object->{$property};
84  if ($guid && !call_user_func($this->_callable_cache_checker, $guid)) {
85  $preload_guids[] = $guid;
86  }
87  }
88  }
89  }
90  return array_unique($preload_guids);
91  }
92 }
$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
Save menu items.
__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:62
preload($objects, array $guid_properties)
Preload entities based on the given objects.
Preload entities based on properties of fetched objects.
http free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:5