Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
29 changed files
with
2,306 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
149 changes: 149 additions & 0 deletions
149
src/Symfony/Bridge/Doctrine/PropertyInfo/DoctrineExtractor.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bridge\Doctrine\PropertyInfo; | ||
|
||
use Doctrine\Common\Persistence\Mapping\ClassMetadataFactory; | ||
use Doctrine\Common\Persistence\Mapping\MappingException; | ||
use Doctrine\ORM\Mapping\ClassMetadataInfo; | ||
use Symfony\Component\PropertyInfo\PropertyListExtractorInterface; | ||
use Symfony\Component\PropertyInfo\PropertyTypeExtractorInterface; | ||
use Symfony\Component\PropertyInfo\Type; | ||
|
||
/** | ||
* Extracts data using Doctrine ORM and ODM metadata. | ||
* | ||
* @author Kévin Dunglas <dunglas@gmail.com> | ||
*/ | ||
class DoctrineExtractor implements PropertyListExtractorInterface, PropertyTypeExtractorInterface | ||
{ | ||
/** | ||
* @var ClassMetadataFactory | ||
*/ | ||
private $classMetadataFactory; | ||
|
||
public function __construct(ClassMetadataFactory $classMetadataFactory) | ||
{ | ||
$this->classMetadataFactory = $classMetadataFactory; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getProperties($class, array $context = array()) | ||
{ | ||
try { | ||
$metadata = $this->classMetadataFactory->getMetadataFor($class); | ||
} catch (MappingException $exception) { | ||
return; | ||
} | ||
|
||
return array_merge($metadata->getFieldNames(), $metadata->getAssociationNames()); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getTypes($class, $property, array $context = array()) | ||
{ | ||
try { | ||
$metadata = $this->classMetadataFactory->getMetadataFor($class); | ||
} catch (MappingException $exception) { | ||
return; | ||
} | ||
|
||
if ($metadata->hasAssociation($property)) { | ||
$class = $metadata->getAssociationTargetClass($property); | ||
|
||
if ($metadata->isSingleValuedAssociation($property)) { | ||
if ($metadata instanceof ClassMetadataInfo) { | ||
$nullable = isset($metadata->discriminatorColumn['nullable']) ? $metadata->discriminatorColumn['nullable'] : false; | ||
} else { | ||
$nullable = false; | ||
} | ||
|
||
return array(new Type(Type::BUILTIN_TYPE_OBJECT, $nullable, $class)); | ||
} | ||
|
||
return array(new Type( | ||
Type::BUILTIN_TYPE_OBJECT, | ||
false, | ||
'Doctrine\Common\Collections\Collection', | ||
true, | ||
new Type(Type::BUILTIN_TYPE_INT), | ||
new Type(Type::BUILTIN_TYPE_OBJECT, false, $class) | ||
)); | ||
} | ||
|
||
if ($metadata->hasField($property)) { | ||
$typeOfField = $metadata->getTypeOfField($property); | ||
$nullable = $metadata instanceof ClassMetadataInfo && $metadata->isNullable($property); | ||
|
||
switch ($typeOfField) { | ||
case 'date': | ||
case 'datetime': | ||
case 'datetimetz': | ||
case 'time': | ||
return array(new Type(Type::BUILTIN_TYPE_OBJECT, $nullable, 'DateTime')); | ||
|
||
case 'array': | ||
return array(new Type(Type::BUILTIN_TYPE_ARRAY, $nullable, null, true)); | ||
|
||
case 'simple_array': | ||
return array(new Type(Type::BUILTIN_TYPE_ARRAY, $nullable, null, true, new Type(Type::BUILTIN_TYPE_INT), new Type(Type::BUILTIN_TYPE_STRING))); | ||
|
||
case 'json_array': | ||
return array(new Type(Type::BUILTIN_TYPE_ARRAY, $nullable, null, true)); | ||
|
||
default: | ||
return array(new Type($this->getPhpType($typeOfField), $nullable)); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Gets the corresponding built-in PHP type. | ||
* | ||
* @param string $doctrineType | ||
* | ||
* @return string | ||
*/ | ||
private function getPhpType($doctrineType) | ||
{ | ||
switch ($doctrineType) { | ||
case 'smallint': | ||
// No break | ||
case 'bigint': | ||
// No break | ||
case 'integer': | ||
return Type::BUILTIN_TYPE_INT; | ||
|
||
case 'decimal': | ||
return Type::BUILTIN_TYPE_FLOAT; | ||
|
||
case 'text': | ||
// No break | ||
case 'guid': | ||
return Type::BUILTIN_TYPE_STRING; | ||
|
||
case 'boolean': | ||
return Type::BUILTIN_TYPE_BOOL; | ||
|
||
case 'blob': | ||
// No break | ||
case 'binary': | ||
return Type::BUILTIN_TYPE_RESOURCE; | ||
|
||
default: | ||
return $doctrineType; | ||
} | ||
} | ||
} |
84 changes: 84 additions & 0 deletions
84
src/Symfony/Bridge/Doctrine/Tests/PropertyInfo/DoctrineExtractorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bridge\Doctrine\PropertyInfo\Tests; | ||
|
||
use Doctrine\ORM\EntityManager; | ||
use Doctrine\ORM\Tools\Setup; | ||
use Symfony\Bridge\Doctrine\PropertyInfo\DoctrineExtractor; | ||
use Symfony\Component\PropertyInfo\Type; | ||
|
||
/** | ||
* @author Kévin Dunglas <dunglas@gmail.com> | ||
*/ | ||
class DoctrineExtractorTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* @var DoctrineExtractor | ||
*/ | ||
private $extractor; | ||
|
||
public function setUp() | ||
{ | ||
$config = Setup::createAnnotationMetadataConfiguration(array(__DIR__.DIRECTORY_SEPARATOR.'Fixtures'), true); | ||
$entityManager = EntityManager::create(array('driver' => 'pdo_sqlite'), $config); | ||
|
||
$this->extractor = new DoctrineExtractor($entityManager->getMetadataFactory()); | ||
} | ||
|
||
public function testGetProperties() | ||
{ | ||
$this->assertEquals( | ||
array( | ||
'id', | ||
'guid', | ||
'time', | ||
'json', | ||
'simpleArray', | ||
'bool', | ||
'binary', | ||
'foo', | ||
'bar', | ||
), | ||
$this->extractor->getProperties('Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineDummy') | ||
); | ||
} | ||
|
||
/** | ||
* @dataProvider typesProvider | ||
*/ | ||
public function testExtract($property, array $type = null) | ||
{ | ||
$this->assertEquals($type, $this->extractor->getTypes('Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineDummy', $property, array())); | ||
} | ||
|
||
public function typesProvider() | ||
{ | ||
return array( | ||
array('id', array(new Type(Type::BUILTIN_TYPE_INT))), | ||
array('guid', array(new Type(Type::BUILTIN_TYPE_STRING))), | ||
array('bool', array(new Type(Type::BUILTIN_TYPE_BOOL))), | ||
array('binary', array(new Type(Type::BUILTIN_TYPE_RESOURCE))), | ||
array('json', array(new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true))), | ||
array('foo', array(new Type(Type::BUILTIN_TYPE_OBJECT, false, 'Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineRelation'))), | ||
array('bar', array(new Type( | ||
Type::BUILTIN_TYPE_OBJECT, | ||
false, | ||
'Doctrine\Common\Collections\Collection', | ||
true, | ||
new Type(Type::BUILTIN_TYPE_INT), | ||
new Type(Type::BUILTIN_TYPE_OBJECT, false, 'Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineRelation') | ||
))), | ||
array('simpleArray', array(new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true, new Type(Type::BUILTIN_TYPE_INT), new Type(Type::BUILTIN_TYPE_STRING)))), | ||
array('notMapped', null), | ||
); | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
src/Symfony/Bridge/Doctrine/Tests/PropertyInfo/Fixtures/DoctrineDummy.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures; | ||
|
||
use Doctrine\ORM\Mapping\Column; | ||
use Doctrine\ORM\Mapping\Entity; | ||
use Doctrine\ORM\Mapping\Id; | ||
use Doctrine\ORM\Mapping\ManyToMany; | ||
use Doctrine\ORM\Mapping\ManyToOne; | ||
|
||
/** | ||
* @Entity | ||
* | ||
* @author Kévin Dunglas <dunglas@gmail.com> | ||
*/ | ||
class DoctrineDummy | ||
{ | ||
/** | ||
* @Id | ||
* @Column(type="smallint") | ||
*/ | ||
public $id; | ||
|
||
/** | ||
* @ManyToOne(targetEntity="DoctrineRelation") | ||
*/ | ||
public $foo; | ||
|
||
/** | ||
* @ManyToMany(targetEntity="DoctrineRelation") | ||
*/ | ||
public $bar; | ||
|
||
/** | ||
* @Column(type="guid") | ||
*/ | ||
protected $guid; | ||
|
||
/** | ||
* @Column(type="time") | ||
*/ | ||
private $time; | ||
|
||
/** | ||
* @Column(type="json_array") | ||
*/ | ||
private $json; | ||
|
||
/** | ||
* @Column(type="simple_array") | ||
*/ | ||
private $simpleArray; | ||
|
||
/** | ||
* @Column(type="boolean") | ||
*/ | ||
private $bool; | ||
|
||
/** | ||
* @Column(type="binary") | ||
*/ | ||
private $binary; | ||
|
||
public $notMapped; | ||
} |
29 changes: 29 additions & 0 deletions
29
src/Symfony/Bridge/Doctrine/Tests/PropertyInfo/Fixtures/DoctrineRelation.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures; | ||
|
||
use Doctrine\ORM\Mapping\Column; | ||
use Doctrine\ORM\Mapping\Id; | ||
|
||
/** | ||
* @Entity | ||
* | ||
* @author Kévin Dunglas <dunglas@gmail.com> | ||
*/ | ||
class DoctrineRelation | ||
{ | ||
/** | ||
* @Id | ||
* @Column(type="smallint") | ||
*/ | ||
public $id; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.