doughnut-wedding/src/Sikofitt/App/Configuration/DatabaseConfiguration.php

97 lines
3.8 KiB
PHP

<?php
/*
* doughnutwedding.com
* Copyright (C) 2017 http://doughnutwedding.com eric@doughnutwedding.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace Sikofitt\App\Configuration;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;
class DatabaseConfiguration implements ConfigurationInterface
{
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('doughnut');
$rootNode->children()
->arrayNode('connections')
->prototype('array')
->children()
->arrayNode('connection')
->children()
->scalarNode('driver')
->isRequired()
->validate()
->ifNotInArray(['pdo_mysql', 'pdo_pgsql', 'pdo_sqlite'])
->thenInvalid('Invalid driver : %s')
->end() // ifNotInArray
->end() // driver
->scalarNode('dbname')->isRequired()->end() // database
->scalarNode('host')->defaultValue('127.0.0.1')->end()
->scalarNode('user')->isRequired()->end()
->scalarNode('password')->isRequired()->end()
->end() // connection.prototype
->end() // connection
->arrayNode('annotation_autoloaders')
->requiresAtLeastOneElement()
->prototype('scalar')
->isRequired()
->end()
->end()
->arrayNode('metadata_mapping')
->prototype('array')
->children()
->arrayNode('path')
->requiresAtLeastOneElement()
->prototype('scalar')
->isRequired()
->end()
->end()
->scalarNode('type')
->isRequired()
->beforeNormalization()
->ifString()
->then(function ($s) {
return $this->normalizeConstant($s);
})
->end()
->end()
->end()
->end()
->end()
->end();
return $treeBuilder;
}
private function normalizeConstant($const)
{
$classParts = explode('::', $const);
if (isset($classParts[1])) {
$reflected = new \ReflectionClass($classParts[0]);
$constant = $reflected->getConstant($classParts[1]);
return $constant;
} else {
return $const;
}
}
}