Completed
Branch master (a1753a)
by Mikhail
01:53
created

FixDoctrineProvider::boot()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 1
Metric Value
c 1
b 1
f 1
dl 0
loc 3
rs 10
cc 1
eloc 1
nc 1
nop 1
1
<?php
2
/**
3
 * @author Dolgov_M <[email protected]>
4
 * @date   11.11.2016 15:39
5
 */
6
7
namespace SilexDoctrineHydrationProfile\Fix;
8
9
use Doctrine\DBAL\Types\Type;
10
use Silex\Application;
11
use Silex\ServiceProviderInterface;
12
13
class FixDoctrineProvider implements ServiceProviderInterface
14
{
15
    public function register(Application $app)
16
    {
17
18
        $app['debesha.class.hydrationDataCollector'] = 'SilexDoctrineHydrationProfile\Fix\FixHydrationDataCollector';
19
        $app['orm.em.default_options'] = array(
20
            'connection'              => 'default',
21
            'mappings'                => array(),
22
            'types'                   => array(),
23
            'class.configuration'     => 'Doctrine\ORM\Configuration',
24
            'class.entityManager'     => 'Doctrine\ORM\EntityManager',
25
            'class.driver.yml'        => 'Doctrine\ORM\Mapping\Driver\YamlDriver',
26
            'class.driver.simple_yml' => 'Doctrine\ORM\Mapping\Driver\SimplifiedYamlDriver',
27
            'class.driver.xml'        => 'Doctrine\ORM\Mapping\Driver\XmlDriver',
28
            'class.driver.simple_xml' => 'Doctrine\ORM\Mapping\Driver\SimplifiedXmlDriver',
29
            'class.driver.php'        => 'Doctrine\Common\Persistence\Mapping\Driver\StaticPHPDriver',
30
        );
31
32
        $app['orm.ems'] = $app->share(function($app) {
33
            /**
34
             * @var \Pimple $app
35
             */
36
            $app['orm.ems.options.initializer']();
37
38
            $ems = new \Pimple();
39
            foreach ($app['orm.ems.options'] as $name => $options) {
40
                if ($app['orm.ems.default'] === $name) {
41
                    // we use shortcuts here in case the default has been overridden
42
                    $config = $app['orm.em.config'];
43
                } else {
44
                    $config = $app['orm.ems.config'][$name];
45
                }
46
47
                $ems[$name] = $app->share(function () use ($app, $options, $config) {
48
                    /**
49
                     * @var $entityManagerClassName \Doctrine\ORM\EntityManager
50
                     */
51
                    $entityManagerClassName = $options['class.entityManager'];
52
                    return $entityManagerClassName::create(
53
                        $app['dbs'][$options['connection']],
54
                        $config,
55
                        $app['dbs.event_manager'][$options['connection']]
56
                    );
57
                });
58
            }
59
            return $ems;
60
        });
61
62
        $app['orm.ems.config'] = $app->share(function(\Pimple $app) {
63
            $app['orm.ems.options.initializer']();
64
65
            $configs = new \Pimple();
66
            foreach ($app['orm.ems.options'] as $name => $options) {
67
                /**
68
                 * @var $config \Doctrine\ORM\Configuration
69
                 */
70
                $configurationClassName = $options['class.configuration'];
71
                $config = new $configurationClassName;
72
73
                $app['orm.cache.configurer']($name, $config, $options);
74
75
                $config->setProxyDir($app['orm.proxies_dir']);
76
                $config->setProxyNamespace($app['orm.proxies_namespace']);
77
                $config->setAutoGenerateProxyClasses($app['orm.auto_generate_proxies']);
78
79
                $config->setCustomStringFunctions($app['orm.custom.functions.string']);
80
                $config->setCustomNumericFunctions($app['orm.custom.functions.numeric']);
81
                $config->setCustomDatetimeFunctions($app['orm.custom.functions.datetime']);
82
                $config->setCustomHydrationModes($app['orm.custom.hydration_modes']);
83
84
                $config->setClassMetadataFactoryName($app['orm.class_metadata_factory_name']);
85
                $config->setDefaultRepositoryClassName($app['orm.default_repository_class']);
86
87
                $config->setEntityListenerResolver($app['orm.entity_listener_resolver']);
88
                $config->setRepositoryFactory($app['orm.repository_factory']);
89
90
                $config->setNamingStrategy($app['orm.strategy.naming']);
91
                $config->setQuoteStrategy($app['orm.strategy.quote']);
92
93
                /**
94
                 * @var \Doctrine\Common\Persistence\Mapping\Driver\MappingDriverChain $chain
95
                 */
96
                $chain = $app['orm.mapping_driver_chain.locator']($name);
97
                foreach ((array) $options['mappings'] as $entity) {
98
                    if (!is_array($entity)) {
99
                        throw new \InvalidArgumentException(
100
                            "The 'orm.em.options' option 'mappings' should be an array of arrays."
101
                        );
102
                    }
103
104
                    if (!empty($entity['resources_namespace'])) {
105
                        if($app->offsetExists('psr0_resource_locator')) {
106
                            $entity['path'] = $app['psr0_resource_locator']->findFirstDirectory($entity['resources_namespace']);
107
                        } else {
108
                            throw new \InvalidArgumentException('Not exist psr0_resource_locator');
109
                        }
110
                    }
111
112
                    if (isset($entity['alias'])) {
113
                        $config->addEntityNamespace($entity['alias'], $entity['namespace']);
114
                    }
115
116
                    if('annotation' === $entity['type']){
117
                        $useSimpleAnnotationReader = isset($entity['use_simple_annotation_reader'])
118
                            ? $entity['use_simple_annotation_reader']
119
                            : true;
120
                        $driver =  $config->newDefaultAnnotationDriver((array) $entity['path'], $useSimpleAnnotationReader);
121
                    } else {
122
                        $driver = $app['orm.driver.factory']( $entity, $options );
123
                    }
124
                    $chain->addDriver($driver, $entity['namespace']);
125
                }
126
                $config->setMetadataDriverImpl($chain);
127
128
                foreach ((array) $options['types'] as $typeName => $typeClass) {
129
                    if (Type::hasType($typeName)) {
130
                        Type::overrideType($typeName, $typeClass);
131
                    } else {
132
                        Type::addType($typeName, $typeClass);
133
                    }
134
                }
135
136
                $configs[$name] = $config;
137
            }
138
139
            return $configs;
140
        });
141
142
143
        $app['orm.driver.factory'] = $app->share(function(){
144
            $simpleList = array(
145
                'simple_yml',
146
                'simple_xml',
147
            );
148
            return function ( $entity, $options ) use ($simpleList) {
149
                if (isset($options[ 'class.driver.' . $entity['type'] ])) {
150
                    $className = $options[ 'class.driver.' . $entity['type'] ];
151
                    if( in_array($entity['type'], $simpleList) ) {
152
                        $param = array($entity['path'] => $entity['namespace']);
153
                    } else {
154
                        $param = $entity['path'];
155
                    }
156
                    return new $className($param);
157
                }
158
                throw new \InvalidArgumentException(sprintf('"%s" is not a recognized driver', $entity['type']));
159
            };
160
        });
161
162
    }
163
164
    public function boot(Application $app)
165
    {
166
    }
167
168
}