Skip to content

Commit

Permalink
[DoctrineBundle] made XML/YAML mapping drivers more BC with their Doc…
Browse files Browse the repository at this point in the history
…trine counterparts

The ultimate goal is to move back these mapping dirvers to the Doctrine project.
  • Loading branch information
fabpot committed Jun 7, 2011
1 parent 116e004 commit 41242dc
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 25 deletions.
Expand Up @@ -193,9 +193,9 @@ protected function registerMappingDrivers($objectManager, ContainerBuilder $cont
$mappingDriverDef = $container->getDefinition($mappingService);
$args = $mappingDriverDef->getArguments();
if ($driverType == 'annotation') {
$args[1] = array_merge($driverPaths, $args[1]);
$args[1] = array_merge(array_values($driverPaths), $args[1]);
} else {
$args[0] = array_merge($driverPaths, $args[0]);
$args[0] = array_merge(array_values($driverPaths), $args[0]);
}
$mappingDriverDef->setArguments($args);
} else if ($driverType == 'annotation') {
Expand All @@ -205,10 +205,14 @@ protected function registerMappingDrivers($objectManager, ContainerBuilder $cont
));
} else {
$mappingDriverDef = new Definition('%'.$this->getObjectManagerElementName('metadata.'.$driverType.'.class%'), array(
$driverPaths
array_values($driverPaths)
));
}
$mappingDriverDef->setPublic(false);
if (false !== strpos($mappingDriverDef->getClass(), 'yml') || false !== strpos($mappingDriverDef->getClass(), 'xml')) {
$mappingDriverDef->addMethodCall('setNamespacePrefixes', array(array_flip($driverPaths)));
$mappingDriverDef->addMethodCall('setGlobalBasename', array('mapping'));
}

$container->setDefinition($mappingService, $mappingDriverDef);

Expand Down
54 changes: 46 additions & 8 deletions src/Symfony/Bundle/DoctrineBundle/Mapping/Driver/XmlDriver.php
Expand Up @@ -21,10 +21,31 @@
*/
class XmlDriver extends BaseXmlDriver
{
protected $_globalFile = 'mapping';
protected $_prefixes = array();
protected $_globalBasename;
protected $_classCache;
protected $_fileExtension = '.orm.xml';

public function setGlobalBasename($file)
{
$this->_globalBasename = $file;
}

public function getGlobalBasename()
{
return $this->_globalBasename;
}

public function setNamespacePrefixes($prefixes)
{
$this->_prefixes = $prefixes;
}

public function getNamespacePrefixes()
{
return $this->_prefixes;
}

public function isTransient($className)
{
return !in_array($className, $this->getAllClassNames());
Expand All @@ -39,7 +60,7 @@ public function getAllClassNames()
$classes = array();

if ($this->_paths) {
foreach ((array) $this->_paths as $prefix => $path) {
foreach ((array) $this->_paths as $path) {
if (!is_dir($path)) {
throw MappingException::fileMappingDriversRequireConfiguredDirectoryPath($path);
}
Expand All @@ -52,12 +73,16 @@ public function getAllClassNames()
foreach ($iterator as $file) {
$fileName = $file->getBasename($this->_fileExtension);

if ($fileName == $file->getBasename() || $fileName == $this->_globalFile) {
if ($fileName == $file->getBasename() || $fileName == $this->_globalBasename) {
continue;
}

// NOTE: All files found here means classes are not transient!
$classes[] = $prefix.'\\'.str_replace('.', '\\', $fileName);
if (isset($this->_prefixes[$path])) {
$classes[] = $this->_prefixes[$path].'\\'.str_replace('.', '\\', $fileName);
} else {
$classes[] = str_replace('.', '\\', $fileName);
}
}
}
}
Expand All @@ -81,16 +106,29 @@ public function getElement($className)
protected function initialize()
{
$this->_classCache = array();
foreach ($this->_paths as $path) {
if (file_exists($file = $path.'/'.$this->_globalFile.$this->_fileExtension)) {
$this->_classCache = array_merge($this->_classCache, $this->_loadMappingFile($file));
if (null !== $this->_globalBasename) {
foreach ($this->_paths as $path) {
if (file_exists($file = $path.'/'.$this->_globalBasename.$this->_fileExtension)) {
$this->_classCache = array_merge($this->_classCache, $this->_loadMappingFile($file));
}
}
}
}

protected function _findMappingFile($className)
{
foreach ($this->_paths as $prefix => $path) {
$defaultFileName = str_replace('\\', '.', $className) . $this->_fileExtension;
foreach ($this->_paths as $path) {
if (!isset($this->_prefixes[$path])) {
if (file_exists($path . DIRECTORY_SEPARATOR . $defaultFileName)) {
return $path . DIRECTORY_SEPARATOR . $defaultFileName;
}

continue;
}

$prefix = $this->_prefixes[$path];

if (0 !== strpos($className, $prefix.'\\')) {
continue;
}
Expand Down
54 changes: 46 additions & 8 deletions src/Symfony/Bundle/DoctrineBundle/Mapping/Driver/YamlDriver.php
Expand Up @@ -21,10 +21,31 @@
*/
class YamlDriver extends BaseYamlDriver
{
protected $_globalFile = 'mapping';
protected $_prefixes = array();
protected $_globalBasename;
protected $_classCache;
protected $_fileExtension = '.orm.yml';

public function setGlobalBasename($file)
{
$this->_globalBasename = $file;
}

public function getGlobalBasename()
{
return $this->_globalBasename;
}

public function setNamespacePrefixes($prefixes)
{
$this->_prefixes = $prefixes;
}

public function getNamespacePrefixes()
{
return $this->_prefixes;
}

public function isTransient($className)
{
return !in_array($className, $this->getAllClassNames());
Expand All @@ -39,7 +60,7 @@ public function getAllClassNames()
$classes = array();

if ($this->_paths) {
foreach ((array) $this->_paths as $prefix => $path) {
foreach ((array) $this->_paths as $path) {
if (!is_dir($path)) {
throw MappingException::fileMappingDriversRequireConfiguredDirectoryPath($path);
}
Expand All @@ -52,12 +73,16 @@ public function getAllClassNames()
foreach ($iterator as $file) {
$fileName = $file->getBasename($this->_fileExtension);

if ($fileName == $file->getBasename() || $fileName == $this->_globalFile) {
if ($fileName == $file->getBasename() || $fileName == $this->_globalBasename) {
continue;
}

// NOTE: All files found here means classes are not transient!
$classes[] = $prefix.'\\'.str_replace('.', '\\', $fileName);
if (isset($this->_prefixes[$path])) {
$classes[] = $this->_prefixes[$path].'\\'.str_replace('.', '\\', $fileName);
} else {
$classes[] = str_replace('.', '\\', $fileName);
}
}
}
}
Expand All @@ -81,16 +106,29 @@ public function getElement($className)
protected function initialize()
{
$this->_classCache = array();
foreach ($this->_paths as $path) {
if (file_exists($file = $path.'/'.$this->_globalFile.$this->_fileExtension)) {
$this->_classCache = array_merge($this->_classCache, $this->_loadMappingFile($file));
if (null !== $this->_globalBasename) {
foreach ($this->_paths as $path) {
if (file_exists($file = $path.'/'.$this->_globalBasename.$this->_fileExtension)) {
$this->_classCache = array_merge($this->_classCache, $this->_loadMappingFile($file));
}
}
}
}

protected function _findMappingFile($className)
{
foreach ($this->_paths as $prefix => $path) {
$defaultFileName = str_replace('\\', '.', $className) . $this->_fileExtension;
foreach ($this->_paths as $path) {
if (!isset($this->_prefixes[$path])) {
if (file_exists($path . DIRECTORY_SEPARATOR . $defaultFileName)) {
return $path . DIRECTORY_SEPARATOR . $defaultFileName;
}

continue;
}

$prefix = $this->_prefixes[$path];

if (0 !== strpos($className, $prefix.'\\')) {
continue;
}
Expand Down
Expand Up @@ -556,12 +556,12 @@ public function testSingleEntityManagerMultipleMappingBundleDefinitions()

$ymlDef = $container->getDefinition('doctrine.orm.default_yml_metadata_driver');
$this->assertDICConstructorArguments($ymlDef, array(
array('Fixtures\Bundles\YamlBundle\Entity' => __DIR__ .DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR.'Bundles'.DIRECTORY_SEPARATOR.'YamlBundle'.DIRECTORY_SEPARATOR.'Resources'.DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'doctrine')
array(__DIR__ .DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR.'Bundles'.DIRECTORY_SEPARATOR.'YamlBundle'.DIRECTORY_SEPARATOR.'Resources'.DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'doctrine')
));

$xmlDef = $container->getDefinition('doctrine.orm.default_xml_metadata_driver');
$this->assertDICConstructorArguments($xmlDef, array(
array('Fixtures\Bundles\XmlBundle' => __DIR__ .DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR.'Bundles'.DIRECTORY_SEPARATOR.'XmlBundle'.DIRECTORY_SEPARATOR.'Resources'.DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'doctrine')
array(__DIR__ .DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR.'Bundles'.DIRECTORY_SEPARATOR.'XmlBundle'.DIRECTORY_SEPARATOR.'Resources'.DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'doctrine')
));
}

Expand Down Expand Up @@ -605,12 +605,12 @@ public function testMultipleEntityManagersMappingBundleDefinitions()

$ymlDef = $container->getDefinition('doctrine.orm.em2_yml_metadata_driver');
$this->assertDICConstructorArguments($ymlDef, array(
array('Fixtures\Bundles\YamlBundle\Entity' => __DIR__ .DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR.'Bundles'.DIRECTORY_SEPARATOR.'YamlBundle'.DIRECTORY_SEPARATOR.'Resources'.DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'doctrine')
array(__DIR__ .DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR.'Bundles'.DIRECTORY_SEPARATOR.'YamlBundle'.DIRECTORY_SEPARATOR.'Resources'.DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'doctrine')
));

$xmlDef = $container->getDefinition('doctrine.orm.em2_xml_metadata_driver');
$this->assertDICConstructorArguments($xmlDef, array(
array('Fixtures\Bundles\XmlBundle' => __DIR__ .DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR.'Bundles'.DIRECTORY_SEPARATOR.'XmlBundle'.DIRECTORY_SEPARATOR.'Resources'.DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'doctrine')
array(__DIR__ .DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR.'Bundles'.DIRECTORY_SEPARATOR.'XmlBundle'.DIRECTORY_SEPARATOR.'Resources'.DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'doctrine')
));
}

Expand Down
Expand Up @@ -22,6 +22,9 @@ protected function getFileExtension()

protected function getDriver(array $paths = array())
{
return new XmlDriver($paths);
$driver = new XmlDriver(array_values($paths));
$driver->setNamespacePrefixes(array_flip($paths));

return $driver;
}
}
Expand Up @@ -22,6 +22,9 @@ protected function getFileExtension()

protected function getDriver(array $paths = array())
{
return new YamlDriver($paths);
$driver = new YamlDriver(array_values($paths));
$driver->setNamespacePrefixes(array_flip($paths));

return $driver;
}
}

0 comments on commit 41242dc

Please sign in to comment.