This repository has been archived by the owner on Jan 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathDoctrineAdapterModel.php
155 lines (139 loc) · 3.91 KB
/
DoctrineAdapterModel.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
/**
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
* @copyright Copyright (c) 2013 Zend Technologies USA Inc. (http://www.zend.com)
*/
namespace ZF\Apigility\Admin\Model;
use ZF\Configuration\ConfigResource;
class DoctrineAdapterModel
{
/**
* @var ConfigResource
*/
protected $globalConfig;
/**
* @var ConfigResource
*/
protected $localConfig;
/**
* @param ConfigResource $globalConfig
* @param ConfigResource $localConfig
*/
public function __construct(ConfigResource $globalConfig, ConfigResource $localConfig)
{
$this->globalConfig = $globalConfig;
$this->localConfig = $localConfig;
}
/**
* Create Doctrine adapter configuration
*
* @param $name
* @param array $adapterConfig
* @return DoctrineAdapterEntity
*/
public function create($name, array $adapterConfig)
{
$key = "doctrine.connection.{$name}";
$this->globalConfig->patchKey($key, []);
$this->localConfig->patchKey($key, $adapterConfig);
return new DoctrineAdapterEntity($name, $adapterConfig);
}
/**
* Update an existing Doctrine adapter
*
* @param $name
* @param array $adapterConfig
* @return DoctrineAdapterEntity
*/
public function update($name, array $adapterConfig)
{
return $this->create($name, $adapterConfig);
}
/**
* Remove a named adapter
*
* @param $name
* @return bool
*/
public function remove($name)
{
$key = "doctrine.connection.{$name}";
$this->globalConfig->deleteKey($key);
$this->localConfig->deleteKey($key);
return true;
}
/**
* Retrieve all named adapters
*
* @return array|bool
*/
public function fetchAll()
{
$fromConfigFile = $this->localConfig->fetch(true);
if (isset($fromConfigFile['doctrine']['connection'])
&& is_array($fromConfigFile['doctrine']['connection'])
) {
foreach ($fromConfigFile['doctrine']['connection'] as $connection) {
if (! is_array($connection)) {
return false;
}
if (! $this->isOrmAdapter($connection) && ! $this->isOdmAdapter($connection)) {
return false;
}
}
$config = $fromConfigFile['doctrine']['connection'];
} else {
return false;
}
$adapters = [];
foreach ($config as $name => $adapterConfig) {
$adapters[] = new DoctrineAdapterEntity($name, $adapterConfig);
}
return $adapters;
}
/**
* Fetch configuration details for a named adapter
*
* @param $name
* @return bool|DoctrineAdapterEntity
*/
public function fetch($name)
{
$config = $this->localConfig->fetch(true);
if (! isset($config['doctrine']['connection'][$name])
|| ! is_array($config['doctrine']['connection'][$name])
) {
return false;
}
return new DoctrineAdapterEntity($name, $config['doctrine']['connection'][$name]);
}
/**
* Does the connection represent an ORM adapter?
*
* To be an ORM adapter, "driverClass" MUST be specified in the
* configuration.
*
* @param array $connection
* @return bool
*/
private function isOrmAdapter(array $connection)
{
return isset($connection['driverClass']);
}
/**
* Does the connection represent an ODM adapter?
*
* To be an ODM adapter, one of "connectionString" OR "dbname" MUST be
* specified in the configuration.
*
* @param array $connection
* @return bool
*/
private function isOdmAdapter(array $connection)
{
return (
isset($connection['connectionString'])
|| isset($connection['dbname'])
);
}
}