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\DriverManager; |
23
|
|
|
use Doctrine\DBAL\Types\Type; |
24
|
|
|
use DoctrineModule\Service\AbstractFactory; |
25
|
|
|
use Interop\Container\ContainerInterface; |
26
|
|
|
use Zend\ServiceManager\ServiceLocatorInterface; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* DBAL Connection ServiceManager factory |
30
|
|
|
* |
31
|
|
|
* @license MIT |
32
|
|
|
* @link http://www.doctrine-project.org/ |
33
|
|
|
* @author Kyle Spraggs <[email protected]> |
34
|
|
|
*/ |
35
|
|
|
class DBALConnectionFactory extends AbstractFactory |
36
|
|
|
{ |
37
|
|
|
/** |
38
|
|
|
* {@inheritDoc} |
39
|
|
|
* |
40
|
|
|
* @return \Doctrine\DBAL\Connection |
41
|
|
|
*/ |
42
|
58 |
|
public function __invoke(ContainerInterface $container, $requestedName, array $options = null) |
|
|
|
|
43
|
|
|
{ |
44
|
|
|
/** @var $options \DoctrineORMModule\Options\DBALConnection */ |
45
|
58 |
|
$options = $this->getOptions($container, 'connection'); |
|
|
|
|
46
|
58 |
|
$pdo = $options->getPdo(); |
47
|
|
|
|
48
|
58 |
|
if (is_string($pdo)) { |
49
|
|
|
$pdo = $container->get($pdo); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$params = array( |
53
|
58 |
|
'driverClass' => $options->getDriverClass(), |
54
|
58 |
|
'wrapperClass' => $options->getWrapperClass(), |
55
|
58 |
|
'pdo' => $pdo, |
56
|
58 |
|
); |
57
|
58 |
|
$params = array_merge($params, $options->getParams()); |
58
|
|
|
|
59
|
58 |
|
$configuration = $container->get($options->getConfiguration()); |
60
|
58 |
|
$eventManager = $container->get($options->getEventManager()); |
61
|
|
|
|
62
|
58 |
|
$connection = DriverManager::getConnection($params, $configuration, $eventManager); |
63
|
58 |
|
$platform = $connection->getDatabasePlatform(); |
64
|
58 |
|
foreach ($options->getDoctrineTypeMappings() as $dbType => $doctrineType) { |
65
|
2 |
|
$platform->registerDoctrineTypeMapping($dbType, $doctrineType); |
66
|
58 |
|
} |
67
|
|
|
|
68
|
58 |
|
foreach ($options->getDoctrineCommentedTypes() as $type) { |
69
|
1 |
|
$platform->markDoctrineTypeCommented(Type::getType($type)); |
70
|
58 |
|
} |
71
|
|
|
|
72
|
58 |
|
return $connection; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* {@inheritDoc} |
77
|
|
|
* @return \Doctrine\DBAL\Connection |
78
|
|
|
*/ |
79
|
58 |
|
public function createService(ServiceLocatorInterface $container) |
80
|
|
|
{ |
81
|
58 |
|
return $this($container, \Doctrine\DBAL\Connection::class); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Get the class name of the options associated with this factory. |
86
|
|
|
* |
87
|
|
|
* @return string |
88
|
|
|
*/ |
89
|
58 |
|
public function getOptionsClass() |
90
|
|
|
{ |
91
|
58 |
|
return 'DoctrineORMModule\Options\DBALConnection'; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.