Skip to content

Commit

Permalink
feat: fixes for subscriber and publisher semantics (#164)
Browse files Browse the repository at this point in the history
  • Loading branch information
emilianozublena authored Feb 11, 2023
1 parent e131739 commit 02e406b
Show file tree
Hide file tree
Showing 24 changed files with 112 additions and 111 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ template/.phpunit.result.cache
.env
template/.env
.idea/
*.DS_Store
6 changes: 3 additions & 3 deletions filters/all.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ function toPHPType(str) {
case 'date':
return 'DateTime';
case 'time':
return '\DateTime';
return '\\DateTime';
case 'dateTime':
case 'date-time':
return '\DateTime';
return '\\DateTime';
case 'string':
case 'password':
case 'byte':
Expand All @@ -26,7 +26,7 @@ function toPHPType(str) {
case 'double':
return 'float';
default:
return '\stdClass';
return '\\stdClass';
}
}

Expand Down
36 changes: 18 additions & 18 deletions template/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# BrokerAPI
# AsyncAPI

[//]: # "[![Latest Version on Packagist][ico-version]][link-packagist]"

Expand All @@ -13,7 +13,7 @@
[//]: # "[![Total Downloads][ico-downloads]][link-downloads]"


BrokerAPI is a wrapper for Message-driven API's built on top of most used industry plugins such as [PHP AMQP lib](https://packagist.org/packages/php-amqplib/php-amqplib) for RabbitMQ.
AsyncAPI is a wrapper for Message-driven API's built on top of most used industry plugins such as [PHP AMQP lib](https://packagist.org/packages/php-amqplib/php-amqplib) for RabbitMQ.
It is built for usage altogether with [AsyncAPI specs and generators](https://github.com/asyncapi/generator)

## Structure
Expand Down Expand Up @@ -46,12 +46,12 @@ Once you hvae the generator installed, you can try to generate code from any val

Refer to the examples folder for further details on PHP usage
``` bash
$brokerAPI = new BrokerAPI();
$brokerAPI = new AsyncAPI();
$factory = $brokerAPI->init();

/** @var \{{ params.packageName }}\Applications\Producer $producer */
$producer = $factory->createApplication(
PRODUCER_KEY,
/** @var \{{ params.packageName }}\Applications\Subscriber $subscriber */
$subscriber = $factory->createApplication(
SUBSCRIBER_KEY,
[
BROKER_HOST_KEY => 'localhost',
BROKER_USER_KEY => 'guest',
Expand All @@ -68,7 +68,7 @@ $message = $factory->createMessage(
]
);

$producer->requestExampleById($message);
$subscriber->requestExampleById($message);
```

## Change log
Expand All @@ -92,17 +92,17 @@ If you discover any security related issues, please email [email protected] ins

The MIT License (MIT). Please see [License File](../LICENSE.md) for more information.

[ico-version]: https://img.shields.io/packagist/v/GA/BrokerAPI.svg?style=flat-square
[ico-version]: https://img.shields.io/packagist/v/GA/AsyncAPI.svg?style=flat-square
[ico-license]: https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square
[ico-travis]: https://img.shields.io/travis/GA/BrokerAPI/master.svg?style=flat-square
[ico-scrutinizer]: https://img.shields.io/scrutinizer/coverage/g/GA/BrokerAPI.svg?style=flat-square
[ico-code-quality]: https://img.shields.io/scrutinizer/g/GA/BrokerAPI.svg?style=flat-square
[ico-downloads]: https://img.shields.io/packagist/dt/GA/BrokerAPI.svg?style=flat-square

[link-packagist]: https://packagist.org/packages/GA/BrokerAPI
[link-travis]: https://travis-ci.org/GA/BrokerAPI
[link-scrutinizer]: https://scrutinizer-ci.com/g/GA/BrokerAPI/code-structure
[link-code-quality]: https://scrutinizer-ci.com/g/GA/BrokerAPI
[link-downloads]: https://packagist.org/packages/GA/BrokerAPI
[ico-travis]: https://img.shields.io/travis/GA/AsyncAPI/master.svg?style=flat-square
[ico-scrutinizer]: https://img.shields.io/scrutinizer/coverage/g/GA/AsyncAPI.svg?style=flat-square
[ico-code-quality]: https://img.shields.io/scrutinizer/g/GA/AsyncAPI.svg?style=flat-square
[ico-downloads]: https://img.shields.io/packagist/dt/GA/AsyncAPI.svg?style=flat-square

[link-packagist]: https://packagist.org/packages/GA/AsyncAPI
[link-travis]: https://travis-ci.org/GA/AsyncAPI
[link-scrutinizer]: https://scrutinizer-ci.com/g/GA/AsyncAPI/code-structure
[link-code-quality]: https://scrutinizer-ci.com/g/GA/AsyncAPI
[link-downloads]: https://packagist.org/packages/GA/AsyncAPI
[link-author]: https://github.com/emilianozublena
[link-contributors]: https://github.com/asyncapi/php-template/graphs/contributors
4 changes: 2 additions & 2 deletions template/configs/consts.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@
/** END OF PROTOCOL LITERALS */

/** APPLICATION LITERALS */
const PRODUCER_KEY = 'producer';
const CONSUMER_KEY = 'consumer';
const SUBSCRIBER_KEY = 'subscriber';
const PUBLISHER_KEY = 'publisher';
/** END OF APPLICATION LITERALS */
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
use {{ params.packageName }};
use {{ params.packageName }}\Messages\Example;

$brokerAPI = new BrokerAPI();
$brokerAPI = new AsyncAPI();
$factory = $brokerAPI->init();

/** @var \{{ params.packageName }}\Applications\Producer $producer */
$producer = $factory->createApplication(
PRODUCER_KEY,
/** @var \{{ params.packageName }}\Applications\Publisher $publisher */
$publisher = $factory->createApplication(
PUBLISHER_KEY,
[
BROKER_HOST_KEY => $_ENV[ENV_BROKER_HOST_KEY] ?? BROKER_HOST_DEFAULT,
BROKER_USER_KEY => $_ENV[ENV_BROKER_USER_KEY] ?? BROKER_USER_DEFAULT,
Expand All @@ -25,4 +25,4 @@
'id' => 1,
]
);
$producer->requestExampleById($message);
$publisher->requestExampleById($message);
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Examples\Basic\Consumer\Handlers;
namespace Examples\Basic\Publisher\Handlers;

use {{ params.packageName }}\Handlers\HandlerContract;
use {{ params.packageName }}\Messages\MessageContract;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

use {{ params.packageName }};

$brokerAPI = new BrokerAPI();
$brokerAPI = new AsyncAPI();
$factory = $brokerAPI->init();

/** @var \{{ params.packageName }}\Applications\Consumer $consumer */
$consumer = $factory->createApplication(
CONSUMER_KEY,
/** @var \{{ params.packageName }}\Applications\Subscriber $subscriber */
$subscriber = $factory->createApplication(
SUBSCRIBER_KEY,
[
BROKER_HOST_KEY => $_ENV[ENV_BROKER_HOST_KEY] ?? BROKER_HOST_DEFAULT,
BROKER_USER_KEY => $_ENV[ENV_BROKER_USER_KEY] ?? BROKER_USER_DEFAULT,
Expand All @@ -18,5 +18,5 @@
BROKER_VIRTUAL_HOST_KEY => $_ENV[ENV_BROKER_VIRTUAL_HOST_KEY] ?? BROKER_VIRTUAL_HOST_DEFAULT,
]
);
$handler = new \Examples\Basic\Consumer\Handlers\ExampleHandler();
$consumer->retrieveExampleById($handler);
$handler = new \Examples\Basic\Subscriber\Handlers\ExampleHandler();
$subscriber->retrieveExampleById($handler);
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
use {{ params.packageName }};
use {{ params.packageName }}\Messages\Example;

$brokerAPI = new BrokerAPI();
$brokerAPI = new AsyncAPI();
$factory = $brokerAPI->init();

/** @var \{{ params.packageName }}\Applications\Producer $producer */
$producer = $factory->createApplication(
PRODUCER_KEY,
/** @var \{{ params.packageName }}\Applications\Publisher $publisher */
$publisher = $factory->createApplication(
PUBLISHER_KEY,
[
BROKER_HOST_KEY => $_ENV[ENV_BROKER_HOST_KEY] ?? BROKER_HOST_DEFAULT,
BROKER_USER_KEY => $_ENV[ENV_BROKER_USER_KEY] ?? BROKER_USER_DEFAULT,
Expand All @@ -26,5 +26,5 @@
]
);
/** @var \PhpAmqpLib\Message\AMQPMessage $return */
$return = $producer->requestExampleByIdRPC($message);
$return = $publisher->requestExampleByIdRPC($message);
print_r($return->getBody());
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Examples\RPC\Consumer\Handlers;
namespace Examples\RPC\Publisher\Handlers;

use PhpAmqpLib\Message\AMQPMessage;
use {{ params.packageName }}\Common\AMQPFactory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

use {{ params.packageName }};

$brokerAPI = new BrokerAPI();
$brokerAPI = new AsyncAPI();
$factory = $brokerAPI->init();

/** @var \{{ params.packageName }}\Applications\Consumer $consumer */
$consumer = $factory->createApplication(
CONSUMER_KEY,
/** @var \{{ params.packageName }}\Applications\Subscriber $subscriber */
$subscriber = $factory->createApplication(
SUBSCRIBER_KEY,
[
BROKER_HOST_KEY => $_ENV[ENV_BROKER_HOST_KEY] ?? BROKER_HOST_DEFAULT,
BROKER_USER_KEY => $_ENV[ENV_BROKER_USER_KEY] ?? BROKER_USER_DEFAULT,
Expand All @@ -18,5 +18,5 @@
BROKER_VIRTUAL_HOST_KEY => $_ENV[ENV_BROKER_VIRTUAL_HOST_KEY] ?? BROKER_VIRTUAL_HOST_DEFAULT,
]
);
$handler = new \Examples\RPC\Consumer\Handlers\RPCExampleHandler();
$consumer->retrieveExampleByIdRPC($handler);
$handler = new \Examples\RPC\Subscriber\Handlers\RPCExampleHandler();
$subscriber->retrieveExampleByIdRPC($handler);
6 changes: 3 additions & 3 deletions template/phpcs.xml.dist
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0"?>
<ruleset name="BrokerAPI">
<description>The coding standard of BrokerAPI package</description>
<ruleset name="AsyncAPI">
<description>The coding standard of AsyncAPI package</description>
<arg value="p" />

<config name="ignore_warnings_on_exit" value="1" />
Expand All @@ -11,4 +11,4 @@

<!-- Use the PSR2 Standard-->
<rule ref="PSR2" />
</ruleset>
</ruleset>
4 changes: 2 additions & 2 deletions template/phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
stopOnFailure = "true">

<testsuites>
<testsuite name="BrokerAPI Test Suite">
<testsuite name="AsyncAPI Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
Expand All @@ -20,4 +20,4 @@
<env name="APP_ENV" value="testing"/>
</php>

</phpunit>
</phpunit>
2 changes: 1 addition & 1 deletion template/src/Applications/ApplicationContract.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/**
* Base interface for all Applications
* In BrokerAPI applications can either be Producers or Consumers
* In AsyncAPI applications can either be Subscribers or Publishers
* (depending on message protocol these often get called as publishers/subscribers)
* Application classes should never be extended
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php
/**
* Producer application is the single final class that you should obtain from concrete factory
* Subscriber application is the single final class that you should obtain from concrete factory
* This application will have methods for each channel/operation and you should only
* pass messages and handlers that will implement any business logic needed (eg: rpc calls)
* Producer = Publisher
* Subscriber = Publisher
*
* User: emiliano
* Date: 30/12/20
Expand All @@ -15,13 +15,13 @@
use {{ params.packageName }}\Messages\MessageContract;
use {{ params.packageName }}\Handlers\AMQPRPCClientHandler;

final class Producer extends ApplicationContract
final class Publisher extends ApplicationContract
{
{%- for channelName, channel in asyncapi.channels() %}
{%- if channel.hasPublish() %}
{%- set methodName = channel.publish().id() %}
{%- set methodDescription = channel.publish().description() %}
{%- set amqpBindings = channel.publish().bindings().amqp %}
{%- if channel.hasSubscribe() %}
{%- set methodName = channel.subscribe().id() %}
{%- set methodDescription = channel.subscribe().description() %}
{%- set amqpBindings = channel.subscribe().bindings().amqp %}
{%- if amqpBindings["x-type"] == 'basic' %}
/**
* {{ methodDescription }}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php
/**
* Consumer application is the single final class that you should obtain from concrete factory
* Publisher application is the single final class that you should obtain from concrete factory
* This application will have methods for each channel/operation and you should only
* Pass custom handlers to those methods for any business logic needed on message received
* Consumer = Worker = Subscriber
* Publisher = Worker = Subscriber
* User: emiliano
* Date: 7/1/21
* Time: 12:24
Expand All @@ -14,13 +14,13 @@
use {{ params.packageName }}\Handlers\HandlerContract;
use {{ params.packageName }}\Handlers\AMQPRPCServerHandler;

final class Consumer extends ApplicationContract
final class Subscriber extends ApplicationContract
{
{%- for channelName, channel in asyncapi.channels() %}
{%- if channel.hasSubscribe() %}
{%- set methodName = channel.subscribe().id() %}
{%- set methodDescription = channel.subscribe().description() %}
{%- set amqpBindings = channel.subscribe().bindings().amqp %}
{%- if channel.hasPublish() %}
{%- set methodName = channel.publish().id() %}
{%- set methodDescription = channel.publish().description() %}
{%- set amqpBindings = channel.publish().bindings().amqp %}
{%- if amqpBindings["x-type"] == 'basic' %}
/**
* {{ methodDescription }}
Expand All @@ -43,7 +43,7 @@ public function {{ methodName }}(
{%- set queueAutoDelete = amqpBindings.queue.autoDelete %}
$config = array_merge([
'queue' => '',
'consumerTag' => '',
'publisherTag' => '',
'noLocal' => false,
'noAck' => false,
'exclusive' => {{queueExclusive}},
Expand Down
6 changes: 3 additions & 3 deletions template/src/BrokerAPI.php → template/src/AsyncAPI.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* This is the single entry point for the BrokerAPI.
* This is the single entry point for the AsyncAPI.
* This class will be in charge of instantiating whatever Factory is needed
* depending on what's the default protocol on async api file
* This default protocol can be overwritten by just sending it while instantiating this class
Expand All @@ -15,13 +15,13 @@
use {{ params.packageName }}\Common\AMQPFactory;
use {{ params.packageName }}\Common\FactoryContract;

final class BrokerAPI
final class AsyncAPI
{
/** @var string $protocol */
private $protocol;

/**
* BrokerAPI constructor.
* AsyncAPI constructor.
* @param string $protocol
*/
public function __construct(string $protocol = '{{-asyncapi | getDefaultProtocol}}')
Expand Down
12 changes: 6 additions & 6 deletions template/src/Common/AMQPFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
use {{ params.packageName }}\Infrastructure\AMQPBrokerClient;
use {{ params.packageName }}\Messages\MessageContract;
use {{ params.packageName }}\Applications\ApplicationContract;
use {{ params.packageName }}\Applications\Consumer;
use {{ params.packageName }}\Applications\Producer;
use {{ params.packageName }}\Applications\Publisher;
use {{ params.packageName }}\Applications\Subscriber;
use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;

Expand Down Expand Up @@ -75,13 +75,13 @@ public function createApplication(
$brokerClient = $this->createBrokerClient($config);

switch ($applicationType) {
case PRODUCER_KEY:
$application = new Producer(
case SUBSCRIBER_KEY:
$application = new Subscriber(
$brokerClient
);
break;
case CONSUMER_KEY:
$application = new Consumer(
case PUBLISHER_KEY:
$application = new Publisher(
$brokerClient
);
break;
Expand Down
2 changes: 1 addition & 1 deletion template/src/Handlers/AMQPRPCClientHandler.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* This class is used by a Consumer as a means to implement business logic "OnRequest"
* This class is used by a Publisher as a means to implement business logic "OnRequest"
* From any given RPC client (Publisher)
*
* User: emiliano
Expand Down
2 changes: 1 addition & 1 deletion template/src/Handlers/AMQPRPCServerHandler.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* This class is used by a Consumer as a means to implement business logic "OnRequest"
* This class is used by a Publisher as a means to implement business logic "OnRequest"
* From any given RPC client (Publisher)
*
* User: emiliano
Expand Down
Loading

0 comments on commit 02e406b

Please sign in to comment.