├── .travis.yml ├── LICENSE ├── README.md ├── composer.json ├── phpunit.xml.dist ├── src ├── Integrations │ ├── LaravelServiceProvider.php │ └── LumenServiceProvider.php ├── Jobs │ └── DispatcherJob.php ├── Sqs │ ├── Connector.php │ └── Queue.php └── config │ └── sqs-plain.php └── tests ├── PlainSqs └── QueueTest.php └── bootstrap.php /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | 3 | php: 4 | - '5.5' 5 | - '5.6' 6 | - '7.0' 7 | 8 | before_script: 9 | - composer install --dev 10 | 11 | addons: 12 | code_climate: 13 | repo_token: efdc0e37fa8e202c7744259f3695262393ebad37b2943e1c70aba01e50cdd92f 14 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | Copyright (c) 2016 Denis Mysenko 3 | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 5 | 6 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 7 | 8 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 9 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Plain Sqs 2 | [![Build Status](https://travis-ci.org/dusterio/laravel-plain-sqs.svg)](https://travis-ci.org/dusterio/laravel-plain-sqs) 3 | [![Code Climate](https://codeclimate.com/github/dusterio/link-preview/badges/gpa.svg)](https://codeclimate.com/github/dusterio/link-preview/badges) 4 | [![Total Downloads](https://poser.pugx.org/dusterio/laravel-plain-sqs/d/total.svg)](https://packagist.org/packages/dusterio/laravel-plain-sqs) 5 | [![Latest Stable Version](https://poser.pugx.org/dusterio/laravel-plain-sqs/v/stable.svg)](https://packagist.org/packages/dusterio/laravel-plain-sqs) 6 | [![Latest Unstable Version](https://poser.pugx.org/dusterio/laravel-plain-sqs/v/unstable.svg)](https://packagist.org/packages/dusterio/laravel-plain-sqs) 7 | [![License](https://poser.pugx.org/dusterio/laravel-plain-sqs/license.svg)](https://packagist.org/packages/dusterio/laravel-plain-sqs) 8 | 9 | A custom SQS connector for Laravel (or Lumen) that supports custom format JSON payloads. Out of the box, Laravel expects 10 | SQS messages to be generated in specific format - format that includes job handler class and a serialized job. 11 | 12 | But in certain cases you may want to parse messages from third party applications, custom JSON messages and so on. 13 | 14 | ## Dependencies 15 | 16 | * PHP >= 5.5 17 | * Laravel (or Lumen) >= 5.2 18 | 19 | ## Installation via Composer 20 | 21 | To install simply run: 22 | 23 | ``` 24 | composer require dusterio/laravel-plain-sqs 25 | ``` 26 | 27 | Or add it to `composer.json` manually: 28 | 29 | ```json 30 | { 31 | "require": { 32 | "dusterio/laravel-plain-sqs": "~0.1" 33 | } 34 | } 35 | ``` 36 | 37 | ### Usage in Laravel 5 38 | 39 | ```php 40 | // Add in your config/app.php 41 | 42 | 'providers' => [ 43 | '...', 44 | 'Dusterio\PlainSqs\Integrations\LaravelServiceProvider', 45 | ]; 46 | ``` 47 | 48 | ### Usage in Lumen 5 49 | 50 | ```php 51 | // Add in your bootstrap/app.php 52 | $app->loadComponent('queue', 'Dusterio\PlainSqs\Integrations\LumenServiceProvider'); 53 | ``` 54 | 55 | ## Configuration 56 | 57 | ```php 58 | // Generate standard config file (Laravel only) 59 | php artisan vendor:publish 60 | 61 | // In Lumen, create it manually (see example below) and load it in bootstrap/app.php 62 | $app->configure('sqs-plain'); 63 | ``` 64 | 65 | Edit config/sqs-plain.php to suit your needs. This config matches SQS queues with handler classes. 66 | 67 | ```php 68 | return [ 69 | 'handlers' => [ 70 | 'base-integrations-updates' => App\Jobs\HandlerJob::class, 71 | ], 72 | 73 | 'default-handler' => App\Jobs\HandlerJob::class 74 | ]; 75 | ``` 76 | 77 | If queue is not found in 'handlers' array, SQS payload is passed to default handler. 78 | 79 | Add sqs-plain connection to your config/queue.php, eg: 80 | ```php 81 | ... 82 | 'sqs-plain' => [ 83 | 'driver' => 'sqs-plain', 84 | 'key' => env('AWS_KEY', ''), 85 | 'secret' => env('AWS_SECRET', ''), 86 | 'prefix' => 'https://sqs.ap-southeast-2.amazonaws.com/123123/', 87 | 'queue' => 'important-music-updates', 88 | 'region' => 'ap-southeast-2', 89 | ], 90 | ... 91 | ``` 92 | 93 | In your .env file, choose sqs-plain as your new default queue driver: 94 | ``` 95 | QUEUE_DRIVER=sqs-plain 96 | ``` 97 | 98 | ## Dispatching to SQS 99 | 100 | If you plan to push plain messages from Laravel or Lumen, you can rely on DispatcherJob: 101 | 102 | ```php 103 | use Dusterio\PlainSqs\Jobs\DispatcherJob; 104 | 105 | class ExampleController extends Controller 106 | { 107 | public function index() 108 | { 109 | // Create a PHP object 110 | $object = [ 111 | 'music' => 'M.I.A. - Bad girls', 112 | 'time' => time() 113 | ]; 114 | 115 | // Pass it to dispatcher job 116 | $job = new DispatcherJob($object); 117 | 118 | // Dispatch the job as you normally would 119 | // By default, your data will be encapsulated in 'data' and 'job' field will be added 120 | $this->dispatch($job); 121 | 122 | // If you wish to submit a true plain JSON, add setPlain() 123 | $this->dispatch($job->setPlain()); 124 | } 125 | } 126 | 127 | ``` 128 | 129 | This will push the following JSON object to SQS: 130 | 131 | ``` 132 | {"job":"App\\Jobs\\HandlerJob@handle","data":{"music":"M.I.A. - Bad girls","time":1462511642}} 133 | ``` 134 | 135 | 'job' field is not used, actually. It's just kept for compatibility sake. 136 | 137 | ### Receiving from SQS 138 | 139 | If a third-party application is creating custom-format JSON messages, just add a handler in the config file and 140 | implement a handler class as follows: 141 | 142 | ```php 143 | use Illuminate\Contracts\Queue\Job as LaravelJob; 144 | 145 | class HandlerJob extends Job 146 | { 147 | protected $data; 148 | 149 | /** 150 | * @param LaravelJob $job 151 | * @param array $data 152 | */ 153 | public function handle(LaravelJob $job, array $data) 154 | { 155 | // This is incoming JSON payload, already decoded to an array 156 | var_dump($data); 157 | 158 | // Raw JSON payload from SQS, if necessary 159 | var_dump($job->getRawBody()); 160 | } 161 | } 162 | 163 | ``` 164 | 165 | ## Todo 166 | 167 | 1. Add more unit and integration tests 168 | 169 | ## Video tutorials 170 | 171 | I've just started a educational YouTube channel that will cover top IT trends in software development and DevOps: [config.sys](https://www.youtube.com/channel/UCIvUJ1iVRjJP_xL0CD7cMpg) 172 | 173 | ## License 174 | 175 | The MIT License (MIT) 176 | Copyright (c) 2016 Denis Mysenko 177 | 178 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 179 | 180 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 181 | 182 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 183 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "dusterio/laravel-plain-sqs", 3 | "type": "library", 4 | "description": "Custom SQS connector for Laravel that supports custom format JSON", 5 | "keywords": [ 6 | "php", 7 | "laravel", 8 | "lumen", 9 | "sqs", 10 | "aws" 11 | ], 12 | "homepage": "https://github.com/dusterio/laravel-plain-sqs", 13 | "license": "MIT", 14 | "authors": [ 15 | { 16 | "name": "Denis Mysenko", 17 | "email": "denis@mysenko.com", 18 | "homepage": "https://www.mysenko.com" 19 | } 20 | ], 21 | "require": { 22 | "php": ">=5.5.0", 23 | "illuminate/support": "5.*|^6.0|^7.0|^8.0|^9.0|^10.0|^11.0|^12.0", 24 | "illuminate/queue": "5.*|^6.0|^7.0|^8.0|^9.0|^10.0|^11.0|^12.0", 25 | "illuminate/bus": "5.*|^6.0|^7.0|^8.0|^9.0|^10.0|^11.0|^12.0", 26 | "aws/aws-sdk-php": "~3.0" 27 | }, 28 | "require-dev": { 29 | "phpunit/phpunit": "3.7.*|^9.5.10|^10.0" 30 | }, 31 | "autoload": { 32 | "psr-4": { 33 | "Dusterio\\PlainSqs\\": "src/" 34 | } 35 | }, 36 | "extra": { 37 | "laravel": { 38 | "providers": [ 39 | "Dusterio\\PlainSqs\\Integrations\\LaravelServiceProvider" 40 | ] 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /phpunit.xml.dist: -------------------------------------------------------------------------------- 1 | 2 | 13 | 14 | 15 | tests/PlainSqs/ 16 | 17 | 18 | 19 | 20 | 21 | src/ 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /src/Integrations/LaravelServiceProvider.php: -------------------------------------------------------------------------------- 1 | publishes([ 24 | __DIR__ . '/../config/sqs-plain.php' => config_path('sqs-plain.php') 25 | ]); 26 | 27 | Queue::after(function (JobProcessed $event) { 28 | $event->job->delete(); 29 | }); 30 | } 31 | 32 | /** 33 | * @return void 34 | */ 35 | public function register() 36 | { 37 | $this->app->booted(function () { 38 | $this->app['queue']->extend('sqs-plain', function () { 39 | return new Connector(); 40 | }); 41 | }); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/Integrations/LumenServiceProvider.php: -------------------------------------------------------------------------------- 1 | job->delete(); 25 | }); 26 | } 27 | 28 | /** 29 | * @return void 30 | */ 31 | public function register() 32 | { 33 | $this->app['queue']->addConnector('sqs-plain', function () { 34 | return new Connector(); 35 | }); 36 | } 37 | } -------------------------------------------------------------------------------- /src/Jobs/DispatcherJob.php: -------------------------------------------------------------------------------- 1 | data = $data; 31 | } 32 | 33 | /** 34 | * @return mixed 35 | */ 36 | public function getPayload() 37 | { 38 | if (! $this->isPlain()) { 39 | return [ 40 | 'job' => app('config')->get('sqs-plain.default-handler'), 41 | 'data' => $this->data 42 | ]; 43 | } 44 | 45 | return $this->data; 46 | } 47 | 48 | /** 49 | * @param bool $plain 50 | * @return $this 51 | */ 52 | public function setPlain($plain = true) 53 | { 54 | $this->plain = $plain; 55 | 56 | return $this; 57 | } 58 | 59 | /** 60 | * @return bool 61 | */ 62 | public function isPlain() 63 | { 64 | return $this->plain; 65 | } 66 | } -------------------------------------------------------------------------------- /src/Sqs/Connector.php: -------------------------------------------------------------------------------- 1 | getDefaultConfiguration($config); 21 | 22 | if (isset($config['key']) && isset($config['secret'])) { 23 | $config['credentials'] = Arr::only($config, ['key', 'secret']); 24 | } 25 | 26 | $queue = new Queue( 27 | new SqsClient($config), 28 | $config['queue'], 29 | Arr::get($config, 'prefix', '') 30 | ); 31 | 32 | return $queue; 33 | } 34 | } -------------------------------------------------------------------------------- /src/Sqs/Queue.php: -------------------------------------------------------------------------------- 1 | getClass($queue) . '@handle'; 31 | 32 | return $job->isPlain() ? json_encode($job->getPayload()) : json_encode(['job' => $handlerJob, 'data' => $job->getPayload()]); 33 | } 34 | 35 | /** 36 | * @param $queue 37 | * @return string 38 | */ 39 | private function getClass($queue = null) 40 | { 41 | if (!$queue) return Config::get('sqs-plain.default-handler'); 42 | 43 | $queue = end(explode('/', $queue)); 44 | 45 | return (array_key_exists($queue, Config::get('sqs-plain.handlers'))) 46 | ? Config::get('sqs-plain.handlers')[$queue] 47 | : Config::get('sqs-plain.default-handler'); 48 | } 49 | 50 | /** 51 | * Pop the next job off of the queue. 52 | * 53 | * @param string $queue 54 | * @return \Illuminate\Contracts\Queue\Job|null 55 | */ 56 | public function pop($queue = null) 57 | { 58 | $queue = $this->getQueue($queue); 59 | 60 | $response = $this->sqs->receiveMessage([ 61 | 'QueueUrl' => $queue, 62 | 'AttributeNames' => ['ApproximateReceiveCount'], 63 | ]); 64 | 65 | if (isset($response['Messages']) && count($response['Messages']) > 0) { 66 | $queueId = explode('/', $queue); 67 | $queueId = array_pop($queueId); 68 | 69 | $class = (array_key_exists($queueId, $this->container['config']->get('sqs-plain.handlers'))) 70 | ? $this->container['config']->get('sqs-plain.handlers')[$queueId] 71 | : $this->container['config']->get('sqs-plain.default-handler'); 72 | 73 | $response = $this->modifyPayload($response['Messages'][0], $class); 74 | 75 | if (preg_match( 76 | '/(5\.[4-8]\..*)|(6\.[0-9]*\..*)|(7\.[0-9]*\..*)|(8\.[0-9]*\..*)|(9\.[0-9]*\..*)|(10\.[0-9]*\..*)|(11\.[0-9]*\..*)|(12\.[0-9]*\..*)/', 77 | $this->container->version()) 78 | ) { 79 | return new SqsJob($this->container, $this->sqs, $response, $this->connectionName, $queue); 80 | } 81 | 82 | return new SqsJob($this->container, $this->sqs, $queue, $response); 83 | } 84 | } 85 | 86 | /** 87 | * @param string|array $payload 88 | * @param string $class 89 | * @return array 90 | */ 91 | private function modifyPayload($payload, $class) 92 | { 93 | if (! is_array($payload)) $payload = json_decode($payload, true); 94 | 95 | $body = json_decode($payload['Body'], true); 96 | 97 | $body = [ 98 | 'job' => $class . '@handle', 99 | 'data' => isset($body['data']) ? $body['data'] : $body, 100 | 'uuid' => $payload['MessageId'] 101 | ]; 102 | 103 | $payload['Body'] = json_encode($body); 104 | 105 | return $payload; 106 | } 107 | 108 | /** 109 | * @param string $payload 110 | * @param null $queue 111 | * @param array $options 112 | * @return mixed|null 113 | */ 114 | public function pushRaw($payload, $queue = null, array $options = []) 115 | { 116 | $payload = json_decode($payload, true); 117 | 118 | if (isset($payload['data']) && isset($payload['job'])) { 119 | $payload = $payload['data']; 120 | } 121 | 122 | return parent::pushRaw(json_encode($payload), $queue, $options); 123 | } 124 | } 125 | -------------------------------------------------------------------------------- /src/config/sqs-plain.php: -------------------------------------------------------------------------------- 1 | [ 8 | 'base-integrations-updates' => App\Jobs\HandlerJob::class, 9 | ], 10 | 11 | 'default-handler' => App\Jobs\HandlerJob::class 12 | ]; -------------------------------------------------------------------------------- /tests/PlainSqs/QueueTest.php: -------------------------------------------------------------------------------- 1 | 'test' 24 | ]; 25 | 26 | $job = new DispatcherJob($content); 27 | 28 | $queue = $this->getMockBuilder(Queue::class) 29 | ->disableOriginalConstructor() 30 | ->getMock(); 31 | 32 | $method = new \ReflectionMethod( 33 | 'Dusterio\PlainSqs\Sqs\Queue', 'createPayload' 34 | ); 35 | 36 | $method->setAccessible(true); 37 | 38 | //$response = $method->invokeArgs($queue, [$job]); 39 | } 40 | } -------------------------------------------------------------------------------- /tests/bootstrap.php: -------------------------------------------------------------------------------- 1 |