|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ZfPhinx\Service; |
|
4
|
|
|
|
|
5
|
|
|
use Phinx\Config\Config; |
|
6
|
|
|
use Phinx\Console\PhinxApplication; |
|
7
|
|
|
use Zend\Db\Adapter\AdapterInterface; |
|
8
|
|
|
use Interop\Container\ContainerInterface; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Phinx service factory |
|
12
|
|
|
*/ |
|
13
|
|
|
class ZfPhinxServiceFactory |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @param ContainerInterface $container |
|
17
|
|
|
* @return ZfPhinxService |
|
18
|
|
|
*/ |
|
19
|
5 |
|
public function __invoke(ContainerInterface $container) |
|
20
|
|
|
{ |
|
21
|
5 |
|
$service = new ZfPhinxService( |
|
22
|
5 |
|
$this->getPhinxApplication(), |
|
23
|
5 |
|
$this->getConfig($container) |
|
24
|
1 |
|
); |
|
25
|
|
|
|
|
26
|
1 |
|
return $service; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Get Phinx application |
|
31
|
|
|
* |
|
32
|
|
|
* @return PhinxApplication |
|
33
|
|
|
*/ |
|
34
|
5 |
|
private function getPhinxApplication() |
|
35
|
|
|
{ |
|
36
|
5 |
|
return new PhinxApplication(); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Gets Phinx config |
|
41
|
|
|
* |
|
42
|
|
|
* @param ContainerInterface $container |
|
43
|
|
|
* @return Config |
|
44
|
|
|
*/ |
|
45
|
5 |
|
private function getConfig(ContainerInterface $container) |
|
46
|
|
|
{ |
|
47
|
5 |
|
$config = $container->get('Config'); |
|
48
|
|
|
|
|
49
|
5 |
|
if(!(array_key_exists('zfphinx', $config) && is_array($config['zfphinx']))) |
|
50
|
5 |
|
{ |
|
51
|
1 |
|
throw new Exception\RuntimeException('zfphinx config is not found'); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
4 |
|
return new Config($this->performConfig($container, $config['zfphinx'])); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Performs config array from ZF to Phinx structure |
|
59
|
|
|
* |
|
60
|
|
|
* @param ContainerInterface $container |
|
61
|
|
|
* @param array $config |
|
62
|
|
|
* @return array |
|
63
|
|
|
*/ |
|
64
|
4 |
|
private function performConfig(ContainerInterface $container, array $config) |
|
65
|
1 |
|
{ |
|
66
|
4 |
|
if(!(array_key_exists('environments', $config) && is_array($config['environments']))) |
|
67
|
4 |
|
{ |
|
68
|
2 |
|
throw new Exception\RuntimeException('zfphinx environment config is not found'); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
3 |
|
array_walk( |
|
72
|
3 |
|
$config['environments'], |
|
73
|
3 |
|
function(&$element, $key) use ($container) |
|
74
|
|
|
{ |
|
75
|
3 |
|
if(is_array($element) && array_key_exists('db_adapter', $element)) |
|
76
|
3 |
|
{ |
|
77
|
3 |
|
if(!$container->has($element['db_adapter'])) |
|
78
|
3 |
|
{ |
|
79
|
1 |
|
$message = sprintf( |
|
80
|
1 |
|
'Adapter for environment %s is not found', |
|
81
|
|
|
$key |
|
82
|
1 |
|
); |
|
83
|
1 |
|
throw new Exception\RuntimeException($message); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
2 |
|
$adapter = $container->get($element['db_adapter']); |
|
87
|
|
|
|
|
88
|
2 |
|
if(!$adapter instanceof AdapterInterface) |
|
89
|
2 |
|
{ |
|
90
|
1 |
|
$message = sprintf( |
|
91
|
1 |
|
'Adapter for environment %s must implement %s; %s given', |
|
92
|
1 |
|
$key, |
|
93
|
1 |
|
AdapterInterface::class, |
|
94
|
1 |
|
is_object($adapter) ? get_class($adapter) : gettype($adapter) |
|
95
|
1 |
|
); |
|
96
|
1 |
|
throw new Exception\RuntimeException($message); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
$connection = $adapter |
|
100
|
1 |
|
->getDriver() |
|
101
|
1 |
|
->getConnection(); |
|
102
|
|
|
|
|
103
|
1 |
|
$element['connection'] = $connection->getResource(); |
|
104
|
1 |
|
$element['name'] = $connection->getCurrentSchema(); |
|
105
|
1 |
|
} |
|
106
|
|
|
|
|
107
|
1 |
|
return $element; |
|
108
|
|
|
} |
|
109
|
3 |
|
); |
|
110
|
|
|
|
|
111
|
1 |
|
return $config; |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|