update 1.0.8.0

Commits for version update
This commit is contained in:
Manish Verma
2016-10-17 12:02:27 +05:30
parent dec927987b
commit 76e85db070
9674 changed files with 495757 additions and 58922 deletions

View File

@@ -0,0 +1,7 @@
/vendor
/.env
composer.phar
composer.lock
.DS_Store
Thumbs.db
.idea

View File

@@ -0,0 +1,12 @@
language: php
php:
- 5.5
- 5.6
- hhvm
sudo: false
install: travis_retry composer install --no-interaction --prefer-source
script: vendor/bin/phpunit --verbose

View File

@@ -0,0 +1,21 @@
The MIT License (MIT)
Copyright (c) <Taylor Otwell>
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:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
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.

View File

@@ -0,0 +1,32 @@
{
"name": "laravelcollective/bus",
"description": "The Laravel Bus (5.1) package for use in Laravel 5.2.",
"license": "MIT",
"homepage": "http://laravelcollective.com",
"support": {
"issues": "https://github.com/LaravelCollective/bus/issues",
"source": "https://github.com/LaravelCollective/bus"
},
"authors": [
{
"name": "Taylor Otwell",
"email": "taylorotwell@gmail.com"
}
],
"require": {
"php": ">=5.5.9",
"illuminate/container": "5.2.*",
"illuminate/contracts": "5.2.*",
"illuminate/pipeline": "5.2.*",
"illuminate/support": "5.2.*"
},
"require-dev": {
"mockery/mockery": "~0.9",
"phpunit/phpunit": "~4.0"
},
"autoload": {
"psr-4": {
"Collective\\Bus\\": "src/"
}
}
}

View File

@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
>
<testsuites>
<testsuite name="Package Test Suite">
<directory suffix=".php">./tests/</directory>
</testsuite>
</testsuites>
</phpunit>

10
vendor/laravelcollective/bus/readme.md vendored Normal file
View File

@@ -0,0 +1,10 @@
# Bus
This package provides an implementation of the `Illuminate\Contracts\Bus\Dispatcher` interface that matches the Laravel 5.1.x implementation with separate commands and handlers.
## Installation
- Remove `Illuminate\Bus\BusServiceProvider` from your `app.php` configuration file.
- Add `Collective\Bus\BusServiceProvider` to your `app.php` configuration file.
If you are type-hinting `Illuminate\Bus\Dispatcher`, you should now type-hint `Collective\Bus\Dispatcher`.

View File

@@ -0,0 +1,51 @@
<?php
namespace Collective\Bus;
use Illuminate\Support\ServiceProvider;
class BusServiceProvider extends ServiceProvider
{
/**
* Indicates if loading of the provider is deferred.
*
* @var bool
*/
protected $defer = true;
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
$this->app->singleton('Collective\Bus\Dispatcher', function ($app) {
return new Dispatcher($app, function () use ($app) {
return $app['Illuminate\Contracts\Queue\Queue'];
});
});
$this->app->alias(
'Collective\Bus\Dispatcher', 'Illuminate\Contracts\Bus\Dispatcher'
);
$this->app->alias(
'Collective\Bus\Dispatcher', 'Illuminate\Contracts\Bus\QueueingDispatcher'
);
}
/**
* Get the services provided by the provider.
*
* @return array
*/
public function provides()
{
return [
'Collective\Bus\Dispatcher',
'Illuminate\Contracts\Bus\Dispatcher',
'Illuminate\Contracts\Bus\QueueingDispatcher',
];
}
}

View File

@@ -0,0 +1,430 @@
<?php
namespace Collective\Bus;
use ArrayAccess;
use Closure;
use Illuminate\Contracts\Bus\Dispatcher as DispatcherContract;
use Illuminate\Contracts\Bus\QueueingDispatcher;
use Illuminate\Contracts\Bus\SelfHandling;
use Illuminate\Contracts\Container\Container;
use Illuminate\Contracts\Queue\Queue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Pipeline\Pipeline;
use Illuminate\Support\Collection;
use InvalidArgumentException;
use ReflectionClass;
use ReflectionParameter;
use RuntimeException;
class Dispatcher implements DispatcherContract, QueueingDispatcher, HandlerResolver
{
/**
* The container implementation.
*
* @var \Illuminate\Contracts\Container\Container
*/
protected $container;
/**
* The pipeline instance for the bus.
*
* @var \Illuminate\Pipeline\Pipeline
*/
protected $pipeline;
/**
* The pipes to send commands through before dispatching.
*
* @var array
*/
protected $pipes = [];
/**
* The queue resolver callback.
*
* @var \Closure|null
*/
protected $queueResolver;
/**
* All of the command-to-handler mappings.
*
* @var array
*/
protected $mappings = [];
/**
* The fallback mapping Closure.
*
* @var \Closure
*/
protected $mapper;
/**
* Create a new command dispatcher instance.
*
* @param \Illuminate\Contracts\Container\Container $container
* @param \Closure|null $queueResolver
*
* @return void
*/
public function __construct(Container $container, Closure $queueResolver = null)
{
$this->container = $container;
$this->queueResolver = $queueResolver;
$this->pipeline = new Pipeline($container);
}
/**
* Marshal a command and dispatch it to its appropriate handler.
*
* @param mixed $command
* @param array $array
*
* @return mixed
*/
public function dispatchFromArray($command, array $array)
{
return $this->dispatch($this->marshalFromArray($command, $array));
}
/**
* Marshal a command and dispatch it to its appropriate handler.
*
* @param mixed $command
* @param \ArrayAccess $source
* @param array $extras
*
* @return mixed
*/
public function dispatchFrom($command, ArrayAccess $source, array $extras = [])
{
return $this->dispatch($this->marshal($command, $source, $extras));
}
/**
* Marshal a command from the given array.
*
* @param string $command
* @param array $array
*
* @return mixed
*/
protected function marshalFromArray($command, array $array)
{
return $this->marshal($command, new Collection(), $array);
}
/**
* Marshal a command from the given array accessible object.
*
* @param string $command
* @param \ArrayAccess $source
* @param array $extras
*
* @return mixed
*/
protected function marshal($command, ArrayAccess $source, array $extras = [])
{
$injected = [];
$reflection = new ReflectionClass($command);
if ($constructor = $reflection->getConstructor()) {
$injected = array_map(function ($parameter) use ($command, $source, $extras) {
return $this->getParameterValueForCommand($command, $source, $parameter, $extras);
}, $constructor->getParameters());
}
return $reflection->newInstanceArgs($injected);
}
/**
* Get a parameter value for a marshaled command.
*
* @param string $command
* @param \ArrayAccess $source
* @param \ReflectionParameter $parameter
* @param array $extras
*
* @return mixed
*/
protected function getParameterValueForCommand($command, ArrayAccess $source,
ReflectionParameter $parameter, array $extras = [])
{
if (array_key_exists($parameter->name, $extras)) {
return $extras[$parameter->name];
}
if (isset($source[$parameter->name])) {
return $source[$parameter->name];
}
if ($parameter->isDefaultValueAvailable()) {
return $parameter->getDefaultValue();
}
MarshalException::whileMapping($command, $parameter);
}
/**
* Dispatch a command to its appropriate handler.
*
* @param mixed $command
* @param \Closure|null $afterResolving
*
* @return mixed
*/
public function dispatch($command, Closure $afterResolving = null)
{
if ($this->queueResolver && $this->commandShouldBeQueued($command)) {
return $this->dispatchToQueue($command);
} else {
return $this->dispatchNow($command, $afterResolving);
}
}
/**
* Dispatch a command to its appropriate handler in the current process.
*
* @param mixed $command
* @param \Closure|null $afterResolving
*
* @return mixed
*/
public function dispatchNow($command, Closure $afterResolving = null)
{
return $this->pipeline->send($command)->through($this->pipes)->then(function ($command) use ($afterResolving) {
if ($command instanceof SelfHandling) {
return $this->container->call([$command, 'handle']);
}
$handler = $this->resolveHandler($command);
if ($afterResolving) {
call_user_func($afterResolving, $handler);
}
return call_user_func(
[$handler, $this->getHandlerMethod($command)], $command
);
});
}
/**
* Determine if the given command should be queued.
*
* @param mixed $command
*
* @return bool
*/
protected function commandShouldBeQueued($command)
{
if ($command instanceof ShouldQueue) {
return true;
}
return (new ReflectionClass($this->getHandlerClass($command)))->implementsInterface(
'Illuminate\Contracts\Queue\ShouldQueue'
);
}
/**
* Dispatch a command to its appropriate handler behind a queue.
*
* @param mixed $command
*
* @throws \RuntimeException
*
* @return mixed
*/
public function dispatchToQueue($command)
{
$queue = call_user_func($this->queueResolver);
if (!$queue instanceof Queue) {
throw new RuntimeException('Queue resolver did not return a Queue implementation.');
}
if (method_exists($command, 'queue')) {
return $command->queue($queue, $command);
} else {
return $this->pushCommandToQueue($queue, $command);
}
}
/**
* Push the command onto the given queue instance.
*
* @param \Illuminate\Contracts\Queue\Queue $queue
* @param mixed $command
*
* @return mixed
*/
protected function pushCommandToQueue($queue, $command)
{
if (isset($command->queue, $command->delay)) {
return $queue->laterOn($command->queue, $command->delay, $command);
}
if (isset($command->queue)) {
return $queue->pushOn($command->queue, $command);
}
if (isset($command->delay)) {
return $queue->later($command->delay, $command);
}
return $queue->push($command);
}
/**
* Get the handler instance for the given command.
*
* @param mixed $command
*
* @return mixed
*/
public function resolveHandler($command)
{
if ($command instanceof SelfHandling) {
return $command;
}
return $this->container->make($this->getHandlerClass($command));
}
/**
* Get the handler class for the given command.
*
* @param mixed $command
*
* @return string
*/
public function getHandlerClass($command)
{
if ($command instanceof SelfHandling) {
return get_class($command);
}
return $this->inflectSegment($command, 0);
}
/**
* Get the handler method for the given command.
*
* @param mixed $command
*
* @return string
*/
public function getHandlerMethod($command)
{
if ($command instanceof SelfHandling) {
return 'handle';
}
return $this->inflectSegment($command, 1);
}
/**
* Get the given handler segment for the given command.
*
* @param mixed $command
* @param int $segment
*
* @return string
*/
protected function inflectSegment($command, $segment)
{
$className = get_class($command);
if (isset($this->mappings[$className])) {
return $this->getMappingSegment($className, $segment);
} elseif ($this->mapper) {
return $this->getMapperSegment($command, $segment);
}
throw new InvalidArgumentException("No handler registered for command [{$className}]");
}
/**
* Get the given segment from a given class handler.
*
* @param string $className
* @param int $segment
*
* @return string
*/
protected function getMappingSegment($className, $segment)
{
return explode('@', $this->mappings[$className])[$segment];
}
/**
* Get the given segment from a given class handler using the custom mapper.
*
* @param mixed $command
* @param int $segment
*
* @return string
*/
protected function getMapperSegment($command, $segment)
{
return explode('@', call_user_func($this->mapper, $command))[$segment];
}
/**
* Register command-to-handler mappings.
*
* @param array $commands
*
* @return void
*/
public function maps(array $commands)
{
$this->mappings = array_merge($this->mappings, $commands);
}
/**
* Register a fallback mapper callback.
*
* @param \Closure $mapper
*
* @return void
*/
public function mapUsing(Closure $mapper)
{
$this->mapper = $mapper;
}
/**
* Map the command to a handler within a given root namespace.
*
* @param mixed $command
* @param string $commandNamespace
* @param string $handlerNamespace
*
* @return string
*/
public static function simpleMapping($command, $commandNamespace, $handlerNamespace)
{
$command = str_replace($commandNamespace, '', get_class($command));
return $handlerNamespace.'\\'.trim($command, '\\').'Handler@handle';
}
/**
* Set the pipes through which commands should be piped before dispatching.
*
* @param array $pipes
*
* @return $this
*/
public function pipeThrough(array $pipes)
{
$this->pipes = $pipes;
return $this;
}
}

View File

@@ -0,0 +1,53 @@
<?php
namespace Collective\Bus;
use Closure;
interface HandlerResolver
{
/**
* Get the handler instance for the given command.
*
* @param mixed $command
*
* @return mixed
*/
public function resolveHandler($command);
/**
* Get the handler class for the given command.
*
* @param mixed $command
*
* @return string
*/
public function getHandlerClass($command);
/**
* Get the handler method for the given command.
*
* @param mixed $command
*
* @return string
*/
public function getHandlerMethod($command);
/**
* Register command to handler mappings.
*
* @param array $commands
*
* @return void
*/
public function maps(array $commands);
/**
* Register a fallback mapper callback.
*
* @param \Closure $mapper
*
* @return void
*/
public function mapUsing(Closure $mapper);
}

View File

@@ -0,0 +1,24 @@
<?php
namespace Collective\Bus;
use ReflectionParameter;
use RuntimeException;
class MarshalException extends RuntimeException
{
/**
* Throw a new exception.
*
* @param string $command
* @param \ReflectionParameter $parameter
*
* @throws static
*
* @return void
*/
public static function whileMapping($command, ReflectionParameter $parameter)
{
throw new static("Unable to map parameter [{$parameter->name}] to command [{$command}]");
}
}

View File

@@ -0,0 +1,48 @@
<?php
namespace Collective\Bus;
trait Queueable
{
/**
* The name of the queue the job should be sent to.
*
* @var string
*/
public $queue;
/**
* The seconds before the job should be made available.
*
* @var int
*/
public $delay;
/**
* Set the desired queue for the job.
*
* @param string $queue
*
* @return $this
*/
public function onQueue($queue)
{
$this->queue = $queue;
return $this;
}
/**
* Set the desired delay for the job.
*
* @param int $delay
*
* @return $this
*/
public function delay($delay)
{
$this->delay = $delay;
return $this;
}
}

View File

@@ -0,0 +1,172 @@
<?php
use Collective\Bus\Dispatcher;
use Illuminate\Container\Container;
use Mockery as m;
class BusDispatcherTest extends PHPUnit_Framework_TestCase
{
public function tearDown()
{
m::close();
}
public function testBasicDispatchingOfCommandsToHandlers()
{
$container = new Container();
$handler = m::mock('StdClass');
$handler->shouldReceive('handle')->once()->andReturn('foo');
$container->instance('Handler', $handler);
$dispatcher = new Dispatcher($container);
$dispatcher->mapUsing(function () { return 'Handler@handle'; });
$result = $dispatcher->dispatch(new BusDispatcherTestBasicCommand());
$this->assertEquals('foo', $result);
}
public function testCommandsThatShouldQueueIsQueued()
{
$container = new Container();
$dispatcher = new Dispatcher($container, function () {
$mock = m::mock('Illuminate\Contracts\Queue\Queue');
$mock->shouldReceive('push')->once();
return $mock;
});
$dispatcher->dispatch(m::mock('Illuminate\Contracts\Queue\ShouldQueue'));
}
public function testCommandsThatShouldQueueIsQueuedUsingCustomHandler()
{
$container = new Container();
$dispatcher = new Dispatcher($container, function () {
$mock = m::mock('Illuminate\Contracts\Queue\Queue');
$mock->shouldReceive('push')->once();
return $mock;
});
$dispatcher->dispatch(new BusDispatcherTestCustomQueueCommand());
}
public function testCommandsThatShouldQueueIsQueuedUsingCustomQueueAndDelay()
{
$container = new Container();
$dispatcher = new Dispatcher($container, function () {
$mock = m::mock('Illuminate\Contracts\Queue\Queue');
$mock->shouldReceive('laterOn')->once()->with('foo', 10, m::type('BusDispatcherTestSpecificQueueAndDelayCommand'));
return $mock;
});
$dispatcher->dispatch(new BusDispatcherTestSpecificQueueAndDelayCommand());
}
public function testHandlersThatShouldQueueIsQueued()
{
$container = new Container();
$dispatcher = new Dispatcher($container, function () {
$mock = m::mock('Illuminate\Contracts\Queue\Queue');
$mock->shouldReceive('push')->once();
return $mock;
});
$dispatcher->mapUsing(function () { return 'BusDispatcherTestQueuedHandler@handle'; });
$dispatcher->dispatch(new BusDispatcherTestBasicCommand());
}
public function testDispatchNowShouldNeverQueue()
{
$container = new Container();
$handler = m::mock('StdClass');
$handler->shouldReceive('handle')->once()->andReturn('foo');
$container->instance('Handler', $handler);
$dispatcher = new Dispatcher($container);
$dispatcher->mapUsing(function () { return 'Handler@handle'; });
$result = $dispatcher->dispatch(m::mock('Illuminate\Contracts\Queue\ShouldQueue'));
$this->assertEquals('foo', $result);
}
public function testDispatchShouldCallAfterResolvingIfCommandNotQueued()
{
$container = new Container();
$handler = m::mock('StdClass')->shouldIgnoreMissing();
$handler->shouldReceive('after')->once();
$container->instance('Handler', $handler);
$dispatcher = new Dispatcher($container);
$dispatcher->mapUsing(function () { return 'Handler@handle'; });
$dispatcher->dispatch(new BusDispatcherTestBasicCommand(), function ($handler) { $handler->after(); });
}
public function testDispatchingFromArray()
{
$instance = new Dispatcher(new Container());
$result = $instance->dispatchFromArray('BusDispatcherTestSelfHandlingCommand', ['firstName' => 'taylor', 'lastName' => 'otwell']);
$this->assertEquals('taylor otwell', $result);
}
public function testMarshallArguments()
{
$instance = new Dispatcher(new Container());
$result = $instance->dispatchFromArray('BusDispatcherTestArgumentMapping', ['flag' => false, 'emptyString' => '']);
$this->assertTrue($result);
}
}
class BusDispatcherTestBasicCommand
{
}
class BusDispatcherTestArgumentMapping implements Illuminate\Contracts\Bus\SelfHandling
{
public $flag, $emptyString;
public function __construct($flag, $emptyString)
{
$this->flag = $flag;
$this->emptyString = $emptyString;
}
public function handle()
{
return true;
}
}
class BusDispatcherTestSelfHandlingCommand implements Illuminate\Contracts\Bus\SelfHandling
{
public $firstName, $lastName;
public function __construct($firstName, $lastName)
{
$this->firstName = $firstName;
$this->lastName = $lastName;
}
public function handle()
{
return $this->firstName.' '.$this->lastName;
}
}
class BusDispatcherTestQueuedHandler implements Illuminate\Contracts\Queue\ShouldQueue
{
}
class BusDispatcherTestCustomQueueCommand implements Illuminate\Contracts\Queue\ShouldQueue
{
public function queue($queue, $command)
{
$queue->push($command);
}
}
class BusDispatcherTestSpecificQueueAndDelayCommand implements Illuminate\Contracts\Queue\ShouldQueue
{
public $queue = 'foo';
public $delay = 10;
}