Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added configuration for DI. Not completed.

  • Loading branch information...
commit ee956e9b17b9a0484b6746f8f013fc84819bbdf9 1 parent 9559766
@golovanov authored
View
88 DependencyInjection/Configuration.php
@@ -33,16 +33,102 @@ public function getConfigTreeBuilder()
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('doctrine_oxm');
+ $this->addXmlEntityManagersSection($rootNode);
+ $this->addStoragesSection($rootNode);
+
$rootNode
->children()
->scalarNode('proxy_namespace')->defaultValue('Proxies')->end()
->scalarNode('proxy_dir')->defaultValue('%kernel.cache_dir%/doctrine/oxm/Proxies')->end()
- ->scalarNode('auto_generate_proxy_classes')->defaultValue(false)->end()
+ ->scalarNode('auto_generate_proxy_classes')->defaultFalse()->end()
+ ->scalarNode('default_xml_entity_manager')->end()
+ ->scalarNode('default_storage')->defaultValue('default')->end()
->end()
;
return $treeBuilder;
}
+
+ /**
+ * Configures the "xml_entity_managers" section
+ */
+ private function addXmlEntityManagersSection(ArrayNodeDefinition $rootNode)
+ {
+ $rootNode
+ ->fixXmlConfig('xml_entity_manager')
+ ->children()
+ ->arrayNode('xml_entity_managers')
+ ->useAttributeAsKey('id')
+ ->prototype('array')
+ ->treatNullLike(array())
+ ->children()
+ ->scalarNode('connection')->end()
+ ->scalarNode('storage')->end()
+ ->booleanNode('logging')->defaultValue($this->debug)->end()
+ ->scalarNode('auto_mapping')->defaultFalse()->end()
+ ->arrayNode('metadata_cache_driver')
+ ->beforeNormalization()
+ ->ifTrue(function($v) { return !is_array($v); })
+ ->then(function($v) { return array('type' => $v); })
+ ->end()
+ ->children()
+ ->scalarNode('type')->end()
+ ->scalarNode('class')->end()
+ ->scalarNode('host')->end()
+ ->scalarNode('port')->end()
+ ->scalarNode('instance_class')->end()
+ ->end()
+ ->end()
+ ->end()
+ ->fixXmlConfig('mapping')
+ ->children()
+ ->arrayNode('mappings')
+ ->useAttributeAsKey('name')
+ ->prototype('array')
+ ->beforeNormalization()
+ ->ifString()
+ ->then(function($v) { return array ('type' => $v); })
+ ->end()
+ ->treatNullLike(array())
+ ->treatFalseLike(array('mapping' => false))
+ ->performNoDeepMerging()
+ ->children()
+ ->scalarNode('mapping')->defaultValue(true)->end()
+ ->scalarNode('type')->end()
+ ->scalarNode('dir')->end()
+ ->scalarNode('prefix')->end()
+ ->scalarNode('alias')->end()
+ ->booleanNode('is_bundle')->end()
+ ->end()
+ ->end()
+ ->end()
+ ->end()
+ ->end()
+ ->end()
+ ->end()
+ ;
+ }
+
+ /**
+ * Adds the configuration for the "storages" key
+ */
+ private function addStoragesSection(ArrayNodeDefinition $rootNode)
+ {
+ $rootNode
+ ->fixXmlConfig('storage')
+ ->children()
+ ->arrayNode('storages')
+ ->useAttributeAsKey('id')
+ ->prototype('array')
+ ->performNoDeepMerging()
+ ->children()
+ ->scalarNode('path')->defaultValue('%kernel.root_dir%/doctrine-oxm-storage')->end()
+ ->end()
+ ->end()
+ ->end()
+ ->end()
+ ;
+ }
}
View
238 DependencyInjection/DoctrineOXMExtension.php
@@ -16,7 +16,7 @@
*
* @author Igor Golovanov <igor.golovanov@gmail.com>
*/
-class DoctrineMongoDBExtension extends AbstractDoctrineExtension
+class DoctrineOXMExtension extends AbstractDoctrineExtension
{
/**
* Responds to the doctrine_mongodb configuration parameter.
@@ -27,11 +27,245 @@ public function load(array $configs, ContainerBuilder $container)
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('oxm.xml');
+ $processor = new Processor();
+ $configuration = new Configuration($container->getParameter('kernel.debug'));
+ $config = $processor->processConfiguration($configuration, $configs);
+ // can't currently default this correctly in Configuration
+ if (!isset($config['metadata_cache_driver'])) {
+ $config['metadata_cache_driver'] = array('type' => 'array');
+ }
+
+ if (empty ($config['default_storage'])) {
+ $keys = array_keys($config['storages']);
+ $config['default_storage'] = reset($keys);
+ }
+
+ if (empty ($config['default_xml_entity_manager'])) {
+ $keys = array_keys($config['xml_entity_managers']);
+ $config['default_xml_entity_manager'] = reset($keys);
+ }
-
// set some options as parameters and unset them
$config = $this->overrideParameters($config, $container);
+
+ // load the storages
+ $this->loadStorages($config['storages'], $container);
+
+ // load the xml-entity managers
+ $this->loadXmlEntityManagers(
+ $config['xml_entity_managers'],
+ $config['default_xml_entity_manager'],
+ $config['default_storage'],
+ $config['metadata_cache_driver'],
+ $container
+ );
+ }
+
+ /**
+ * Loads the xml-entity managers configuration.
+ *
+ * @param array $xemConfigs An array of xml-entity manager configs
+ * @param string $defaultXEM The default xml-entity manager name
+ * @param string $defaultStorage The default db name
+ * @param string $defaultMetadataCache The default metadata cache configuration
+ * @param ContainerBuilder $container A ContainerBuilder instance
+ */
+ protected function loadXmlEntityManagers(array $xemConfigs, $defaultXEM, $defaultStorage, $defaultMetadataCache, ContainerBuilder $container)
+ {
+ foreach ($xemConfigs as $name => $xmlEntityManager) {
+ $xmlEntityManager['name'] = $name;
+ $this->loadXmlEntityManager(
+ $xmlEntityManager,
+ $defaultXEM,
+ $defaultStorage,
+ $defaultMetadataCache,
+ $container
+ );
+ }
+ $container->setParameter('doctrine.oxm.xml_entity_managers', array_keys($xemConfigs));
+ }
+
+ /**
+ * Loads a xml-entity manager configuration.
+ *
+ * @param array $xmlEntityManager A xml-entity manager configuration array
+ * @param string $defaultXEM The default xml-entity manager name
+ * @param string $defaultStorage The default db name
+ * @param string $defaultMetadataCache The default metadata cache configuration
+ * @param ContainerBuilder $container A ContainerBuilder instance
+ */
+ protected function loadXmlEntityManager(array $xmlEntityManager, $defaultXEM, $defaultStorage, $defaultMetadataCache, ContainerBuilder $container)
+ {
+ $defaultStorage = isset($xmlEntityManager['storage']) ? $xmlEntityManager['storage'] : $defaultStorage;
+ $configServiceName = sprintf('doctrine.oxm.%s_configuration', $xmlEntityManager['name']);
+
+ if ($container->hasDefinition($configServiceName)) {
+ $oxmConfigDef = $container->getDefinition($configServiceName);
+ } else {
+ $oxmConfigDef = new Definition('%doctrine.oxm.configuration.class%');
+ $container->setDefinition($configServiceName, $oxmConfigDef);
+ }
+
+ $this->loadXmlEntityManagerBundlesMappingInformation($xmlEntityManager, $oxmConfigDef, $container);
+ $this->loadXmlEntityManagerMetadataCacheDriver($xmlEntityManager, $container, $defaultMetadataCache);
+
+ $methods = array(
+ 'setMetadataCacheImpl' => new Reference(sprintf('doctrine.oxm.%s_metadata_cache', $xmlEntityManager['name'])),
+ 'setMetadataDriverImpl' => new Reference(sprintf('doctrine.oxm.%s_metadata_driver', $xmlEntityManager['name'])),
+ 'setProxyDir' => '%doctrine.oxm.proxy_dir%',
+ 'setProxyNamespace' => '%doctrine.oxm.proxy_namespace%',
+ 'setAutoGenerateProxyClasses' => '%doctrine.oxm.auto_generate_proxy_classes%',
+ 'setDefaultStorage' => $defaultDatabase,
+ );
+
+// if ($xmlEntityManager['logging']) {
+// $methods['setLoggerCallable'] = array(new Reference('doctrine.oxm.logger'), 'logQuery');
+// }
+
+ foreach ($methods as $method => $arg) {
+ if ($oxmConfigDef->hasMethodCall($method)) {
+ $oxmConfigDef->removeMethodCall($method);
+ }
+ $oxmConfigDef->addMethodCall($method, array($arg));
+ }
+
+ // event manager
+ $eventManagerName = isset($xmlEntityManager['event_manager']) ? $xmlEntityManager['event_manager'] : $xmlEntityManager['name'];
+ $eventManagerId = sprintf('doctrine.oxm.%s_event_manager', $eventManagerName);
+ if (!$container->hasDefinition($eventManagerId)) {
+ $eventManagerDef = new Definition('%doctrine.oxm.event_manager.class%');
+ $eventManagerDef->addTag('doctrine.oxm.event_manager');
+ $eventManagerDef->setPublic(false);
+ $container->setDefinition($eventManagerId, $eventManagerDef);
+ }
+
+ $oxmXemArgs = array(
+ new Reference(sprintf('doctrine.oxm.%s_connection', isset($xmlEntityManager['storage']) ? $xmlEntityManager['storage'] : $xmlEntityManager['name'])),
+ new Reference(sprintf('doctrine.oxm.%s_configuration', $xmlEntityManager['name'])),
+ new Reference($eventManagerId),
+ );
+ $oxmXemDef = new Definition('%doctrine.oxm.xml_entity_manager.class%', $oxmXemArgs);
+ $oxmXemDef->setFactoryClass('%doctrine.oxm.xml_entity_manager.class%');
+ $oxmXemDef->setFactoryMethod('create');
+ $oxmXemDef->addTag('doctrine.oxm.xml_entity_manager');
+ $container->setDefinition(sprintf('doctrine.oxm.%s_xml_entity_manager', $xmlEntityManager['name']), $oxmXemDef);
+
+ if ($xmlEntityManager['name'] == $defaultXEM) {
+ $container->setAlias(
+ 'doctrine.oxm.xml_entity_manager',
+ new Alias(sprintf('doctrine.oxm.%s_xml_entity_manager', $xmlEntityManager['name']))
+ );
+ $container->setAlias(
+ 'doctrine.oxm.event_manager',
+ new Alias(sprintf('doctrine.oxm.%s_event_manager', $xmlEntityManager['name']))
+ );
+ }
+ }
+
+
+ /**
+ * Loads the configured xml-entity manager metadata cache driver.
+ *
+ * @param array $config A configured xml-entity manager array
+ * @param ContainerBuilder $container A ContainerBuilder instance
+ * @param array $defaultMetadataCache The default metadata cache configuration array
+ */
+ protected function loadXmlEntityManagerMetadataCacheDriver(array $xmlEntityManager, ContainerBuilder $container, $defaultMetadataCache)
+ {
+ $xemMetadataCacheDriver = isset($xmlEntityManager['metadata_cache_driver']) ? $xmlEntityManager['metadata_cache_driver'] : $defaultMetadataCache;
+ $type = $xemMetadataCacheDriver['type'];
+
+ if ('memcache' === $type) {
+ $memcacheClass = isset($xemMetadataCacheDriver['class']) ? $xemMetadataCacheDriver['class'] : sprintf('%%doctrine.oxm.cache.%s.class%%', $type);
+ $cacheDef = new Definition($memcacheClass);
+ $memcacheHost = isset($xemMetadataCacheDriver['host']) ? $xemMetadataCacheDriver['host'] : '%doctrine.oxm.cache.memcache_host%';
+ $memcachePort = isset($xemMetadataCacheDriver['port']) ? $xemMetadataCacheDriver['port'] : '%doctrine.oxm.cache.memcache_port%';
+ $memcacheInstanceClass = isset($xemMetadataCacheDriver['instance-class']) ? $xemMetadataCacheDriver['instance-class'] : (isset($xemMetadataCacheDriver['instance_class']) ? $xemMetadataCacheDriver['instance_class'] : '%doctrine.oxm.cache.memcache_instance.class%');
+ $memcacheInstance = new Definition($memcacheInstanceClass);
+ $memcacheInstance->addMethodCall('connect', array($memcacheHost, $memcachePort));
+ $container->setDefinition(sprintf('doctrine.oxm.%s_memcache_instance', $xmlEntityManager['name']), $memcacheInstance);
+ $cacheDef->addMethodCall('setMemcache', array(new Reference(sprintf('doctrine.oxm.%s_memcache_instance', $xmlEntityManager['name']))));
+ } else {
+ $cacheDef = new Definition(sprintf('%%doctrine.oxm.cache.%s.class%%', $type));
+ }
+
+ $container->setDefinition(sprintf('doctrine.oxm.%s_metadata_cache', $xmlEntityManager['name']), $cacheDef);
+ }
+
+ /**
+ * Loads the configured storages.
+ *
+ * @param array $storages An array of storages configurations
+ * @param ContainerBuilder $container A ContainerBuilder instance
+ */
+ protected function loadStorages(array $storages, ContainerBuilder $container)
+ {
+ foreach ($storages as $name => $storage) {
+ $oxmStorageArgs = array(
+ isset($connection['server']) ? $connection['server'] : null,
+ isset($connection['options']) ? $connection['options'] : array(),
+ new Reference(sprintf('doctrine.oxm.%s_configuration', $name))
+ );
+ $oxmStorageDef = new Definition('%doctrine.oxm.storage.class%', $oxmStorageArgs);
+ $container->setDefinition(sprintf('doctrine.oxm.%s_storage', $name), $oxmStorageDef);
+ }
+ }
+
+
+ /**
+ * Loads an OXM xml-entity managers bundle mapping information.
+ *
+ * There are two distinct configuration possibilities for mapping information:
+ *
+ * 1. Specify a bundle and optionally details where the entity and mapping information reside.
+ * 2. Specify an arbitrary mapping location.
+ *
+ * @example
+ *
+ * doctrine.oxm:
+ * mappings:
+ * MyBundle1: ~
+ * MyBundle2: yml
+ * MyBundle3: { type: annotation, dir: XmlEntity/ }
+ * MyBundle4: { type: xml, dir: Resources/config/doctrine/mapping }
+ * MyBundle5:
+ * type: yml
+ * dir: [bundle-mappings1/, bundle-mappings2/]
+ * alias: BundleAlias
+ * arbitrary_key:
+ * type: xml
+ * dir: %kernel.dir%/../src/vendor/DoctrineExtensions/lib/DoctrineExtensions/XmlEntity
+ * prefix: DoctrineExtensions\XmlEntity\
+ * alias: DExt
+ *
+ * In the case of bundles everything is really optional (which leads to autodetection for this bundle) but
+ * in the mappings key everything except alias is a required argument.
+ *
+ * @param array $xmlEntityManager A configured OXM xml-entity manager.
+ * @param Definition A Definition instance
+ * @param ContainerBuilder $container A ContainerBuilder instance
+ */
+ protected function loadDocumentManagerBundlesMappingInformation(array $xmlEntityManager, Definition $oxmConfigDef, ContainerBuilder $container)
+ {
+ // reset state of drivers and alias map. They are only used by this methods and children.
+ $this->drivers = array();
+ $this->aliasMap = array();
+
+ $this->loadMappingInformation($xmlEntityManager, $container);
+ $this->registerMappingDrivers($xmlEntityManager, $container);
+
+ if ($oxmConfigDef->hasMethodCall('setXmlEntityNamespaces')) {
+ // TODO: Can we make a method out of it on Definition? replaceMethodArguments() or something.
+ $calls = $oxmConfigDef->getMethodCalls();
+ foreach ($calls as $call) {
+ if ($call[0] == 'setXmlEntityNamespaces') {
+ $this->aliasMap = array_merge($call[1][0], $this->aliasMap);
+ }
+ }
+ $method = $oxmConfigDef->removeMethodCall('setXmlEntityNamespaces');
+ }
+ $oxmConfigDef->addMethodCall('setXmlEntityNamespaces', array($this->aliasMap));
}
Please sign in to comment.
Something went wrong with that request. Please try again.