1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the API Platform project. |
5
|
|
|
* |
6
|
|
|
* (c) Kévin Dunglas <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace ApiPlatform\Core\Api; |
15
|
|
|
|
16
|
|
|
use ApiPlatform\Core\Exception\InvalidArgumentException; |
17
|
|
|
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceNameCollectionFactoryInterface; |
18
|
|
|
use ApiPlatform\Core\Util\ClassInfoTrait; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* {@inheritdoc} |
22
|
|
|
* |
23
|
|
|
* @author Kévin Dunglas <[email protected]> |
24
|
|
|
* @author Samuel ROZE <[email protected]> |
25
|
|
|
*/ |
26
|
|
|
final class ResourceClassResolver implements ResourceClassResolverInterface |
27
|
|
|
{ |
28
|
|
|
use ClassInfoTrait; |
29
|
|
|
|
30
|
|
|
private $resourceNameCollectionFactory; |
31
|
|
|
private $localIsResourceClassCache = []; |
32
|
|
|
|
33
|
|
|
public function __construct(ResourceNameCollectionFactoryInterface $resourceNameCollectionFactory) |
34
|
|
|
{ |
35
|
|
|
$this->resourceNameCollectionFactory = $resourceNameCollectionFactory; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* {@inheritdoc} |
40
|
|
|
*/ |
41
|
|
|
public function getResourceClass($value, string $resourceClass = null, bool $strict = false): string |
42
|
|
|
{ |
43
|
|
|
if ($strict && null === $resourceClass) { |
44
|
|
|
throw new InvalidArgumentException('Strict checking is only possible when resource class is specified.'); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$actualClass = \is_object($value) && !$value instanceof \Traversable ? $this->getObjectClass($value) : null; |
48
|
|
|
|
49
|
|
|
if (null === $actualClass && null === $resourceClass) { |
50
|
|
|
throw new InvalidArgumentException('Resource type could not be determined. Resource class must be specified.'); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
if (null !== $actualClass && !$this->isResourceClass($actualClass)) { |
54
|
|
|
throw new InvalidArgumentException(sprintf('No resource class found for object of type "%s".', $actualClass)); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
if (null !== $resourceClass && !$this->isResourceClass($resourceClass)) { |
58
|
|
|
throw new InvalidArgumentException(sprintf('Specified class "%s" is not a resource class.', $resourceClass)); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
if ($strict && null !== $actualClass && !is_a($actualClass, $resourceClass, true)) { |
62
|
|
|
throw new InvalidArgumentException(sprintf('Object of type "%s" does not match "%s" resource class.', $actualClass, $resourceClass)); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$targetClass = $actualClass ?? $resourceClass; |
66
|
|
|
$mostSpecificResourceClass = null; |
67
|
|
|
|
68
|
|
|
foreach ($this->resourceNameCollectionFactory->create() as $resourceClassName) { |
69
|
|
|
if (!is_a($targetClass, $resourceClassName, true)) { |
70
|
|
|
continue; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
if (null === $mostSpecificResourceClass || is_subclass_of($resourceClassName, $mostSpecificResourceClass)) { |
|
|
|
|
74
|
|
|
$mostSpecificResourceClass = $resourceClassName; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
if (null === $mostSpecificResourceClass) { |
|
|
|
|
79
|
|
|
throw new \LogicException('Unexpected execution flow.'); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $mostSpecificResourceClass; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* {@inheritdoc} |
87
|
|
|
*/ |
88
|
|
|
public function isResourceClass(string $type): bool |
89
|
|
|
{ |
90
|
|
|
if (isset($this->localIsResourceClassCache[$type])) { |
91
|
|
|
return $this->localIsResourceClassCache[$type]; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
foreach ($this->resourceNameCollectionFactory->create() as $resourceClass) { |
95
|
|
|
if (is_a($type, $resourceClass, true)) { |
96
|
|
|
return $this->localIsResourceClassCache[$type] = true; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return $this->localIsResourceClassCache[$type] = false; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|