alexfn / nano-service
This is my package nano-service
Fund package maintenance!
AlexFN
Installs: 4 990
Dependents: 0
Suggesters: 0
Security: 0
Stars: 3
Watchers: 1
Forks: 1
Open Issues: 3
Requires
- php: ^8.1
- ext-curl: *
- league/statsd: ^2.0
- myclabs/php-enum: ^1.8
- php-amqplib/php-amqplib: ^3.5
- ramsey/uuid: ^3 || ^4
- spatie/crypto: ^2.0
Requires (Dev)
- phpunit/phpunit: ^9.6
- dev-main
- v1.7.4
- v1.7.3
- v1.7.2
- v1.7.1
- v1.7.0
- v1.6.31
- v1.6.30
- v1.6.29
- v1.6.28
- v1.6.27
- v1.6.26
- v1.6.25
- v1.6.24
- v1.6.23
- v1.6.22
- v1.6.21
- v1.6.20
- v1.6.19
- v1.6.18
- v1.6.17
- v1.6.16
- v1.6.15
- v1.6.14
- v1.6.12
- v1.6.11
- v1.6.10
- v1.6.9
- v1.6.8
- v1.6.7
- v1.6.6
- v1.6.5
- v1.6.4
- v1.6.3
- v1.6.2
- v1.6.1
- v1.6.0
- v1.5.1
- v1.5.0
- v1.4.0
- v1.2.0
- v1.1.1
- v1.1.0
- v1.0.0
- dev-dependabot/github_actions/dependabot/fetch-metadata-1.4.0
- dev-dependabot/github_actions/aglipanci/laravel-pint-action-2.2.0
This package is auto-updated.
Last update: 2025-03-07 09:07:38 UTC
README
This is where your description should go. Try and limit it to a paragraph or two. Consider adding a small example.
Support us
We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.
We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.
Installation
You can install the package via composer:
composer require alexfn/nano-service
Usage
- Add environment variables
AMQP_PROJECT="project-name" AMQP_HOST="rabbitmq-host" AMQP_PORT="5672" AMQP_USER="rmuser" AMQP_PASS="rmpassword" AMQP_VHOST="/" # Required for the consumer AMQP_MICROSERVICE_NAME="microservice-name" # For publisher encryption AMQP_PRIVATE_KEY="private-key" # For message getters AMQP_PUBLIC_KEY="public-key"
- Create message
$message = new NanoServiceMessage( // Body data [ 'key' => 'Value', ], // Message property (Optional) [ 'content_type' => 'text/json', 'delivery_mode' => AMQPMessage::DELIVERY_MODE_PERSISTENT, ] ); $message->addPayload([ 'key1' => 'Value 1', 'key2' => 'Value 2', ]);
- Publish message
$message = (new NanoServiceMessage()) ->addPayload([ 'key' => 'Value', ]); (new NanoPublisher()) ->setMessage($message) ->publish('event-name');
- Consume messages
$consumer = new NanoConsumer(); $consumer ->events('event-one', 'event-two') ->consume(function (NanoServiceMessage $message) { $payload = $message->getPayload();// array });
Additional message methods
$message = (new NanoServiceMessage()) ->addPayload([ 'key' => 'Value', ]);
$message = (new NanoServiceMessage()) ->addMeta([ 'key' => 'Value', ]);
$message->getPayload(); $message->getPayloadAttribute('key'); $message->getPayloadAttribute('key', 'default_value'); $message->addPayload([]); $message->getPayloadAttribute('attribute', []); $message->getMeta(); $message->getMetaAttribute('key'); $message->getMetaAttribute('key', 'default_value'); $message->addMeta([]); $message->getMetaAttribute('attribute', []); $message->getStatusCode(); // Default 'unknown' $message->setStatusCode('success'); $message->getStatusData(); // Default [] $message->setStatusData([]);
Encrypting data using private/public keys
// Encrypting a message with a private key $message = (new NanoServiceMessage()) ->setEncryptedAttribute('attribute', 'My secret data');
// Decrypting with the public key $message->getEncryptedAttribute('attribute'); // My secret data
Replace attributes
$message = (new NanoServiceMessage()) ->addPayload([ 'key1' => 'Value 1', 'key2' => 'Value 2', ]) ->addPayload([ 'key1' => 'New value 1', 'key3' => 'New value 3', ]); // Result: {"key1":"Value 1","key2":"Value 2","key3":"New value 3"}
$message = (new NanoServiceMessage()) ->addPayload([ 'key1' => 'Value 1', 'key2' => 'Value 2', ]) ->addPayload( [ 'key1' => 'New value 1', 'key3' => 'New value 3', ], true ); // Result: {"key1":"New value 1","key2":"Value 2","key3":"New value 3"}
$message = (new NanoServiceMessage()) ->setStatusData([ 'key1' => 'Value 1', 'key2' => 'Value 2', ]) ->setStatusData( [ 'key1' => 'New value 1', 'key3' => 'New value 3', ], true ); // Result: {"key1":"New value 1","key3":"New value 3"}
Debug mode
$message->setDebug();
(new NanoPublisher())
->setMessage($message)
->publish('event-name');
$consumer
->events('event-name')
->consume($callback, function (NanoServiceMessage $message) {
// debugCallback (Optional)
});
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.