|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Symfony\Bundle\DoctrineMongoDBBundle\DependencyInjection; |
| 4 | + |
| 5 | +use Symfony\Component\Config\Definition\Builder\NodeBuilder; |
| 6 | +use Symfony\Component\Config\Definition\Builder\TreeBuilder; |
| 7 | + |
| 8 | +/** |
| 9 | + * FrameworkExtension configuration structure. |
| 10 | + * |
| 11 | + * @author Ryan Weaver <ryan@thatsquality.com> |
| 12 | + */ |
| 13 | +class Configuration |
| 14 | +{ |
| 15 | + /** |
| 16 | + * Generates the configuration tree. |
| 17 | + * |
| 18 | + * @param boolean $kernelDebug The kernel.debug DIC parameter |
| 19 | + * @return \Symfony\Component\DependencyInjection\Configuration\NodeInterface |
| 20 | + */ |
| 21 | + public function getConfigTree() |
| 22 | + { |
| 23 | + $treeBuilder = new TreeBuilder(); |
| 24 | + $rootNode = $treeBuilder->root('doctrine_mongo_db', 'array'); |
| 25 | + |
| 26 | + $this->addSingleDocumentManagerSection($rootNode); |
| 27 | + $this->addDocumentManagersSection($rootNode); |
| 28 | + $this->addSingleConnectionSection($rootNode); |
| 29 | + $this->addConnectionsSection($rootNode); |
| 30 | + |
| 31 | + $rootNode |
| 32 | + ->scalarNode('proxy_namespace')->defaultValue('Proxies')->end() |
| 33 | + ->scalarNode('auto_generate_proxy_classes')->defaultValue(false)->end() |
| 34 | + ->scalarNode('hydrator_namespace')->defaultValue('Hydrators')->end() |
| 35 | + ->scalarNode('auto_generate_hydrator_classes')->defaultValue(false)->end() |
| 36 | + ; |
| 37 | + |
| 38 | + return $treeBuilder->buildTree(); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Builds the nodes responsible for the config that supports the single |
| 43 | + * document manager. |
| 44 | + */ |
| 45 | + private function addSingleDocumentManagerSection(NodeBuilder $rootNode) |
| 46 | + { |
| 47 | + $rootNode |
| 48 | + ->scalarNode('default_document_manager')->defaultValue('default')->end() |
| 49 | + ->scalarNode('default_database')->defaultValue('default')->end() |
| 50 | + ->builder($this->getMetadataCacheDriverNode()) |
| 51 | + ->fixXmlConfig('mapping') |
| 52 | + ->builder($this->getMappingsNode()) |
| 53 | + ; |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Configures the "document_managers" section |
| 58 | + */ |
| 59 | + private function addDocumentManagersSection(NodeBuilder $rootNode) |
| 60 | + { |
| 61 | + $rootNode |
| 62 | + ->fixXmlConfig('document_manager') |
| 63 | + ->arrayNode('document_managers') |
| 64 | + ->useAttributeAsKey('id') |
| 65 | + ->prototype('array') |
| 66 | + ->performNoDeepMerging() |
| 67 | + ->treatNullLike(array()) |
| 68 | + ->builder($this->getMetadataCacheDriverNode()) |
| 69 | + ->scalarNode('default_database')->end() |
| 70 | + ->scalarNode('connection')->end() |
| 71 | + ->scalarNode('database')->end() |
| 72 | + ->fixXmlConfig('mapping') |
| 73 | + ->builder($this->getMappingsNode()) |
| 74 | + ->end() |
| 75 | + ->end() |
| 76 | + ; |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Configures the single-connection section: |
| 81 | + * * default_connection |
| 82 | + * * server |
| 83 | + * * options |
| 84 | + */ |
| 85 | + private function addSingleConnectionSection(NodeBuilder $rootNode) |
| 86 | + { |
| 87 | + $rootNode |
| 88 | + ->scalarNode('default_connection')->defaultValue('default')->end() |
| 89 | + ->builder($this->addConnectionServerNode()) |
| 90 | + ->builder($this->addConnectionOptionsNode()) |
| 91 | + ; |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Adds the configuration for the "connections" key |
| 96 | + */ |
| 97 | + private function addConnectionsSection(NodeBuilder $rootNode) |
| 98 | + { |
| 99 | + $rootNode |
| 100 | + ->fixXmlConfig('connection') |
| 101 | + ->arrayNode('connections') |
| 102 | + ->useAttributeAsKey('id') |
| 103 | + ->prototype('array') |
| 104 | + ->performNoDeepMerging() |
| 105 | + ->builder($this->addConnectionServerNode()) |
| 106 | + ->builder($this->addConnectionOptionsNode()) |
| 107 | + ->end() |
| 108 | + ->end() |
| 109 | + ; |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Returns the array node used for "mappings". |
| 114 | + * |
| 115 | + * This is used in two different parts of the tree. |
| 116 | + * |
| 117 | + * @param NodeBuilder $rootNode The parent node |
| 118 | + * @return NodeBuilder |
| 119 | + */ |
| 120 | + protected function getMappingsNode() |
| 121 | + { |
| 122 | + $node = new Nodebuilder('mappings', 'array'); |
| 123 | + $node |
| 124 | + ->useAttributeAsKey('name') |
| 125 | + ->prototype('array') |
| 126 | + ->beforeNormalization() |
| 127 | + // if it's not an array, then the scalar is the type key |
| 128 | + ->ifString() |
| 129 | + ->then(function($v) { return array ('type' => $v); }) |
| 130 | + ->end() |
| 131 | + // I believe that "null" should *not* set the type |
| 132 | + // it's guessed in AbstractDoctrineExtension::detectMetadataDriver |
| 133 | + ->treatNullLike(array()) |
| 134 | + ->scalarNode('type')->end() |
| 135 | + ->scalarNode('dir')->end() |
| 136 | + ->scalarNode('prefix')->end() |
| 137 | + ->scalarNode('alias')->end() |
| 138 | + ->booleanNode('is_bundle')->end() |
| 139 | + ->performNoDeepMerging() |
| 140 | + ->end() |
| 141 | + ; |
| 142 | + |
| 143 | + return $node; |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * Adds the NodeBuilder for the "server" key of a connection. |
| 148 | + */ |
| 149 | + private function addConnectionServerNode() |
| 150 | + { |
| 151 | + $node = new NodeBuilder('server', 'scalar'); |
| 152 | + |
| 153 | + $node |
| 154 | + ->defaultValue(null) |
| 155 | + ->end(); |
| 156 | + |
| 157 | + return $node; |
| 158 | + } |
| 159 | + |
| 160 | + /** |
| 161 | + * Adds the NodeBuilder for the "options" key of a connection. |
| 162 | + */ |
| 163 | + private function addConnectionOptionsNode() |
| 164 | + { |
| 165 | + $node = new NodeBuilder('options', 'array'); |
| 166 | + |
| 167 | + $node |
| 168 | + ->performNoDeepMerging() |
| 169 | + ->addDefaultsIfNotSet() // adds an empty array of omitted |
| 170 | + |
| 171 | + // options go into the Mongo constructor |
| 172 | + // http://www.php.net/manual/en/mongo.construct.php |
| 173 | + ->booleanNode('connect')->end() |
| 174 | + ->scalarNode('persist')->end() |
| 175 | + ->scalarNode('timeout')->end() |
| 176 | + ->booleanNode('replicaSet')->end() |
| 177 | + ->scalarNode('username')->end() |
| 178 | + ->scalarNode('password')->end() |
| 179 | + ->end(); |
| 180 | + |
| 181 | + return $node; |
| 182 | + } |
| 183 | + |
| 184 | + private function getMetadataCacheDriverNode() |
| 185 | + { |
| 186 | + $node = new NodeBuilder('metadata_cache_driver', 'array'); |
| 187 | + |
| 188 | + $node |
| 189 | + ->beforeNormalization() |
| 190 | + // if scalar |
| 191 | + ->ifTrue(function($v) { return !is_array($v); }) |
| 192 | + ->then(function($v) { return array('type' => $v); }) |
| 193 | + ->end() |
| 194 | + ->scalarNode('type')->end() |
| 195 | + ->scalarNode('class')->end() |
| 196 | + ->scalarNode('host')->end() |
| 197 | + ->scalarNode('port')->end() |
| 198 | + ->scalarNode('instance_class')->end() |
| 199 | + ->end(); |
| 200 | + |
| 201 | + return $node; |
| 202 | + } |
| 203 | +} |
0 commit comments