Skip to content

Commit

Permalink
[DoctrineBundle] Making mapping information more flexible to allow di…
Browse files Browse the repository at this point in the history
…fferent metadata mapping files for each bundle
  • Loading branch information
jwage authored and fabpot committed Mar 1, 2010
1 parent 1a45bb6 commit 85ed6df
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/Symfony/Framework/DoctrineBundle/Bundle.php
Expand Up @@ -28,7 +28,7 @@ class Bundle extends BaseBundle
{
public function buildContainer(ContainerInterface $container)
{
Loader::registerExtension(new DoctrineExtension());
Loader::registerExtension(new DoctrineExtension($container));

$metadataDirs = array();
$entityDirs = array();
Expand Down
Expand Up @@ -7,6 +7,7 @@
use Symfony\Components\DependencyInjection\BuilderConfiguration;
use Symfony\Components\DependencyInjection\Definition;
use Symfony\Components\DependencyInjection\Reference;
use Symfony\Components\DependencyInjection\Container;

/*
* This file is part of the symfony framework.
Expand All @@ -33,6 +34,11 @@ class DoctrineExtension extends LoaderExtension

protected $alias;

public function __construct(Container $container)
{
$this->container = $container;
}

public function setAlias($alias)
{
$this->alias = $alias;
Expand Down Expand Up @@ -188,6 +194,29 @@ public function ormLoad($config)
$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->container->getParameter('kernel.bundle_dirs');
foreach ($this->container->getParameter('kernel.bundles') as $className)
{
$tmp = dirname(str_replace('\\', '/', $className));
$namespace = str_replace('/', '\\', dirname($tmp));
$class = basename($tmp);

if (isset($bundleDirs[$namespace]) && is_dir($dir = $bundleDirs[$namespace].'/'.$class.'/Resources/config/doctrine/metadata'))
{
$type = $this->detectMappingType($dir);
$mappingDriverDef->addMethodCall('addDriver', array(
new Reference(sprintf('doctrine.orm.metadata_driver.%s', $type)),
$namespace.'\\'.$class.'\\Entities'
)
);
}
}

$configuration->setDefinition('doctrine.orm.metadata_driver', $mappingDriverDef);

$methods = array(
'setMetadataCacheImpl' => new Reference('doctrine.orm.metadata_cache'),
'setQueryCacheImpl' => new Reference('doctrine.orm.query_cache'),
Expand Down Expand Up @@ -227,14 +256,6 @@ public function ormLoad($config)
}
}

$configuration->setAlias(
'doctrine.orm.metadata_driver',
sprintf(
'doctrine.orm.metadata_driver.%s',
$configuration->getParameter('doctrine.orm.metadata_driver')
)
);

$configuration->setAlias(
'doctrine.orm.cache',
sprintf(
Expand All @@ -246,6 +267,26 @@ public function ormLoad($config)
return $configuration;
}

/**
* Detect the type of Doctrine 2 mapping files located in a given directory.
* Simply finds the first file in a directory and returns the extension. If no
* mapping files are found then the annotation type is returned.
*
* @param string $dir
* @return string $type
*/
protected function detectMappingType($dir)
{
$files = glob($dir.'/*.*');
if (!$files)
{
return 'annotation';
}
$info = pathinfo($files[0]);

return $info['extension'];
}

/**
* Returns the base path for the XSD files.
*
Expand Down
Expand Up @@ -5,7 +5,6 @@
xsi:schemaLocation="http://www.symfony-project.org/schema/dic/services http://www.symfony-project.org/schema/dic/services/services-1.0.xsd">

<parameters>
<parameter key="doctrine.orm.metadata_driver">xml</parameter>
<parameter key="doctrine.orm.cache_driver">array</parameter>
<parameter key="doctrine.orm.cache.memcache.host">localhost</parameter>
<parameter key="doctrine.orm.cache.memcache.port">11211</parameter>
Expand Down

0 comments on commit 85ed6df

Please sign in to comment.