Skip to content

Commit

Permalink
Improved configuration builder readability
Browse files Browse the repository at this point in the history
  • Loading branch information
wmozejkostp authored and ilario-pierbattista committed Dec 27, 2018
1 parent af667a5 commit e44ebeb
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 68 deletions.
44 changes: 26 additions & 18 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,18 @@ mongo_db_bundle:
clients:

foo_client_name: # choose your client name
# uri: 'mongodb://host1:3062,host2' # default null (will use hosts to build connection URI)
uri: 'mongodb://host1:3062,host2' # default null (will use hosts to build connection URI)
hosts: # required if uri is not set - will compose your connection URI (mongodb://host1:3062,host2:27017)
- { host: host1, port: 3062 } # this
- { host: host2 }
username: ''
password: ''
authSource: '' # the database name associated with the user’s credentials, defaults to connection
database_name if not specified
replicaSet: '' # default null (no replica) (experimental)
ssl: false
username: ''
password: ''
authSource: '' # the database name associated with the user’s credentials, defaults to connection
# 'database_name' if not specified
replicaSet: '' # default null (no replica) (experimental)
ssl: false
connectTimeoutMS: 3000 # default null (no timeout)
readPreference: primaryPreferred # see https://docs.mongodb.com/manual/reference/read-preference/#primary for info
readPreference: primaryPreferred # see https://docs.mongodb.com/manual/reference/read-preference/#primary for info

other_client: ~ # same as upper configuration

Expand Down Expand Up @@ -128,22 +128,30 @@ On dev environment all queries executed by the library MongoDB\Collection class

### Contributing

Feel free to contribute by opening a pull request, if you find a bug or to suggest a new feature.
If you like docker, this repository is provided with a dev environment with some scripts to prepare and use it.
All you need is docker and docker-compose installed on your system.
Feel free to contribute by opening a pull request.
Bug fixes or feature suggestions are always welcome.

```
make setup # will build the needed containers and setup the project
```
#### Development environment

```
make start # will start the needed containers and put you inside the php-cli container
If you like docker there are some scripts to setup an insulated development environment.
Please be sure to have `docker` and `docker-compose` installed on your system.

To setup the project:
```bash
make setup
```

To start the container and log into the php-cli:
```bash
make start
```
make test # will launch the test suite

To execute the test suite:
```bash
make test
```
Note: All these scripts are meant to be used outside the containers.

Note: All the above are meant to be used outside the containers.

#### Port binding setup

Expand Down
154 changes: 104 additions & 50 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Facile\MongoDbBundle\DependencyInjection;

use Symfony\Component\Config\Definition\Builder\NodeBuilder;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

Expand All @@ -12,63 +13,116 @@
*/
final class Configuration implements ConfigurationInterface
{
const READ_PREFERENCE_VALID_OPTIONS = ['primary', 'primaryPreferred', 'secondary', 'secondaryPreferred', 'nearest'];

/**
* {@inheritdoc}
*/
public function getConfigTreeBuilder()
{
$readPreferenceValidOptions = ['primary', 'primaryPreferred', 'secondary', 'secondaryPreferred', 'nearest'];

$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('mongo_db_bundle');
$rootNode
->children()
->booleanNode('data_collection')->defaultTrue()->info('Disables Data Collection if needed')->end()
->arrayNode('clients')
->isRequired()
->requiresAtLeastOneElement()
->useAttributeAsKey('name')
->prototype('array')
->children()
->arrayNode('hosts')->info('Hosts addresses and ports')
->prototype('array')
->children()
->scalarNode('host')->isRequired()->end()
->integerNode('port')->defaultValue(27017)->end()
->end()
->end()
->end()
->scalarNode('uri')->defaultNull()->info('Overrides hosts configuration')->end()
->scalarNode('username')->defaultValue('')->end()
->scalarNode('password')->defaultValue('')->end()
->scalarNode('authSource')->defaultNull()->info('Database name associated with the user’s credentials')->end()
->scalarNode('readPreference')
->defaultValue('primaryPreferred')
->validate()
->ifNotInArray($readPreferenceValidOptions)
->thenInvalid('Invalid readPreference option %s, must be one of ['.implode(', ', $readPreferenceValidOptions).']')
->end()
->end()
->scalarNode('replicaSet')->defaultNull()->end()
->booleanNode('ssl')->defaultFalse()->end()
->integerNode('connectTimeoutMS')->defaultNull()->end()
->end()
->end()
->end()
->end();
$rootNode
->children()
->arrayNode('connections')->isRequired()->requiresAtLeastOneElement()
->useAttributeAsKey('name')
->prototype('array')
->children()
->scalarNode('client_name')->isRequired()->info('Desired client name')->end()
->scalarNode('database_name')->isRequired()->info('Database name')->end()
->end()
->end()
->end()
->end();
$rootBuilder = $treeBuilder->root('mongo_db_bundle')->children();

$this->addDataCollection($rootBuilder);
$this->addClients($rootBuilder);
$this->addConnections($rootBuilder);

return $treeBuilder;
}

private function addDataCollection(NodeBuilder $builder)
{
$builder
->booleanNode('data_collection')
->defaultTrue()
->info('Disables Data Collection if needed');
}

private function addClients(NodeBuilder $builder)
{
$clientsBuilder = $builder
->arrayNode('clients')
->isRequired()
->requiresAtLeastOneElement()
->useAttributeAsKey('name')
->prototype('array')
->children();

$this->addClientsHosts($clientsBuilder);

$clientsBuilder
->scalarNode('uri')
->defaultNull()
->info('Overrides hosts configuration');

$clientsBuilder
->scalarNode('username')
->defaultValue('');

$clientsBuilder
->scalarNode('password')
->defaultValue('');

$clientsBuilder
->scalarNode('authSource')
->defaultNull()
->info('Database name associated with the user’s credentials');

$clientsBuilder
->scalarNode('readPreference')
->defaultValue('primaryPreferred')
->validate()
->ifNotInArray(self::READ_PREFERENCE_VALID_OPTIONS)
->thenInvalid('Invalid readPreference option %s, must be one of [' . implode(', ', self::READ_PREFERENCE_VALID_OPTIONS) . ']');

$clientsBuilder
->scalarNode('replicaSet')
->defaultNull();

$clientsBuilder
->booleanNode('ssl')
->defaultFalse();

$clientsBuilder
->integerNode('connectTimeoutMS')
->defaultNull();
}

private function addClientsHosts(NodeBuilder $builder)
{
$hostsBuilder = $builder
->arrayNode('hosts')
->info('Hosts addresses and ports')
->prototype('array')
->children();

$hostsBuilder
->scalarNode('host')
->isRequired();

$hostsBuilder
->integerNode('port')
->defaultValue(27017);
}

private function addConnections(NodeBuilder $builder)
{
$connectionBuilder = $builder
->arrayNode('connections')
->isRequired()
->requiresAtLeastOneElement()
->useAttributeAsKey('name')
->prototype('array')
->children();

$connectionBuilder
->scalarNode('client_name')
->isRequired()
->info('Desired client name');

$connectionBuilder
->scalarNode('database_name')
->isRequired()
->info('Database name');
}
}

0 comments on commit e44ebeb

Please sign in to comment.