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 (\is_object($value) && !$value instanceof \Traversable) { |
44
|
|
|
$typeToFind = $type = $this->getObjectClass($value); |
45
|
|
|
if (null === $resourceClass) { |
46
|
|
|
$resourceClass = $typeToFind; |
47
|
|
|
} |
48
|
|
|
} elseif (null === $resourceClass) { |
49
|
|
|
throw new InvalidArgumentException(sprintf('No resource class found.')); |
50
|
|
|
} else { |
51
|
|
|
$typeToFind = $type = $resourceClass; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
if (($strict && $resourceClass !== $type) || false === $isResourceClass = $this->isResourceClass($typeToFind)) { |
55
|
|
|
if (is_subclass_of($type, $resourceClass) && $this->isResourceClass($resourceClass)) { |
56
|
|
|
return $type; |
57
|
|
|
} |
58
|
|
|
if ($isResourceClass ?? $this->isResourceClass($typeToFind)) { |
59
|
|
|
return $typeToFind; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
throw new InvalidArgumentException(sprintf('No resource class found for object of type "%s".', $typeToFind)); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return $resourceClass; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* {@inheritdoc} |
70
|
|
|
*/ |
71
|
|
|
public function isResourceClass(string $type): bool |
72
|
|
|
{ |
73
|
|
|
if (isset($this->localIsResourceClassCache[$type])) { |
74
|
|
|
return $this->localIsResourceClassCache[$type]; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
foreach ($this->resourceNameCollectionFactory->create() as $resourceClass) { |
78
|
|
|
if ($type === $resourceClass) { |
79
|
|
|
return $this->localIsResourceClassCache[$type] = true; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return $this->localIsResourceClassCache[$type] = false; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|