ILIAS  trunk Revision v11.0_alpha-1689-g66c127b4ae8
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
Collections.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
30 
38 {
39  private array $cache = [];
40  private array $rid_cache = [];
41 
45  public function __construct(private ResourceBuilder $resource_builder, private CollectionBuilder $collection_builder, private RepositoryPreloader $preloader, private Subject $events)
46  {
47  }
48 
53  public function id(
54  ?string $collection_identification = null,
55  ?int $owner = null
56  ): ResourceCollectionIdentification {
57  if ($collection_identification === null
58  || $collection_identification === ''
59  || !$this->collection_builder->has(new ResourceCollectionIdentification($collection_identification))
60  ) {
61  $collection = $this->collection_builder->new($owner);
62  $identification = $collection->getIdentification();
63  $this->cache[$identification->serialize()] = $collection;
64 
65  return $identification;
66  }
67 
68  return new ResourceCollectionIdentification($collection_identification);
69  }
70 
71  public function exists(string $collection_identification): bool
72  {
73  return $this->collection_builder->has(new ResourceCollectionIdentification($collection_identification));
74  }
75 
76  public function idOrNull(
77  ?string $collection_identification = null,
78  ?int $owner = null
79  ): ?ResourceCollectionIdentification {
80  if ($this->exists($collection_identification)) {
81  return $this->id($collection_identification, $owner);
82  }
83  return null;
84  }
85 
86  public function get(
87  ResourceCollectionIdentification $identification,
88  ?int $owner = null
90  $rcid = $identification->serialize();
91 
92  $collection = $this->cache[$rcid] ?? $this->collection_builder->get(
93  $identification,
94  $owner
95  );
96 
97  $preload = [];
98 
99  $rids = isset($this->cache[$rcid])
100  ? $this->cache[$rcid]->getResourceIdentifications()
101  : $this->rid_cache[$rcid]
102  ?? iterator_to_array($this->collection_builder->getResourceIds($identification));
103 
104  $this->rid_cache[$rcid] = []; // reset cache for identification, rebuild it now
105  $collection->clear();
106 
107  foreach ($rids as $resource_identification) {
108  if ($this->resource_builder->has($resource_identification)) {
109  $collection->add($resource_identification);
110  $preload[] = $resource_identification;
111  }
112  $this->rid_cache[$rcid][] = $resource_identification;
113  }
114  $this->preloader->preload($preload);
115 
116  return $this->cache[$rcid] = $collection;
117  }
118 
119  public function store(ResourceCollection $collection): bool
120  {
121  $this->cache[$collection->getIdentification()->serialize()] = $collection;
122 
123  return $this->collection_builder->store($collection);
124  }
125 
126  public function clone(ResourceCollectionIdentification $source_collection_id): ResourceCollectionIdentification
127  {
128  $target_collection_id = $this->id();
129  $target_collection = $this->get($target_collection_id);
130  $source_collection = $this->get($source_collection_id);
131 
132  foreach ($source_collection->getResourceIdentifications() as $identification) {
133  $resource = $this->resource_builder->get($identification);
134  $cloned_resource = $this->resource_builder->clone($resource);
135  $target_collection->add($cloned_resource->getIdentification());
136  }
137  $this->store($target_collection);
138 
139  return $target_collection_id;
140  }
141 
142  public function remove(
143  ResourceCollectionIdentification $collection_id,
144  ResourceStakeholder $stakeholder,
145  bool $delete_resources_as_well = false
146  ): bool {
147  $collection = $this->get($collection_id);
148  if ($delete_resources_as_well) {
149  foreach ($collection->getResourceIdentifications() as $resource_identification) {
150  $resource = $this->resource_builder->get($resource_identification);
151  $this->resource_builder->remove($resource, $stakeholder);
152  }
153  }
154  unset($this->cache[$collection_id->serialize()]);
155 
156  return $this->collection_builder->delete($collection_id);
157  }
158 
162  public function rangeAsArray(ResourceCollection $collection, int $from, int $amout): array
163  {
164  $return = [];
165  foreach ($collection->getResourceIdentifications() as $position => $identification) {
166  if ($position >= $from && $position < $from + $amout) {
167  $return[] = $identification;
168  }
169  }
170  return $return;
171  }
172 
176  public function rangeAsGenerator(ResourceCollection $collection, int $from, int $to): \Generator
177  {
178  foreach ($collection->getResourceIdentifications() as $position => $identification) {
179  if ($position >= $from && $position <= $to) {
180  yield $identification;
181  }
182  }
183  }
184 
185  public function sort(ResourceCollection $collection): Sorter
186  {
187  return new Sorter(
188  $this->resource_builder,
189  $this->collection_builder,
190  $collection
191  );
192  }
193 }
clone(ResourceCollectionIdentification $source_collection_id)
rangeAsGenerator(ResourceCollection $collection, int $from, int $to)
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
idOrNull(?string $collection_identification=null, ?int $owner=null)
Definition: Collections.php:76
rangeAsArray(ResourceCollection $collection, int $from, int $amout)
id(?string $collection_identification=null, ?int $owner=null)
Definition: Collections.php:53
__construct(private ResourceBuilder $resource_builder, private CollectionBuilder $collection_builder, private RepositoryPreloader $preloader, private Subject $events)
Consumers constructor.
Definition: Collections.php:45