1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Psi\Component\Description\Enhancer\Doctrine; |
4
|
|
|
|
5
|
|
|
use Doctrine\ODM\PHPCR\Mapping\ClassMetadataFactory; |
6
|
|
|
use Doctrine\Common\Util\ClassUtils; |
7
|
|
|
use Psi\Component\Description\EnhancerInterface; |
8
|
|
|
use Psi\Component\Description\DescriptionInterface; |
9
|
|
|
use Psi\Component\Description\Subject; |
10
|
|
|
use Psi\Component\Description\Descriptor\BooleanDescriptor; |
11
|
|
|
use Psi\Component\Description\Descriptor\ArrayDescriptor; |
12
|
|
|
use Psi\Component\Description\Descriptor\ClassDescriptor; |
13
|
|
|
use Doctrine\ODM\PHPCR\DocumentManagerInterface; |
14
|
|
|
use Psi\Component\Description\Descriptor\StringDescriptor; |
15
|
|
|
|
16
|
|
|
class PhpcrOdmEnhancer implements EnhancerInterface |
17
|
|
|
{ |
18
|
|
|
private $documentManager; |
19
|
|
|
|
20
|
|
|
public function __construct(DocumentManagerInterface $documentManager) |
21
|
|
|
{ |
22
|
|
|
$this->documentManager = $documentManager; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* {@inheritdoc} |
27
|
|
|
*/ |
28
|
|
|
public function enhanceFromClass(DescriptionInterface $description, \ReflectionClass $class) |
29
|
|
|
{ |
30
|
|
|
$metadataFactory = $this->documentManager->getMetadataFactory(); |
31
|
|
|
$metadata = $metadataFactory->getMetadataFor(ClassUtils::getRealClass($class->getName())); |
|
|
|
|
32
|
|
|
$childClasses = $metadata->getChildClasses(); |
33
|
|
|
|
34
|
|
|
// cast child classes to strings because of https://github.com/doctrine/phpcr-odm/issues/723 |
35
|
|
|
$childClasses = array_map(function ($value) { |
36
|
|
|
return (string) $value; |
37
|
|
|
}, $childClasses); |
38
|
|
|
|
39
|
|
|
$childTypes = []; |
40
|
|
|
// explode the allowed types into concrete classes |
41
|
|
|
foreach ($metadataFactory->getAllMetadata() as $childMetadata) { |
42
|
|
|
foreach ($childClasses as $childClass) { |
43
|
|
|
$childRefl = $childMetadata->getReflectionClass(); |
44
|
|
|
if ($childClass == $childRefl->getName() || $childRefl->isSubclassOf($childClass)) { |
45
|
|
|
$childTypes[] = $childRefl->getName(); |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$description->set('hierarchy.allow_children', new BooleanDescriptor(!$metadata->isLeaf())); |
51
|
|
|
$description->set('hierarchy.children_types', new ArrayDescriptor($childTypes)); |
52
|
|
|
$description->set('std.class', new ClassDescriptor(new \ReflectionClass(ClassUtils::getRealClass($class->getName())))); |
|
|
|
|
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* {@inheritdoc} |
57
|
|
|
*/ |
58
|
|
|
public function enhanceFromObject(DescriptionInterface $description, Subject $subject) |
59
|
|
|
{ |
60
|
|
|
$node = $this->documentManager->getNodeForDocument($subject->getObject()); |
61
|
|
|
$description->set('std.identifier', new StringDescriptor($node->getIdentifier())); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* {@inheritdoc} |
66
|
|
|
*/ |
67
|
|
|
public function supports(Subject $subject) |
68
|
|
|
{ |
69
|
|
|
return $this->documentManager->getMetadataFactory()->hasMetadataFor(ClassUtils::getRealClass($subject->getClass()->getName())); |
|
|
|
|
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|