Completed
Push — master ( 20b2e7...221c15 )
by Tom
16:49 queued 06:52
created

Service/DBALConfigurationFactory.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace DoctrineORMModule\Service;
4
5
use DoctrineORMModule\Options\Configuration as DoctrineORMModuleConfiguration;
6
use Interop\Container\ContainerInterface;
7
use RuntimeException;
8
use Doctrine\DBAL\Configuration;
9
use Doctrine\DBAL\Types\Type;
10
use Zend\ServiceManager\FactoryInterface;
11
use Zend\ServiceManager\ServiceLocatorInterface;
12
13
/**
14
 * DBAL Configuration ServiceManager factory
15
 *
16
 * @license MIT
17
 * @link    http://www.doctrine-project.org/
18
 * @author  Kyle Spraggs <[email protected]>
19
 */
20
class DBALConfigurationFactory implements FactoryInterface
0 ignored issues
show
Deprecated Code introduced by
The interface Zend\ServiceManager\FactoryInterface has been deprecated with message: Use Zend\ServiceManager\Factory\FactoryInterface instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
21
{
22
    /**
23
     * @var string
24
     */
25
    protected $name;
26
27
    /**
28
     * @param string $name
29
     */
30 88
    public function __construct($name)
31
    {
32 88
        $this->name = $name;
33 88
    }
34
35
    /**
36
     * {@inheritDoc}
37
     *
38
     * @return \Doctrine\DBAL\Configuration
39
     */
40
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
41
    {
42
        $config  = new Configuration();
43
        $this->setupDBALConfiguration($container, $config);
44
45
        return $config;
46
    }
47
48
    /**
49
     * {@inheritDoc}
50
     * @return \Doctrine\DBAL\Configuration
51
     */
52
    public function createService(ServiceLocatorInterface $container)
53
    {
54
        return $this($container, \Doctrine\DBAL\Configuration::class);
55
    }
56
57
    /**
58
     * @param ContainerInterface $container
59
     * @param Configuration      $config
60
     */
61 86
    public function setupDBALConfiguration(ContainerInterface $container, Configuration $config)
62
    {
63 86
        $options = $this->getOptions($container);
64 86
        $config->setResultCacheImpl($container->get($options->resultCache));
65
66 86
        $sqlLogger = $options->sqlLogger;
67 86
        if (is_string($sqlLogger) and $container->has($sqlLogger)) {
68
            $sqlLogger = $container->get($sqlLogger);
69
        }
70 86
        $config->setSQLLogger($sqlLogger);
71
72 86
        foreach ($options->types as $name => $class) {
73
            if (Type::hasType($name)) {
74
                Type::overrideType($name, $class);
75
            } else {
76
                Type::addType($name, $class);
77
            }
78 86
        }
79 86
    }
80
81
    /**
82
     * @param  ContainerInterface $serviceLocator
83
     * @return mixed
84
     * @throws RuntimeException
85
     */
86 88
    public function getOptions(ContainerInterface $serviceLocator)
87
    {
88 88
        $options = $serviceLocator->get('config');
89 88
        $options = $options['doctrine'];
90 88
        $options = $options['configuration'][$this->name] ?? null;
91
92 88
        if (null === $options) {
93
            throw new RuntimeException(
94
                sprintf(
95
                    'Configuration with name "%s" could not be found in "doctrine.configuration".',
96
                    $this->name
97
                )
98
            );
99
        }
100
101 88
        $optionsClass = $this->getOptionsClass();
102
103 88
        return new $optionsClass($options);
104
    }
105
106
    /**
107
     * @return string
108
     */
109
    protected function getOptionsClass()
110
    {
111
        return DoctrineORMModuleConfiguration::class;
112
    }
113
}
114