2010-10-28 14:41:03 +02:00
|
|
|
<?php
|
|
|
|
|
2011-02-14 22:31:28 +01:00
|
|
|
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;
|
2011-02-23 16:07:48 +01:00
|
|
|
use Symfony\Component\Config\FileLocator;
|
2011-04-06 01:52:59 +02:00
|
|
|
use Symfony\Component\Config\Definition\Processor;
|
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
|
|
|
|
*/
|
2011-02-23 16:07:48 +01:00
|
|
|
public function load(array $configs, ContainerBuilder $container)
|
2010-10-28 14:41:03 +02:00
|
|
|
{
|
2011-04-06 01:52:59 +02:00
|
|
|
$processor = new Processor();
|
|
|
|
$configuration = new Configuration($container->getParameter('kernel.debug'));
|
|
|
|
$config = $processor->processConfiguration($configuration, $configs);
|
|
|
|
|
|
|
|
/*
|
2011-02-23 16:29:29 +01: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];
|
2011-04-06 01:52:59 +02:00
|
|
|
*/
|
2010-10-28 14:41:03 +02:00
|
|
|
if (!$container->hasParameter('propel.path')) {
|
|
|
|
if (!isset($config['path'])) {
|
|
|
|
throw new \InvalidArgumentException('The "path" parameter is mandatory.');
|
2011-04-06 01:58:44 +02:00
|
|
|
} else {
|
|
|
|
$container->setParameter('propel.path', $config['path']);
|
2010-10-28 14:41:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-06 01:58:44 +02:00
|
|
|
if (!$container->hasParameter('propel.phing_path')) {
|
|
|
|
if (!isset($config['phing_path'])) {
|
|
|
|
throw new \InvalidArgumentException('The "phing_path" parameter is mandatory.');
|
|
|
|
} else {
|
|
|
|
$container->setParameter('propel.phing_path', $config['phing_path']);
|
|
|
|
}
|
2010-10-28 14:41:03 +02:00
|
|
|
}
|
2011-01-26 21:47:06 +01:00
|
|
|
|
|
|
|
if (isset($config['charset'])) {
|
|
|
|
$charset = $config['charset'];
|
|
|
|
} else {
|
|
|
|
$charset = 'UTF8';
|
|
|
|
}
|
2011-04-06 01:52:59 +02:00
|
|
|
|
2011-01-26 21:47:06 +01:00
|
|
|
$container->setParameter('propel.charset', $charset);
|
2011-02-23 16:29:29 +01:00
|
|
|
|
2011-04-05 21:16:32 +02:00
|
|
|
if (isset($config['logging']) && $config['logging']) {
|
2011-04-05 16:38:13 +02:00
|
|
|
$logging = $config['logging'];
|
|
|
|
} else {
|
|
|
|
$logging = false;
|
|
|
|
}
|
|
|
|
|
2011-04-06 01:52:59 +02:00
|
|
|
$container->setParameter('propel.logging', $logging);
|
2011-04-05 16:38:13 +02:00
|
|
|
|
2011-04-06 01:52:59 +02:00
|
|
|
if (!empty($config['dbal'])) {
|
|
|
|
$this->dbalLoad($config['dbal'], $container);
|
|
|
|
} else {
|
|
|
|
throw new \RuntimeException('No "dbal" configuration found.');
|
2011-02-23 16:29:29 +01:00
|
|
|
}
|
2010-10-28 14:41:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Loads the DBAL configuration.
|
|
|
|
*
|
2011-04-06 01:52:59 +02:00
|
|
|
* @param array $config An array of configuration settings
|
2010-10-28 14:41:03 +02:00
|
|
|
* @param ContainerBuilder $container A ContainerBuilder instance
|
|
|
|
*/
|
2011-04-06 01:52:59 +02:00
|
|
|
protected function dbalLoad(array $config, ContainerBuilder $container)
|
2010-10-28 14:41:03 +02:00
|
|
|
{
|
|
|
|
if (!$container->hasDefinition('propel')) {
|
2011-04-05 16:01:14 +02:00
|
|
|
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
|
2010-10-28 14:41:03 +02:00
|
|
|
$loader->load('propel.xml');
|
|
|
|
}
|
2011-04-06 01:58:44 +02:00
|
|
|
|
|
|
|
/*
|
2011-01-25 21:24:26 +01:00
|
|
|
$mergedConfig = array(
|
|
|
|
'default_connection' => 'default',
|
|
|
|
);
|
2011-01-26 21:32:57 +01:00
|
|
|
|
2011-01-30 22:37:30 +01:00
|
|
|
if ($container->hasParameter('kernel.debug')) {
|
|
|
|
$className = $container->getParameter('kernel.debug') ? 'DebugPDO' : 'PropelPDO';
|
|
|
|
} else {
|
|
|
|
$className = 'PropelPDO';
|
|
|
|
}
|
2011-01-26 21:32:57 +01:00
|
|
|
|
2010-10-28 14:41:03 +02:00
|
|
|
$defaultConnection = array(
|
|
|
|
'driver' => 'mysql',
|
|
|
|
'user' => 'root',
|
2011-01-25 21:24:26 +01:00
|
|
|
'password' => '',
|
|
|
|
'dsn' => '',
|
2011-01-26 21:32:57 +01:00
|
|
|
'classname' => $className,
|
2010-10-28 14:41:03 +02:00
|
|
|
'options' => array(),
|
|
|
|
'attributes' => array(),
|
2011-01-26 21:47:06 +01:00
|
|
|
'settings' => array('charset' => array('value' => $container->getParameter('propel.charset'))),
|
2010-10-28 14:41:03 +02:00
|
|
|
);
|
|
|
|
|
2011-01-25 21:24:26 +01: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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-25 21:24:26 +01:00
|
|
|
foreach ($configs as $config) {
|
|
|
|
if (isset($config['connections'])) {
|
2011-03-25 16:17:43 +01:00
|
|
|
$configConnections = $config['connections'];
|
2011-01-25 21:24:26 +01:00
|
|
|
if (isset($config['connections']['connection']) && isset($config['connections']['connection'][0])) {
|
|
|
|
$configConnections = $config['connections']['connection'];
|
|
|
|
}
|
2010-10-28 14:41:03 +02:00
|
|
|
} else {
|
2011-01-25 21:24:26 +01:00
|
|
|
$configConnections[$mergedConfig['default_connection']] = $config;
|
2010-10-28 14:41:03 +02:00
|
|
|
}
|
|
|
|
|
2011-01-25 21:24:26 +01: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
|
|
|
}
|
2011-01-25 21:24:26 +01: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 : '';
|
2011-01-25 21:24:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$config = $mergedConfig;
|
2011-04-06 01:52:59 +02:00
|
|
|
*/
|
2011-04-06 01:58:44 +02:00
|
|
|
if (empty ($config['default_connection'])) {
|
|
|
|
$keys = array_keys($config['connections']);
|
|
|
|
$config['default_connection'] = reset($keys);
|
|
|
|
}
|
|
|
|
|
2011-01-25 21:24:26 +01:00
|
|
|
$connectionName = $config['default_connection'];
|
|
|
|
$container->setParameter('propel.dbal.default_connection', $connectionName);
|
|
|
|
|
|
|
|
$c = array();
|
2011-03-26 12:29:22 +01:00
|
|
|
|
|
|
|
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';
|
|
|
|
}
|
|
|
|
}
|