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\Foundation\Bus;
4
 
5
use Illuminate\Bus\UniqueLock;
6
use Illuminate\Container\Container;
7
use Illuminate\Contracts\Bus\Dispatcher;
8
use Illuminate\Contracts\Cache\Repository as Cache;
9
use Illuminate\Contracts\Queue\ShouldBeUnique;
10
 
11
class PendingDispatch
12
{
13
    /**
14
     * The job.
15
     *
16
     * @var mixed
17
     */
18
    protected $job;
19
 
20
    /**
21
     * Indicates if the job should be dispatched immediately after sending the response.
22
     *
23
     * @var bool
24
     */
25
    protected $afterResponse = false;
26
 
27
    /**
28
     * Create a new pending job dispatch.
29
     *
30
     * @param  mixed  $job
31
     * @return void
32
     */
33
    public function __construct($job)
34
    {
35
        $this->job = $job;
36
    }
37
 
38
    /**
39
     * Set the desired connection for the job.
40
     *
41
     * @param  string|null  $connection
42
     * @return $this
43
     */
44
    public function onConnection($connection)
45
    {
46
        $this->job->onConnection($connection);
47
 
48
        return $this;
49
    }
50
 
51
    /**
52
     * Set the desired queue for the job.
53
     *
54
     * @param  string|null  $queue
55
     * @return $this
56
     */
57
    public function onQueue($queue)
58
    {
59
        $this->job->onQueue($queue);
60
 
61
        return $this;
62
    }
63
 
64
    /**
65
     * Set the desired connection for the chain.
66
     *
67
     * @param  string|null  $connection
68
     * @return $this
69
     */
70
    public function allOnConnection($connection)
71
    {
72
        $this->job->allOnConnection($connection);
73
 
74
        return $this;
75
    }
76
 
77
    /**
78
     * Set the desired queue for the chain.
79
     *
80
     * @param  string|null  $queue
81
     * @return $this
82
     */
83
    public function allOnQueue($queue)
84
    {
85
        $this->job->allOnQueue($queue);
86
 
87
        return $this;
88
    }
89
 
90
    /**
91
     * Set the desired delay in seconds for the job.
92
     *
93
     * @param  \DateTimeInterface|\DateInterval|int|null  $delay
94
     * @return $this
95
     */
96
    public function delay($delay)
97
    {
98
        $this->job->delay($delay);
99
 
100
        return $this;
101
    }
102
 
103
    /**
104
     * Indicate that the job should be dispatched after all database transactions have committed.
105
     *
106
     * @return $this
107
     */
108
    public function afterCommit()
109
    {
110
        $this->job->afterCommit();
111
 
112
        return $this;
113
    }
114
 
115
    /**
116
     * Indicate that the job should not wait until database transactions have been committed before dispatching.
117
     *
118
     * @return $this
119
     */
120
    public function beforeCommit()
121
    {
122
        $this->job->beforeCommit();
123
 
124
        return $this;
125
    }
126
 
127
    /**
128
     * Set the jobs that should run if this job is successful.
129
     *
130
     * @param  array  $chain
131
     * @return $this
132
     */
133
    public function chain($chain)
134
    {
135
        $this->job->chain($chain);
136
 
137
        return $this;
138
    }
139
 
140
    /**
141
     * Indicate that the job should be dispatched after the response is sent to the browser.
142
     *
143
     * @return $this
144
     */
145
    public function afterResponse()
146
    {
147
        $this->afterResponse = true;
148
 
149
        return $this;
150
    }
151
 
152
    /**
153
     * Determine if the job should be dispatched.
154
     *
155
     * @return bool
156
     */
157
    protected function shouldDispatch()
158
    {
159
        if (! $this->job instanceof ShouldBeUnique) {
160
            return true;
161
        }
162
 
163
        return (new UniqueLock(Container::getInstance()->make(Cache::class)))
164
                    ->acquire($this->job);
165
    }
166
 
167
    /**
168
     * Dynamically proxy methods to the underlying job.
169
     *
170
     * @param  string  $method
171
     * @param  array  $parameters
172
     * @return $this
173
     */
174
    public function __call($method, $parameters)
175
    {
176
        $this->job->{$method}(...$parameters);
177
 
178
        return $this;
179
    }
180
 
181
    /**
182
     * Handle the object's destruction.
183
     *
184
     * @return void
185
     */
186
    public function __destruct()
187
    {
188
        if (! $this->shouldDispatch()) {
189
            return;
190
        } elseif ($this->afterResponse) {
191
            app(Dispatcher::class)->dispatchAfterResponse($this->job);
192
        } else {
193
            app(Dispatcher::class)->dispatch($this->job);
194
        }
195
    }
196
}