Skip to content

Commit

Permalink
Merge pull request #3 from ipublikuj/symfonyevents
Browse files Browse the repository at this point in the history
Task: Added support for symfony events
  • Loading branch information
akadlec authored Nov 14, 2019
2 parents 958a853 + 776d036 commit 6386e92
Show file tree
Hide file tree
Showing 18 changed files with 992 additions and 6 deletions.
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@

"tracy/tracy" : "~2.4",

"contributte/event-dispatcher": "~0.5",
"contributte/console": "~0.6",

"pds/skeleton" : "~1.0"
},

Expand Down
107 changes: 102 additions & 5 deletions src/IPub/MQTTClient/DI/MQTTClientExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,16 @@

use BinSoul\Net\Mqtt;

use Symfony\Component\EventDispatcher;

use React;

use Psr\Log;

use IPub\MQTTClient;
use IPub\MQTTClient\Client;
use IPub\MQTTClient\Commands;
use IPub\MQTTClient\Events;
use IPub\MQTTClient\Logger;

/**
Expand All @@ -49,7 +52,7 @@ final class MQTTClientExtension extends DI\CompilerExtension
* @var array
*/
private $defaults = [
'broker' => [
'broker' => [
'httpHost' => NULL,
'port' => 1883,
'address' => NULL,
Expand All @@ -62,16 +65,17 @@ final class MQTTClientExtension extends DI\CompilerExtension
'sslSettings' => [],
],
],
'connection' => [
'connection' => [
'username' => '',
'password' => '',
'clientID' => '',
'keepAlive' => 60,
'protocol' => 4,
'clean' => TRUE,
],
'loop' => NULL,
'console' => FALSE,
'loop' => NULL,
'console' => FALSE,
'symfonyEvets' => FALSE,
];

/**
Expand Down Expand Up @@ -135,7 +139,7 @@ public function loadConfiguration()
'configuration' => $clientConfiguration,
]);

if ($configuration['console'] === NULL) {
if ($configuration['console'] === TRUE) {
// Define all console commands
$commands = [
'client' => Commands\ClientCommand::class,
Expand All @@ -148,6 +152,99 @@ public function loadConfiguration()
}
}

/**
* {@inheritdoc}
*/
public function beforeCompile()
{
parent::beforeCompile();

// Get container builder
$builder = $this->getContainerBuilder();

// Merge extension default config
$this->setConfig(DI\Config\Helpers::merge($this->config, DI\Helpers::expand($this->defaults, $builder->parameters)));

// Get extension configuration
$configuration = $this->getConfig();

// Get container builder
$builder = $this->getContainerBuilder();

if ($configuration['symfonyEvets'] === TRUE) {
$dispatcher = $builder->getDefinition($builder->getByType(EventDispatcher\EventDispatcherInterface::class));

$client = $builder->getDefinition($builder->getByType(Client\Client::class));
assert($client instanceof DI\ServiceDefinition);

$client->addSetup('?->onStart[] = function() {?->dispatch(new ?(...func_get_args()));}', [
'@self',
$dispatcher,
new Nette\PhpGenerator\PhpLiteral(Events\StartEvent::class),
]);
$client->addSetup('?->onOpen[] = function() {?->dispatch(new ?(...func_get_args()));}', [
'@self',
$dispatcher,
new Nette\PhpGenerator\PhpLiteral(Events\OpenEvent::class),
]);
$client->addSetup('?->onConnect[] = function() {?->dispatch(new ?(...func_get_args()));}', [
'@self',
$dispatcher,
new Nette\PhpGenerator\PhpLiteral(Events\ConnectEvent::class),
]);
$client->addSetup('?->onDisconnect[] = function() {?->dispatch(new ?(...func_get_args()));}', [
'@self',
$dispatcher,
new Nette\PhpGenerator\PhpLiteral(Events\DisconnectEvent::class),
]);
$client->addSetup('?->onClose[] = function() {?->dispatch(new ?(...func_get_args()));}', [
'@self',
$dispatcher,
new Nette\PhpGenerator\PhpLiteral(Events\CloseEvent::class),
]);
$client->addSetup('?->onPing[] = function() {?->dispatch(new ?(...func_get_args()));}', [
'@self',
$dispatcher,
new Nette\PhpGenerator\PhpLiteral(Events\PingEvent::class),
]);
$client->addSetup('?->onPong[] = function() {?->dispatch(new ?(...func_get_args()));}', [
'@self',
$dispatcher,
new Nette\PhpGenerator\PhpLiteral(Events\PongEvent::class),
]);
$client->addSetup('?->onPublish[] = function() {?->dispatch(new ?(...func_get_args()));}', [
'@self',
$dispatcher,
new Nette\PhpGenerator\PhpLiteral(Events\PublishEvent::class),
]);
$client->addSetup('?->onSubscribe[] = function() {?->dispatch(new ?(...func_get_args()));}', [
'@self',
$dispatcher,
new Nette\PhpGenerator\PhpLiteral(Events\SubscribeEvent::class),
]);
$client->addSetup('?->onUnsubscribe[] = function() {?->dispatch(new ?(...func_get_args()));}', [
'@self',
$dispatcher,
new Nette\PhpGenerator\PhpLiteral(Events\UnsubscribeEvent::class),
]);
$client->addSetup('?->onMessage[] = function() {?->dispatch(new ?(...func_get_args()));}', [
'@self',
$dispatcher,
new Nette\PhpGenerator\PhpLiteral(Events\MessageEvent::class),
]);
$client->addSetup('?->onWarning[] = function() {?->dispatch(new ?(...func_get_args()));}', [
'@self',
$dispatcher,
new Nette\PhpGenerator\PhpLiteral(Events\WarningEvent::class),
]);
$client->addSetup('?->onError[] = function() {?->dispatch(new ?(...func_get_args()));}', [
'@self',
$dispatcher,
new Nette\PhpGenerator\PhpLiteral(Events\ErrorEvent::class),
]);
}
}

/**
* @param Nette\Configurator $config
* @param string $extensionName
Expand Down
70 changes: 70 additions & 0 deletions src/IPub/MQTTClient/Events/CloseEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php
/**
* CloseEvent.php
*
* @copyright More in license.md
* @license https://www.ipublikuj.eu
* @author Adam Kadlec <adam.kadlec@ipublikuj.eu>
* @package iPublikuj:MQTTClient!
* @subpackage Events
* @since 1.0.0
*
* @date 14.11.19
*/

namespace IPub\MQTTClient\Events;

use Symfony\Contracts\EventDispatcher;

use BinSoul\Net\Mqtt;

use IPub\MQTTClient\Client;

/**
* Connection close event
*
* @package iPublikuj:MQTTClient!
* @subpackage Events
*
* @author Adam Kadlec <adam.kadlec@ipublikuj.eu>
*/
final class CloseEvent extends EventDispatcher\Event
{
/**
* @var Mqtt\Connection
*/
private $connection;

/**
* @var Client\IClient
*/
private $client;

/**
* @param Mqtt\Connection $connection
* @param Client\IClient $client
*/
public function __construct(
Mqtt\Connection $connection,
Client\IClient $client
) {
$this->connection = $connection;
$this->client = $client;
}

/**
* @return Mqtt\Connection
*/
public function getConnection() : Mqtt\Connection
{
return $this->connection;
}

/**
* @return Client\IClient
*/
public function getClient() : Client\IClient
{
return $this->client;
}
}
70 changes: 70 additions & 0 deletions src/IPub/MQTTClient/Events/ConnectEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php
/**
* ConnectEvent.php
*
* @copyright More in license.md
* @license https://www.ipublikuj.eu
* @author Adam Kadlec <adam.kadlec@ipublikuj.eu>
* @package iPublikuj:MQTTClient!
* @subpackage Events
* @since 1.0.0
*
* @date 14.11.19
*/

namespace IPub\MQTTClient\Events;

use Symfony\Contracts\EventDispatcher;

use BinSoul\Net\Mqtt;

use IPub\MQTTClient\Client;

/**
* Connected event
*
* @package iPublikuj:MQTTClient!
* @subpackage Events
*
* @author Adam Kadlec <adam.kadlec@ipublikuj.eu>
*/
final class ConnectEvent extends EventDispatcher\Event
{
/**
* @var Mqtt\Connection
*/
private $connection;

/**
* @var Client\IClient
*/
private $client;

/**
* @param Mqtt\Connection $connection
* @param Client\IClient $client
*/
public function __construct(
Mqtt\Connection $connection,
Client\IClient $client
) {
$this->connection = $connection;
$this->client = $client;
}

/**
* @return Mqtt\Connection
*/
public function getConnection() : Mqtt\Connection
{
return $this->connection;
}

/**
* @return Client\IClient
*/
public function getClient() : Client\IClient
{
return $this->client;
}
}
70 changes: 70 additions & 0 deletions src/IPub/MQTTClient/Events/DisconnectEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php
/**
* DisconnectEvent.php
*
* @copyright More in license.md
* @license https://www.ipublikuj.eu
* @author Adam Kadlec <adam.kadlec@ipublikuj.eu>
* @package iPublikuj:MQTTClient!
* @subpackage Events
* @since 1.0.0
*
* @date 14.11.19
*/

namespace IPub\MQTTClient\Events;

use Symfony\Contracts\EventDispatcher;

use BinSoul\Net\Mqtt;

use IPub\MQTTClient\Client;

/**
* Disconnected event
*
* @package iPublikuj:MQTTClient!
* @subpackage Events
*
* @author Adam Kadlec <adam.kadlec@ipublikuj.eu>
*/
final class DisconnectEvent extends EventDispatcher\Event
{
/**
* @var Mqtt\Connection
*/
private $connection;

/**
* @var Client\IClient
*/
private $client;

/**
* @param Mqtt\Connection $connection
* @param Client\IClient $client
*/
public function __construct(
Mqtt\Connection $connection,
Client\IClient $client
) {
$this->connection = $connection;
$this->client = $client;
}

/**
* @return Mqtt\Connection
*/
public function getConnection() : Mqtt\Connection
{
return $this->connection;
}

/**
* @return Client\IClient
*/
public function getClient() : Client\IClient
{
return $this->client;
}
}
Loading

0 comments on commit 6386e92

Please sign in to comment.