1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace DoctrineModule\Service; |
6
|
|
|
|
7
|
|
|
use Interop\Container\ContainerInterface; |
8
|
|
|
use Laminas\ServiceManager\Factory\FactoryInterface; |
9
|
|
|
use Laminas\Stdlib\AbstractOptions; |
10
|
|
|
use RuntimeException; |
11
|
|
|
use function sprintf; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Base ServiceManager factory to be extended |
15
|
|
|
*/ |
16
|
|
|
// phpcs:disable SlevomatCodingStandard.Classes.SuperfluousAbstractClassNaming |
17
|
|
|
abstract class AbstractFactory implements FactoryInterface |
18
|
|
|
{ |
19
|
|
|
// phpcs:enable SlevomatCodingStandard.Classes.SuperfluousAbstractClassNaming |
20
|
|
|
/** |
21
|
|
|
* Would normally be set to orm | odm |
22
|
|
|
* |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected $mappingType; |
26
|
|
|
|
27
|
|
|
/** @var string */ |
28
|
|
|
protected $name; |
29
|
|
|
|
30
|
|
|
/** @var AbstractOptions */ |
31
|
|
|
protected $options; |
32
|
|
|
|
33
|
13 |
|
public function __construct(string $name) |
34
|
|
|
{ |
35
|
13 |
|
$this->name = $name; |
36
|
13 |
|
} |
37
|
|
|
|
38
|
13 |
|
public function getName() : string |
39
|
|
|
{ |
40
|
13 |
|
return $this->name; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Would normally be set to orm | odm |
45
|
|
|
*/ |
46
|
13 |
|
public function getMappingType() : string |
47
|
|
|
{ |
48
|
13 |
|
return (string) $this->mappingType; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Gets options from configuration based on name. |
53
|
|
|
* |
54
|
|
|
* @throws RuntimeException |
55
|
|
|
*/ |
56
|
13 |
|
public function getOptions(ContainerInterface $container, string $key, ?string $name = null) : AbstractOptions |
57
|
|
|
{ |
58
|
13 |
|
if ($name === null) { |
59
|
13 |
|
$name = $this->getName(); |
60
|
|
|
} |
61
|
|
|
|
62
|
13 |
|
$options = $container->get('config'); |
63
|
13 |
|
$options = $options['doctrine']; |
64
|
13 |
|
$mappingType = $this->getMappingType(); |
65
|
13 |
|
if ($mappingType) { |
66
|
|
|
$options = $options[$mappingType]; |
67
|
|
|
} |
68
|
|
|
|
69
|
13 |
|
$options = $options[$key][$name] ?? null; |
70
|
|
|
|
71
|
13 |
|
if ($options === null) { |
72
|
|
|
throw new RuntimeException( |
73
|
|
|
sprintf( |
74
|
|
|
'Options with name "%s" could not be found in "doctrine.%s".', |
75
|
|
|
$name, |
76
|
|
|
$key |
77
|
|
|
) |
78
|
|
|
); |
79
|
|
|
} |
80
|
|
|
|
81
|
13 |
|
$optionsClass = $this->getOptionsClass(); |
82
|
|
|
|
83
|
13 |
|
return new $optionsClass($options); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Get the class name of the options associated with this factory. |
88
|
|
|
* |
89
|
|
|
* @abstract |
90
|
|
|
*/ |
91
|
|
|
abstract public function getOptionsClass() : string; |
92
|
|
|
} |
93
|
|
|
|