Skip to content

Commit 860b29b

Browse files
committed
Initial run of broadcasting events.
Includes back-end agnostic driver based system for broadcasting events onto various web socket providers or message systems.
1 parent 819e7a3 commit 860b29b

13 files changed

+525
-0
lines changed

build/illuminate-split-faster.sh

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ split()
3030
}
3131

3232
split auth src/Illuminate/Auth:[email protected]:illuminate/auth.git "master 5.0 4.2"
33+
split broadcasting src/Illuminate/Broadcasting:[email protected]:illuminate/broadcasting.git "master"
3334
split bus src/Illuminate/Bus:[email protected]:illuminate/bus.git "master 5.0"
3435
split cache src/Illuminate/Cache:[email protected]:illuminate/cache.git "master 5.0 4.2"
3536
split config src/Illuminate/Config:[email protected]:illuminate/config.git "master 5.0 4.2"

composer.json

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
"replace": {
4646
"illuminate/auth": "self.version",
4747
"illuminate/bus": "self.version",
48+
"illuminate/broadcasting": "self.version",
4849
"illuminate/cache": "self.version",
4950
"illuminate/config": "self.version",
5051
"illuminate/console": "self.version",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php namespace Illuminate\Broadcasting;
2+
3+
use Pusher;
4+
use ReflectionClass;
5+
use Illuminate\Contracts\Queue\Job;
6+
use Illuminate\Contracts\Support\Arrayable;
7+
use Illuminate\Contracts\Broadcasting\Broadcaster;
8+
9+
class BroadcastEvent
10+
{
11+
12+
/**
13+
* The broadcaster implementation.
14+
*
15+
* @var \Illuminate\Contracts\Broadcasting\Broadcaster
16+
*/
17+
protected $broadcaster;
18+
19+
/**
20+
* Create a new job handler instance.
21+
*
22+
* @param \Illuminate\Contracts\Broadcasting\Broadcaster $broadcaster
23+
* @return void
24+
*/
25+
public function __construct(Broadcaster $broadcaster)
26+
{
27+
$this->broadcaster = $broadcaster;
28+
}
29+
30+
/**
31+
* Handle the queued job.
32+
*
33+
* @param \Illuminate\Contracts\Jobs\Job $job
34+
* @param array $data
35+
* @return void
36+
*/
37+
public function fire(Job $job, array $data)
38+
{
39+
$event = unserialize($data['event']);
40+
41+
$this->broadcaster->broadcast(
42+
$event->broadcastOn(), get_class($event), $this->getPayloadFromEvent($event)
43+
);
44+
45+
$job->delete();
46+
}
47+
48+
/**
49+
* Get the payload for the given event.
50+
*
51+
* @param mixed $event
52+
* @return array
53+
*/
54+
protected function getPayloadFromEvent($event)
55+
{
56+
if (method_exists($event, 'broadcastWith')) {
57+
return $event->broadcastWith();
58+
}
59+
60+
$payload = [];
61+
62+
foreach ((new ReflectionClass($event))->getProperties() as $property) {
63+
if ($property->isPublic()) {
64+
$payload[$property->getName()] = $this->formatProperty($property->getValue($event));
65+
}
66+
}
67+
68+
return $payload;
69+
}
70+
71+
/**
72+
* Format the given value for a property.
73+
*
74+
* @param mixed $value
75+
* @return mixed
76+
*/
77+
protected function formatProperty($value)
78+
{
79+
if ($value instanceof Arrayable) {
80+
return $value->toArray();
81+
}
82+
83+
return $value;
84+
}
85+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
<?php namespace Illuminate\Broadcasting;
2+
3+
use Pusher;
4+
use Closure;
5+
use InvalidArgumentException;
6+
use Illuminate\Broadcasting\PusherBroadcaster;
7+
use Illuminate\Contracts\Broadcasting\Factory as FactoryContract;
8+
9+
class BroadcastManager implements FactoryContract
10+
{
11+
12+
/**
13+
* The application instance.
14+
*
15+
* @var \Illuminate\Foundation\Application
16+
*/
17+
protected $app;
18+
19+
/**
20+
* The array of resolved broadcast drivers.
21+
*
22+
* @var array
23+
*/
24+
protected $drivers = [];
25+
26+
/**
27+
* The registered custom driver creators.
28+
*
29+
* @var array
30+
*/
31+
protected $customCreators = [];
32+
33+
/**
34+
* Create a new manager instance.
35+
*
36+
* @param \Illuminate\Foundation\Application $app
37+
* @return void
38+
*/
39+
public function __construct($app)
40+
{
41+
$this->app = $app;
42+
}
43+
44+
/**
45+
* Get a driver instance.
46+
*
47+
* @param string $driver
48+
* @return mixed
49+
*/
50+
public function connection($driver = null)
51+
{
52+
return $this->driver($driver);
53+
}
54+
55+
/**
56+
* Get a driver instance.
57+
*
58+
* @param string $name
59+
* @return mixed
60+
*/
61+
public function driver($name = null)
62+
{
63+
$name = $name ?: $this->getDefaultDriver();
64+
65+
return $this->drivers[$name] = $this->get($name);
66+
}
67+
68+
/**
69+
* Attempt to get the connection from the local cache.
70+
*
71+
* @param string $name
72+
* @return \Illuminate\Contracts\Broadcasting\Broadcaster
73+
*/
74+
protected function get($name)
75+
{
76+
return isset($this->drivers[$name]) ? $this->drivers[$name] : $this->resolve($name);
77+
}
78+
79+
/**
80+
* Resolve the given store.
81+
*
82+
* @param string $name
83+
* @return \Illuminate\Contracts\Broadcasting\Broadcaster
84+
*/
85+
protected function resolve($name)
86+
{
87+
$config = $this->getConfig($name);
88+
89+
if (is_null($config)) {
90+
throw new InvalidArgumentException("Broadcaster [{$name}] is not defined.");
91+
}
92+
93+
if (isset($this->customCreators[$config['driver']])) {
94+
return $this->callCustomCreator($config);
95+
} else {
96+
return $this->{"create".ucfirst($config['driver'])."Driver"}($config);
97+
}
98+
}
99+
100+
/**
101+
* Call a custom driver creator.
102+
*
103+
* @param array $config
104+
* @return mixed
105+
*/
106+
protected function callCustomCreator(array $config)
107+
{
108+
return $this->customCreators[$config['driver']]($this->app, $config);
109+
}
110+
111+
/**
112+
* Create an instance of the driver.
113+
*
114+
* @param array $config
115+
* @return \Illuminate\Contracts\Broadcasting\Broadcaster
116+
*/
117+
protected function createPusherDriver(array $config)
118+
{
119+
return new PusherBroadcaster(
120+
new Pusher($config['key'], $config['secret'], $config['app_id'])
121+
);
122+
}
123+
124+
/**
125+
* Get the connection configuration.
126+
*
127+
* @param string $name
128+
* @return array
129+
*/
130+
protected function getConfig($name)
131+
{
132+
return $this->app['config']["broadcast.connections.{$name}"];
133+
}
134+
135+
/**
136+
* Get the default driver name.
137+
*
138+
* @return string
139+
*/
140+
public function getDefaultDriver()
141+
{
142+
return $this->app['config']['broadcast.default'];
143+
}
144+
145+
/**
146+
* Set the default driver name.
147+
*
148+
* @param string $name
149+
* @return void
150+
*/
151+
public function setDefaultDriver($name)
152+
{
153+
$this->app['config']['broadcast.default'] = $name;
154+
}
155+
156+
/**
157+
* Register a custom driver creator Closure.
158+
*
159+
* @param string $driver
160+
* @param \Closure $callback
161+
* @return $this
162+
*/
163+
public function extend($driver, Closure $callback)
164+
{
165+
$this->customCreators[$driver] = $callback;
166+
167+
return $this;
168+
}
169+
170+
/**
171+
* Dynamically call the default driver instance.
172+
*
173+
* @param string $method
174+
* @param array $parameters
175+
* @return mixed
176+
*/
177+
public function __call($method, $parameters)
178+
{
179+
return call_user_func_array(array($this->driver(), $method), $parameters);
180+
}
181+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php namespace Illuminate\Broadcasting;
2+
3+
use Illuminate\Support\ServiceProvider;
4+
5+
class BroadcastServiceProvider extends ServiceProvider
6+
{
7+
8+
/**
9+
* Indicates if loading of the provider is deferred.
10+
*
11+
* @var bool
12+
*/
13+
protected $defer = true;
14+
15+
/**
16+
* Register the service provider.
17+
*
18+
* @return void
19+
*/
20+
public function register()
21+
{
22+
$this->app->singleton('Illuminate\Broadcasting\BroadcastManager', function ($app) {
23+
return new BroadcastManager($app);
24+
});
25+
26+
$this->app->singleton('Illuminate\Contracts\Broadcasting\Broadcaster', function ($app) {
27+
return $app->make('Illuminate\Broadcasting\BroadcastManager')->connection();
28+
});
29+
30+
$this->app->alias(
31+
'Illuminate\Broadcasting\BroadcastManager', 'Illuminate\Contracts\Broadcasting\Factory'
32+
);
33+
}
34+
35+
/**
36+
* Get the services provided by the provider.
37+
*
38+
* @return array
39+
*/
40+
public function provides()
41+
{
42+
return [
43+
'Illuminate\Broadcasting\BroadcastManager',
44+
'Illuminate\Contracts\Broadcasting\Factory',
45+
'Illuminate\Contracts\Broadcasting\Broadcaster',
46+
];
47+
}
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php namespace Illuminate\Broadcasting;
2+
3+
use Pusher;
4+
use Illuminate\Contracts\Broadcasting\Broadcaster;
5+
6+
class PusherBroadcaster implements Broadcaster
7+
{
8+
9+
/**
10+
* The Pusher SDK instance.
11+
*
12+
* @var \Pusher
13+
*/
14+
protected $pusher;
15+
16+
/**
17+
* Create a new broadcaster instance.
18+
*
19+
* @param Pusher $pusher
20+
* @return void
21+
*/
22+
public function __construct(Pusher $pusher)
23+
{
24+
$this->pusher = $pusher;
25+
}
26+
27+
/**
28+
* {@inheritdoc}
29+
*/
30+
public function broadcast(array $channels, $event, array $payload = array())
31+
{
32+
$this->pusher->trigger($channels, $event, $payload);
33+
}
34+
}

0 commit comments

Comments
 (0)