Skip to content
This repository has been archived by the owner on Feb 5, 2024. It is now read-only.

Commit

Permalink
Updates to use latest Doctrine/common faclities
Browse files Browse the repository at this point in the history
  • Loading branch information
Saem Ghani committed Sep 26, 2012
1 parent a0749ee commit ef1b9d6
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 242 deletions.
127 changes: 9 additions & 118 deletions Mapping/Driver/XmlDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,134 +20,25 @@

namespace Doctrine\Bundle\CouchDBBundle\Mapping\Driver;

use Doctrine\ODM\CouchDB\Mapping\MappingException;
use Doctrine\ODM\CouchDB\Mapping\Driver\XmlDriver as BaseXmlDriver;
use Doctrine\Common\Persistence\Mapping\Driver\SymfonyFileLocator;

/**
* XmlDriver that additionally looks for mapping information in a global file.
*
* @author Fabien Potencier <fabien@symfony.com>
* @author Benjamin Eberlei <kontakt@beberlei.de>
*/
class XmlDriver extends BaseXmlDriver
{
protected $prefixes = array();
protected $globalBasename;
protected $classCache;
protected $fileExtension = '.couchdb.xml';
const DEFAULT_FILE_EXTENSION = '.couchdb.xml';

public function setGlobalBasename($file)
/**
* {@inheritdoc}
*/
public function __construct($prefixes, $fileExtension = self::DEFAULT_FILE_EXTENSION)
{
$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());
}

public function getAllClassNames()
{
if (null === $this->classCache) {
$this->initialize();
}

$classes = array();

if ($this->paths) {
foreach ((array) $this->paths as $path) {
if (!is_dir($path)) {
throw MappingException::fileMappingDriversRequireConfiguredDirectoryPath($path);
}

$iterator = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator($path),
\RecursiveIteratorIterator::LEAVES_ONLY
);

foreach ($iterator as $file) {
$fileName = $file->getBasename($this->fileExtension);

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

// NOTE: All files found here means classes are not transient!
if (isset($this->prefixes[$path])) {
$classes[] = $this->prefixes[$path].'\\'.str_replace('.', '\\', $fileName);
} else {
$classes[] = str_replace('.', '\\', $fileName);
}
}
}
}

return array_merge($classes, array_keys($this->classCache));
}

public function getElement($className)
{
if (null === $this->classCache) {
$this->initialize();
}

if (!isset($this->classCache[$className])) {
$this->classCache[$className] = parent::getElement($className);
}

return $this->classCache[$className];
}

protected function initialize()
{
$this->classCache = array();
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)
{
$defaultFileName = str_replace('\\', '.', $className) . $this->fileExtension;
foreach ($this->paths as $path => $prefix) {
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;
}

$filename = $path.'/'.strtr(substr($className, strlen($prefix)+1), '\\', '.').$this->fileExtension;
if (file_exists($filename)) {
return $filename;
}
}

throw MappingException::mappingFileNotFound($className, $defaultFileName);
$locator = new SymfonyFileLocator((array) $prefixes, $fileExtension);
parent::__construct($locator, $fileExtension);
}
}
129 changes: 10 additions & 119 deletions Mapping/Driver/YamlDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,134 +20,25 @@

namespace Doctrine\Bundle\CouchDBBundle\Mapping\Driver;

use Doctrine\ODM\CouchDB\Mapping\MappingException;
use Doctrine\ODM\CouchDB\Mapping\Driver\YamlDriver as BaseYamlDriver;
use Doctrine\ODM\CouchDB\Mapping\Driver\XmlDriver as BaseYamlDriver;
use Doctrine\Common\Persistence\Mapping\Driver\SymfonyFileLocator;

/**
* YamlDriver that additionally looks for mapping information in a global file.
*
* @author Fabien Potencier <fabien@symfony.com>
* @author Benjamin Eberlei <kontakt@beberlei.de>
*/
class YamlDriver extends BaseYamlDriver
{
protected $prefixes = array();
protected $globalBasename;
protected $classCache;
protected $fileExtension = '.couchdb.yml';
const DEFAULT_FILE_EXTENSION = '.couchdb.yml';

public function setGlobalBasename($file)
/**
* {@inheritdoc}
*/
public function __construct($prefixes, $fileExtension = self::DEFAULT_FILE_EXTENSION)
{
$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());
}

public function getAllClassNames()
{
if (null === $this->classCache) {
$this->initialize();
}

$classes = array();

if ($this->paths) {
foreach ((array) $this->paths as $path) {
if (!is_dir($path)) {
throw MappingException::fileMappingDriversRequireConfiguredDirectoryPath($path);
}

$iterator = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator($path),
\RecursiveIteratorIterator::LEAVES_ONLY
);

foreach ($iterator as $file) {
$fileName = $file->getBasename($this->fileExtension);

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

// NOTE: All files found here means classes are not transient!
if (isset($this->prefixes[$path])) {
$classes[] = $this->prefixes[$path].'\\'.str_replace('.', '\\', $fileName);
} else {
$classes[] = str_replace('.', '\\', $fileName);
}
}
}
}

return array_merge($classes, array_keys($this->classCache));
}

public function getElement($className)
{
if (null === $this->classCache) {
$this->initialize();
}

if (!isset($this->classCache[$className])) {
$this->classCache[$className] = parent::getElement($className);
}

return $this->classCache[$className];
}

protected function initialize()
{
$this->classCache = array();
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)
{
$defaultFileName = str_replace('\\', '.', $className) . $this->fileExtension;
foreach ($this->paths as $path => $prefix) {
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;
}

$filename = $path.'/'.strtr(substr($className, strlen($prefix)+1), '\\', '.').$this->fileExtension;
if (file_exists($filename)) {
return $filename;
}
}

throw MappingException::mappingFileNotFound($className, $defaultFileName);
$locator = new SymfonyFileLocator((array) $prefixes, $fileExtension);
parent::__construct($locator, $fileExtension);
}
}
2 changes: 1 addition & 1 deletion Resources/config/odm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<!-- form field factory guesser -->
<parameter key="form.type_guesser.doctrine_couchdb.class">Doctrine\Bundle\CouchDBBundle\Form\CouchDBTypeGuesser</parameter>

<parameter key="doctrine_couchdb.odm.metadata.driver_chain.class">Doctrine\ODM\CouchDB\Mapping\Driver\DriverChain</parameter>
<parameter key="doctrine_couchdb.odm.metadata.driver_chain.class">Doctrine\Common\Persistence\Mapping\Driver\MappingDriverChain</parameter>
<parameter key="doctrine_couchdb.odm.metadata.annotation.class">Doctrine\ODM\CouchDB\Mapping\Driver\AnnotationDriver</parameter>
<parameter key="doctrine_couchdb.odm.metadata.annotation_reader.class">Doctrine\Common\Annotations\AnnotationReader</parameter>
<parameter key="doctrine_couchdb.odm.metadata.xml.class">Doctrine\Bundle\CouchDBBundle\Mapping\Driver\XmlDriver</parameter>
Expand Down
7 changes: 3 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,13 @@
],
"require": {
"php": ">=5.3.2",
"doctrine/couchdb": "*",
"symfony/symfony": "2.1.*"
"doctrine/couchdb-odm": "*",
"symfony/doctrine-bridge": ">=2.0.0,<2.2.0-dev",
"symfony/framework-bundle": ">=2.0.0,<2.2.0-dev"
},
"require-dev": {
"doctrine/couchdb-odm": "*"
},
"suggest": {
"doctrine/couchdb-odm": "*"
},
"autoload": {
"psr-0": { "Doctrine\\Bundle\\CouchDBBundle": "" }
Expand Down

0 comments on commit ef1b9d6

Please sign in to comment.