|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
4
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
5
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
6
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
7
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
8
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
9
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
10
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
11
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
12
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
13
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
14
|
|
|
* |
|
15
|
|
|
* This software consists of voluntary contributions made by many individuals |
|
16
|
|
|
* and is licensed under the MIT license. For more information, see |
|
17
|
|
|
* <http://www.doctrine-project.org>. |
|
18
|
|
|
*/ |
|
19
|
|
|
|
|
20
|
|
|
namespace DoctrineORMModule\Service; |
|
21
|
|
|
|
|
22
|
|
|
use Doctrine\DBAL\Connection; |
|
23
|
|
|
use Doctrine\DBAL\DriverManager; |
|
24
|
|
|
use Doctrine\DBAL\Types\Type; |
|
25
|
|
|
use DoctrineModule\Service\AbstractFactory; |
|
26
|
|
|
use DoctrineORMModule\Options\DBALConnection; |
|
27
|
|
|
use Interop\Container\ContainerInterface; |
|
28
|
|
|
use Zend\ServiceManager\ServiceLocatorInterface; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* DBAL Connection ServiceManager factory |
|
32
|
|
|
* |
|
33
|
|
|
* @license MIT |
|
34
|
|
|
* @link http://www.doctrine-project.org/ |
|
35
|
|
|
* @author Kyle Spraggs <[email protected]> |
|
36
|
|
|
*/ |
|
37
|
|
|
class DBALConnectionFactory extends AbstractFactory |
|
38
|
|
|
{ |
|
39
|
|
|
/** |
|
40
|
|
|
* {@inheritDoc} |
|
41
|
|
|
* |
|
42
|
|
|
* @return Connection |
|
43
|
|
|
*/ |
|
44
|
75 |
|
public function __invoke(ContainerInterface $container, $requestedName, array $options = null) |
|
45
|
|
|
{ |
|
46
|
|
|
/** @var $options DBALConnection */ |
|
47
|
75 |
|
$options = $this->getOptions($container, 'connection'); |
|
48
|
75 |
|
$pdo = $options->getPdo(); |
|
49
|
|
|
|
|
50
|
75 |
|
if (is_string($pdo)) { |
|
51
|
|
|
$pdo = $container->get($pdo); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
$params = [ |
|
55
|
75 |
|
'driverClass' => $options->getDriverClass(), |
|
56
|
75 |
|
'wrapperClass' => $options->getWrapperClass(), |
|
57
|
75 |
|
'pdo' => $pdo, |
|
58
|
75 |
|
]; |
|
59
|
75 |
|
$params = array_merge($params, $options->getParams()); |
|
60
|
|
|
|
|
61
|
75 |
|
$configuration = $container->get($options->getConfiguration()); |
|
62
|
75 |
|
$eventManager = $container->get($options->getEventManager()); |
|
63
|
|
|
|
|
64
|
75 |
|
$connection = DriverManager::getConnection($params, $configuration, $eventManager); |
|
65
|
75 |
|
foreach ($options->getDoctrineTypeMappings() as $dbType => $doctrineType) { |
|
66
|
2 |
|
$connection->getDatabasePlatform()->registerDoctrineTypeMapping($dbType, $doctrineType); |
|
67
|
75 |
|
} |
|
68
|
|
|
|
|
69
|
75 |
|
foreach ($options->getDoctrineCommentedTypes() as $type) { |
|
70
|
1 |
|
$connection->getDatabasePlatform()->markDoctrineTypeCommented(Type::getType($type)); |
|
71
|
75 |
|
} |
|
72
|
|
|
|
|
73
|
75 |
|
return $connection; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* {@inheritDoc} |
|
78
|
|
|
* @return Connection |
|
79
|
|
|
*/ |
|
80
|
75 |
|
public function createService(ServiceLocatorInterface $container) |
|
81
|
|
|
{ |
|
82
|
75 |
|
return $this($container, Connection::class); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Get the class name of the options associated with this factory. |
|
87
|
|
|
* |
|
88
|
|
|
* @return string |
|
89
|
|
|
*/ |
|
90
|
75 |
|
public function getOptionsClass() |
|
91
|
|
|
{ |
|
92
|
75 |
|
return DBALConnection::class; |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|