Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Added configuration for DI. Not completed.

  • Loading branch information...
commit ee956e9b17b9a0484b6746f8f013fc84819bbdf9 1 parent 9559766
Igor Golovanov authored
88  DependencyInjection/Configuration.php
@@ -33,16 +33,102 @@ public function getConfigTreeBuilder()
33 33
         $treeBuilder = new TreeBuilder();
34 34
         $rootNode = $treeBuilder->root('doctrine_oxm');
35 35
 
  36
+        $this->addXmlEntityManagersSection($rootNode);
  37
+        $this->addStoragesSection($rootNode);
  38
+        
36 39
         $rootNode
37 40
             ->children()
38 41
                 ->scalarNode('proxy_namespace')->defaultValue('Proxies')->end()
39 42
                 ->scalarNode('proxy_dir')->defaultValue('%kernel.cache_dir%/doctrine/oxm/Proxies')->end()
40  
-                ->scalarNode('auto_generate_proxy_classes')->defaultValue(false)->end()
  43
+                ->scalarNode('auto_generate_proxy_classes')->defaultFalse()->end()
  44
+                ->scalarNode('default_xml_entity_manager')->end()
  45
+                ->scalarNode('default_storage')->defaultValue('default')->end()
41 46
             ->end()
42 47
         ;     
43 48
 
44 49
         return $treeBuilder;
45 50
     }
  51
+    
  52
+        /**
  53
+     * Configures the "xml_entity_managers" section
  54
+     */
  55
+    private function addXmlEntityManagersSection(ArrayNodeDefinition $rootNode)
  56
+    {
  57
+        $rootNode
  58
+            ->fixXmlConfig('xml_entity_manager')
  59
+            ->children()
  60
+                ->arrayNode('xml_entity_managers')
  61
+                    ->useAttributeAsKey('id')
  62
+                    ->prototype('array')
  63
+                        ->treatNullLike(array())
  64
+                        ->children()
  65
+                            ->scalarNode('connection')->end()
  66
+                            ->scalarNode('storage')->end()
  67
+                            ->booleanNode('logging')->defaultValue($this->debug)->end()
  68
+                            ->scalarNode('auto_mapping')->defaultFalse()->end()
  69
+                            ->arrayNode('metadata_cache_driver')
  70
+                                ->beforeNormalization()
  71
+                                    ->ifTrue(function($v) { return !is_array($v); })
  72
+                                    ->then(function($v) { return array('type' => $v); })
  73
+                                ->end()
  74
+                                ->children()
  75
+                                    ->scalarNode('type')->end()
  76
+                                    ->scalarNode('class')->end()
  77
+                                    ->scalarNode('host')->end()
  78
+                                    ->scalarNode('port')->end()
  79
+                                    ->scalarNode('instance_class')->end()
  80
+                                ->end()
  81
+                            ->end()
  82
+                        ->end()
  83
+                        ->fixXmlConfig('mapping')
  84
+                        ->children()
  85
+                            ->arrayNode('mappings')
  86
+                                ->useAttributeAsKey('name')
  87
+                                ->prototype('array')
  88
+                                    ->beforeNormalization()
  89
+                                        ->ifString()
  90
+                                        ->then(function($v) { return array ('type' => $v); })
  91
+                                    ->end()
  92
+                                    ->treatNullLike(array())
  93
+                                    ->treatFalseLike(array('mapping' => false))
  94
+                                    ->performNoDeepMerging()
  95
+                                    ->children()
  96
+                                        ->scalarNode('mapping')->defaultValue(true)->end()
  97
+                                        ->scalarNode('type')->end()
  98
+                                        ->scalarNode('dir')->end()
  99
+                                        ->scalarNode('prefix')->end()
  100
+                                        ->scalarNode('alias')->end()
  101
+                                        ->booleanNode('is_bundle')->end()
  102
+                                    ->end()
  103
+                                ->end()
  104
+                            ->end()
  105
+                        ->end()
  106
+                    ->end()
  107
+                ->end()
  108
+            ->end()
  109
+        ;
  110
+    }
  111
+        
  112
+    /**
  113
+     * Adds the configuration for the "storages" key
  114
+     */
  115
+    private function addStoragesSection(ArrayNodeDefinition $rootNode)
  116
+    {
  117
+        $rootNode
  118
+            ->fixXmlConfig('storage')
  119
+            ->children()
  120
+                ->arrayNode('storages')
  121
+                    ->useAttributeAsKey('id')
  122
+                    ->prototype('array')
  123
+                        ->performNoDeepMerging()
  124
+                        ->children()
  125
+                            ->scalarNode('path')->defaultValue('%kernel.root_dir%/doctrine-oxm-storage')->end()
  126
+                        ->end()
  127
+                    ->end()
  128
+                ->end()
  129
+            ->end()
  130
+        ;
  131
+    }
46 132
 
47 133
 
48 134
 }
238  DependencyInjection/DoctrineOXMExtension.php
@@ -16,7 +16,7 @@
16 16
  *
17 17
  * @author Igor Golovanov <igor.golovanov@gmail.com>
18 18
  */
19  
-class DoctrineMongoDBExtension extends AbstractDoctrineExtension
  19
+class DoctrineOXMExtension extends AbstractDoctrineExtension
20 20
 {
21 21
     /**
22 22
      * Responds to the doctrine_mongodb configuration parameter.
@@ -27,11 +27,245 @@ public function load(array $configs, ContainerBuilder $container)
27 27
         $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
28 28
         $loader->load('oxm.xml');
29 29
         
  30
+        $processor = new Processor();
  31
+        $configuration = new Configuration($container->getParameter('kernel.debug'));
  32
+        $config = $processor->processConfiguration($configuration, $configs);
30 33
         
  34
+        // can't currently default this correctly in Configuration
  35
+        if (!isset($config['metadata_cache_driver'])) {
  36
+            $config['metadata_cache_driver'] = array('type' => 'array');
  37
+        }
  38
+
  39
+        if (empty ($config['default_storage'])) {
  40
+            $keys = array_keys($config['storages']);
  41
+            $config['default_storage'] = reset($keys);
  42
+        }
  43
+
  44
+        if (empty ($config['default_xml_entity_manager'])) {
  45
+            $keys = array_keys($config['xml_entity_managers']);
  46
+            $config['default_xml_entity_manager'] = reset($keys);
  47
+        }
31 48
 
32  
-        
33 49
         // set some options as parameters and unset them
34 50
         $config = $this->overrideParameters($config, $container);
  51
+
  52
+        // load the storages
  53
+        $this->loadStorages($config['storages'], $container);
  54
+
  55
+        // load the xml-entity managers
  56
+        $this->loadXmlEntityManagers(
  57
+            $config['xml_entity_managers'],
  58
+            $config['default_xml_entity_manager'],
  59
+            $config['default_storage'],
  60
+            $config['metadata_cache_driver'],
  61
+            $container
  62
+        );
  63
+    }
  64
+    
  65
+    /**
  66
+     * Loads the xml-entity managers configuration.
  67
+     *
  68
+     * @param array $xemConfigs An array of xml-entity manager configs
  69
+     * @param string $defaultXEM The default xml-entity manager name
  70
+     * @param string $defaultStorage The default db name
  71
+     * @param string $defaultMetadataCache The default metadata cache configuration
  72
+     * @param ContainerBuilder $container A ContainerBuilder instance
  73
+     */
  74
+    protected function loadXmlEntityManagers(array $xemConfigs, $defaultXEM, $defaultStorage, $defaultMetadataCache, ContainerBuilder $container)
  75
+    {
  76
+        foreach ($xemConfigs as $name => $xmlEntityManager) {
  77
+            $xmlEntityManager['name'] = $name;
  78
+            $this->loadXmlEntityManager(
  79
+                $xmlEntityManager,
  80
+                $defaultXEM,
  81
+                $defaultStorage,
  82
+                $defaultMetadataCache,
  83
+                $container
  84
+            );
  85
+        }
  86
+        $container->setParameter('doctrine.oxm.xml_entity_managers', array_keys($xemConfigs));
  87
+    }
  88
+
  89
+    /**
  90
+     * Loads a xml-entity manager configuration.
  91
+     *
  92
+     * @param array $xmlEntityManager        A xml-entity manager configuration array
  93
+     * @param string $defaultXEM The default xml-entity manager name
  94
+     * @param string $defaultStorage The default db name
  95
+     * @param string $defaultMetadataCache The default metadata cache configuration
  96
+     * @param ContainerBuilder $container A ContainerBuilder instance
  97
+     */
  98
+    protected function loadXmlEntityManager(array $xmlEntityManager, $defaultXEM, $defaultStorage, $defaultMetadataCache, ContainerBuilder $container)
  99
+    {
  100
+        $defaultStorage = isset($xmlEntityManager['storage']) ? $xmlEntityManager['storage'] : $defaultStorage;
  101
+        $configServiceName = sprintf('doctrine.oxm.%s_configuration', $xmlEntityManager['name']);
  102
+
  103
+        if ($container->hasDefinition($configServiceName)) {
  104
+            $oxmConfigDef = $container->getDefinition($configServiceName);
  105
+        } else {
  106
+            $oxmConfigDef = new Definition('%doctrine.oxm.configuration.class%');
  107
+            $container->setDefinition($configServiceName, $oxmConfigDef);
  108
+        }
  109
+
  110
+        $this->loadXmlEntityManagerBundlesMappingInformation($xmlEntityManager, $oxmConfigDef, $container);
  111
+        $this->loadXmlEntityManagerMetadataCacheDriver($xmlEntityManager, $container, $defaultMetadataCache);
  112
+
  113
+        $methods = array(
  114
+            'setMetadataCacheImpl' => new Reference(sprintf('doctrine.oxm.%s_metadata_cache', $xmlEntityManager['name'])),
  115
+            'setMetadataDriverImpl' => new Reference(sprintf('doctrine.oxm.%s_metadata_driver', $xmlEntityManager['name'])),
  116
+            'setProxyDir' => '%doctrine.oxm.proxy_dir%',
  117
+            'setProxyNamespace' => '%doctrine.oxm.proxy_namespace%',
  118
+            'setAutoGenerateProxyClasses' => '%doctrine.oxm.auto_generate_proxy_classes%',
  119
+            'setDefaultStorage' => $defaultDatabase,
  120
+        );
  121
+
  122
+//        if ($xmlEntityManager['logging']) {
  123
+//            $methods['setLoggerCallable'] = array(new Reference('doctrine.oxm.logger'), 'logQuery');
  124
+//        }
  125
+
  126
+        foreach ($methods as $method => $arg) {
  127
+            if ($oxmConfigDef->hasMethodCall($method)) {
  128
+                $oxmConfigDef->removeMethodCall($method);
  129
+            }
  130
+            $oxmConfigDef->addMethodCall($method, array($arg));
  131
+        }
  132
+
  133
+        // event manager
  134
+        $eventManagerName = isset($xmlEntityManager['event_manager']) ? $xmlEntityManager['event_manager'] : $xmlEntityManager['name'];
  135
+        $eventManagerId = sprintf('doctrine.oxm.%s_event_manager', $eventManagerName);
  136
+        if (!$container->hasDefinition($eventManagerId)) {
  137
+            $eventManagerDef = new Definition('%doctrine.oxm.event_manager.class%');
  138
+            $eventManagerDef->addTag('doctrine.oxm.event_manager');
  139
+            $eventManagerDef->setPublic(false);
  140
+            $container->setDefinition($eventManagerId, $eventManagerDef);
  141
+        }
  142
+
  143
+        $oxmXemArgs = array(
  144
+            new Reference(sprintf('doctrine.oxm.%s_connection', isset($xmlEntityManager['storage']) ? $xmlEntityManager['storage'] : $xmlEntityManager['name'])),
  145
+            new Reference(sprintf('doctrine.oxm.%s_configuration', $xmlEntityManager['name'])),
  146
+            new Reference($eventManagerId),
  147
+        );
  148
+        $oxmXemDef = new Definition('%doctrine.oxm.xml_entity_manager.class%', $oxmXemArgs);
  149
+        $oxmXemDef->setFactoryClass('%doctrine.oxm.xml_entity_manager.class%');
  150
+        $oxmXemDef->setFactoryMethod('create');
  151
+        $oxmXemDef->addTag('doctrine.oxm.xml_entity_manager');
  152
+        $container->setDefinition(sprintf('doctrine.oxm.%s_xml_entity_manager', $xmlEntityManager['name']), $oxmXemDef);
  153
+
  154
+        if ($xmlEntityManager['name'] == $defaultXEM) {
  155
+            $container->setAlias(
  156
+                'doctrine.oxm.xml_entity_manager',
  157
+                new Alias(sprintf('doctrine.oxm.%s_xml_entity_manager', $xmlEntityManager['name']))
  158
+            );
  159
+            $container->setAlias(
  160
+                'doctrine.oxm.event_manager',
  161
+                new Alias(sprintf('doctrine.oxm.%s_event_manager', $xmlEntityManager['name']))
  162
+            );
  163
+        }
  164
+    }
  165
+    
  166
+    
  167
+    /**
  168
+     * Loads the configured xml-entity manager metadata cache driver.
  169
+     *
  170
+     * @param array $config                 A configured xml-entity manager array
  171
+     * @param ContainerBuilder $container   A ContainerBuilder instance
  172
+     * @param array $defaultMetadataCache   The default metadata cache configuration array
  173
+     */
  174
+    protected function loadXmlEntityManagerMetadataCacheDriver(array $xmlEntityManager, ContainerBuilder $container, $defaultMetadataCache)
  175
+    {
  176
+        $xemMetadataCacheDriver = isset($xmlEntityManager['metadata_cache_driver']) ? $xmlEntityManager['metadata_cache_driver'] : $defaultMetadataCache;
  177
+        $type = $xemMetadataCacheDriver['type'];
  178
+
  179
+        if ('memcache' === $type) {
  180
+            $memcacheClass = isset($xemMetadataCacheDriver['class']) ? $xemMetadataCacheDriver['class'] : sprintf('%%doctrine.oxm.cache.%s.class%%', $type);
  181
+            $cacheDef = new Definition($memcacheClass);
  182
+            $memcacheHost = isset($xemMetadataCacheDriver['host']) ? $xemMetadataCacheDriver['host'] : '%doctrine.oxm.cache.memcache_host%';
  183
+            $memcachePort = isset($xemMetadataCacheDriver['port']) ? $xemMetadataCacheDriver['port'] : '%doctrine.oxm.cache.memcache_port%';
  184
+            $memcacheInstanceClass = isset($xemMetadataCacheDriver['instance-class']) ? $xemMetadataCacheDriver['instance-class'] : (isset($xemMetadataCacheDriver['instance_class']) ? $xemMetadataCacheDriver['instance_class'] : '%doctrine.oxm.cache.memcache_instance.class%');
  185
+            $memcacheInstance = new Definition($memcacheInstanceClass);
  186
+            $memcacheInstance->addMethodCall('connect', array($memcacheHost, $memcachePort));
  187
+            $container->setDefinition(sprintf('doctrine.oxm.%s_memcache_instance', $xmlEntityManager['name']), $memcacheInstance);
  188
+            $cacheDef->addMethodCall('setMemcache', array(new Reference(sprintf('doctrine.oxm.%s_memcache_instance', $xmlEntityManager['name']))));
  189
+        } else {
  190
+             $cacheDef = new Definition(sprintf('%%doctrine.oxm.cache.%s.class%%', $type));
  191
+        }
  192
+
  193
+        $container->setDefinition(sprintf('doctrine.oxm.%s_metadata_cache', $xmlEntityManager['name']), $cacheDef);
  194
+    }
  195
+    
  196
+    /**
  197
+     * Loads the configured storages.
  198
+     *
  199
+     * @param array $storages An array of storages configurations
  200
+     * @param ContainerBuilder $container A ContainerBuilder instance
  201
+     */
  202
+    protected function loadStorages(array $storages, ContainerBuilder $container)
  203
+    {
  204
+        foreach ($storages as $name => $storage) {
  205
+            $oxmStorageArgs = array(
  206
+                isset($connection['server']) ? $connection['server'] : null,
  207
+                isset($connection['options']) ? $connection['options'] : array(),
  208
+                new Reference(sprintf('doctrine.oxm.%s_configuration', $name))
  209
+            );
  210
+            $oxmStorageDef = new Definition('%doctrine.oxm.storage.class%', $oxmStorageArgs);
  211
+            $container->setDefinition(sprintf('doctrine.oxm.%s_storage', $name), $oxmStorageDef);
  212
+        }
  213
+    }
  214
+    
  215
+    
  216
+    /**
  217
+     * Loads an OXM xml-entity managers bundle mapping information.
  218
+     *
  219
+     * There are two distinct configuration possibilities for mapping information:
  220
+     *
  221
+     * 1. Specify a bundle and optionally details where the entity and mapping information reside.
  222
+     * 2. Specify an arbitrary mapping location.
  223
+     *
  224
+     * @example
  225
+     *
  226
+     *  doctrine.oxm:
  227
+     *     mappings:
  228
+     *         MyBundle1: ~
  229
+     *         MyBundle2: yml
  230
+     *         MyBundle3: { type: annotation, dir: XmlEntity/ }
  231
+     *         MyBundle4: { type: xml, dir: Resources/config/doctrine/mapping }
  232
+     *         MyBundle5:
  233
+     *             type: yml
  234
+     *             dir: [bundle-mappings1/, bundle-mappings2/]
  235
+     *             alias: BundleAlias
  236
+     *         arbitrary_key:
  237
+     *             type: xml
  238
+     *             dir: %kernel.dir%/../src/vendor/DoctrineExtensions/lib/DoctrineExtensions/XmlEntity
  239
+     *             prefix: DoctrineExtensions\XmlEntity\
  240
+     *             alias: DExt
  241
+     *
  242
+     * In the case of bundles everything is really optional (which leads to autodetection for this bundle) but
  243
+     * in the mappings key everything except alias is a required argument.
  244
+     *
  245
+     * @param array $xmlEntityManager A configured OXM xml-entity manager.
  246
+     * @param Definition A Definition instance
  247
+     * @param ContainerBuilder $container A ContainerBuilder instance
  248
+     */
  249
+    protected function loadDocumentManagerBundlesMappingInformation(array $xmlEntityManager, Definition $oxmConfigDef, ContainerBuilder $container)
  250
+    {
  251
+        // reset state of drivers and alias map. They are only used by this methods and children.
  252
+        $this->drivers = array();
  253
+        $this->aliasMap = array();
  254
+
  255
+        $this->loadMappingInformation($xmlEntityManager, $container);
  256
+        $this->registerMappingDrivers($xmlEntityManager, $container);
  257
+
  258
+        if ($oxmConfigDef->hasMethodCall('setXmlEntityNamespaces')) {
  259
+            // TODO: Can we make a method out of it on Definition? replaceMethodArguments() or something.
  260
+            $calls = $oxmConfigDef->getMethodCalls();
  261
+            foreach ($calls as $call) {
  262
+                if ($call[0] == 'setXmlEntityNamespaces') {
  263
+                    $this->aliasMap = array_merge($call[1][0], $this->aliasMap);
  264
+                }
  265
+            }
  266
+            $method = $oxmConfigDef->removeMethodCall('setXmlEntityNamespaces');
  267
+        }
  268
+        $oxmConfigDef->addMethodCall('setXmlEntityNamespaces', array($this->aliasMap));
35 269
     }
36 270
     
37 271
     

0 notes on commit ee956e9

Please sign in to comment.
Something went wrong with that request. Please try again.