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\Database\Eloquent\Concerns;
4
 
5
use Illuminate\Support\Facades\Date;
6
 
7
trait HasTimestamps
8
{
9
    /**
10
     * Indicates if the model should be timestamped.
11
     *
12
     * @var bool
13
     */
14
    public $timestamps = true;
15
 
16
    /**
17
     * The list of models classes that have timestamps temporarily disabled.
18
     *
19
     * @var array
20
     */
21
    protected static $ignoreTimestampsOn = [];
22
 
23
    /**
24
     * Update the model's update timestamp.
25
     *
26
     * @param  string|null  $attribute
27
     * @return bool
28
     */
29
    public function touch($attribute = null)
30
    {
31
        if ($attribute) {
32
            $this->$attribute = $this->freshTimestamp();
33
 
34
            return $this->save();
35
        }
36
 
37
        if (! $this->usesTimestamps()) {
38
            return false;
39
        }
40
 
41
        $this->updateTimestamps();
42
 
43
        return $this->save();
44
    }
45
 
46
    /**
47
     * Update the model's update timestamp without raising any events.
48
     *
49
     * @param  string|null  $attribute
50
     * @return bool
51
     */
52
    public function touchQuietly($attribute = null)
53
    {
54
        return static::withoutEvents(fn () => $this->touch($attribute));
55
    }
56
 
57
    /**
58
     * Update the creation and update timestamps.
59
     *
60
     * @return $this
61
     */
62
    public function updateTimestamps()
63
    {
64
        $time = $this->freshTimestamp();
65
 
66
        $updatedAtColumn = $this->getUpdatedAtColumn();
67
 
68
        if (! is_null($updatedAtColumn) && ! $this->isDirty($updatedAtColumn)) {
69
            $this->setUpdatedAt($time);
70
        }
71
 
72
        $createdAtColumn = $this->getCreatedAtColumn();
73
 
74
        if (! $this->exists && ! is_null($createdAtColumn) && ! $this->isDirty($createdAtColumn)) {
75
            $this->setCreatedAt($time);
76
        }
77
 
78
        return $this;
79
    }
80
 
81
    /**
82
     * Set the value of the "created at" attribute.
83
     *
84
     * @param  mixed  $value
85
     * @return $this
86
     */
87
    public function setCreatedAt($value)
88
    {
89
        $this->{$this->getCreatedAtColumn()} = $value;
90
 
91
        return $this;
92
    }
93
 
94
    /**
95
     * Set the value of the "updated at" attribute.
96
     *
97
     * @param  mixed  $value
98
     * @return $this
99
     */
100
    public function setUpdatedAt($value)
101
    {
102
        $this->{$this->getUpdatedAtColumn()} = $value;
103
 
104
        return $this;
105
    }
106
 
107
    /**
108
     * Get a fresh timestamp for the model.
109
     *
110
     * @return \Illuminate\Support\Carbon
111
     */
112
    public function freshTimestamp()
113
    {
114
        return Date::now();
115
    }
116
 
117
    /**
118
     * Get a fresh timestamp for the model.
119
     *
120
     * @return string
121
     */
122
    public function freshTimestampString()
123
    {
124
        return $this->fromDateTime($this->freshTimestamp());
125
    }
126
 
127
    /**
128
     * Determine if the model uses timestamps.
129
     *
130
     * @return bool
131
     */
132
    public function usesTimestamps()
133
    {
134
        return $this->timestamps && ! static::isIgnoringTimestamps($this::class);
135
    }
136
 
137
    /**
138
     * Get the name of the "created at" column.
139
     *
140
     * @return string|null
141
     */
142
    public function getCreatedAtColumn()
143
    {
144
        return static::CREATED_AT;
145
    }
146
 
147
    /**
148
     * Get the name of the "updated at" column.
149
     *
150
     * @return string|null
151
     */
152
    public function getUpdatedAtColumn()
153
    {
154
        return static::UPDATED_AT;
155
    }
156
 
157
    /**
158
     * Get the fully qualified "created at" column.
159
     *
160
     * @return string|null
161
     */
162
    public function getQualifiedCreatedAtColumn()
163
    {
164
        return $this->qualifyColumn($this->getCreatedAtColumn());
165
    }
166
 
167
    /**
168
     * Get the fully qualified "updated at" column.
169
     *
170
     * @return string|null
171
     */
172
    public function getQualifiedUpdatedAtColumn()
173
    {
174
        return $this->qualifyColumn($this->getUpdatedAtColumn());
175
    }
176
 
177
    /**
178
     * Disable timestamps for the current class during the given callback scope.
179
     *
180
     * @param  callable  $callback
181
     * @return mixed
182
     */
183
    public static function withoutTimestamps(callable $callback)
184
    {
185
        return static::withoutTimestampsOn([static::class], $callback);
186
    }
187
 
188
    /**
189
     * Disable timestamps for the given model classes during the given callback scope.
190
     *
191
     * @param  array  $models
192
     * @param  callable  $callback
193
     * @return mixed
194
     */
195
    public static function withoutTimestampsOn($models, $callback)
196
    {
197
        static::$ignoreTimestampsOn = array_values(array_merge(static::$ignoreTimestampsOn, $models));
198
 
199
        try {
200
            return $callback();
201
        } finally {
202
            static::$ignoreTimestampsOn = array_values(array_diff(static::$ignoreTimestampsOn, $models));
203
        }
204
    }
205
 
206
    /**
207
     * Determine if the given model is ignoring timestamps / touches.
208
     *
209
     * @param  string|null  $class
210
     * @return bool
211
     */
212
    public static function isIgnoringTimestamps($class = null)
213
    {
214
        $class ??= static::class;
215
 
216
        foreach (static::$ignoreTimestampsOn as $ignoredClass) {
217
            if ($class === $ignoredClass || is_subclass_of($class, $ignoredClass)) {
218
                return true;
219
            }
220
        }
221
 
222
        return false;
223
    }
224
}