19 declare(strict_types=1);
45 public function __construct(
private ResourceBuilder $resource_builder,
private CollectionBuilder $collection_builder,
private RepositoryPreloader $preloader,
private Subject $events)
54 ?
string $collection_identification =
null,
56 ): ResourceCollectionIdentification {
57 if ($collection_identification ===
null 58 || $collection_identification ===
'' 59 || !$this->collection_builder->has(
new ResourceCollectionIdentification($collection_identification))
61 $collection = $this->collection_builder->new($owner);
62 $identification = $collection->getIdentification();
63 $this->cache[$identification->serialize()] = $collection;
65 return $identification;
68 return new ResourceCollectionIdentification($collection_identification);
71 public function exists(
string $collection_identification): bool
73 return $this->collection_builder->has(
new ResourceCollectionIdentification($collection_identification));
77 ?
string $collection_identification =
null,
79 ): ?ResourceCollectionIdentification {
80 if ($this->
exists($collection_identification)) {
81 return $this->
id($collection_identification, $owner);
87 ResourceCollectionIdentification $identification,
90 $rcid = $identification->serialize();
92 $collection = $this->cache[$rcid] ?? $this->collection_builder->get(
99 $rids = isset($this->cache[$rcid])
101 : $this->rid_cache[$rcid]
102 ?? iterator_to_array($this->collection_builder->getResourceIds($identification));
104 $this->rid_cache[$rcid] = [];
105 $collection->clear();
107 foreach ($rids as $resource_identification) {
108 if ($this->resource_builder->has($resource_identification)) {
109 $collection->add($resource_identification);
110 $preload[] = $resource_identification;
112 $this->rid_cache[$rcid][] = $resource_identification;
114 $this->preloader->preload($preload);
116 return $this->cache[$rcid] = $collection;
123 return $this->collection_builder->store($collection);
126 public function clone(ResourceCollectionIdentification $source_collection_id): ResourceCollectionIdentification
128 $target_collection_id = $this->
id();
129 $target_collection = $this->
get($target_collection_id);
130 $source_collection = $this->
get($source_collection_id);
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());
137 $this->
store($target_collection);
139 return $target_collection_id;
142 public function remove(
143 ResourceCollectionIdentification $collection_id,
144 ResourceStakeholder $stakeholder,
145 bool $delete_resources_as_well = false
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);
154 unset($this->cache[$collection_id->serialize()]);
156 return $this->collection_builder->delete($collection_id);
166 if ($position >= $from && $position < $from + $amout) {
167 $return[] = $identification;
179 if ($position >= $from && $position <= $to) {
180 yield $identification;
188 $this->resource_builder,
189 $this->collection_builder,
store(ResourceCollection $collection)
clone(ResourceCollectionIdentification $source_collection_id)
Class ResourceCollection.
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)
rangeAsArray(ResourceCollection $collection, int $from, int $amout)
getResourceIdentifications()
sort(ResourceCollection $collection)
id(?string $collection_identification=null, ?int $owner=null)
exists(string $collection_identification)
__construct(private ResourceBuilder $resource_builder, private CollectionBuilder $collection_builder, private RepositoryPreloader $preloader, private Subject $events)
Consumers constructor.