-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
Copy pathExtensionAttributesFactory.php
118 lines (106 loc) · 4.53 KB
/
ExtensionAttributesFactory.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Framework\Api;
/**
* Factory class for instantiation of extension attributes objects.
*/
class ExtensionAttributesFactory
{
const EXTENSIBLE_INTERFACE_NAME = \Magento\Framework\Api\ExtensibleDataInterface::class;
/**
* Object Manager instance
*
* @var \Magento\Framework\ObjectManagerInterface
*/
protected $objectManager;
/**
* Map is used for performance optimization.
*
* @var array
*/
private $classInterfaceMap = [];
/**
* Factory constructor
*
* @param \Magento\Framework\ObjectManagerInterface $objectManager
*/
public function __construct(\Magento\Framework\ObjectManagerInterface $objectManager)
{
$this->objectManager = $objectManager;
}
/**
* Create extension attributes object, custom for each extensible class.
*
* @param string $extensibleClassName
* @param array $data
* @return \Magento\Framework\Api\ExtensionAttributesInterface
*/
public function create($extensibleClassName, $data = [])
{
$interfaceReflection = new \ReflectionClass($this->getExtensibleInterfaceName($extensibleClassName));
$methodReflection = $interfaceReflection->getMethod('getExtensionAttributes');
if ($methodReflection->getDeclaringClass()->getName() === self::EXTENSIBLE_INTERFACE_NAME) {
throw new \LogicException(
"Method 'getExtensionAttributes' must be overridden in the interfaces "
. "which extend '" . self::EXTENSIBLE_INTERFACE_NAME . "'. "
. "Concrete return type should be specified."
);
}
$interfaceName = '\\' . $interfaceReflection->getName();
$extensionClassName = substr($interfaceName, 0, -strlen('Interface')) . 'Extension';
$extensionInterfaceName = $extensionClassName . 'Interface';
/** Ensure that proper return type of getExtensionAttributes() method is specified */
$methodDocBlock = $methodReflection->getDocComment();
$pattern = "/@return\s+" . str_replace('\\', '\\\\', $extensionInterfaceName) . "/";
if (!preg_match($pattern, $methodDocBlock)) {
throw new \LogicException(
"Method 'getExtensionAttributes' must be overridden in the interfaces "
. "which extend '" . self::EXTENSIBLE_INTERFACE_NAME . "'. "
. "Concrete return type must be specified. Please fix :" . $interfaceName
);
}
$extensionFactoryName = $extensionClassName . 'Factory';
$extensionFactory = $this->objectManager->create($extensionFactoryName);
return $extensionFactory->create($data);
}
/**
* Identify concrete extensible interface name based on the class name.
*
* @param string $extensibleClassName
* @return string
*/
public function getExtensibleInterfaceName($extensibleClassName)
{
$exceptionMessage = "Class '{$extensibleClassName}' must implement an interface, "
. "which extends from '" . self::EXTENSIBLE_INTERFACE_NAME . "'";
$notExtensibleClassFlag = '';
if (isset($this->classInterfaceMap[$extensibleClassName])) {
if ($notExtensibleClassFlag === $this->classInterfaceMap[$extensibleClassName]) {
throw new \LogicException($exceptionMessage);
} else {
return $this->classInterfaceMap[$extensibleClassName];
}
}
$modelReflection = new \ReflectionClass($extensibleClassName);
if ($modelReflection->isInterface()
&& $modelReflection->isSubclassOf(self::EXTENSIBLE_INTERFACE_NAME)
&& $modelReflection->hasMethod('getExtensionAttributes')
) {
$this->classInterfaceMap[$extensibleClassName] = $extensibleClassName;
return $this->classInterfaceMap[$extensibleClassName];
}
foreach ($modelReflection->getInterfaces() as $interfaceReflection) {
if ($interfaceReflection->isSubclassOf(self::EXTENSIBLE_INTERFACE_NAME)
&& $interfaceReflection->hasMethod('getExtensionAttributes')
) {
$this->classInterfaceMap[$extensibleClassName] = $interfaceReflection->getName();
return $this->classInterfaceMap[$extensibleClassName];
}
}
$this->classInterfaceMap[$extensibleClassName] = $notExtensibleClassFlag;
throw new \LogicException($exceptionMessage);
}
}