├── .php_cs.dist.php ├── CHANGELOG.md ├── LICENSE.md ├── README.md ├── composer.json └── src ├── AllJobs.php ├── Concerns └── InteractsWithPayload.php ├── Facades └── AllJobs.php └── InteractsWithPayloadServiceProvider.php /.php_cs.dist.php: -------------------------------------------------------------------------------- 1 | in([ 5 | __DIR__ . '/src', 6 | __DIR__ . '/tests', 7 | ]) 8 | ->name('*.php') 9 | ->notName('*.blade.php') 10 | ->ignoreDotFiles(true) 11 | ->ignoreVCS(true); 12 | 13 | return (new PhpCsFixer\Config()) 14 | ->setRules([ 15 | '@PSR12' => true, 16 | 'array_syntax' => ['syntax' => 'short'], 17 | 'ordered_imports' => ['sort_algorithm' => 'alpha'], 18 | 'no_unused_imports' => true, 19 | 'not_operator_with_successor_space' => true, 20 | 'trailing_comma_in_multiline' => true, 21 | 'phpdoc_scalar' => true, 22 | 'unary_operator_spaces' => true, 23 | 'binary_operator_spaces' => true, 24 | 'blank_line_before_statement' => [ 25 | 'statements' => ['break', 'continue', 'declare', 'return', 'throw', 'try'], 26 | ], 27 | 'phpdoc_single_line_var_spacing' => true, 28 | 'phpdoc_var_without_name' => true, 29 | 'class_attributes_separation' => [ 30 | 'elements' => [ 31 | 'method' => 'one', 32 | ], 33 | ], 34 | 'method_argument_space' => [ 35 | 'on_multiline' => 'ensure_fully_multiline', 36 | 'keep_multiple_spaces_after_comma' => true, 37 | ], 38 | 'single_trait_insert_per_statement' => true, 39 | ]) 40 | ->setFinder($finder); 41 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All notable changes to `laravel-interacts-with-payload` will be documented in this file. 4 | 5 | ## 1.2.2 - 2025-03-08 6 | 7 | ### What's Changed 8 | 9 | * Laravel 12.x Compatibility by @laravel-shift in https://github.com/spatie/laravel-interacts-with-payload/pull/6 10 | 11 | **Full Changelog**: https://github.com/spatie/laravel-interacts-with-payload/compare/1.2.1...1.2.2 12 | 13 | ## 1.2.1 - 2024-12-11 14 | 15 | ### What's Changed 16 | 17 | * Add alias for AllJobs by @demianbinsh in https://github.com/spatie/laravel-interacts-with-payload/pull/5 18 | * Use InteractsWithQueue by @demianbinsh in https://github.com/spatie/laravel-interacts-with-payload/pull/4 19 | 20 | ### New Contributors 21 | 22 | * @demianbinsh made their first contribution in https://github.com/spatie/laravel-interacts-with-payload/pull/5 23 | 24 | **Full Changelog**: https://github.com/spatie/laravel-interacts-with-payload/compare/1.2.0...1.2.1 25 | 26 | ## 1.2.0 - 2024-03-08 27 | 28 | ### What's Changed 29 | 30 | * Laravel 11.x Compatibility by @laravel-shift in https://github.com/spatie/laravel-interacts-with-payload/pull/3 31 | 32 | **Full Changelog**: https://github.com/spatie/laravel-interacts-with-payload/compare/1.1.2...1.2.0 33 | 34 | ## 1.1.2 - 2023-02-02 35 | 36 | ### What's Changed 37 | 38 | - Laravel 10.x Compatibility by @laravel-shift in https://github.com/spatie/laravel-interacts-with-payload/pull/2 39 | 40 | ### New Contributors 41 | 42 | - @laravel-shift made their first contribution in https://github.com/spatie/laravel-interacts-with-payload/pull/2 43 | 44 | **Full Changelog**: https://github.com/spatie/laravel-interacts-with-payload/compare/1.1.1...1.1.2 45 | 46 | ## 1.1.1 - 2022-04-29 47 | 48 | ## What's Changed 49 | 50 | - Fix variable name by @joelvh in https://github.com/spatie/laravel-interacts-with-payload/pull/1 51 | 52 | ## New Contributors 53 | 54 | - @joelvh made their first contribution in https://github.com/spatie/laravel-interacts-with-payload/pull/1 55 | 56 | **Full Changelog**: https://github.com/spatie/laravel-interacts-with-payload/compare/1.1.0...1.1.1 57 | 58 | ## 1.1.0 - 2022-01-13 59 | 60 | - support Laravel 9 61 | 62 | ## 1.0.1 - 2021-04-20 63 | 64 | - fix package discovery 65 | 66 | ## 1.0.0 - 2021-04-20 67 | 68 | - initial release 69 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) Spatie 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Inject extra info to the payloads of all jobs in a Laravel app 2 | 3 | [![Latest Version on Packagist](https://img.shields.io/packagist/v/spatie/laravel-interacts-with-payload.svg?style=flat-square)](https://packagist.org/packages/spatie/laravel-interacts-with-payload) 4 | [![run-tests](https://github.com/spatie/laravel-interacts-with-payload/actions/workflows/run-tests.yml/badge.svg)](https://github.com/spatie/laravel-interacts-with-payload/actions/workflows/run-tests.yml) 5 | [![Total Downloads](https://img.shields.io/packagist/dt/spatie/laravel-interacts-with-payload.svg?style=flat-square)](https://packagist.org/packages/spatie/laravel-interacts-with-payload) 6 | 7 | This package makes it easy to inject things in every job. 8 | 9 | Imagine that you want to have the user who initiated the queued of a job available in every queued job. This is how you would implement that using this package. 10 | 11 | ```php 12 | // typically in a service provider 13 | 14 | use Spatie\InteractsWithPayload\Facades\AllJobs; 15 | 16 | AllJobs::add('user', fn() => auth()->user()); 17 | ``` 18 | 19 | To retrieve the user in your queued job you can call `getFromPayload` which is available through the `InteractsWithPayload` trait. 20 | 21 | ```php 22 | use Illuminate\Contracts\Queue\ShouldQueue; 23 | use Spatie\InteractsWithPayload\Concerns\InteractsWithPayload; 24 | 25 | class YourJob implements ShouldQueue 26 | { 27 | use InteractsWithPayload; 28 | 29 | public function handle() 30 | { 31 | // instance of User model or `null` 32 | $user = $this->getFromPayload('user'); 33 | } 34 | } 35 | ``` 36 | 37 | ## Are you a visual learner? 38 | 39 | [In this livestream](https://youtu.be/DY8i5eaXCbM?t=1688), you'll see our developer Freek explains how you can use the package, it's internals and how we test this package. 40 | 41 | ## Support us 42 | 43 | [](https://spatie.be/github-ad-click/laravel-interacts-with-payload) 44 | 45 | We invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can support us by [buying one of our paid products](https://spatie.be/open-source/support-us). 46 | 47 | 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](https://spatie.be/about-us). We publish all received postcards on [our virtual postcard wall](https://spatie.be/open-source/postcards). 48 | 49 | ## Installation 50 | 51 | You can install the package via composer: 52 | 53 | ```bash 54 | composer require spatie/laravel-interacts-with-payload 55 | ``` 56 | 57 | ## Usage 58 | 59 | To add a value to all jobs call the `add` method on the `AllJobs` facade with a name and a closure that returns the value. 60 | 61 | ```php 62 | use Spatie\InteractsWithPayload\Facades\AllJobs; 63 | 64 | AllJobs::add('extraValue', fn() => 'My extra value') 65 | ``` 66 | 67 | To retrieve the user in your queued job you can call `getFromPayload` which is available through the `InteractsWithPayload` trait. 68 | 69 | ```php 70 | use Illuminate\Contracts\Queue\ShouldQueue; 71 | use Spatie\InteractsWithPayload\Concerns\InteractsWithPayload; 72 | 73 | class YourJob implements ShouldQueue 74 | { 75 | use InteractsWithPayload; 76 | 77 | public function handle() 78 | { 79 | // will contain "My extra value" 80 | $value = $this->getFromPayload('extraValue'); 81 | } 82 | } 83 | ``` 84 | 85 | ### Using models 86 | 87 | It is safe to let the closure you pass to `add` return an Eloquent model. 88 | 89 | ```php 90 | use Spatie\InteractsWithPayload\Facades\AllJobs; 91 | 92 | AllJobs::add('user', fn() => auth()->user()) 93 | ``` 94 | 95 | You can retrieve the model with `getFromPayload` 96 | 97 | ```php 98 | use Illuminate\Contracts\Queue\ShouldQueue; 99 | use Spatie\InteractsWithPayload\Concerns\InteractsWithPayload; 100 | 101 | class YourJob implements ShouldQueue 102 | { 103 | use InteractsWithPayload; 104 | 105 | public function handle() 106 | { 107 | // instance of User model or `null` if the user has been deleted in the meantime 108 | $user = $this->getFromPayload('user'); 109 | } 110 | } 111 | ``` 112 | 113 | ## Testing 114 | 115 | ```bash 116 | composer test 117 | ``` 118 | 119 | ## Changelog 120 | 121 | Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently. 122 | 123 | ## Contributing 124 | 125 | Please see [CONTRIBUTING](https://github.com/spatie/.github/blob/main/CONTRIBUTING.md) for details. 126 | 127 | ## Security Vulnerabilities 128 | 129 | Please review [our security policy](../../security/policy) on how to report security vulnerabilities. 130 | 131 | ## Credits 132 | 133 | - [Freek Van der Herten](https://github.com/freekmurze) 134 | - [All Contributors](../../contributors) 135 | 136 | This package is inspired by [this awesome blogpost](https://james.brooks.page/blog/injecting-additional-data-into-laravel-queued-jobs/) by [James Brooks](https://twitter.com/jbrooksuk). Thank you James for also having helped hunting down that queueing bug in Laravel 👍 137 | 138 | ## License 139 | 140 | The MIT License (MIT). Please see [License File](LICENSE.md) for more information. 141 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "spatie/laravel-interacts-with-payload", 3 | "description": "Add variables to the payloads of all jobs in a Laravel app", 4 | "keywords": [ 5 | "Spatie", 6 | "laravel", 7 | "laravel_interacts_with_payload" 8 | ], 9 | "homepage": "https://github.com/spatie/laravel_interacts_with_payload", 10 | "license": "MIT", 11 | "authors": [ 12 | { 13 | "name": "Freek Van der herten", 14 | "email": "freek@spatie.be", 15 | "role": "Developer" 16 | } 17 | ], 18 | "require": { 19 | "php": "^8.2", 20 | "illuminate/contracts": "^10.0|^11.0|^12.0", 21 | "nesbot/carbon": "^2.72|^3.0", 22 | "spatie/laravel-package-tools": "^1.4.3" 23 | }, 24 | "require-dev": { 25 | "nunomaduro/collision": "^7.0|^8.0", 26 | "orchestra/testbench": "^8.0|^9.0|^10.0", 27 | "phpunit/phpunit": "^10.5|^11.5.3|^12.0", 28 | "spatie/laravel-ray": "^1.28" 29 | }, 30 | "autoload": { 31 | "psr-4": { 32 | "Spatie\\InteractsWithPayload\\": "src" 33 | } 34 | }, 35 | "autoload-dev": { 36 | "psr-4": { 37 | "Spatie\\InteractsWithPayload\\Tests\\": "tests" 38 | } 39 | }, 40 | "scripts": { 41 | "test": "./vendor/bin/phpunit", 42 | "test-coverage": "vendor/bin/phpunit --coverage-html coverage" 43 | }, 44 | "config": { 45 | "sort-packages": true, 46 | "allow-plugins": { 47 | "composer/package-versions-deprecated": true 48 | } 49 | }, 50 | "extra": { 51 | "laravel": { 52 | "providers": [ 53 | "Spatie\\InteractsWithPayload\\InteractsWithPayloadServiceProvider" 54 | ], 55 | "aliases": { 56 | "AllJobs": "Spatie\\InteractsWithPayload\\Facades\\AllJobs" 57 | } 58 | } 59 | }, 60 | "minimum-stability": "dev", 61 | "prefer-stable": true 62 | } 63 | -------------------------------------------------------------------------------- /src/AllJobs.php: -------------------------------------------------------------------------------- 1 | $closureInArray) { 16 | $this->add($nameInArray, $closureInArray); 17 | } 18 | 19 | return $this; 20 | } 21 | 22 | $this->addToPayload[$name] = $closure; 23 | 24 | return $this; 25 | } 26 | 27 | public function addAllToPayloadData(array $payloadData): array 28 | { 29 | foreach ($this->addToPayload as $addToPayloadName => $addToPayloadClosure) { 30 | if (isset($payloadData[$addToPayloadName])) { 31 | continue; 32 | } 33 | 34 | $rawValue = $addToPayloadClosure(); 35 | 36 | $preparedValue = $this->getPreparedValueForPayload($addToPayloadName, $rawValue); 37 | 38 | $payloadData = array_merge($payloadData, array_filter([ 39 | 40 | $addToPayloadName => $preparedValue, 41 | ])); 42 | } 43 | 44 | return $payloadData; 45 | } 46 | 47 | protected function getPreparedValueForPayload(string $name, mixed $rawValue): array 48 | { 49 | if ($rawValue instanceof Model) { 50 | return ['value' => $rawValue->getKey(), 'type' => get_class($rawValue)]; 51 | } 52 | 53 | return [ 54 | 'value' => $rawValue, 'type' => '', 55 | ]; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/Concerns/InteractsWithPayload.php: -------------------------------------------------------------------------------- 1 | job->payload(); 15 | 16 | $valueAndType = $payload['data'][$name] ?? null; 17 | 18 | if (is_null($valueAndType)) { 19 | return null; 20 | } 21 | 22 | return $this->castPayloadValue($name, $valueAndType); 23 | } 24 | 25 | protected function castPayloadValue(string $name, array $valueAndType) 26 | { 27 | ['value' => $value, 'type' => $type] = $valueAndType; 28 | 29 | if (is_subclass_of($type, Model::class)) { 30 | return $type::find($value); 31 | } 32 | 33 | return $value; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/Facades/AllJobs.php: -------------------------------------------------------------------------------- 1 | name('laravel_interacts_with_payload'); 14 | } 15 | 16 | public function packageRegistered() 17 | { 18 | $this->app->singleton('all-jobs', function () { 19 | return new AllJobs(); 20 | }); 21 | $this->app->alias('all-jobs', AllJobs::class); 22 | } 23 | 24 | public function packageBooted() 25 | { 26 | Queue::createPayloadUsing(function ($connection, $queue, $payload) { 27 | $payloadData = $payload['data']; 28 | 29 | /** @var \Spatie\InteractsWithPayload\AllJobs $allJobs */ 30 | $allJobs = app('all-jobs'); 31 | 32 | $modifiedPayloadData = $allJobs->addAllToPayloadData($payloadData); 33 | 34 | return ['data' => $modifiedPayloadData]; 35 | }); 36 | } 37 | } 38 | --------------------------------------------------------------------------------