-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8185661
commit 7618ecf
Showing
16 changed files
with
384 additions
and
67 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,25 @@ | ||
<?php | ||
|
||
namespace Convenia\Pigeon\Events; | ||
|
||
use Illuminate\Foundation\Events\Dispatchable; | ||
use Convenia\Pigeon\Publisher\PublisherContract; | ||
|
||
abstract class BaseEvent | ||
{ | ||
use Dispatchable; | ||
|
||
public PublisherContract $publisher; | ||
public array $userData = []; | ||
public array $userMetaData = []; | ||
|
||
public function __construct( | ||
PublisherContract $publisher, | ||
array $userData = [], | ||
array $userMetaData = [] | ||
) { | ||
$this->publisher = $publisher; | ||
$this->userData = $userData; | ||
$this->userMetaData = $userMetaData; | ||
} | ||
} |
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,20 @@ | ||
<?php | ||
|
||
namespace Convenia\Pigeon\Events; | ||
|
||
use Convenia\Pigeon\Publisher\PublisherContract; | ||
|
||
class DispatchingEvent extends BaseEvent | ||
{ | ||
public string $eventName; | ||
|
||
public function __construct( | ||
PublisherContract $publisher, | ||
string $eventName, | ||
array $userData = [], | ||
array $userMetaData = [] | ||
) { | ||
parent::__construct($publisher, $userData, $userMetaData); | ||
$this->eventName = $eventName; | ||
} | ||
} |
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,20 @@ | ||
<?php | ||
|
||
namespace Convenia\Pigeon\Events; | ||
|
||
use Convenia\Pigeon\Publisher\PublisherContract; | ||
|
||
class EventDispatched extends BaseEvent | ||
{ | ||
public string $eventName; | ||
|
||
public function __construct( | ||
PublisherContract $publisher, | ||
string $eventName, | ||
array $userData = [], | ||
array $userMetaData = [] | ||
) { | ||
parent::__construct($publisher, $userData, $userMetaData); | ||
$this->eventName = $eventName; | ||
} | ||
} |
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,7 @@ | ||
<?php | ||
|
||
namespace Convenia\Pigeon\Events; | ||
|
||
class MessagePublished extends BaseEvent | ||
{ | ||
} |
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,7 @@ | ||
<?php | ||
|
||
namespace Convenia\Pigeon\Events; | ||
|
||
class PublishingMessage extends BaseEvent | ||
{ | ||
} |
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,83 @@ | ||
<?php | ||
|
||
namespace Convenia\Pigeon\Publisher; | ||
|
||
use Convenia\Pigeon\Drivers\DriverContract; | ||
use Illuminate\Foundation\Application; | ||
use Illuminate\Support\Arr; | ||
use Illuminate\Support\Str; | ||
use PhpAmqpLib\Message\AMQPMessage; | ||
use PhpAmqpLib\Wire\AMQPTable; | ||
|
||
trait PublisherConcern | ||
{ | ||
protected $app; | ||
protected $driver; | ||
protected $exchange; | ||
protected $routing; | ||
protected $headers = []; | ||
|
||
public function __construct(Application $app, DriverContract $driver, string $exchange) | ||
{ | ||
$this->app = $app; | ||
$this->driver = $driver; | ||
$this->exchange = $exchange; | ||
} | ||
|
||
public function routing(string $key): PublisherContract | ||
{ | ||
$this->routing = $key; | ||
|
||
return $this; | ||
} | ||
|
||
public function getRoute(): ?string | ||
{ | ||
return $this->routing; | ||
} | ||
|
||
private function makeMessage(array $data, array $properties = []) | ||
{ | ||
return new AMQPMessage( | ||
json_encode($data), | ||
$this->getMessageProps($properties) | ||
); | ||
} | ||
|
||
private function getMessageProps(array $userProps): array | ||
{ | ||
return array_merge([ | ||
'content_type' => 'application/json', | ||
'content_encoding' => 'utf8', | ||
'correlation_id' => Str::uuid(), | ||
'expiration' => 60000000, | ||
'app_id' => $this->app['config']['app_name'], | ||
'application_headers' => new AMQPTable($this->getHeaders()), | ||
], $userProps); | ||
} | ||
|
||
public function header(string $key, $value): PublisherContract | ||
{ | ||
$this->headers = Arr::add($this->headers, $key, $value); | ||
|
||
return $this; | ||
} | ||
|
||
public function getHeaders(): array | ||
{ | ||
$configHeaders = Arr::dot($this->app['config']->get('pigeon.headers')); | ||
$mapped = $this->mapToValues($configHeaders); | ||
|
||
return array_merge($mapped, $this->headers); | ||
} | ||
|
||
protected function mapToValues(array $headers) | ||
{ | ||
$result = []; | ||
foreach ($headers as $key => $value) { | ||
$result[$key] = is_callable($value) ? call_user_func($value) : $value; | ||
} | ||
|
||
return $result; | ||
} | ||
} |
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,21 @@ | ||
<?php | ||
|
||
namespace Convenia\Pigeon\Support\Testing; | ||
|
||
use Convenia\Pigeon\Publisher\PublisherConcern; | ||
use Convenia\Pigeon\Publisher\PublisherContract; | ||
|
||
class FakePublisher implements PublisherContract | ||
{ | ||
use PublisherConcern; | ||
|
||
public function bind(string $queue): PublisherContract | ||
{ | ||
throw new \Exception('Pigeon Fake does not support binding'); | ||
} | ||
|
||
public function publish(array $message, array $properties = [], int $channelId = null) | ||
{ | ||
throw new \Exception('Pigeon Fake does not support publishing'); | ||
} | ||
} |
Oops, something went wrong.