Пример #1
0
 /**
  * Loads the DBAL configuration.
  *
  * @param array $config A configuration array
  *
  * @return BuilderConfiguration A BuilderConfiguration instance
  */
 public function dbalLoad($config, BuilderConfiguration $configuration)
 {
     if (!$configuration->hasDefinition('propel')) {
         $loader = new XmlFileLoader(__DIR__ . '/../Resources/config');
         $configuration->merge($loader->load($this->resources['propel']));
     }
     $defaultConnection = array('driver' => 'mysql', 'user' => 'root', 'password' => null, 'dsn' => null, 'classname' => 'DebugPDO', 'options' => array(), 'attributes' => array(), 'settings' => array('charset' => array('value' => 'UTF8')));
     $defaultConnectionName = isset($config['default_connection']) ? $config['default_connection'] : $configuration->getParameter('propel.dbal.default_connection');
     $configuration->setParameter('propel.dbal.default_connection', $defaultConnectionName);
     $connections = array();
     if (isset($config['connections'])) {
         foreach ($config['connections'] as $name => $connection) {
             $connections[isset($connection['id']) ? $connection['id'] : $name] = $connection;
         }
     } else {
         $connections = array($defaultConnectionName => $config);
     }
     $arguments = $configuration->getDefinition('propel.configuration')->getArguments();
     if (count($arguments)) {
         $c = $arguments[0];
     } else {
         $c = array('log' => array('level' => 7), 'datasources' => array());
     }
     foreach ($connections as $name => $connection) {
         if (isset($c['datasources'][$name])) {
         } else {
             $connection = array_replace($defaultConnection, $connection);
             $c['datasources'][$name] = array('connection' => array());
         }
         if (isset($connection['driver'])) {
             $c['datasources'][$name]['adapter'] = $connection['driver'];
         }
         foreach (array('dsn', 'user', 'password', 'classname', 'options', 'attributes', 'settings') as $att) {
             if (isset($connection[$att])) {
                 $c['datasources'][$name]['connection'][$att] = $connection[$att];
             }
         }
     }
     $configuration->getDefinition('propel.configuration')->setArguments(array($c));
     return $configuration;
 }
Пример #2
0
 /**
  * Loads the templating configuration.
  *
  * @param array $config A configuration array
  *
  * @return BuilderConfiguration A BuilderConfiguration instance
  */
 public function templatingLoad($config)
 {
     $configuration = new BuilderConfiguration();
     $loader = new XmlFileLoader(__DIR__ . '/../Resources/config');
     $configuration->merge($loader->load($this->resources['templating']));
     $configuration->setParameter('templating.output_escaper', array_key_exists('escaping', $config) ? $config['escaping'] : false);
     $configuration->setParameter('templating.assets.version', array_key_exists('assets_version', $config) ? $config['assets_version'] : null);
     // path for the filesystem loader
     if (isset($config['path'])) {
         $configuration->setParameter('templating.loader.filesystem.path', $config['path']);
     }
     // loaders
     if (isset($config['loader'])) {
         $loaders = array();
         $ids = is_array($config['loader']) ? $config['loader'] : array($config['loader']);
         foreach ($ids as $id) {
             $loaders[] = new Reference($id);
         }
     } else {
         $loaders = array(new Reference('templating.loader.filesystem'));
     }
     if (1 === count($loaders)) {
         $configuration->setAlias('templating.loader', (string) $loaders[0]);
     } else {
         $configuration->getDefinition('templating.loader.chain')->addArgument($loaders);
         $configuration->setAlias('templating.loader', 'templating.loader.chain');
     }
     // cache?
     if (isset($config['cache'])) {
         // wrap the loader with some cache
         $configuration->setDefinition('templating.loader.wrapped', $configuration->findDefinition('templating.loader'));
         $configuration->setDefinition('templating.loader', $configuration->getDefinition('templating.loader.cache'));
         $configuration->setParameter('templating.loader.cache.path', $config['cache']);
     }
     return $configuration;
 }
Пример #3
0
 /**
  * Loads the DBAL configuration.
  *
  * @param array $config A configuration array
  *
  * @return BuilderConfiguration A BuilderConfiguration instance
  */
 public function dbalLoad($config)
 {
     $configuration = new BuilderConfiguration();
     $loader = new XmlFileLoader(__DIR__ . '/../Resources/config');
     $configuration->merge($loader->load($this->resources['propel']));
     $defaultConnection = array('driver' => 'mysql', 'user' => 'root', 'password' => null, 'dsn' => null, 'classname' => 'DebugPDO', 'options' => array(), 'attributes' => array(), 'settings' => array('charset' => array('value' => 'UTF8')));
     $config['default_connection'] = isset($config['default_connection']) ? $config['default_connection'] : 'default';
     $connections = array();
     if (isset($config['connections'])) {
         foreach ($config['connections'] as $name => $connection) {
             $connections[isset($connection['id']) ? $connection['id'] : $name] = $connection;
         }
     } else {
         $connections = array($config['default_connection'] => $config);
     }
     $c = array('log' => array('level' => 7), 'datasources' => array());
     foreach ($connections as $name => $connection) {
         $connection = array_replace($defaultConnection, $connection);
         $c['datasources'][$name] = array('adapter' => $connection['driver'], 'connection' => array('dsn' => $connection['dsn'], 'user' => $connection['user'], 'password' => $connection['password'], 'classname' => $connection['classname'], 'options' => $connection['options'], 'attributes' => $connection['attributes'], 'settings' => $connection['settings']));
     }
     $configuration->getDefinition('propel.configuration')->setArguments(array($c));
     return $configuration;
 }
$t->is($configuration->getAliases(), array('bar' => 'foo', 'barbar' => 'foofoo', 'foo' => 'bar'), '->addAliases() adds some aliases');

// ->setDefinitions() ->addDefinitions() ->getDefinitions() ->setDefinition() ->getDefinition() ->hasDefinition()
$t->diag('->setDefinitions() ->addDefinitions() ->getDefinitions() ->setDefinition() ->getDefinition() ->hasDefinition()');
$configuration = new BuilderConfiguration();
$definitions = array(
  'foo' => new Definition('FooClass'),
  'bar' => new Definition('BarClass'),
);
$configuration->setDefinitions($definitions);
$t->is($configuration->getDefinitions(), $definitions, '->setDefinitions() sets the service definitions');
$t->ok($configuration->hasDefinition('foo'), '->hasDefinition() returns true if a service definition exists');
$t->ok(!$configuration->hasDefinition('foobar'), '->hasDefinition() returns false if a service definition does not exist');

$configuration->setDefinition('foobar', $foo = new Definition('FooBarClass'));
$t->is($configuration->getDefinition('foobar'), $foo, '->getDefinition() returns a service definition if defined');
$t->ok($configuration->setDefinition('foobar', $foo = new Definition('FooBarClass')) === $foo, '->setDefinition() implements a fuild interface by returning the service reference');

$configuration->addDefinitions($defs = array('foobar' => new Definition('FooBarClass')));
$t->is($configuration->getDefinitions(), array_merge($definitions, $defs), '->addDefinitions() adds the service definitions');

try
{
  $configuration->getDefinition('baz');
  $t->fail('->getDefinition() throws an InvalidArgumentException if the service definition does not exist');
}
catch (InvalidArgumentException $e)
{
  $t->pass('->getDefinition() throws an InvalidArgumentException if the service definition does not exist');
}
Пример #5
0
 /**
  * Loads the Doctrine ORM configuration.
  *
  * @param array $config A configuration array
  *
  * @return BuilderConfiguration A BuilderConfiguration instance
  */
 public function ormLoad($config)
 {
     $configuration = new BuilderConfiguration();
     $loader = new XmlFileLoader(__DIR__ . '/../Resources/config');
     $configuration->merge($loader->load($this->resources['orm']));
     $config['default_entity_manager'] = isset($config['default_entity_manager']) ? $config['default_entity_manager'] : 'default';
     foreach (array('metadata_driver', 'cache_driver') as $key) {
         if (isset($config[$key])) {
             $configuration->setParameter('doctrine.orm.' . $key, $config[$key]);
         }
     }
     $config['entity_managers'] = isset($config['entity_managers']) ? $config['entity_managers'] : array($config['default_entity_manager'] => array());
     foreach ($config['entity_managers'] as $name => $connection) {
         $ormConfigDef = new Definition('Doctrine\\ORM\\Configuration');
         $configuration->setDefinition(sprintf('doctrine.orm.%s_configuration', $name), $ormConfigDef);
         $drivers = array('metadata', 'query', 'result');
         foreach ($drivers as $driver) {
             $definition = $configuration->getDefinition(sprintf('doctrine.orm.cache.%s', $configuration->getParameter('doctrine.orm.cache_driver')));
             $clone = clone $definition;
             $clone->addMethodCall('setNamespace', array(sprintf('doctrine_%s_', $driver)));
             $configuration->setDefinition(sprintf('doctrine.orm.%s_cache', $driver), $clone);
         }
         // configure metadata driver for each bundle based on the type of mapping files found
         $mappingDriverDef = new Definition('Doctrine\\ORM\\Mapping\\Driver\\DriverChain');
         $bundleEntityMappings = array();
         $bundleDirs = $this->bundleDirs;
         $aliasMap = array();
         foreach (array_reverse($this->bundles) as $className) {
             $tmp = dirname(str_replace('\\', '/', $className));
             $namespace = str_replace('/', '\\', dirname($tmp));
             $class = basename($tmp);
             if (!isset($bundleDirs[$namespace])) {
                 continue;
             }
             $type = false;
             if (is_dir($dir = $bundleDirs[$namespace] . '/' . $class . '/Resources/config/doctrine/metadata')) {
                 $type = $this->detectMappingType($dir);
             }
             if (is_dir($dir = $bundleDirs[$namespace] . '/' . $class . '/Entities')) {
                 if ($type === false) {
                     $type = 'annotation';
                 }
                 $aliasMap[$class] = $namespace . '\\' . $class . '\\Entities';
             }
             if (false !== $type) {
                 $mappingDriverDef->addMethodCall('addDriver', array(new Reference(sprintf('doctrine.orm.metadata_driver.%s', $type)), $namespace . '\\' . $class . '\\Entities'));
             }
         }
         $ormConfigDef->addMethodCall('setEntityNamespaces', array($aliasMap));
         $configuration->setDefinition('doctrine.orm.metadata_driver', $mappingDriverDef);
         $methods = array('setMetadataCacheImpl' => new Reference('doctrine.orm.metadata_cache'), 'setQueryCacheImpl' => new Reference('doctrine.orm.query_cache'), 'setResultCacheImpl' => new Reference('doctrine.orm.result_cache'), 'setMetadataDriverImpl' => new Reference('doctrine.orm.metadata_driver'), 'setProxyDir' => '%kernel.cache_dir%/doctrine/Proxies', 'setProxyNamespace' => 'Proxies', 'setAutoGenerateProxyClasses' => true);
         foreach ($methods as $method => $arg) {
             $ormConfigDef->addMethodCall($method, array($arg));
         }
         $ormEmArgs = array(new Reference(sprintf('doctrine.dbal.%s_connection', isset($connection['connection']) ? $connection['connection'] : $name)), new Reference(sprintf('doctrine.orm.%s_configuration', $name)));
         $ormEmDef = new Definition('Doctrine\\ORM\\EntityManager', $ormEmArgs);
         $ormEmDef->setConstructor('create');
         $configuration->setDefinition(sprintf('doctrine.orm.%s_entity_manager', $name), $ormEmDef);
         if ($name == $config['default_entity_manager']) {
             $configuration->setAlias('doctrine.orm.entity_manager', sprintf('doctrine.orm.%s_entity_manager', $name));
         }
     }
     $configuration->setAlias('doctrine.orm.cache', sprintf('doctrine.orm.cache.%s', $configuration->getParameter('doctrine.orm.cache_driver')));
     return $configuration;
 }
 public function testDefinitions()
 {
     $configuration = new BuilderConfiguration();
     $definitions = array('foo' => new Definition('FooClass'), 'bar' => new Definition('BarClass'));
     $configuration->setDefinitions($definitions);
     $this->assertEquals($definitions, $configuration->getDefinitions(), '->setDefinitions() sets the service definitions');
     $this->assertTrue($configuration->hasDefinition('foo'), '->hasDefinition() returns true if a service definition exists');
     $this->assertFalse($configuration->hasDefinition('foobar'), '->hasDefinition() returns false if a service definition does not exist');
     $configuration->setDefinition('foobar', $foo = new Definition('FooBarClass'));
     $this->assertEquals($foo, $configuration->getDefinition('foobar'), '->getDefinition() returns a service definition if defined');
     $this->assertTrue($configuration->setDefinition('foobar', new Definition('FooBarClass')) === $configuration, '->setDefinition() implements a fuild interface');
     $configuration->addDefinitions($defs = array('foobar' => new Definition('FooBarClass')));
     $this->assertEquals(array_merge($definitions, $defs), $configuration->getDefinitions(), '->addDefinitions() adds the service definitions');
     try {
         $configuration->getDefinition('baz');
         $this->fail('->getDefinition() throws an InvalidArgumentException if the service definition does not exist');
     } catch (\Exception $e) {
         $this->assertInstanceOf('\\InvalidArgumentException', $e, '->getDefinition() throws an InvalidArgumentException if the service definition does not exist');
         $this->assertEquals('The service definition "baz" does not exist.', $e->getMessage(), '->getDefinition() throws an InvalidArgumentException if the service definition does not exist');
     }
 }
 public function odmLoad($config)
 {
     $configuration = new BuilderConfiguration();
     $loader = new XmlFileLoader(__DIR__ . '/../Resources/config');
     $configuration->merge($loader->load($this->_resources['odm']));
     $configuration->setParameter('doctrine.odm.mongo.default_database', $this->appName);
     $configuration->setParameter('doctrine.odm.mongo.proxy_dir', sprintf('%s/Proxies', $this->tmpDir));
     if (isset($config['cache_driver'])) {
         $configuration->setAlias('doctrine.odm.cache', sprintf('doctrine.odm.cache.%s', $config['cache_driver']));
     }
     $config['metadata_driver'] = isset($config['metadata_driver']) ? $config['metadata_driver'] : 'auto';
     if ('auto' == $config['metadata_driver']) {
         $configuration->setAlias('doctrine.odm.mongo.metadata_driver', 'doctrine.odm.mongo.metadata_driver.chain');
         $driverChainDef = $configuration->getDefinition('doctrine.odm.mongo.metadata_driver.chain');
         foreach (array_reverse($this->bundles) as $className) {
             $tmp = dirname(str_replace('\\', '/', $className));
             $namespace = str_replace('/', '\\', dirname($tmp));
             $class = basename($tmp);
             if (!isset($this->bundleDirs[$namespace])) {
                 continue;
             }
             $type = false;
             if (is_dir($dir = $this->bundleDirs[$namespace] . '/' . $class . '/Resources/config/doctrine/metadata')) {
                 $type = $this->detectMappingType($dir);
             }
             if (is_dir($dir = $this->bundleDirs[$namespace] . '/' . $class . '/Documents')) {
                 $type = 'annotation';
             }
             if (false !== $type) {
                 $driverChainDef->addMethodCall('addDriver', array(new Reference(sprintf('doctrine.odm.mongo.metadata_driver.%s', $type)), $namespace . '\\' . $class));
             }
         }
     } else {
         $configuration->setAlias('doctrine.odm.mongo.metadata_driver', sprintf('doctrine.odm.mongo.metadata_driver.%s', $config['metadata_driver']));
     }
     $config['default_document_manager'] = isset($config['default_document_manager']) ? $config['default_document_manager'] : 'default';
     $config['document_managers'] = isset($config['document_managers']) ? $config['document_managers'] : array($config['default_document_manager'] => array());
     $defaultManagerParams = array('connection' => 'default');
     $defaultConnectionParams = array('server' => sprintf('localhost:27017/%s', $this->appName), 'options' => array('connect' => true));
     $documentManagers = array();
     $connections = array();
     foreach ($config['document_managers'] as $name => $params) {
         $managerParams = array_merge($defaultManagerParams, $params);
         $documentManagers[$name] = $managerParams;
         $connectionName = $managerParams['connection'];
         if (isset($config['connections']) && isset($config['connections'][$connectionName])) {
             $conn = array_merge($defaultConnectionParams, $config['connections'][$connectionName]);
         } else {
             $conn = $defaultConnectionParams;
         }
         $connections[$connectionName] = $conn;
     }
     $defaultConfDef = $configuration->getDefinition('doctrine.odm.mongo.configuration');
     $defaultConnection = $configuration->getDefinition('doctrine.odm.mongo.connection');
     $defaultDocManager = $configuration->getDefinition('doctrine.odm.document_manager');
     if (count($documentManagers) > 1) {
         foreach ($documentManagers as $name => $connection) {
             $server = $connections[$connection['connection']]['server'];
             $options = $connections[$connection['connection']]['options'];
             $pieces = explode('/', $server);
             $db = isset($pieces[1]) ? $pieces[1] : null;
             $odmConfiguration = clone $defaultConfDef;
             $odmConfiguration->setClass($configuration->getParameter('doctrine.odm.mongo.configuration_class'));
             if (isset($db)) {
                 $odmConfiguration->addMethodCall('setDefaultDB', array($db));
             }
             $odmConnection = clone $defaultConnection;
             $odmConnection->setClass($configuration->getParameter('doctrine.odm.mongo_class'));
             $odmConnection->setArguments(array($server, $options));
             $configuration->setDefinition(sprintf('doctrine.odm.mongo.%s_connection', $name), $odmConnection);
             $configuration->setDefinition(sprintf('doctrine.odm.mongo.%s_configuration', $name), $odmConfiguration);
             $documentManager = clone $defaultDocManager;
             $documentManager->setClass($configuration->getParameter('doctrine.odm.mongo.document_manager_class'));
             $odmManagerArgs = array(new Reference(sprintf('doctrine.odm.mongo.%s_connection', $name)), new Reference(sprintf('doctrine.odm.mongo.%s_configuration', $name)));
             $documentManager->setArguments($odmManagerArgs);
             $configuration->setDefinition(sprintf('doctrine.odm.%s_document_manager', $name), $documentManager);
             if ($name == $config['default_document_manager']) {
                 $configuration->setAlias('doctrine.odm.document_manager', sprintf('doctrine.odm.%s_document_manager', $name));
             }
         }
     } else {
         list($name, $connection) = each($documentManagers);
         $server = $connections[$connection['connection']]['server'];
         $options = $connections[$connection['connection']]['options'];
         $pieces = explode('/', $server);
         $db = isset($pieces[1]) ? $pieces[1] : null;
         $configuration->setParameter('doctrine.odm.mongo.default_database', $db);
         $configuration->setParameter('doctrine.odm.mongo.default_server', $server);
         $configuration->setParameter('doctrine.odm.mongo.default_connection_options', $options);
         $configuration->setAlias(sprintf('doctrine.odm.%s_document_manager', $name), 'doctrine.odm.document_manager');
     }
     return $configuration;
 }
 public function testDefinitions()
 {
     $configuration = new BuilderConfiguration();
     $definitions = array('foo' => new Definition('FooClass'), 'bar' => new Definition('BarClass'));
     $configuration->setDefinitions($definitions);
     $this->assertEquals($configuration->getDefinitions(), $definitions, '->setDefinitions() sets the service definitions');
     $this->assertTrue($configuration->hasDefinition('foo'), '->hasDefinition() returns true if a service definition exists');
     $this->assertTrue(!$configuration->hasDefinition('foobar'), '->hasDefinition() returns false if a service definition does not exist');
     $configuration->setDefinition('foobar', $foo = new Definition('FooBarClass'));
     $this->assertEquals($configuration->getDefinition('foobar'), $foo, '->getDefinition() returns a service definition if defined');
     $this->assertTrue($configuration->setDefinition('foobar', $foo = new Definition('FooBarClass')) === $foo, '->setDefinition() implements a fuild interface by returning the service reference');
     $configuration->addDefinitions($defs = array('foobar' => new Definition('FooBarClass')));
     $this->assertEquals($configuration->getDefinitions(), array_merge($definitions, $defs), '->addDefinitions() adds the service definitions');
     try {
         $configuration->getDefinition('baz');
         $this->fail('->getDefinition() throws an InvalidArgumentException if the service definition does not exist');
     } catch (\InvalidArgumentException $e) {
     }
 }