1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace TogglJira\Handler; |
5
|
|
|
|
6
|
|
|
use Psr\Container\ContainerInterface; |
7
|
|
|
use Psr\Log\LoggerAwareInterface; |
8
|
|
|
use Psr\Log\LoggerAwareTrait; |
9
|
|
|
use TogglJira\Exception\CommandNotFoundException; |
10
|
|
|
use Zend\Console\Adapter\AdapterInterface; |
11
|
|
|
use Zend\Console\Request; |
12
|
|
|
use ZF\Console\Route; |
13
|
|
|
|
14
|
|
|
class CommandHandler implements LoggerAwareInterface |
15
|
|
|
{ |
16
|
|
|
use LoggerAwareTrait; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var ContainerInterface |
20
|
|
|
*/ |
21
|
|
|
private $container; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param ContainerInterface $container |
25
|
|
|
*/ |
26
|
4 |
|
public function __construct(ContainerInterface $container) |
27
|
|
|
{ |
28
|
4 |
|
$this->container = $container; |
29
|
4 |
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param Route $route |
33
|
|
|
* @param AdapterInterface $console |
34
|
|
|
* @return int |
35
|
|
|
* @throws \Zend\Console\Exception\RuntimeException |
36
|
|
|
* @throws \Psr\Container\NotFoundExceptionInterface |
37
|
|
|
* @throws \Psr\Container\ContainerExceptionInterface |
38
|
|
|
* @throws CommandNotFoundException |
39
|
|
|
*/ |
40
|
3 |
|
public function __invoke(Route $route, AdapterInterface $console): int |
41
|
|
|
{ |
42
|
3 |
|
$name = $route->getName(); |
43
|
3 |
|
$matches = $route->getMatches(); |
44
|
|
|
|
45
|
3 |
|
if (!$this->container->has($name)) { |
46
|
1 |
|
throw new CommandNotFoundException("The command {$name} was not found in the service manager.", 1); |
47
|
|
|
} |
48
|
|
|
|
49
|
2 |
|
$command = $this->container->get($name); |
50
|
|
|
/** @var Request $request */ |
51
|
2 |
|
$request = $this->container->get(Request::class); |
52
|
2 |
|
$request->getParams()->fromArray($matches); |
53
|
|
|
|
54
|
|
|
try { |
55
|
2 |
|
return $command->execute($request, $console); |
56
|
1 |
|
} catch (\Exception $e) { |
57
|
1 |
|
$this->logger->error($e->getMessage(), ['context' => $e]); |
58
|
|
|
} |
59
|
|
|
|
60
|
1 |
|
return 1; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|