2010-10-28 14:41:03 +02:00
< ? php
2011-08-30 23:29:49 +02:00
/**
* 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
*/
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 ;
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
2011-04-07 10:01:30 +02:00
/**
* PropelExtension loads the PropelBundle configuration .
*
* @ author William DURAND < william . durand1 @ gmail . com >
*/
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 ();
2012-07-06 17:24:55 +02:00
$configuration = $this -> getConfiguration ( $configs , $container );
2011-04-06 01:52:59 +02:00
$config = $processor -> processConfiguration ( $configuration , $configs );
2012-04-07 21:16:26 +02:00
// Composer
if ( file_exists ( $propelPath = $container -> getParameter ( 'kernel.root_dir' ) . '/../vendor/propel/propel1' )) {
$container -> setParameter ( 'propel.path' , $propelPath );
}
if ( file_exists ( $phingPath = $container -> getParameter ( 'kernel.root_dir' ) . '/../vendor/phing/phing/classes' )) {
$container -> setParameter ( 'propel.phing_path' , $phingPath );
}
2010-10-28 14:41:03 +02:00
if ( ! $container -> hasParameter ( 'propel.path' )) {
if ( ! isset ( $config [ 'path' ])) {
2011-10-09 19:49:08 +02:00
throw new \InvalidArgumentException ( 'PropelBundle expects a "path" parameter that must contain the absolute path to the Propel ORM vendor library. The "path" parameter must be defined under the "propel" root node in your configuration.' );
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' ])) {
2011-10-09 19:49:08 +02:00
throw new \InvalidArgumentException ( 'PropelBundle expects a "phing_path" parameter that must contain the absolute path to the Phing vendor library. The "phing_path" parameter must be defined under the "propel" root node in your configuration.' );
2011-04-06 01:58:44 +02:00
} 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
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-18 22:51:27 +02:00
// Load services
if ( ! $container -> hasDefinition ( 'propel' )) {
$loader = new XmlFileLoader ( $container , new FileLocator ( __DIR__ . '/../Resources/config' ));
$loader -> load ( 'propel.xml' );
2011-09-22 16:11:14 +02:00
$loader -> load ( 'converters.xml' );
2011-04-18 22:51:27 +02:00
}
2012-07-16 12:44:36 +02:00
// build properties
2012-01-23 09:33:10 +01:00
if ( isset ( $config [ 'build_properties' ]) && is_array ( $config [ 'build_properties' ])) {
$buildProperties = $config [ 'build_properties' ];
} else {
$buildProperties = array ();
2011-04-18 22:51:27 +02:00
}
2012-07-16 12:44:36 +02:00
// behaviors
if ( isset ( $config [ 'behaviors' ]) && is_array ( $config [ 'behaviors' ])) {
foreach ( $config [ 'behaviors' ] as $name => $class ) {
$buildProperties [ sprintf ( 'propel.behavior.%s.class' , $name )] = $class ;
}
}
2012-01-23 09:33:10 +01:00
$container -> getDefinition ( 'propel.build_properties' ) -> setArguments ( array ( $buildProperties ));
2011-04-06 01:52:59 +02:00
if ( ! empty ( $config [ 'dbal' ])) {
$this -> dbalLoad ( $config [ 'dbal' ], $container );
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
{
2011-04-06 10:40:46 +02:00
if ( empty ( $config [ 'default_connection' ])) {
2011-04-06 01:58:44 +02:00
$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 );
2011-04-11 13:46:23 +02:00
if ( 0 === count ( $config [ 'connections' ])) {
2011-04-06 10:40:46 +02:00
$config [ 'connections' ] = array ( $connectionName => $config );
}
2011-03-26 12:29:22 +01:00
2011-04-06 10:40:46 +02:00
$c = array ();
2011-03-26 12:29:22 +01:00
foreach ( $config [ 'connections' ] as $name => $conf ) {
2011-04-11 13:46:23 +02:00
$c [ 'datasources' ][ $name ][ 'adapter' ] = $conf [ 'driver' ];
2011-11-28 14:37:47 +01:00
if ( ! empty ( $conf [ 'slaves' ])) {
$c [ 'datasources' ][ $name ][ 'slaves' ][ 'connection' ] = $conf [ 'slaves' ];
}
2011-03-26 12:29:22 +01:00
2013-04-11 10:37:16 +02:00
foreach ( array ( 'dsn' , 'user' , 'password' , 'classname' , 'options' , 'attributes' , 'settings' , 'model_paths' ) as $att ) {
2011-04-11 13:46:23 +02:00
if ( isset ( $conf [ $att ])) {
$c [ 'datasources' ][ $name ][ 'connection' ][ $att ] = $conf [ $att ];
2011-03-26 12:29:22 +01:00
}
2010-10-28 14:41:03 +02:00
}
}
2011-04-17 12:21:45 +02:00
// Alias the default connection if not defined
if ( ! isset ( $c [ 'datasources' ][ 'default' ])) {
$c [ 'datasources' ][ 'default' ] = $c [ 'datasources' ][ $connectionName ];
}
2010-10-28 14:41:03 +02:00
$container -> getDefinition ( 'propel.configuration' ) -> setArguments ( array ( $c ));
}
2012-07-06 17:24:55 +02:00
public function getConfiguration ( array $config , ContainerBuilder $container )
{
return new Configuration ( $container -> getParameter ( 'kernel.debug' ));
}
2010-10-28 14:41:03 +02:00
/**
* Returns the base path for the XSD files .
*
* @ return string The XSD base path
*/
public function getXsdValidationBasePath ()
{
return __DIR__ . '/../Resources/config/schema' ;
}
/**
* 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' ;
}
}