├── LICENSE ├── composer.json └── src ├── BusServiceProvider.php └── Dispatcher.php /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015-2020 Alt Three Services Limited 4 | Copyright (c) 2014-2015 Taylor Otwell 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "alt-three/bus", 3 | "description": "An improved command bus for Laravel", 4 | "keywords": ["bus", "command", "job", "command bus", "Bus", "Alt Three"], 5 | "license": "MIT", 6 | "authors": [ 7 | { 8 | "name": "Alt Three", 9 | "email": "support@alt-three.com" 10 | } 11 | ], 12 | "require": { 13 | "php": "^7.1.3 || ^8.0", 14 | "illuminate/bus": "^5.5 || ^6.0 || ^7.0 || ^8.0", 15 | "illuminate/container": "^5.5 || ^6.0 || ^7.0 || ^8.0", 16 | "illuminate/contracts": "^5.5 || ^6.0 || ^7.0 || ^8.0", 17 | "illuminate/pipeline": "^5.5 || ^6.0 || ^7.0 || ^8.0", 18 | "illuminate/support": "^5.5 || ^6.0 || ^7.0 || ^8.0" 19 | }, 20 | "require-dev": { 21 | "graham-campbell/analyzer": "^2.4 || ^3.0", 22 | "graham-campbell/testbench": "^5.5", 23 | "mockery/mockery": "^1.3.1", 24 | "phpunit/phpunit": "^6.5 || ^7.5 || ^8.4 || ^9.0" 25 | }, 26 | "autoload": { 27 | "psr-4": { 28 | "AltThree\\Bus\\": "src/" 29 | } 30 | }, 31 | "autoload-dev": { 32 | "psr-4": { 33 | "AltThree\\Tests\\Bus\\": "tests/" 34 | } 35 | }, 36 | "config": { 37 | "preferred-install": "dist" 38 | }, 39 | "minimum-stability": "dev", 40 | "prefer-stable": true 41 | } 42 | -------------------------------------------------------------------------------- /src/BusServiceProvider.php: -------------------------------------------------------------------------------- 1 | 27 | * @author Taylor Otwell 28 | */ 29 | class BusServiceProvider extends ServiceProvider 30 | { 31 | /** 32 | * Register the service provider. 33 | * 34 | * @return void 35 | */ 36 | public function register() 37 | { 38 | $this->app->singleton(Dispatcher::class, function (Container $app) { 39 | return new Dispatcher($app, function ($connection = null) use ($app) { 40 | return $app->make(Factory::class)->connection($connection); 41 | }); 42 | }); 43 | 44 | $this->app->alias(Dispatcher::class, DispatcherContract::class); 45 | $this->app->alias(Dispatcher::class, QueueingDispatcherContract::class); 46 | } 47 | 48 | /** 49 | * Get the services provided by the provider. 50 | * 51 | * @return array 52 | */ 53 | public function provides() 54 | { 55 | return [ 56 | Dispatcher::class, 57 | DispatcherContract::class, 58 | QueueingDispatcherContract::class, 59 | ]; 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /src/Dispatcher.php: -------------------------------------------------------------------------------- 1 | 23 | * @author Taylor Otwell 24 | */ 25 | class Dispatcher extends IlluminateDispatcher 26 | { 27 | /** 28 | * The fallback mapping callback. 29 | * 30 | * @var callable|null 31 | */ 32 | protected $mapper; 33 | 34 | /** 35 | * Determine if the given command has a handler. 36 | * 37 | * @param mixed $command 38 | * 39 | * @return bool 40 | */ 41 | public function hasCommandHandler($command) 42 | { 43 | $class = get_class($command); 44 | 45 | if (isset($this->handlers[$class])) { 46 | return true; 47 | } 48 | 49 | $callback = $this->mapper; 50 | 51 | if ($callback === null || method_exists($command, 'handle') || (method_exists($this, 'dispatchSync') && method_exists($command, '__invoke'))) { 52 | return false; 53 | } 54 | 55 | $this->handlers[$class] = $callback($command); 56 | 57 | return true; 58 | } 59 | 60 | /** 61 | * Register a fallback mapper callback. 62 | * 63 | * @param callable|null $mapper 64 | * 65 | * @return void 66 | */ 67 | public function mapUsing(callable $mapper = null) 68 | { 69 | $this->mapper = $mapper; 70 | } 71 | 72 | /** 73 | * Map the command to a handler within a given root namespace. 74 | * 75 | * @param object $command 76 | * @param string $commandNamespace 77 | * @param string $handlerNamespace 78 | * 79 | * @return string 80 | */ 81 | public static function simpleMapping($command, string $commandNamespace, string $handlerNamespace) 82 | { 83 | $command = str_replace($commandNamespace, '', get_class($command)); 84 | 85 | return $handlerNamespace.'\\'.trim($command, '\\').'Handler'; 86 | } 87 | } 88 | --------------------------------------------------------------------------------