2013-06-09 00:19:30 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This file is part of the PropelBundle package.
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*
|
|
|
|
* @license MIT License
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Propel\PropelBundle;
|
|
|
|
|
2014-12-03 18:38:11 +01:00
|
|
|
use Propel\PropelBundle\DependencyInjection\Security\UserProvider\PropelFactory;
|
2013-10-15 23:48:33 +02:00
|
|
|
use Propel\Runtime\Propel;
|
|
|
|
use Propel\Runtime\Connection\ConnectionManagerSingle;
|
2013-10-31 22:46:54 +01:00
|
|
|
use Propel\Runtime\Connection\ConnectionManagerMasterSlave;
|
2013-10-15 23:48:33 +02:00
|
|
|
|
2013-06-09 00:19:30 +02:00
|
|
|
use Symfony\Component\HttpKernel\Bundle\Bundle;
|
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* PropelBundle
|
|
|
|
*
|
|
|
|
* @author William DURAND <william.durand1@gmail.com>
|
|
|
|
*/
|
|
|
|
class PropelBundle extends Bundle
|
|
|
|
{
|
2013-10-15 23:48:33 +02:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2013-06-09 00:19:30 +02:00
|
|
|
public function boot()
|
|
|
|
{
|
2013-11-04 23:01:44 +01:00
|
|
|
try {
|
|
|
|
$this->configureConnections();
|
2013-11-01 18:49:56 +01:00
|
|
|
|
2013-11-04 23:01:44 +01:00
|
|
|
if ($this->container->getParameter('propel.logging')) {
|
|
|
|
$this->configureLogging();
|
|
|
|
}
|
2014-12-03 18:38:11 +01:00
|
|
|
} catch( \Exception $e ) {
|
|
|
|
}
|
2013-06-09 00:19:30 +02:00
|
|
|
}
|
|
|
|
|
2013-10-15 23:48:33 +02:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2013-06-09 00:19:30 +02:00
|
|
|
public function build(ContainerBuilder $container)
|
|
|
|
{
|
2014-12-03 18:38:11 +01:00
|
|
|
parent::build($container);
|
|
|
|
|
|
|
|
if ($container->hasExtension('security')) {
|
|
|
|
$container->getExtension('security')->addUserProviderFactory(new PropelFactory('propel', 'propel.security.user.provider'));
|
|
|
|
}
|
2013-06-09 00:19:30 +02:00
|
|
|
}
|
2013-10-15 23:48:33 +02:00
|
|
|
|
|
|
|
protected function configureConnections()
|
|
|
|
{
|
2014-09-09 22:59:13 +02:00
|
|
|
$config = $this->container->getParameter('propel.configuration');
|
2014-11-21 14:01:36 +01:00
|
|
|
$defaultConnection = !empty($config['runtime']['defaultConnection']) ? $config['runtime']['defaultConnection'] : key($config['database']['connections']);
|
2013-10-15 23:48:33 +02:00
|
|
|
|
|
|
|
$serviceContainer = Propel::getServiceContainer();
|
2014-11-21 14:01:36 +01:00
|
|
|
$serviceContainer->setDefaultDatasource($defaultConnection);
|
2013-10-15 23:48:33 +02:00
|
|
|
|
2014-09-09 22:59:13 +02:00
|
|
|
foreach ($config['database']['connections'] as $name => $config) {
|
|
|
|
if (!empty($config['slaves'])) {
|
2013-10-31 22:46:54 +01:00
|
|
|
$manager = new ConnectionManagerMasterSlave();
|
|
|
|
|
|
|
|
// configure the master (write) connection
|
2015-03-12 18:02:40 +01:00
|
|
|
$manager->setWriteConfiguration($config);
|
|
|
|
|
2013-10-31 22:46:54 +01:00
|
|
|
// configure the slave (read) connections
|
2015-03-12 18:02:40 +01:00
|
|
|
$slaveConnections = [];
|
|
|
|
foreach ($config['slaves'] as $slave) {
|
|
|
|
$slaveConnections[] = array_merge($config, [
|
|
|
|
'dsn' => $slave['dsn'],
|
|
|
|
'slaves' => null
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
$manager->setReadConfiguration($slaveConnections);
|
2013-10-31 22:46:54 +01:00
|
|
|
} else {
|
|
|
|
$manager = new ConnectionManagerSingle();
|
2014-09-09 22:59:13 +02:00
|
|
|
$manager->setConfiguration($config);
|
2013-10-31 22:46:54 +01:00
|
|
|
}
|
2013-10-15 23:48:33 +02:00
|
|
|
|
|
|
|
$serviceContainer->setAdapterClass($name, $config['adapter']);
|
|
|
|
$serviceContainer->setConnectionManager($name, $manager);
|
|
|
|
}
|
|
|
|
}
|
2013-11-01 18:49:56 +01:00
|
|
|
|
|
|
|
protected function configureLogging()
|
|
|
|
{
|
|
|
|
$serviceContainer = Propel::getServiceContainer();
|
|
|
|
$serviceContainer->setLogger('defaultLogger', $this->container->get('propel.logger'));
|
|
|
|
|
|
|
|
foreach ($serviceContainer->getConnectionManagers() as $manager) {
|
|
|
|
$connection = $manager->getReadConnection($serviceContainer->getAdapter($manager->getName()));
|
|
|
|
$connection->setLogMethods(array_merge($connection->getLogMethods(), array('prepare')));
|
|
|
|
|
|
|
|
$connection = $manager->getWriteConnection();
|
|
|
|
$connection->setLogMethods(array_merge($connection->getLogMethods(), array('prepare')));
|
|
|
|
}
|
|
|
|
}
|
2013-06-09 00:19:30 +02:00
|
|
|
}
|