-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[PropertyInfo] Import the component #15858
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
<?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\PropertyListRetrieverInterface; | ||
use Symfony\Component\PropertyInfo\PropertyTypeInfoInterface; | ||
use Symfony\Component\PropertyInfo\Type; | ||
|
||
/** | ||
* Extracts data using Doctrine ORM and ODM metadata. | ||
* | ||
* @author Kévin Dunglas <dunglas@gmail.com> | ||
*/ | ||
class DoctrineExtractor implements PropertyListRetrieverInterface, PropertyTypeInfoInterface | ||
{ | ||
/** | ||
* @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); | ||
if ($metadata instanceof ClassMetadataInfo) { | ||
$nullable = $metadata->isNullable($property); | ||
} else { | ||
$nullable = false; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unnecessary else. You can either move $nullable = $metadata instanceof ClassMetadataInfo && $metadata->isNullable($property); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 for the second proposal. |
||
} | ||
|
||
switch ($typeOfField) { | ||
case 'date': | ||
// No break | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would drop the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is PSR-2 Standard 😉
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
It's an empty case here so it's not needed. |
||
case 'datetime': | ||
// No break | ||
case 'datetimetz': | ||
// No break | ||
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; | ||
} | ||
} | ||
} |
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), | ||
); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?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; | ||
} |
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; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You've added a
conflict
block below for versions < 1.0.7. Shouldn't we change the version constraint instead to^1.0.7
and drop theconflict
block?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://github.com/symfony/symfony/pull/15858/files#r40200338
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, I've just found the other composer.json with the discussion about this topic. I'll join there. Nevermind. ;-)