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\Broadcasting;
4
 
5
use Illuminate\Bus\Queueable;
6
use Illuminate\Contracts\Broadcasting\Factory as BroadcastingFactory;
7
use Illuminate\Contracts\Queue\ShouldQueue;
8
use Illuminate\Contracts\Support\Arrayable;
9
use Illuminate\Support\Arr;
10
use ReflectionClass;
11
use ReflectionProperty;
12
 
13
class BroadcastEvent implements ShouldQueue
14
{
15
    use Queueable;
16
 
17
    /**
18
     * The event instance.
19
     *
20
     * @var mixed
21
     */
22
    public $event;
23
 
24
    /**
25
     * The number of times the job may be attempted.
26
     *
27
     * @var int
28
     */
29
    public $tries;
30
 
31
    /**
32
     * The number of seconds the job can run before timing out.
33
     *
34
     * @var int
35
     */
36
    public $timeout;
37
 
38
    /**
39
     * The number of seconds to wait before retrying the job when encountering an uncaught exception.
40
     *
41
     * @var int
42
     */
43
    public $backoff;
44
 
45
    /**
46
     * Create a new job handler instance.
47
     *
48
     * @param  mixed  $event
49
     * @return void
50
     */
51
    public function __construct($event)
52
    {
53
        $this->event = $event;
54
        $this->tries = property_exists($event, 'tries') ? $event->tries : null;
55
        $this->timeout = property_exists($event, 'timeout') ? $event->timeout : null;
56
        $this->backoff = property_exists($event, 'backoff') ? $event->backoff : null;
57
        $this->afterCommit = property_exists($event, 'afterCommit') ? $event->afterCommit : null;
58
    }
59
 
60
    /**
61
     * Handle the queued job.
62
     *
63
     * @param  \Illuminate\Contracts\Broadcasting\Factory  $manager
64
     * @return void
65
     */
66
    public function handle(BroadcastingFactory $manager)
67
    {
68
        $name = method_exists($this->event, 'broadcastAs')
69
                ? $this->event->broadcastAs() : get_class($this->event);
70
 
71
        $channels = Arr::wrap($this->event->broadcastOn());
72
 
73
        if (empty($channels)) {
74
            return;
75
        }
76
 
77
        $connections = method_exists($this->event, 'broadcastConnections')
78
                            ? $this->event->broadcastConnections()
79
                            : [null];
80
 
81
        $payload = $this->getPayloadFromEvent($this->event);
82
 
83
        foreach ($connections as $connection) {
84
            $manager->connection($connection)->broadcast(
85
                $channels, $name, $payload
86
            );
87
        }
88
    }
89
 
90
    /**
91
     * Get the payload for the given event.
92
     *
93
     * @param  mixed  $event
94
     * @return array
95
     */
96
    protected function getPayloadFromEvent($event)
97
    {
98
        if (method_exists($event, 'broadcastWith') &&
99
            ! is_null($payload = $event->broadcastWith())) {
100
            return array_merge($payload, ['socket' => data_get($event, 'socket')]);
101
        }
102
 
103
        $payload = [];
104
 
105
        foreach ((new ReflectionClass($event))->getProperties(ReflectionProperty::IS_PUBLIC) as $property) {
106
            $payload[$property->getName()] = $this->formatProperty($property->getValue($event));
107
        }
108
 
109
        unset($payload['broadcastQueue']);
110
 
111
        return $payload;
112
    }
113
 
114
    /**
115
     * Format the given value for a property.
116
     *
117
     * @param  mixed  $value
118
     * @return mixed
119
     */
120
    protected function formatProperty($value)
121
    {
122
        if ($value instanceof Arrayable) {
123
            return $value->toArray();
124
        }
125
 
126
        return $value;
127
    }
128
 
129
    /**
130
     * Get the display name for the queued job.
131
     *
132
     * @return string
133
     */
134
    public function displayName()
135
    {
136
        return get_class($this->event);
137
    }
138
 
139
    /**
140
     * Prepare the instance for cloning.
141
     *
142
     * @return void
143
     */
144
    public function __clone()
145
    {
146
        $this->event = clone $this->event;
147
    }
148
}