|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Magium\TestCase\Configurable; |
|
4
|
|
|
|
|
5
|
|
|
use Magium\Util\Log\LoggerInterface; |
|
6
|
|
|
use Zend\Di\Di; |
|
7
|
|
|
|
|
8
|
|
|
class InstructionsCollection |
|
9
|
|
|
{ |
|
10
|
|
|
|
|
11
|
|
|
protected $instructions = []; |
|
12
|
|
|
protected $container; |
|
13
|
|
|
protected $interpolator; |
|
14
|
|
|
protected $logger; |
|
15
|
|
|
|
|
16
|
|
|
public function __construct( |
|
17
|
|
|
Di $container, |
|
18
|
|
|
Interpolator $interpolator, |
|
19
|
|
|
LoggerInterface $logger |
|
20
|
|
|
) |
|
21
|
|
|
{ |
|
22
|
|
|
$this->container = $container; |
|
23
|
|
|
$this->interpolator = $interpolator; |
|
24
|
|
|
$this->logger = $logger; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
public function addInstruction(InstructionInterface $instruction) |
|
28
|
|
|
{ |
|
29
|
|
|
$this->instructions[] = $instruction; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function execute() |
|
33
|
|
|
{ |
|
34
|
|
|
foreach ($this->instructions as $instruction) { |
|
35
|
|
|
if ($instruction instanceof InstructionInterface) { |
|
36
|
|
|
$instance = $this->container->get($instruction->getClassName()); |
|
37
|
|
|
$callback = [$instance, $instruction->getMethod()]; |
|
38
|
|
|
if (!is_callable($callback)) { |
|
39
|
|
|
throw new InvalidInstructionException('Unable to execute instruction'); |
|
40
|
|
|
} |
|
41
|
|
|
$params = []; |
|
42
|
|
|
$callParams = $instruction->getParams(); |
|
43
|
|
|
if ($callParams) { |
|
44
|
|
|
$params = $callParams; |
|
45
|
|
|
} |
|
46
|
|
|
$this->logger->info(sprintf('Executing %s', $instruction->getClassName()), [ |
|
47
|
|
|
'class' => get_class($instance), |
|
48
|
|
|
'params' => json_encode($callParams) |
|
49
|
|
|
]); |
|
50
|
|
|
try { |
|
51
|
|
|
call_user_func_array($callback, $params); |
|
52
|
|
|
} catch (\Exception $e) { |
|
53
|
|
|
|
|
54
|
|
|
$this->logger->err($e->getMessage()); |
|
55
|
|
|
throw $e; |
|
56
|
|
|
} |
|
57
|
|
|
$this->logger->info(sprintf('%s completed', $instruction->getClassName())); |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
|
|
63
|
|
|
} |
|
64
|
|
|
|