diff --git a/Command/AbstractCommand.php b/Command/AbstractCommand.php index 69c3ca3..d921d77 100644 --- a/Command/AbstractCommand.php +++ b/Command/AbstractCommand.php @@ -75,11 +75,8 @@ abstract class AbstractCommand extends ContainerAwareCommand // collect all schemas $this->copySchemas($kernel, $this->cacheDir); - // build.properties - $this->createBuildPropertiesFile($kernel, $this->cacheDir.'/build.properties'); - - // buildtime-conf.xml - $this->createBuildTimeFile($this->cacheDir.'/buildtime-conf.xml'); + // propel.json + $this->createPropelConfigurationFile($this->cacheDir.'/propel.json'); } /** @@ -126,7 +123,7 @@ abstract class AbstractCommand extends ContainerAwareCommand } if ($this->input->hasOption('connection')) { - $connections = $this->input->getOption('connection') ?: array($this->getContainer()->getParameter('propel.dbal.default_connection')); + $connections = $this->input->getOption('connection') ?: array($this->getDefaultConnection()); if (!in_array((string) $database['name'], $connections)) { // we skip this schema because the connection name doesn't @@ -203,48 +200,15 @@ abstract class AbstractCommand extends ContainerAwareCommand /* * Create an XML file which represents propel.configuration * - * @param string $file Should be 'buildtime-conf.xml'. + * @param string $file Should be 'propel.json'. */ - protected function createBuildTimeFile($file) + protected function createPropelConfigurationFile($file) { - $xml = strtr(<< - - - + $config = array( + 'propel' => $this->getContainer()->getParameter('propel.configuration') + ); -EOT - , array('%default_connection%' => $this->getContainer()->getParameter('propel.dbal.default_connection'))); - - $datasources = $this->getContainer()->getParameter('propel.configuration'); - foreach ($datasources as $name => $datasource) { - $xml .= strtr(<< -%adapter% - -%dsn% -%username% -%password% - - - -EOT - , array( - '%name%' => $name, - '%adapter%' => $datasource['adapter'], - '%dsn%' => $datasource['connection']['dsn'], - '%username%' => $datasource['connection']['user'], - '%password%' => isset($datasource['connection']['password']) ? $datasource['connection']['password'] : '', - )); - } - - $xml .= << - - -EOT; - - file_put_contents($file, $xml); + file_put_contents($file, json_encode($config, JSON_PRETTY_PRINT)); } /** @@ -274,11 +238,11 @@ EOT; protected function getConnectionData($name) { $knownConnections = $this->getContainer()->getParameter('propel.configuration'); - if (!isset($knownConnections[$name])) { + if (!isset($knownConnections['database']['connections'][$name])) { throw new \InvalidArgumentException(sprintf('Unknown connection "%s"', $name)); } - return $knownConnections[$name]; + return $knownConnections['database']['connections'][$name]; } /** @@ -291,32 +255,8 @@ EOT; protected function getDsn($connectionName) { $connection = $this->getConnectionData($connectionName); - $connectionData = $connection['connection']; - return sprintf('%s;user=%s;password=%s', $connectionData['dsn'], $connectionData['user'], $connectionData['password']); - } - - /** - * Create a 'build.properties' file. - * - * @param KernelInterface $kernel The application kernel. - * @param string $file Should be 'build.properties'. - */ - protected function createBuildPropertiesFile(KernelInterface $kernel, $file) - { - $fs = new Filesystem(); - - $buildProperties = $this->getContainer()->getParameter('propel.build_properties'); - $iniFile = $kernel->getRootDir().'/config/propel.ini'; - - if ($fs->exists($iniFile)) { - $buildProperties = array_merge( - parse_ini_file($iniFile), - $buildProperties - ); - } - - $fs->dumpFile($file, $this->arrayToIni($buildProperties)); + return $connection['dsn']; } /** @@ -374,24 +314,6 @@ EOT; return $this->cacheDir; } - /** - * Converts an array to its INI equivalent - * - * @param array $data The array to convert. - * - * @return string The INI formatted array. - */ - protected function arrayToIni(array $data) - { - $lines = array(); - - foreach ($data as $key => $value) { - $lines[] = $key . ' =' . (!empty($value) ? ' ' . $value : ''); - } - - return implode(PHP_EOL, $lines); - } - /** * Extract the database name from a given DSN * @@ -409,4 +331,16 @@ EOT; // e.g. SQLite return null; } + + /** + * Returns the name of the default connection. + * + * @return string + */ + protected function getDefaultConnection() + { + $config = $this->getContainer()->getParameter('propel.configuration'); + + return $config['generator']['defaultConnection']; + } } diff --git a/Command/DatabaseCreateCommand.php b/Command/DatabaseCreateCommand.php index 5c6b6bd..b9ae0d4 100644 --- a/Command/DatabaseCreateCommand.php +++ b/Command/DatabaseCreateCommand.php @@ -44,9 +44,9 @@ class DatabaseCreateCommand extends AbstractCommand */ protected function execute(InputInterface $input, OutputInterface $output) { - $connectionName = $input->getOption('connection') ?: $this->getContainer()->getParameter('propel.dbal.default_connection'); + $connectionName = $input->getOption('connection') ?: $this->getDefaultConnection(); $config = $this->getConnectionData($connectionName); - $dbName = $this->parseDbName($config['connection']['dsn']); + $dbName = $this->parseDbName($config['dsn']); if (null === $dbName) { return $output->writeln('No database name found.'); @@ -56,7 +56,7 @@ class DatabaseCreateCommand extends AbstractCommand try { $manager = new ConnectionManagerSingle(); - $manager->setConfiguration($this->getTemporaryConfiguration($config['connection'])); + $manager->setConfiguration($this->getTemporaryConfiguration($config)); $serviceContainer = Propel::getServiceContainer(); $serviceContainer->setAdapterClass($connectionName, $config['adapter']); diff --git a/Command/DatabaseDropCommand.php b/Command/DatabaseDropCommand.php index 57f11ab..4e859e8 100644 --- a/Command/DatabaseDropCommand.php +++ b/Command/DatabaseDropCommand.php @@ -68,10 +68,10 @@ EOT } } - $connectionName = $input->getOption('connection') ?: $this->getContainer()->getParameter('propel.dbal.default_connection'); + $connectionName = $input->getOption('connection') ?: $this->getDefaultConnection(); $config = $this->getConnectionData($connectionName); $connection = Propel::getConnection($connectionName); - $dbName = $this->parseDbName($config['connection']['dsn']); + $dbName = $this->parseDbName($config['dsn']); if (null === $dbName) { return $output->writeln('No database name found.'); diff --git a/Command/FormGenerateCommand.php b/Command/FormGenerateCommand.php index e46802c..75fc15c 100644 --- a/Command/FormGenerateCommand.php +++ b/Command/FormGenerateCommand.php @@ -12,7 +12,6 @@ namespace Propel\PropelBundle\Command; use Propel\Generator\Config\GeneratorConfig; use Propel\Generator\Command\ModelBuildCommand as BaseModelBuildCommand; -use Propel\Generator\Command\AbstractCommand as BaseCommand; use Propel\Generator\Model\Database; use Propel\Generator\Model\Table; use Propel\Generator\Manager\ModelManager; @@ -42,7 +41,7 @@ class FormGenerateCommand extends AbstractCommand ->setDescription('Generate Form types stubs based on the schema.xml') ->addOption('force', 'f', InputOption::VALUE_NONE, 'Overwrite existing Form types') - ->addOption('platform', null, InputOption::VALUE_REQUIRED, 'The platform', BaseCommand::DEFAULT_PLATFORM) + ->addOption('platform', null, InputOption::VALUE_REQUIRED, 'The platform') ->addArgument('bundle', InputArgument::REQUIRED, 'The bundle to use to generate Form types (Ex: @AcmeDemoBundle)') ->addArgument('models', InputArgument::IS_ARRAY, 'Model classes to generate Form Types from') diff --git a/Command/MigrationDiffCommand.php b/Command/MigrationDiffCommand.php index 8bae9bb..b83624e 100644 --- a/Command/MigrationDiffCommand.php +++ b/Command/MigrationDiffCommand.php @@ -33,7 +33,7 @@ class MigrationDiffCommand extends WrappedCommand ->addOption('connection', null, InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL, 'Connection to use. Example: default, bookstore') ->addOption('output-dir', null, InputOption::VALUE_OPTIONAL, 'The output directory') - ->addOption('migration-table', null, InputOption::VALUE_REQUIRED, 'Migration table name', BaseMigrationCommand::DEFAULT_MIGRATION_TABLE) + ->addOption('migration-table', null, InputOption::VALUE_REQUIRED, 'Migration table name', null) ->addOption('table-renaming', null, InputOption::VALUE_NONE, 'Detect table renaming', null) ->addOption('editor', null, InputOption::VALUE_OPTIONAL, 'The text editor to use to open diff files', null) ->addOption('skip-removed-table', null, InputOption::VALUE_NONE, 'Option to skip removed table from the migration') diff --git a/Command/SqlInsertCommand.php b/Command/SqlInsertCommand.php index 844f795..48afa54 100644 --- a/Command/SqlInsertCommand.php +++ b/Command/SqlInsertCommand.php @@ -45,7 +45,7 @@ class SqlInsertCommand extends WrappedCommand { return array( '--connection' => $this->getConnections($input->getOption('connection')), - '--sql-dir' => $this->cacheDir, + '--sql-dir' => $this->cacheDir, ); } } diff --git a/Command/WrappedCommand.php b/Command/WrappedCommand.php index e751641..a63d9c5 100644 --- a/Command/WrappedCommand.php +++ b/Command/WrappedCommand.php @@ -10,8 +10,6 @@ namespace Propel\PropelBundle\Command; -use Propel\Generator\Command\AbstractCommand as BaseCommand; - use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; @@ -41,7 +39,7 @@ abstract class WrappedCommand extends AbstractCommand protected function configure() { $this - ->addOption('platform', null, InputOption::VALUE_REQUIRED, 'The platform', BaseCommand::DEFAULT_PLATFORM) + ->addOption('platform', null, InputOption::VALUE_OPTIONAL, 'The platform') ; } diff --git a/Controller/PanelController.php b/Controller/PanelController.php index 4f3e9a5..dcae128 100644 --- a/Controller/PanelController.php +++ b/Controller/PanelController.php @@ -33,7 +33,6 @@ class PanelController extends ContainerAware array( 'propel_version' => Propel::VERSION, 'configuration' => $this->container->getParameter('propel.configuration'), - 'default_connection' => $this->container->getParameter('propel.dbal.default_connection'), 'logging' => $this->container->getParameter('propel.logging'), ) ); diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 916e1ca..973b239 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -10,237 +10,11 @@ namespace Propel\PropelBundle\DependencyInjection; -use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; -use Symfony\Component\Config\Definition\Builder\TreeBuilder; -use Symfony\Component\Config\Definition\ConfigurationInterface; +use Propel\Common\Config\PropelConfiguration; /** * This class contains the configuration information for the bundle - * - * This information is solely responsible for how the different configuration - * sections are normalized, and merged. - * - * @author William DURAND */ -class Configuration implements ConfigurationInterface +class Configuration extends PropelConfiguration { - private $debug; - - /** - * Constructor - * - * @param Boolean $debug Wether to use the debug mode - */ - public function __construct($debug) - { - $this->debug = (Boolean) $debug; - } - - /** - * Generates the configuration tree builder. - * - * @return \Symfony\Component\Config\Definition\Builder\TreeBuilder The tree builder - */ - public function getConfigTreeBuilder() - { - $treeBuilder = new TreeBuilder(); - $rootNode = $treeBuilder->root('propel'); - - $this->addGeneralSection($rootNode); - $this->addDbalSection($rootNode); - - return $treeBuilder; - } - - /** - * Adds 'general' configuration. - * - * propel: - * logging: %kernel.debug% - * build_properties: - * xxxx.xxxx: xxxxxx - * ... - * behaviors: - * fooable: My\FooableBehavior - * barable: src.barable.BarableBehavior - */ - private function addGeneralSection(ArrayNodeDefinition $node) - { - $node - ->children() - ->scalarNode('logging')->defaultValue($this->debug)->end() - ->arrayNode('build_properties') - ->useAttributeAsKey('key') - ->prototype('scalar')->end() - ->end() - ->arrayNode('behaviors') - ->useAttributeAsKey('key') - ->prototype('scalar')->end() - ->end() - ; - } - - /** - * Adds 'dbal' configuration. - * - * propel: - * dbal: - * driver: mysql - * user: root - * password: null - * dsn: xxxxxxxx - * options: {} - * attributes: {} - * settings: {} - * default_connection: xxxxxx - */ - private function addDbalSection(ArrayNodeDefinition $node) - { - $node - ->children() - ->arrayNode('dbal') - ->beforeNormalization() - ->ifNull() - ->then(function ($v) { return array ('connections' => array('default' => array())); }) - ->end() - ->children() - ->scalarNode('default_connection')->defaultValue('default')->end() - ->scalarNode('driver') - ->beforeNormalization() - ->always() - ->then(function ($v) { return str_replace('pdo_', '', $v); }) - ->end() - ->defaultValue('mysql') - ->end() - ->scalarNode('user')->defaultValue('root')->end() - ->scalarNode('password')->defaultValue('')->end() - ->scalarNode('dsn') - ->beforeNormalization() - ->always() - ->then(function ($v) { return str_replace('pdo_', '', $v); }) - ->end() - ->defaultValue('') - ->end() - ->scalarNode('classname')->defaultValue($this->debug ? '\Propel\Runtime\Connection\DebugPDO' : '\Propel\Runtime\Connection\ConnectionWrapper')->end() - ->end() - ->fixXmlConfig('option') - ->children() - ->arrayNode('options') - ->useAttributeAsKey('key') - ->prototype('scalar')->end() - ->end() - ->end() - ->fixXmlConfig('attribute') - ->children() - ->arrayNode('attributes') - ->useAttributeAsKey('key') - ->prototype('scalar')->end() - ->end() - ->end() - ->fixXmlConfig('setting') - ->children() - ->arrayNode('settings') - ->useAttributeAsKey('key') - ->prototype('scalar')->end() - ->end() - ->end() - ->fixXmlConfig('connection') - ->append($this->getDbalConnectionsNode()) - ->end() - ; - } - - /** - * Returns a tree configuration for this part of configuration: - * - * connections: - * default: - * driver: mysql - * user: root - * password: null - * dsn: xxxxxxxx - * classname: PropelPDO - * options: {} - * attributes: {} - * settings: {} - * - * @return \Symfony\Component\Config\Definition\Builder\TreeBuilder The tree builder - */ - private function getDbalConnectionsNode() - { - $treeBuilder = new TreeBuilder(); - $node = $treeBuilder->root('connections'); - - $node - ->requiresAtLeastOneElement() - ->useAttributeAsKey('name') - ->prototype('array') - ->children() - ->scalarNode('driver') - ->beforeNormalization() - ->always() - ->then(function ($v) { return str_replace('pdo_', '', $v); }) - ->end() - ->defaultValue('mysql') - ->end() - ->scalarNode('user')->defaultValue('root')->end() - ->scalarNode('password')->defaultValue('')->end() - ->scalarNode('dsn') - ->beforeNormalization() - ->always() - ->then(function ($v) { return str_replace('pdo_', '', $v); }) - ->end() - ->defaultValue('') - ->end() - ->scalarNode('classname')->defaultValue($this->debug ? '\Propel\Runtime\Connection\DebugPDO' : '\Propel\Runtime\Connection\ConnectionWrapper')->end() - ->arrayNode('slaves') - ->useAttributeAsKey('name') - ->prototype('array') - ->children() - ->scalarNode('driver') - ->beforeNormalization() - ->always() - ->then(function ($v) { return str_replace('pdo_', '', $v); }) - ->end() - ->defaultValue('mysql') - ->end() - ->scalarNode('user')->defaultValue('root')->end() - ->scalarNode('password')->defaultValue('')->end() - ->scalarNode('dsn') - ->beforeNormalization() - ->always() - ->then(function ($v) { return str_replace('pdo_', '', $v); }) - ->end() - ->defaultValue('') - ->end() - ->scalarNode('classname')->defaultValue($this->debug ? '\Propel\Runtime\Connection\DebugPDO' : '\Propel\Runtime\Connection\ConnectionWrapper')->end() - ->end() - ->end() - ->end() - ->end() - ->fixXmlConfig('option') - ->children() - ->arrayNode('options') - ->useAttributeAsKey('key') - ->prototype('scalar')->end() - ->end() - ->end() - ->fixXmlConfig('attribute') - ->children() - ->arrayNode('attributes') - ->useAttributeAsKey('key') - ->prototype('scalar')->end() - ->end() - ->end() - ->fixXmlConfig('setting') - ->children() - ->arrayNode('settings') - ->useAttributeAsKey('key') - ->prototype('scalar')->end() - ->end() - ->end() - ; - - return $node; - } } diff --git a/DependencyInjection/PropelExtension.php b/DependencyInjection/PropelExtension.php index ba542c5..f5b24be 100644 --- a/DependencyInjection/PropelExtension.php +++ b/DependencyInjection/PropelExtension.php @@ -36,8 +36,9 @@ class PropelExtension extends Extension $configuration = $this->getConfiguration($configs, $container); $config = $processor->processConfiguration($configuration, $configs); - $container->setParameter('propel.logging', $config['logging']); - $container->setParameter('propel.configuration', array()); + // @todo: restore + $container->setParameter('propel.logging', true); //$config['logging']); + $container->setParameter('propel.configuration', $config); // Load services if (!$container->hasDefinition('propel')) { @@ -46,69 +47,6 @@ class PropelExtension extends Extension $loader->load('converters.xml'); $loader->load('security.xml'); } - - // build properties - $buildProperties = $config['build_properties']; - - // behaviors - if (isset($config['behaviors']) && is_array($config['behaviors'])) { - foreach ($config['behaviors'] as $name => $class) { - $buildProperties[sprintf('propel.behavior.%s.class', $name)] = $class; - } - } - - $container->setParameter('propel.build_properties', $buildProperties); - - if (!empty($config['dbal'])) { - $this->dbalLoad($config['dbal'], $container); - } - } - - /** - * Loads the DBAL configuration. - * - * @param array $config An array of configuration settings - * @param ContainerBuilder $container A ContainerBuilder instance - */ - protected function dbalLoad(array $config, ContainerBuilder $container) - { - if (empty($config['default_connection'])) { - $keys = array_keys($config['connections']); - $config['default_connection'] = reset($keys); - } - - $connectionName = $config['default_connection']; - $container->setParameter('propel.dbal.default_connection', $connectionName); - - if (0 === count($config['connections'])) { - $config['connections'] = array($connectionName => $config); - } - - $c = array(); - foreach ($config['connections'] as $name => $conf) { - $c[$name]['adapter'] = $conf['driver']; - if (!empty($conf['slaves'])) { - $c[$name]['slaves']['connection'] = $conf['slaves']; - } - - foreach (array('dsn', 'user', 'password', 'classname', 'options', 'attributes', 'settings', 'model_paths') as $att) { - if (array_key_exists($att, $conf)) { - $c[$name]['connection'][$att] = $conf[$att]; - } - } - } - - // Alias the default connection if not defined - if (!isset($c['default'])) { - $c['default'] = $c[$connectionName]; - } - - $container->setParameter('propel.configuration', $c); - } - - public function getConfiguration(array $config, ContainerBuilder $container) - { - return new Configuration($container->getParameter('kernel.debug')); } /** diff --git a/PropelBundle.php b/PropelBundle.php index 100762c..a6d46f5 100644 --- a/PropelBundle.php +++ b/PropelBundle.php @@ -47,14 +47,13 @@ class PropelBundle extends Bundle protected function configureConnections() { - $connections_config = $this->container->getParameter('propel.configuration'); - $default_datasource = $this->container->getParameter('propel.dbal.default_connection'); + $config = $this->container->getParameter('propel.configuration'); $serviceContainer = Propel::getServiceContainer(); - $serviceContainer->setDefaultDatasource($default_datasource); + $serviceContainer->setDefaultDatasource($config['runtime']['defaultConnection']); - foreach ($connections_config as $name => $config) { - if (isset($config['slaves'])) { + foreach ($config['database']['connections'] as $name => $config) { + if (!empty($config['slaves'])) { $manager = new ConnectionManagerMasterSlave(); // configure the master (write) connection @@ -63,7 +62,7 @@ class PropelBundle extends Bundle $manager->setReadConfiguration($config['slaves']); } else { $manager = new ConnectionManagerSingle(); - $manager->setConfiguration($config['connection']); + $manager->setConfiguration($config); } $serviceContainer->setAdapterClass($name, $config['adapter']); diff --git a/Request/ParamConverter/PropelParamConverter.php b/Request/ParamConverter/PropelParamConverter.php index 69e542a..76891c8 100644 --- a/Request/ParamConverter/PropelParamConverter.php +++ b/Request/ParamConverter/PropelParamConverter.php @@ -4,7 +4,6 @@ namespace Propel\PropelBundle\Request\ParamConverter; use Propel\PropelBundle\Util\PropelInflector; use Propel\Runtime\ActiveQuery\Criteria; -use Sensio\Bundle\FrameworkExtraBundle\Configuration\ConfigurationInterface; use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter; use Sensio\Bundle\FrameworkExtraBundle\Request\ParamConverter\ParamConverterInterface; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; @@ -66,7 +65,7 @@ class PropelParamConverter implements ParamConverterInterface /** * @param Request $request - * @param ConfigurationInterface $configuration + * @param ParamConverter $configuration * * @return bool * @@ -74,7 +73,7 @@ class PropelParamConverter implements ParamConverterInterface * @throws NotFoundHttpException * @throws \Exception */ - public function apply(Request $request, ConfigurationInterface $configuration) + public function apply(Request $request, ParamConverter $configuration) { $class = $configuration->getClass(); $classQuery = $class . 'Query'; @@ -148,11 +147,11 @@ class PropelParamConverter implements ParamConverterInterface } /** - * @param ConfigurationInterface $configuration + * @param ParamConverter $configuration * * @return bool */ - public function supports(ConfigurationInterface $configuration) + public function supports(ParamConverter $configuration) { if (null === ($classname = $configuration->getClass())) { return false; diff --git a/Resources/views/Panel/configuration.html.twig b/Resources/views/Panel/configuration.html.twig index 596d244..8dc8a7e 100644 --- a/Resources/views/Panel/configuration.html.twig +++ b/Resources/views/Panel/configuration.html.twig @@ -10,7 +10,7 @@ Default connection - {{ default_connection }} + {{ configuration.runtime.defaultConnection }} Logging {{ logging ? 'enabled' : 'disabled' }} @@ -28,7 +28,7 @@ - {% for name, config in configuration %} + {% for name, config in configuration.database.connections %} {{ name }} @@ -38,17 +38,17 @@ DSN - {{ config.connection.dsn }} + {{ config.dsn }} Class - {{ config.connection.classname }} + {{ config.classname }} Options @@ -58,7 +58,7 @@ Attributes