Skip to content

Commit 933696f

Browse files
committed
fix all failing tests for laravel 5.2 support
1 parent f7da9bf commit 933696f

File tree

58 files changed

+472
-105
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+472
-105
lines changed

src/Darryldecode/Backend/BackendServiceProvider.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Illuminate\Filesystem\Filesystem;
77
use Illuminate\Routing\Router;
88
use Illuminate\Support\ServiceProvider;
9+
use Darryldecode\Backend\Base\Services\Bus\Dispatcher;
910

1011
class BackendServiceProvider extends ServiceProvider {
1112

@@ -102,6 +103,11 @@ public function bootBackend()
102103
*/
103104
public function register()
104105
{
105-
106+
$this->app->singleton('Darryldecode\Backend\Base\Services\Bus\Dispatcher', function ($app) {
107+
return new Dispatcher($app);
108+
});
109+
$this->app->alias(
110+
'Darryldecode\Backend\Base\Services\Bus\Dispatcher', 'Darryldecode\Backend\Base\Contracts\Bus\Dispatcher'
111+
);
106112
}
107113
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace Darryldecode\Backend\Base\Contracts\Bus;
4+
5+
use ArrayAccess;
6+
7+
interface Dispatcher
8+
{
9+
/**
10+
* Marshal a command and dispatch it to its appropriate handler.
11+
*
12+
* @param mixed $command
13+
* @param array $array
14+
* @return mixed
15+
*/
16+
public function dispatchFromArray($command, array $array);
17+
18+
/**
19+
* Marshal a command and dispatch it to its appropriate handler.
20+
*
21+
* @param mixed $command
22+
* @param \ArrayAccess $source
23+
* @param array $extras
24+
* @return mixed
25+
*/
26+
public function dispatchFrom($command, ArrayAccess $source, array $extras = []);
27+
28+
/**
29+
* Dispatch a command to its appropriate handler.
30+
*
31+
* @param mixed $command
32+
* @return mixed
33+
*/
34+
public function dispatch($command);
35+
36+
/**
37+
* Dispatch a command to its appropriate handler in the current process.
38+
*
39+
* @param mixed $command
40+
* @return mixed
41+
*/
42+
public function dispatchNow($command);
43+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace Darryldecode\Backend\Base\Contracts\Bus;
4+
5+
use Closure;
6+
7+
interface HandlerResolver
8+
{
9+
/**
10+
* Get the handler instance for the given command.
11+
*
12+
* @param mixed $command
13+
* @return mixed
14+
*/
15+
public function resolveHandler($command);
16+
17+
/**
18+
* Get the handler class for the given command.
19+
*
20+
* @param mixed $command
21+
* @return string
22+
*/
23+
public function getHandlerClass($command);
24+
25+
/**
26+
* Get the handler method for the given command.
27+
*
28+
* @param mixed $command
29+
* @return string
30+
*/
31+
public function getHandlerMethod($command);
32+
33+
/**
34+
* Register command to handler mappings.
35+
*
36+
* @param array $commands
37+
* @return void
38+
*/
39+
public function maps(array $commands);
40+
41+
/**
42+
* Register a fallback mapper callback.
43+
*
44+
* @param \Closure $mapper
45+
* @return void
46+
*/
47+
public function mapUsing(Closure $mapper);
48+
}

src/Darryldecode/Backend/Base/Controllers/BaseController.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,14 @@ public function triggerBeforeBackendHook()
3636
$hook($this->user);
3737
}
3838
}
39+
40+
public function dispatchFromArray($command, array $array)
41+
{
42+
return app('Darryldecode\Backend\Base\Contracts\Bus\Dispatcher')->dispatchFromArray($command,$array);
43+
}
44+
45+
public function dispatchFrom($command, \ArrayAccess $source, array $extras)
46+
{
47+
return app('Darryldecode\Backend\Base\Contracts\Bus\Dispatcher')->dispatchFrom($command,$source,$extras);
48+
}
3949
}
Lines changed: 239 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,239 @@
1+
<?php
2+
3+
namespace Darryldecode\Backend\Base\Services\Bus;
4+
5+
use Closure;
6+
use ArrayAccess;
7+
use ReflectionClass;
8+
use ReflectionParameter;
9+
use InvalidArgumentException;
10+
use Illuminate\Support\Collection;
11+
use Illuminate\Contracts\Container\Container;
12+
use Darryldecode\Backend\Base\Contracts\Bus\Dispatcher as DispatcherContract;
13+
14+
class Dispatcher implements DispatcherContract
15+
{
16+
/**
17+
* The container implementation.
18+
*
19+
* @var \Illuminate\Contracts\Container\Container
20+
*/
21+
protected $container;
22+
23+
/**
24+
* All of the command-to-handler mappings.
25+
*
26+
* @var array
27+
*/
28+
protected $mappings = [];
29+
30+
/**
31+
* The fallback mapping Closure.
32+
*
33+
* @var \Closure
34+
*/
35+
protected $mapper;
36+
37+
/**
38+
* Create a new command dispatcher instance.
39+
*
40+
* @param \Illuminate\Contracts\Container\Container $container
41+
* @return void
42+
*/
43+
public function __construct(Container $container)
44+
{
45+
$this->container = $container;
46+
}
47+
48+
/**
49+
* Marshal a command and dispatch it to its appropriate handler.
50+
*
51+
* @param mixed $command
52+
* @param array $array
53+
* @return mixed
54+
*/
55+
public function dispatchFromArray($command, array $array)
56+
{
57+
return $this->dispatch($this->marshalFromArray($command, $array));
58+
}
59+
60+
/**
61+
* Marshal a command and dispatch it to its appropriate handler.
62+
*
63+
* @param mixed $command
64+
* @param \ArrayAccess $source
65+
* @param array $extras
66+
* @return mixed
67+
*/
68+
public function dispatchFrom($command, ArrayAccess $source, array $extras = [])
69+
{
70+
return $this->dispatch($this->marshal($command, $source, $extras));
71+
}
72+
73+
/**
74+
* Marshal a command from the given array.
75+
*
76+
* @param string $command
77+
* @param array $array
78+
* @return mixed
79+
*/
80+
protected function marshalFromArray($command, array $array)
81+
{
82+
return $this->marshal($command, new Collection, $array);
83+
}
84+
85+
/**
86+
* Marshal a command from the given array accessible object.
87+
*
88+
* @param string $command
89+
* @param \ArrayAccess $source
90+
* @param array $extras
91+
* @return mixed
92+
*/
93+
protected function marshal($command, ArrayAccess $source, array $extras = [])
94+
{
95+
$injected = [];
96+
97+
$reflection = new ReflectionClass($command);
98+
99+
if ($constructor = $reflection->getConstructor()) {
100+
$injected = array_map(function ($parameter) use ($command, $source, $extras) {
101+
return $this->getParameterValueForCommand($command, $source, $parameter, $extras);
102+
103+
}, $constructor->getParameters());
104+
}
105+
106+
return $reflection->newInstanceArgs($injected);
107+
}
108+
109+
/**
110+
* Get a parameter value for a marshaled command.
111+
*
112+
* @param string $command
113+
* @param \ArrayAccess $source
114+
* @param \ReflectionParameter $parameter
115+
* @param array $extras
116+
* @return mixed
117+
*/
118+
protected function getParameterValueForCommand($command, ArrayAccess $source,
119+
ReflectionParameter $parameter, array $extras = [])
120+
{
121+
if (array_key_exists($parameter->name, $extras)) {
122+
return $extras[$parameter->name];
123+
}
124+
125+
if (isset($source[$parameter->name])) {
126+
return $source[$parameter->name];
127+
}
128+
129+
if ($parameter->isDefaultValueAvailable()) {
130+
return $parameter->getDefaultValue();
131+
}
132+
133+
MarshalException::whileMapping($command, $parameter);
134+
}
135+
136+
/**
137+
* Dispatch a command to its appropriate handler.
138+
*
139+
* @param mixed $command
140+
* @return mixed
141+
*/
142+
public function dispatch($command)
143+
{
144+
return $this->dispatchNow($command);
145+
}
146+
147+
/**
148+
* Dispatch a command to its appropriate handler in the current process.
149+
*
150+
* @param mixed $command
151+
* @return mixed
152+
*/
153+
public function dispatchNow($command)
154+
{
155+
return $this->container->call([$command, 'handle']);
156+
}
157+
158+
159+
/**
160+
* Get the given handler segment for the given command.
161+
*
162+
* @param mixed $command
163+
* @param int $segment
164+
* @return string
165+
*/
166+
protected function inflectSegment($command, $segment)
167+
{
168+
$className = get_class($command);
169+
170+
if (isset($this->mappings[$className])) {
171+
return $this->getMappingSegment($className, $segment);
172+
} elseif ($this->mapper) {
173+
return $this->getMapperSegment($command, $segment);
174+
}
175+
176+
throw new InvalidArgumentException("No handler registered for command [{$className}]");
177+
}
178+
179+
/**
180+
* Get the given segment from a given class handler.
181+
*
182+
* @param string $className
183+
* @param int $segment
184+
* @return string
185+
*/
186+
protected function getMappingSegment($className, $segment)
187+
{
188+
return explode('@', $this->mappings[$className])[$segment];
189+
}
190+
191+
/**
192+
* Get the given segment from a given class handler using the custom mapper.
193+
*
194+
* @param mixed $command
195+
* @param int $segment
196+
* @return string
197+
*/
198+
protected function getMapperSegment($command, $segment)
199+
{
200+
return explode('@', call_user_func($this->mapper, $command))[$segment];
201+
}
202+
203+
/**
204+
* Register command-to-handler mappings.
205+
*
206+
* @param array $commands
207+
* @return void
208+
*/
209+
public function maps(array $commands)
210+
{
211+
$this->mappings = array_merge($this->mappings, $commands);
212+
}
213+
214+
/**
215+
* Register a fallback mapper callback.
216+
*
217+
* @param \Closure $mapper
218+
* @return void
219+
*/
220+
public function mapUsing(Closure $mapper)
221+
{
222+
$this->mapper = $mapper;
223+
}
224+
225+
/**
226+
* Map the command to a handler within a given root namespace.
227+
*
228+
* @param mixed $command
229+
* @param string $commandNamespace
230+
* @param string $handlerNamespace
231+
* @return string
232+
*/
233+
public static function simpleMapping($command, $commandNamespace, $handlerNamespace)
234+
{
235+
$command = str_replace($commandNamespace, '', get_class($command));
236+
237+
return $handlerNamespace.'\\'.trim($command, '\\').'Handler@handle';
238+
}
239+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Darryldecode\Backend\Base\Services\Bus;
4+
5+
use RuntimeException;
6+
use ReflectionParameter;
7+
8+
class MarshalException extends RuntimeException
9+
{
10+
/**
11+
* Throw new a new exception.
12+
*
13+
* @param string $command
14+
* @param \ReflectionParameter $parameter
15+
* @return void
16+
*/
17+
public static function whileMapping($command, ReflectionParameter $parameter)
18+
{
19+
throw new static("Unable to map parameter [{$parameter->name}] to command [{$command}]");
20+
}
21+
}

0 commit comments

Comments
 (0)