This repository has been archived by the owner on Aug 9, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(broker): add generic broker bus, transporter and handler
Signed-off-by: Domingo Oropeza <doropeza@teclib.com>
- Loading branch information
Showing
14 changed files
with
810 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
<?php | ||
/** | ||
* LICENSE | ||
* | ||
* Copyright © 2016-2018 Teclib' | ||
* Copyright © 2010-2018 by the FusionInventory Development Team. | ||
* | ||
* This file is part of Flyve MDM Plugin for GLPI. | ||
* | ||
* Flyve MDM Plugin for GLPI is a subproject of Flyve MDM. Flyve MDM is a mobile | ||
* device management software. | ||
* | ||
* Flyve MDM Plugin for GLPI is free software: you can redistribute it and/or | ||
* modify it under the terms of the GNU Affero General Public License as published | ||
* by the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* Flyve MDM Plugin for GLPI is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with Flyve MDM Plugin for GLPI. If not, see http://www.gnu.org/licenses/. | ||
* ------------------------------------------------------------------------------ | ||
* @author Domingo Oropeza | ||
* @copyright Copyright © 2018 Teclib | ||
* @license AGPLv3+ http://www.gnu.org/licenses/agpl.txt | ||
* @link https://github.com/flyve-mdm/glpi-plugin | ||
* @link https://flyve-mdm.com/ | ||
* ------------------------------------------------------------------------------ | ||
*/ | ||
|
||
namespace GlpiPlugin\Flyvemdm\Broker; | ||
|
||
use GlpiPlugin\Flyvemdm\Interfaces\BrokerEnvelopeAwareInterface; | ||
|
||
if (!defined('GLPI_ROOT')) { | ||
die("Sorry. You can't access this file directly"); | ||
} | ||
|
||
class BrokerBus { | ||
|
||
private $middlewareHandlers; | ||
/** | ||
* @var GlpiPlugin\Flyvemdm\Interfaces\BrokerMiddlewareInterface[]|null | ||
*/ | ||
private $indexedMiddlewareHandlers; | ||
|
||
/** | ||
* @param GlpiPlugin\Flyvemdm\Interfaces\BrokerMiddlewareInterface[]|iterable $middlewareHandlers | ||
*/ | ||
public function __construct($middlewareHandlers = []) { | ||
$this->middlewareHandlers = $middlewareHandlers; | ||
} | ||
|
||
/** | ||
* Dispatches the given message. | ||
* | ||
* The bus can return a value coming from handlers, but is not required to do so. | ||
* | ||
* @param object|BrokerEnvelope $message The message or the message pre-wrapped in an envelope | ||
* | ||
* @return mixed | ||
*/ | ||
public function dispatch($message) { | ||
if (!\is_object($message)) { | ||
throw new \InvalidArgumentException(sprintf( | ||
__('Invalid type for message argument. Expected object, but got "%s".', 'flyvemdm'), | ||
\gettype($message))); | ||
} | ||
return \call_user_func($this->callableForNextMiddleware(0, | ||
BrokerEnvelope::wrap($message)), $message); | ||
} | ||
|
||
private function callableForNextMiddleware( | ||
$index, | ||
BrokerEnvelope $currentEnvelope | ||
) { | ||
if (null === $this->indexedMiddlewareHandlers) { | ||
$this->indexedMiddlewareHandlers = \is_array($this->middlewareHandlers) ? array_values($this->middlewareHandlers) : iterator_to_array($this->middlewareHandlers, | ||
false); | ||
} | ||
if (!isset($this->indexedMiddlewareHandlers[$index])) { | ||
return function () { | ||
}; | ||
} | ||
$middleware = $this->indexedMiddlewareHandlers[$index]; | ||
return function ($message) use ($middleware, $index, $currentEnvelope) { | ||
if ($message instanceof BrokerEnvelope) { | ||
$currentEnvelope = $message; | ||
} else { | ||
$message = $currentEnvelope->withMessage($message); | ||
} | ||
if (!$middleware instanceof BrokerEnvelopeAwareInterface) { | ||
// Do not provide the envelope if the middleware cannot read it: | ||
$message = $message->getMessage(); | ||
} | ||
return $middleware->handle($message, | ||
$this->callableForNextMiddleware($index + 1, $currentEnvelope)); | ||
}; | ||
} | ||
} |
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,100 @@ | ||
<?php | ||
/** | ||
* LICENSE | ||
* | ||
* Copyright © 2016-2018 Teclib' | ||
* Copyright © 2010-2018 by the FusionInventory Development Team. | ||
* | ||
* This file is part of Flyve MDM Plugin for GLPI. | ||
* | ||
* Flyve MDM Plugin for GLPI is a subproject of Flyve MDM. Flyve MDM is a mobile | ||
* device management software. | ||
* | ||
* Flyve MDM Plugin for GLPI is free software: you can redistribute it and/or | ||
* modify it under the terms of the GNU Affero General Public License as published | ||
* by the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* Flyve MDM Plugin for GLPI is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with Flyve MDM Plugin for GLPI. If not, see http://www.gnu.org/licenses/. | ||
* ------------------------------------------------------------------------------ | ||
* @author Domingo Oropeza | ||
* @copyright Copyright © 2018 Teclib | ||
* @license AGPLv3+ http://www.gnu.org/licenses/agpl.txt | ||
* @link https://github.com/flyve-mdm/glpi-plugin | ||
* @link https://flyve-mdm.com/ | ||
* ------------------------------------------------------------------------------ | ||
*/ | ||
|
||
namespace GlpiPlugin\Flyvemdm\Broker; | ||
|
||
use GlpiPlugin\Flyvemdm\Interfaces\BrokerEnvelopeItemInterface; | ||
|
||
if (!defined('GLPI_ROOT')) { | ||
die("Sorry. You can't access this file directly"); | ||
} | ||
|
||
final class BrokerEnvelope { | ||
|
||
private $items = []; | ||
private $message; | ||
|
||
/** | ||
* @param object $message | ||
* @param BrokerEnvelopeItemInterface[] $items | ||
*/ | ||
public function __construct($message, array $items = []) { | ||
$this->message = $message; | ||
foreach ($items as $item) { | ||
$this->items[\get_class($item)] = $item; | ||
} | ||
} | ||
|
||
/** | ||
* Wrap a message into an envelope if not already wrapped. | ||
* | ||
* @param BrokerEnvelope|object $message | ||
* @return object|BrokerEnvelope | ||
*/ | ||
public static function wrap($message) { | ||
return $message instanceof self ? $message : new self($message); | ||
} | ||
|
||
/** | ||
* new Envelope instance with additional item | ||
* @param BrokerEnvelopeItemInterface $item | ||
* @return BrokerEnvelope | ||
*/ | ||
public function with(BrokerEnvelopeItemInterface $item) { | ||
$cloned = clone $this; | ||
$cloned->items[\get_class($item)] = $item; | ||
return $cloned; | ||
} | ||
|
||
public function withMessage($message) { | ||
$cloned = clone $this; | ||
$cloned->message = $message; | ||
return $cloned; | ||
} | ||
|
||
public function get($itemFqcn) { | ||
return isset($this->items[$itemFqcn]) ? $this->items[$itemFqcn] : null; | ||
} | ||
|
||
/** | ||
* @return BrokerEnvelopeItemInterface[] indexed by fqcn | ||
*/ | ||
public function all() { | ||
return $this->items; | ||
} | ||
|
||
/** | ||
* @return object The original message contained in the envelope | ||
*/ | ||
public function getMessage() { | ||
return $this->message; | ||
} | ||
} |
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,73 @@ | ||
<?php | ||
/** | ||
* LICENSE | ||
* | ||
* Copyright © 2016-2018 Teclib' | ||
* Copyright © 2010-2018 by the FusionInventory Development Team. | ||
* | ||
* This file is part of Flyve MDM Plugin for GLPI. | ||
* | ||
* Flyve MDM Plugin for GLPI is a subproject of Flyve MDM. Flyve MDM is a mobile | ||
* device management software. | ||
* | ||
* Flyve MDM Plugin for GLPI is free software: you can redistribute it and/or | ||
* modify it under the terms of the GNU Affero General Public License as published | ||
* by the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* Flyve MDM Plugin for GLPI is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with Flyve MDM Plugin for GLPI. If not, see http://www.gnu.org/licenses/. | ||
* ------------------------------------------------------------------------------ | ||
* @author Domingo Oropeza | ||
* @copyright Copyright © 2018 Teclib | ||
* @license AGPLv3+ http://www.gnu.org/licenses/agpl.txt | ||
* @link https://github.com/flyve-mdm/glpi-plugin | ||
* @link https://flyve-mdm.com/ | ||
* ------------------------------------------------------------------------------ | ||
*/ | ||
|
||
namespace GlpiPlugin\Flyvemdm\Broker; | ||
|
||
if (!defined('GLPI_ROOT')) { | ||
die("Sorry. You can't access this file directly"); | ||
} | ||
|
||
|
||
class BrokerHandlerLocator { | ||
/** | ||
* @param $message | ||
* @return callable|null | ||
* @throws Exception | ||
*/ | ||
public function resolve($message) { | ||
$class = \get_class($message); | ||
if ($handler = $this->getHandler($class)) { | ||
return $handler; | ||
} | ||
foreach (class_implements($class, false) as $interface) { | ||
if ($handler = $this->getHandler($interface)) { | ||
return $handler; | ||
} | ||
} | ||
foreach (class_parents($class, false) as $parent) { | ||
if ($handler = $this->getHandler($parent)) { | ||
return $handler; | ||
} | ||
} | ||
throw new Exception(sprintf(__('No handler for message "%s".', 'flyvemdm'), $class)); | ||
} | ||
|
||
/** | ||
* @param $class | ||
* @return callable|null | ||
*/ | ||
protected function getHandler($class) { | ||
if (class_exists($class)) { | ||
return new $class; | ||
} | ||
return null; | ||
} | ||
} |
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,49 @@ | ||
<?php | ||
/** | ||
* LICENSE | ||
* | ||
* Copyright © 2016-2018 Teclib' | ||
* Copyright © 2010-2018 by the FusionInventory Development Team. | ||
* | ||
* This file is part of Flyve MDM Plugin for GLPI. | ||
* | ||
* Flyve MDM Plugin for GLPI is a subproject of Flyve MDM. Flyve MDM is a mobile | ||
* device management software. | ||
* | ||
* Flyve MDM Plugin for GLPI is free software: you can redistribute it and/or | ||
* modify it under the terms of the GNU Affero General Public License as published | ||
* by the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* Flyve MDM Plugin for GLPI is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with Flyve MDM Plugin for GLPI. If not, see http://www.gnu.org/licenses/. | ||
* ------------------------------------------------------------------------------ | ||
* @author Thierry Bugier | ||
* @copyright Copyright © 2018 Teclib | ||
* @license AGPLv3+ http://www.gnu.org/licenses/agpl.txt | ||
* @link https://github.com/flyve-mdm/glpi-plugin | ||
* @link https://flyve-mdm.com/ | ||
* ------------------------------------------------------------------------------ | ||
*/ | ||
|
||
namespace GlpiPlugin\Flyvemdm\Broker; | ||
|
||
if (!defined('GLPI_ROOT')) { | ||
die("Sorry. You can't access this file directly"); | ||
} | ||
|
||
class BrokerMessage { | ||
|
||
private $message; | ||
|
||
public function __construct($message) { | ||
$this->message = $message; | ||
} | ||
|
||
public function getMessage() { | ||
return $this->message; | ||
} | ||
} |
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,46 @@ | ||
<?php | ||
/** | ||
* LICENSE | ||
* | ||
* Copyright © 2016-2018 Teclib' | ||
* Copyright © 2010-2018 by the FusionInventory Development Team. | ||
* | ||
* This file is part of Flyve MDM Plugin for GLPI. | ||
* | ||
* Flyve MDM Plugin for GLPI is a subproject of Flyve MDM. Flyve MDM is a mobile | ||
* device management software. | ||
* | ||
* Flyve MDM Plugin for GLPI is free software: you can redistribute it and/or | ||
* modify it under the terms of the GNU Affero General Public License as published | ||
* by the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* Flyve MDM Plugin for GLPI is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with Flyve MDM Plugin for GLPI. If not, see http://www.gnu.org/licenses/. | ||
* ------------------------------------------------------------------------------ | ||
* @author Thierry Bugier | ||
* @copyright Copyright © 2018 Teclib | ||
* @license AGPLv3+ http://www.gnu.org/licenses/agpl.txt | ||
* @link https://github.com/flyve-mdm/glpi-plugin | ||
* @link https://flyve-mdm.com/ | ||
* ------------------------------------------------------------------------------ | ||
*/ | ||
|
||
namespace GlpiPlugin\Flyvemdm\Broker; | ||
|
||
use GlpiPlugin\Flyvemdm\Interfaces\BrokerEnvelopeItemInterface; | ||
|
||
if (!defined('GLPI_ROOT')) { | ||
die("Sorry. You can't access this file directly"); | ||
} | ||
|
||
/** | ||
* Marker config for a received message. | ||
* | ||
* This is mainly used by the middleware to identify a message should not be sent if it was just received. | ||
*/ | ||
final class BrokerReceivedMessage implements BrokerEnvelopeItemInterface { | ||
} |
Oops, something went wrong.