propel-bundle/DependencyInjection/PropelExtension.php

188 lines
6.3 KiB
PHP
Raw Normal View History

2010-10-28 14:41:03 +02:00
<?php
namespace Propel\PropelBundle\DependencyInjection;
2010-10-28 14:41:03 +02:00
2011-03-11 20:51:54 +01:00
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
2010-10-28 14:41:03 +02:00
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\Config\FileLocator;
2010-10-28 14:41:03 +02:00
class PropelExtension extends Extension
{
/**
* Loads the Propel configuration.
*
2011-01-23 21:28:30 +01:00
* @param array $configs An array of configuration settings
2010-10-28 14:41:03 +02:00
* @param ContainerBuilder $container A ContainerBuilder instance
*/
public function load(array $configs, ContainerBuilder $container)
2010-10-28 14:41:03 +02:00
{
$dbal = array();
foreach ($configs as $config) {
if (isset($config['dbal'])) {
$dbal[] = $config['dbal'];
}
}
2011-01-23 21:28:30 +01:00
$config = $configs[0];
2010-10-28 14:41:03 +02:00
if (!$container->hasDefinition('propel')) {
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
2010-10-28 14:41:03 +02:00
$loader->load('propel.xml');
}
if (!$container->hasParameter('propel.path')) {
if (!isset($config['path'])) {
throw new \InvalidArgumentException('The "path" parameter is mandatory.');
}
$container->setParameter('propel.path', $config['path']);
}
if (isset($config['path'])) {
$container->setParameter('propel.path', $config['path']);
}
if (isset($config['phing_path'])) {
$container->setParameter('propel.phing_path', $config['phing_path']);
}
if (isset($config['charset'])) {
$charset = $config['charset'];
} else {
$charset = 'UTF8';
}
$container->setParameter('propel.charset', $charset);
if (!empty($dbal)) {
$this->dbalLoad($dbal, $container);
}
2010-10-28 14:41:03 +02:00
}
/**
* Loads the DBAL configuration.
*
* @param array $configs An array of configuration settings
2010-10-28 14:41:03 +02:00
* @param ContainerBuilder $container A ContainerBuilder instance
*/
protected function dbalLoad(array $configs, ContainerBuilder $container)
2010-10-28 14:41:03 +02:00
{
if (!$container->hasDefinition('propel')) {
$loader = new XmlFileLoader($container, __DIR__.'/../Resources/config');
$loader->load('propel.xml');
}
$mergedConfig = array(
'default_connection' => 'default',
);
if (!$container->hasParameter('propel.charset')) {
$container->setParameter('propel.charset', 'utf8');
}
if ($container->hasParameter('kernel.debug')) {
$className = $container->getParameter('kernel.debug') ? 'DebugPDO' : 'PropelPDO';
} else {
$className = 'PropelPDO';
}
2010-10-28 14:41:03 +02:00
$defaultConnection = array(
'driver' => 'mysql',
'user' => 'root',
'password' => '',
'dsn' => '',
'classname' => $className,
2010-10-28 14:41:03 +02:00
'options' => array(),
'attributes' => array(),
'settings' => array('charset' => array('value' => $container->getParameter('propel.charset'))),
2010-10-28 14:41:03 +02:00
);
foreach ($configs as $config) {
if (isset($config['default-connection'])) {
$mergedConfig['default_connection'] = $config['default-connection'];
} else if (isset($config['default_connection'])) {
$mergedConfig['default_connection'] = $config['default_connection'];
2010-10-28 14:41:03 +02:00
}
}
foreach ($configs as $config) {
if (isset($config['connections'])) {
2011-03-25 16:17:43 +01:00
$configConnections = $config['connections'];
if (isset($config['connections']['connection']) && isset($config['connections']['connection'][0])) {
$configConnections = $config['connections']['connection'];
}
2010-10-28 14:41:03 +02:00
} else {
$configConnections[$mergedConfig['default_connection']] = $config;
2010-10-28 14:41:03 +02:00
}
foreach ($configConnections as $name => $connection) {
$connectionName = isset($connection['name']) ? $connection['name'] : $name;
if (!isset($mergedConfig['connections'][$connectionName])) {
$mergedConfig['connections'][$connectionName] = $defaultConnection;
2010-10-28 14:41:03 +02:00
}
$mergedConfig['connections'][$connectionName]['name'] = $connectionName;
foreach ($connection as $k => $v) {
if (isset($defaultConnection[$k])) {
2011-02-14 23:05:23 +01:00
$mergedConfig['connections'][$connectionName][$k] = null !== $v ? $v : '';
}
}
}
}
$config = $mergedConfig;
$connectionName = $config['default_connection'];
$container->setParameter('propel.dbal.default_connection', $connectionName);
$c = array();
foreach ($config['connections'] as $name => $conf) {
$c['datasources'][$name]['adapter'] = $config['connections'][$name]['driver'];
foreach (array('dsn', 'user', 'password', 'classname', 'options', 'attributes', 'settings') as $att) {
if (isset($config['connections'][$name][$att])) {
$c['datasources'][$name]['connection'][$att] = $config['connections'][$name][$att];
}
2010-10-28 14:41:03 +02:00
}
}
$container->getDefinition('propel.configuration')->setArguments(array($c));
}
/**
* Returns the base path for the XSD files.
*
* @return string The XSD base path
*/
public function getXsdValidationBasePath()
{
return __DIR__.'/../Resources/config/schema';
}
/**
* Returns the namespace to be used for this extension (XML namespace).
*
* @return string The XML namespace
*/
public function getNamespace()
{
return 'http://www.symfony-project.org/schema/dic/propel';
}
/**
* Returns the recommended alias to use in XML.
*
* This alias is also the mandatory prefix to use when using YAML.
*
* @return string The alias
*/
public function getAlias()
{
return 'propel';
}
}