Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Illuminate\Queue;
4
 
5
use Illuminate\Contracts\Database\ModelIdentifier;
6
use Illuminate\Contracts\Queue\QueueableCollection;
7
use Illuminate\Contracts\Queue\QueueableEntity;
8
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
9
use Illuminate\Database\Eloquent\Relations\Concerns\AsPivot;
10
use Illuminate\Database\Eloquent\Relations\Pivot;
11
 
12
trait SerializesAndRestoresModelIdentifiers
13
{
14
    /**
15
     * Get the property value prepared for serialization.
16
     *
17
     * @param  mixed  $value
18
     * @return mixed
19
     */
20
    protected function getSerializedPropertyValue($value)
21
    {
22
        if ($value instanceof QueueableCollection) {
23
            return (new ModelIdentifier(
24
                $value->getQueueableClass(),
25
                $value->getQueueableIds(),
26
                $value->getQueueableRelations(),
27
                $value->getQueueableConnection()
28
            ))->useCollectionClass(
29
                ($collectionClass = get_class($value)) !== EloquentCollection::class
30
                    ? $collectionClass
31
                    : null
32
            );
33
        }
34
 
35
        if ($value instanceof QueueableEntity) {
36
            return new ModelIdentifier(
37
                get_class($value),
38
                $value->getQueueableId(),
39
                $value->getQueueableRelations(),
40
                $value->getQueueableConnection()
41
            );
42
        }
43
 
44
        return $value;
45
    }
46
 
47
    /**
48
     * Get the restored property value after deserialization.
49
     *
50
     * @param  mixed  $value
51
     * @return mixed
52
     */
53
    protected function getRestoredPropertyValue($value)
54
    {
55
        if (! $value instanceof ModelIdentifier) {
56
            return $value;
57
        }
58
 
59
        return is_array($value->id)
60
                ? $this->restoreCollection($value)
61
                : $this->restoreModel($value);
62
    }
63
 
64
    /**
65
     * Restore a queueable collection instance.
66
     *
67
     * @param  \Illuminate\Contracts\Database\ModelIdentifier  $value
68
     * @return \Illuminate\Database\Eloquent\Collection
69
     */
70
    protected function restoreCollection($value)
71
    {
72
        if (! $value->class || count($value->id) === 0) {
73
            return ! is_null($value->collectionClass ?? null)
74
                ? new $value->collectionClass
75
                : new EloquentCollection;
76
        }
77
 
78
        $collection = $this->getQueryForModelRestoration(
79
            (new $value->class)->setConnection($value->connection), $value->id
80
        )->useWritePdo()->get();
81
 
82
        if (is_a($value->class, Pivot::class, true) ||
83
            in_array(AsPivot::class, class_uses($value->class))) {
84
            return $collection;
85
        }
86
 
87
        $collection = $collection->keyBy->getKey();
88
 
89
        $collectionClass = get_class($collection);
90
 
91
        return new $collectionClass(
92
            collect($value->id)->map(function ($id) use ($collection) {
93
                return $collection[$id] ?? null;
94
            })->filter()
95
        );
96
    }
97
 
98
    /**
99
     * Restore the model from the model identifier instance.
100
     *
101
     * @param  \Illuminate\Contracts\Database\ModelIdentifier  $value
102
     * @return \Illuminate\Database\Eloquent\Model
103
     */
104
    public function restoreModel($value)
105
    {
106
        return $this->getQueryForModelRestoration(
107
            (new $value->class)->setConnection($value->connection), $value->id
108
        )->useWritePdo()->firstOrFail()->load($value->relations ?? []);
109
    }
110
 
111
    /**
112
     * Get the query for model restoration.
113
     *
114
     * @param  \Illuminate\Database\Eloquent\Model  $model
115
     * @param  array|int  $ids
116
     * @return \Illuminate\Database\Eloquent\Builder
117
     */
118
    protected function getQueryForModelRestoration($model, $ids)
119
    {
120
        return $model->newQueryForRestoration($ids);
121
    }
122
}