1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Standard.php |
5
|
|
|
* |
6
|
|
|
* @author Dominik Kocuj |
7
|
|
|
* @license https://opensource.org/licenses/MIT The MIT License |
8
|
|
|
* @copyright Copyright (c) 2017-2018 kocuj.pl |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Kocuj\Di\Service\Standard; |
12
|
|
|
|
13
|
|
|
use Kocuj\Di\ArgumentParser\ArgumentParserFactoryInterface; |
14
|
|
|
use Kocuj\Di\Container\ContainerInterface; |
15
|
|
|
use Kocuj\Di\Service\ServiceInterface; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Standard service creator |
19
|
|
|
* |
20
|
|
|
* @package Kocuj\Di\Service\Standard |
21
|
|
|
*/ |
22
|
|
|
class Standard implements ServiceInterface |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* Service argument parser factory |
26
|
|
|
* |
27
|
|
|
* @var ArgumentParserFactoryInterface |
28
|
|
|
*/ |
29
|
|
|
private $argumentParserFactory; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Dependency injection container for services |
33
|
|
|
* |
34
|
|
|
* @var ContainerInterface |
35
|
|
|
*/ |
36
|
|
|
private $container; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Service identifier |
40
|
|
|
* |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
private $id; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Source for service to create |
47
|
|
|
* |
48
|
|
|
* @var string |
49
|
|
|
*/ |
50
|
|
|
private $source; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Service arguments to parse |
54
|
|
|
* |
55
|
|
|
* @var array |
56
|
|
|
*/ |
57
|
|
|
private $arguments; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Constructor |
61
|
|
|
* |
62
|
|
|
* @param ArgumentParserFactoryInterface $argumentParserFactory Service argument parser factory |
63
|
|
|
* @param ContainerInterface $container Dependency injection container for services |
64
|
|
|
* @param string $id Service identifier |
65
|
|
|
* @param string $source Source for service to create |
66
|
|
|
* @param array $arguments Service arguments to parse |
67
|
|
|
*/ |
68
|
12 |
|
public function __construct( |
69
|
|
|
ArgumentParserFactoryInterface $argumentParserFactory, |
70
|
|
|
ContainerInterface $container, |
71
|
|
|
string $id, |
72
|
|
|
string $source, |
73
|
|
|
array $arguments = [] |
74
|
|
|
) { |
75
|
|
|
// remember arguments |
76
|
12 |
|
$this->argumentParserFactory = $argumentParserFactory; |
77
|
12 |
|
$this->container = $container; |
78
|
12 |
|
$this->id = $id; |
79
|
12 |
|
$this->source = $source; |
80
|
12 |
|
$this->arguments = $arguments; |
81
|
12 |
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Get service |
85
|
|
|
* |
86
|
|
|
* @return object Service object |
87
|
|
|
* @see \Kocuj\Di\Service\ServiceInterface::getService() |
88
|
|
|
*/ |
89
|
12 |
|
public function getService() |
90
|
|
|
{ |
91
|
|
|
// parse arguments |
92
|
12 |
|
$parsedArgs = []; |
93
|
12 |
|
foreach ($this->arguments as $argument) { |
94
|
6 |
|
$obj = $this->argumentParserFactory->create($this->container, $this->id, $argument); |
95
|
6 |
|
$parsedArgs[] = $obj->parse(); |
96
|
|
|
} |
97
|
|
|
// execute service constructor |
98
|
12 |
|
$service = new $this->source(...$parsedArgs); |
99
|
|
|
// exit |
100
|
12 |
|
return $service; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|