forked from laravel/framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Includes back-end agnostic driver based system for broadcasting events onto various web socket providers or message systems.
- Loading branch information
1 parent
819e7a3
commit 860b29b
Showing
13 changed files
with
525 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?php namespace Illuminate\Broadcasting; | ||
|
||
use Pusher; | ||
use ReflectionClass; | ||
use Illuminate\Contracts\Queue\Job; | ||
use Illuminate\Contracts\Support\Arrayable; | ||
use Illuminate\Contracts\Broadcasting\Broadcaster; | ||
|
||
class BroadcastEvent | ||
{ | ||
|
||
/** | ||
* The broadcaster implementation. | ||
* | ||
* @var \Illuminate\Contracts\Broadcasting\Broadcaster | ||
*/ | ||
protected $broadcaster; | ||
|
||
/** | ||
* Create a new job handler instance. | ||
* | ||
* @param \Illuminate\Contracts\Broadcasting\Broadcaster $broadcaster | ||
* @return void | ||
*/ | ||
public function __construct(Broadcaster $broadcaster) | ||
{ | ||
$this->broadcaster = $broadcaster; | ||
} | ||
|
||
/** | ||
* Handle the queued job. | ||
* | ||
* @param \Illuminate\Contracts\Jobs\Job $job | ||
* @param array $data | ||
* @return void | ||
*/ | ||
public function fire(Job $job, array $data) | ||
{ | ||
$event = unserialize($data['event']); | ||
|
||
$this->broadcaster->broadcast( | ||
$event->broadcastOn(), get_class($event), $this->getPayloadFromEvent($event) | ||
); | ||
|
||
$job->delete(); | ||
} | ||
|
||
/** | ||
* Get the payload for the given event. | ||
* | ||
* @param mixed $event | ||
* @return array | ||
*/ | ||
protected function getPayloadFromEvent($event) | ||
{ | ||
if (method_exists($event, 'broadcastWith')) { | ||
return $event->broadcastWith(); | ||
} | ||
|
||
$payload = []; | ||
|
||
foreach ((new ReflectionClass($event))->getProperties() as $property) { | ||
if ($property->isPublic()) { | ||
$payload[$property->getName()] = $this->formatProperty($property->getValue($event)); | ||
} | ||
} | ||
|
||
return $payload; | ||
} | ||
|
||
/** | ||
* Format the given value for a property. | ||
* | ||
* @param mixed $value | ||
* @return mixed | ||
*/ | ||
protected function formatProperty($value) | ||
{ | ||
if ($value instanceof Arrayable) { | ||
return $value->toArray(); | ||
} | ||
|
||
return $value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,181 @@ | ||
<?php namespace Illuminate\Broadcasting; | ||
|
||
use Pusher; | ||
use Closure; | ||
use InvalidArgumentException; | ||
use Illuminate\Broadcasting\PusherBroadcaster; | ||
use Illuminate\Contracts\Broadcasting\Factory as FactoryContract; | ||
|
||
class BroadcastManager implements FactoryContract | ||
{ | ||
|
||
/** | ||
* The application instance. | ||
* | ||
* @var \Illuminate\Foundation\Application | ||
*/ | ||
protected $app; | ||
|
||
/** | ||
* The array of resolved broadcast drivers. | ||
* | ||
* @var array | ||
*/ | ||
protected $drivers = []; | ||
|
||
/** | ||
* The registered custom driver creators. | ||
* | ||
* @var array | ||
*/ | ||
protected $customCreators = []; | ||
|
||
/** | ||
* Create a new manager instance. | ||
* | ||
* @param \Illuminate\Foundation\Application $app | ||
* @return void | ||
*/ | ||
public function __construct($app) | ||
{ | ||
$this->app = $app; | ||
} | ||
|
||
/** | ||
* Get a driver instance. | ||
* | ||
* @param string $driver | ||
* @return mixed | ||
*/ | ||
public function connection($driver = null) | ||
{ | ||
return $this->driver($driver); | ||
} | ||
|
||
/** | ||
* Get a driver instance. | ||
* | ||
* @param string $name | ||
* @return mixed | ||
*/ | ||
public function driver($name = null) | ||
{ | ||
$name = $name ?: $this->getDefaultDriver(); | ||
|
||
return $this->drivers[$name] = $this->get($name); | ||
} | ||
|
||
/** | ||
* Attempt to get the connection from the local cache. | ||
* | ||
* @param string $name | ||
* @return \Illuminate\Contracts\Broadcasting\Broadcaster | ||
*/ | ||
protected function get($name) | ||
{ | ||
return isset($this->drivers[$name]) ? $this->drivers[$name] : $this->resolve($name); | ||
} | ||
|
||
/** | ||
* Resolve the given store. | ||
* | ||
* @param string $name | ||
* @return \Illuminate\Contracts\Broadcasting\Broadcaster | ||
*/ | ||
protected function resolve($name) | ||
{ | ||
$config = $this->getConfig($name); | ||
|
||
if (is_null($config)) { | ||
throw new InvalidArgumentException("Broadcaster [{$name}] is not defined."); | ||
} | ||
|
||
if (isset($this->customCreators[$config['driver']])) { | ||
return $this->callCustomCreator($config); | ||
} else { | ||
return $this->{"create".ucfirst($config['driver'])."Driver"}($config); | ||
} | ||
} | ||
|
||
/** | ||
* Call a custom driver creator. | ||
* | ||
* @param array $config | ||
* @return mixed | ||
*/ | ||
protected function callCustomCreator(array $config) | ||
{ | ||
return $this->customCreators[$config['driver']]($this->app, $config); | ||
} | ||
|
||
/** | ||
* Create an instance of the driver. | ||
* | ||
* @param array $config | ||
* @return \Illuminate\Contracts\Broadcasting\Broadcaster | ||
*/ | ||
protected function createPusherDriver(array $config) | ||
{ | ||
return new PusherBroadcaster( | ||
new Pusher($config['key'], $config['secret'], $config['app_id']) | ||
); | ||
} | ||
|
||
/** | ||
* Get the connection configuration. | ||
* | ||
* @param string $name | ||
* @return array | ||
*/ | ||
protected function getConfig($name) | ||
{ | ||
return $this->app['config']["broadcast.connections.{$name}"]; | ||
} | ||
|
||
/** | ||
* Get the default driver name. | ||
* | ||
* @return string | ||
*/ | ||
public function getDefaultDriver() | ||
{ | ||
return $this->app['config']['broadcast.default']; | ||
} | ||
|
||
/** | ||
* Set the default driver name. | ||
* | ||
* @param string $name | ||
* @return void | ||
*/ | ||
public function setDefaultDriver($name) | ||
{ | ||
$this->app['config']['broadcast.default'] = $name; | ||
} | ||
|
||
/** | ||
* Register a custom driver creator Closure. | ||
* | ||
* @param string $driver | ||
* @param \Closure $callback | ||
* @return $this | ||
*/ | ||
public function extend($driver, Closure $callback) | ||
{ | ||
$this->customCreators[$driver] = $callback; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Dynamically call the default driver instance. | ||
* | ||
* @param string $method | ||
* @param array $parameters | ||
* @return mixed | ||
*/ | ||
public function __call($method, $parameters) | ||
{ | ||
return call_user_func_array(array($this->driver(), $method), $parameters); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php namespace Illuminate\Broadcasting; | ||
|
||
use Illuminate\Support\ServiceProvider; | ||
|
||
class BroadcastServiceProvider extends ServiceProvider | ||
{ | ||
|
||
/** | ||
* Indicates if loading of the provider is deferred. | ||
* | ||
* @var bool | ||
*/ | ||
protected $defer = true; | ||
|
||
/** | ||
* Register the service provider. | ||
* | ||
* @return void | ||
*/ | ||
public function register() | ||
{ | ||
$this->app->singleton('Illuminate\Broadcasting\BroadcastManager', function ($app) { | ||
return new BroadcastManager($app); | ||
}); | ||
|
||
$this->app->singleton('Illuminate\Contracts\Broadcasting\Broadcaster', function ($app) { | ||
return $app->make('Illuminate\Broadcasting\BroadcastManager')->connection(); | ||
}); | ||
|
||
$this->app->alias( | ||
'Illuminate\Broadcasting\BroadcastManager', 'Illuminate\Contracts\Broadcasting\Factory' | ||
); | ||
} | ||
|
||
/** | ||
* Get the services provided by the provider. | ||
* | ||
* @return array | ||
*/ | ||
public function provides() | ||
{ | ||
return [ | ||
'Illuminate\Broadcasting\BroadcastManager', | ||
'Illuminate\Contracts\Broadcasting\Factory', | ||
'Illuminate\Contracts\Broadcasting\Broadcaster', | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php namespace Illuminate\Broadcasting; | ||
|
||
use Pusher; | ||
use Illuminate\Contracts\Broadcasting\Broadcaster; | ||
|
||
class PusherBroadcaster implements Broadcaster | ||
{ | ||
|
||
/** | ||
* The Pusher SDK instance. | ||
* | ||
* @var \Pusher | ||
*/ | ||
protected $pusher; | ||
|
||
/** | ||
* Create a new broadcaster instance. | ||
* | ||
* @param Pusher $pusher | ||
* @return void | ||
*/ | ||
public function __construct(Pusher $pusher) | ||
{ | ||
$this->pusher = $pusher; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function broadcast(array $channels, $event, array $payload = array()) | ||
{ | ||
$this->pusher->trigger($channels, $event, $payload); | ||
} | ||
} |
Oops, something went wrong.