-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Doctrine config #99
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Doctrine config #99
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
652bca1
Added a Configuration class for DoctrineBundle
stof 477109f
Completed the Configuration class and refactored the extension to use it
stof 0d0f053
Updated the XSD schema
stof a9a300e
Updated the tests
stof 527749c
Made mandatory to have at least one entity manager when using the ORM…
stof f37a2a9
Refactored the config to use the right parameters instead of using th…
stof e522424
Changed default logging param to kernel.debug and fixed container uni…
stof File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
211 changes: 211 additions & 0 deletions
211
src/Symfony/Bundle/DoctrineBundle/DependencyInjection/Configuration.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,211 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien.potencier@symfony-project.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bundle\DoctrineBundle\DependencyInjection; | ||
|
||
use Symfony\Component\Config\Definition\Builder\NodeBuilder; | ||
use Symfony\Component\Config\Definition\Builder\TreeBuilder; | ||
|
||
/** | ||
* This class contains the configuration information for the bundle | ||
* | ||
* This information is solely responsible for how the different configuration | ||
* sections are normalized, and merged. | ||
* | ||
* @author Christophe Coevoet <stof@notk.org> | ||
*/ | ||
class Configuration | ||
{ | ||
private $kernelDebug; | ||
|
||
/** | ||
* Generates the configuration tree. | ||
* | ||
* @param Boolean $kernelDebug | ||
* @return \Symfony\Component\Config\Definition\NodeInterface | ||
*/ | ||
public function getConfigTree($kernelDebug) | ||
{ | ||
$this->kernelDebug = (bool) $kernelDebug; | ||
|
||
$treeBuilder = new TreeBuilder(); | ||
$rootNode = $treeBuilder->root('doctrine', 'array'); | ||
|
||
$this->addDbalSection($rootNode); | ||
$this->addOrmSection($rootNode); | ||
|
||
return $treeBuilder->buildTree(); | ||
} | ||
|
||
private function addDbalSection(NodeBuilder $node) | ||
{ | ||
$node | ||
->arrayNode('dbal') | ||
->beforeNormalization() | ||
->ifNull() | ||
->then(function($v) { return array (); }) // Let use the default values with the subsequent closure. | ||
->end() | ||
->beforeNormalization() | ||
->ifTrue(function($v){ return is_array($v) && !array_key_exists('connections', $v) && !array_key_exists('connection', $v); }) | ||
->then(function($v) { | ||
$connection = array (); | ||
$keys = array ('dbname', 'host', 'port', 'user', 'password', 'driver', 'driver_class', 'options', 'path', 'memory', 'unix_socket', 'wrapper_class', 'platform_service', 'charset', 'logging'); | ||
foreach ($keys as $key) { | ||
if (array_key_exists($key, $v)) { | ||
$connection[$key] = $v[$key]; | ||
unset($v[$key]); | ||
} | ||
} | ||
$defaultConnection = isset($v['default_connection']) ? (string) $v['default_connection'] : 'default'; | ||
$v['connections'] = array ($defaultConnection => $connection); | ||
$v['default_connection'] = $defaultConnection; | ||
return $v; | ||
}) | ||
->end() | ||
->scalarNode('default_connection')->isRequired()->cannotBeEmpty()->end() | ||
->fixXmlConfig('type') | ||
->arrayNode('types') | ||
->useAttributeAsKey('name') | ||
->prototype('scalar') | ||
->beforeNormalization() | ||
->ifTrue(function($v) { return is_array($v) && isset($v['class']); }) | ||
->then(function($v) { return $v['class']; }) | ||
->end() | ||
->end() | ||
->end() | ||
->fixXmlConfig('connection') | ||
->builder($this->getDbalConnectionsNode()) | ||
->end() | ||
; | ||
} | ||
|
||
private function getDbalConnectionsNode() | ||
{ | ||
$node = new NodeBuilder('connections', 'array'); | ||
$node | ||
->requiresAtLeastOneElement() | ||
->useAttributeAsKey('name') | ||
->prototype('array') | ||
->scalarNode('dbname')->end() | ||
->scalarNode('host')->defaultValue('localhost')->end() | ||
->scalarNode('port')->defaultNull()->end() | ||
->scalarNode('user')->defaultValue('root')->end() | ||
->scalarNode('password')->defaultNull()->end() | ||
->scalarNode('driver')->defaultValue('pdo_mysql')->end() | ||
->fixXmlConfig('driver_class', 'driverClass') | ||
->scalarNode('driver_class')->end() | ||
->fixXmlConfig('options', 'driverOptions') | ||
->arrayNode('driverOptions') | ||
->useAttributeAsKey('key') | ||
->prototype('scalar')->end() | ||
->end() | ||
->scalarNode('path')->end() | ||
->booleanNode('memory')->end() | ||
->scalarNode('unix_socket')->end() | ||
->fixXmlConfig('wrapper_class', 'wrapperClass') | ||
->scalarNode('wrapper_class')->end() | ||
->scalarNode('platform_service')->end() | ||
->scalarNode('charset')->defaultValue('UTF-8')->end() | ||
->booleanNode('logging')->defaultValue($this->kernelDebug)->end() | ||
->end() | ||
; | ||
|
||
return $node; | ||
} | ||
|
||
private function addOrmSection(NodeBuilder $node) | ||
{ | ||
$node | ||
->arrayNode('orm') | ||
->beforeNormalization() | ||
->ifTrue(function($v){ return is_array($v) && !array_key_exists('entity_managers', $v) && !array_key_exists('entity_manager', $v); }) | ||
->then(function($v) { | ||
$entityManager = array (); | ||
$keys = array ('result_cache_driver', 'result-cache-driver', 'metadata_cache_driver', 'metadata-cache-driver', 'query_cache_driver', 'query-cache-driver', 'mappings', 'mapping', 'connection'); | ||
foreach ($keys as $key) { | ||
if (array_key_exists($key, $v)) { | ||
$entityManager[$key] = $v[$key]; | ||
unset($v[$key]); | ||
} | ||
} | ||
$defaultEntityManager = isset($v['default_entity_manager']) ? (string) $v['default_entity_manager'] : 'default'; | ||
$v['entity_managers'] = array ($defaultEntityManager => $entityManager); | ||
$v['default_entity_manager'] = $defaultEntityManager; | ||
return $v; | ||
}) | ||
->end() | ||
->scalarNode('default_entity_manager')->isRequired()->cannotBeEmpty()->end() | ||
->booleanNode('auto_generate_proxy_classes')->defaultFalse()->end() | ||
->scalarNode('proxy_dir')->defaultValue('%kernel.cache_dir%/doctrine/orm/Proxies')->end() | ||
->scalarNode('proxy_namespace')->defaultValue('Proxies')->end() | ||
->fixXmlConfig('entity_manager') | ||
->builder($this->getOrmEntityManagersNode()) | ||
->end() | ||
; | ||
} | ||
|
||
private function getOrmEntityManagersNode() | ||
{ | ||
$node = new NodeBuilder('entity_managers', 'array'); | ||
$node | ||
->requiresAtLeastOneElement() | ||
->useAttributeAsKey('name') | ||
->prototype('array') | ||
->addDefaultsIfNotSet() | ||
->builder($this->getOrmCacheDriverNode('query_cache_driver')) | ||
->builder($this->getOrmCacheDriverNode('metadata_cache_driver')) | ||
->builder($this->getOrmCacheDriverNode('result_cache_driver')) | ||
->scalarNode('connection')->end() | ||
->scalarNode('class_metadata_factory_name')->defaultValue('%doctrine.orm.class_metadata_factory_name%')->end() | ||
->fixXmlConfig('mapping') | ||
->arrayNode('mappings') | ||
->isRequired() | ||
->requiresAtLeastOneElement() | ||
->useAttributeAsKey('name') | ||
->prototype('array') | ||
->beforeNormalization() | ||
->ifString() | ||
->then(function($v) { return array ('type' => $v); }) | ||
->end() | ||
->treatNullLike(array ()) | ||
->scalarNode('type')->end() | ||
->scalarNode('dir')->end() | ||
->scalarNode('alias')->end() | ||
->scalarNode('prefix')->end() | ||
->booleanNode('is_bundle')->end() | ||
->performNoDeepMerging() | ||
->end() | ||
->end() | ||
->end() | ||
; | ||
|
||
return $node; | ||
} | ||
|
||
private function getOrmCacheDriverNode($name) | ||
{ | ||
$node = new NodeBuilder($name, 'array'); | ||
$node | ||
->addDefaultsIfNotSet() | ||
->beforeNormalization() | ||
->ifString() | ||
->then(function($v) { return array ('type' => $v); }) | ||
->end() | ||
->scalarNode('type')->defaultValue('array')->isRequired()->end() | ||
->scalarNode('host')->end() | ||
->scalarNode('port')->end() | ||
->scalarNode('instance_class')->end() | ||
->scalarNode('class')->end() | ||
; | ||
|
||
return $node; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For MySQL, this must be UTF8. I think , we need to remove the default value and skip it if it's not explicitly defined by the user.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in #216