Subversion-Projekte lars-tiefland.laravel_shop

Revision

Zur aktuellen Revision | Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Illuminate\Console\Scheduling;
4
 
5
use Illuminate\Contracts\Container\Container;
6
use Illuminate\Support\Reflector;
7
use InvalidArgumentException;
8
use LogicException;
9
use RuntimeException;
10
use Throwable;
11
 
12
class CallbackEvent extends Event
13
{
14
    /**
15
     * The callback to call.
16
     *
17
     * @var string
18
     */
19
    protected $callback;
20
 
21
    /**
22
     * The parameters to pass to the method.
23
     *
24
     * @var array
25
     */
26
    protected $parameters;
27
 
28
    /**
29
     * The result of the callback's execution.
30
     *
31
     * @var mixed
32
     */
33
    protected $result;
34
 
35
    /**
36
     * The exception that was thrown when calling the callback, if any.
37
     *
38
     * @var \Throwable|null
39
     */
40
    protected $exception;
41
 
42
    /**
43
     * Create a new event instance.
44
     *
45
     * @param  \Illuminate\Console\Scheduling\EventMutex  $mutex
46
     * @param  string|callable  $callback
47
     * @param  array  $parameters
48
     * @param  \DateTimeZone|string|null  $timezone
49
     * @return void
50
     *
51
     * @throws \InvalidArgumentException
52
     */
53
    public function __construct(EventMutex $mutex, $callback, array $parameters = [], $timezone = null)
54
    {
55
        if (! is_string($callback) && ! Reflector::isCallable($callback)) {
56
            throw new InvalidArgumentException(
57
                'Invalid scheduled callback event. Must be a string or callable.'
58
            );
59
        }
60
 
61
        $this->mutex = $mutex;
62
        $this->callback = $callback;
63
        $this->parameters = $parameters;
64
        $this->timezone = $timezone;
65
    }
66
 
67
    /**
68
     * Run the callback event.
69
     *
70
     * @param  \Illuminate\Contracts\Container\Container  $container
71
     * @return mixed
72
     *
73
     * @throws \Throwable
74
     */
75
    public function run(Container $container)
76
    {
77
        parent::run($container);
78
 
79
        if ($this->exception) {
80
            throw $this->exception;
81
        }
82
 
83
        return $this->result;
84
    }
85
 
86
    /**
87
     * Determine if the event should skip because another process is overlapping.
88
     *
89
     * @return bool
90
     */
91
    public function shouldSkipDueToOverlapping()
92
    {
93
        return $this->description && parent::shouldSkipDueToOverlapping();
94
    }
95
 
96
    /**
97
     * Indicate that the callback should run in the background.
98
     *
99
     * @return void
100
     *
101
     * @throws \RuntimeException
102
     */
103
    public function runInBackground()
104
    {
105
        throw new RuntimeException('Scheduled closures can not be run in the background.');
106
    }
107
 
108
    /**
109
     * Run the callback.
110
     *
111
     * @param  \Illuminate\Contracts\Container\Container  $container
112
     * @return int
113
     */
114
    protected function execute($container)
115
    {
116
        try {
117
            $this->result = is_object($this->callback)
118
                ? $container->call([$this->callback, '__invoke'], $this->parameters)
119
                : $container->call($this->callback, $this->parameters);
120
 
121
            return $this->result === false ? 1 : 0;
122
        } catch (Throwable $e) {
123
            $this->exception = $e;
124
 
125
            return 1;
126
        }
127
    }
128
 
129
    /**
130
     * Do not allow the event to overlap each other.
131
     *
132
     * @param  int  $expiresAt
133
     * @return $this
134
     *
135
     * @throws \LogicException
136
     */
137
    public function withoutOverlapping($expiresAt = 1440)
138
    {
139
        if (! isset($this->description)) {
140
            throw new LogicException(
141
                "A scheduled event name is required to prevent overlapping. Use the 'name' method before 'withoutOverlapping'."
142
            );
143
        }
144
 
145
        return parent::withoutOverlapping($expiresAt);
146
    }
147
 
148
    /**
149
     * Allow the event to only run on one server for each cron expression.
150
     *
151
     * @return $this
152
     *
153
     * @throws \LogicException
154
     */
155
    public function onOneServer()
156
    {
157
        if (! isset($this->description)) {
158
            throw new LogicException(
159
                "A scheduled event name is required to only run on one server. Use the 'name' method before 'onOneServer'."
160
            );
161
        }
162
 
163
        return parent::onOneServer();
164
    }
165
 
166
    /**
167
     * Get the summary of the event for display.
168
     *
169
     * @return string
170
     */
171
    public function getSummaryForDisplay()
172
    {
173
        if (is_string($this->description)) {
174
            return $this->description;
175
        }
176
 
177
        return is_string($this->callback) ? $this->callback : 'Callback';
178
    }
179
 
180
    /**
181
     * Get the mutex name for the scheduled command.
182
     *
183
     * @return string
184
     */
185
    public function mutexName()
186
    {
187
        return 'framework/schedule-'.sha1($this->description ?? '');
188
    }
189
 
190
    /**
191
     * Clear the mutex for the event.
192
     *
193
     * @return void
194
     */
195
    protected function removeMutex()
196
    {
197
        if ($this->description) {
198
            parent::removeMutex();
199
        }
200
    }
201
}