1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* ServiceFactory.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; |
12
|
|
|
|
13
|
|
|
use Kocuj\Di\ArgumentParser\ArgumentParserFactoryInterface; |
14
|
|
|
use Kocuj\Di\Container\ContainerInterface; |
15
|
|
|
use Kocuj\Di\Service\Shared\Shared; |
16
|
|
|
use Kocuj\Di\Service\Standard\Standard; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Service factory |
20
|
|
|
* |
21
|
|
|
* @package Kocuj\Di\Service |
22
|
|
|
*/ |
23
|
|
|
class ServiceFactory implements ServiceFactoryInterface |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* Service argument parser factory |
27
|
|
|
* |
28
|
|
|
* @var ArgumentParserFactoryInterface |
29
|
|
|
*/ |
30
|
|
|
private $argumentParserFactory; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Constructor |
34
|
|
|
* |
35
|
|
|
* @param ArgumentParserFactoryInterface $argumentParserFactory Service argument parser factory |
36
|
|
|
* @codeCoverageIgnore |
37
|
|
|
*/ |
38
|
|
|
public function __construct(ArgumentParserFactoryInterface $argumentParserFactory) |
39
|
|
|
{ |
40
|
|
|
// remember arguments |
41
|
|
|
$this->argumentParserFactory = $argumentParserFactory; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Create standard or shared service |
46
|
|
|
* |
47
|
|
|
* @param ContainerInterface $container Dependency injection container for services |
48
|
|
|
* @param ServiceType $serviceType Service type |
49
|
|
|
* @param string $id Service identifier |
50
|
|
|
* @param string $source Source for service to create |
51
|
|
|
* @param array $arguments Service arguments to parse |
52
|
|
|
* @return ServiceInterface Service creator object |
53
|
|
|
* @throws Exception |
54
|
|
|
* @see \Kocuj\Di\Service\ServiceFactoryInterface::create() |
55
|
|
|
* @codeCoverageIgnore |
56
|
|
|
*/ |
57
|
|
|
public function create( |
58
|
|
|
ContainerInterface $container, |
59
|
|
|
ServiceType $serviceType, |
60
|
|
|
string $id, |
61
|
|
|
string $source, |
62
|
|
|
array $arguments = [] |
63
|
|
|
): ServiceInterface { |
64
|
|
|
// exit |
65
|
|
|
switch ($serviceType->getValue()) { |
66
|
|
|
case ServiceType::STANDARD: |
67
|
|
|
return new Standard($this->argumentParserFactory, $container, $id, $source, $arguments); |
68
|
|
|
case ServiceType::SHARED: |
69
|
|
|
return new Shared($this->argumentParserFactory, $container, $id, $source, $arguments); |
70
|
|
|
default: |
71
|
|
|
throw new Exception(sprintf('Unknown service type "%s"', $serviceType->getValue())); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|