|
11 | 11 |
|
12 | 12 | namespace Dunglas\JsonLdApiBundle;
|
13 | 13 |
|
| 14 | +use Dunglas\JsonLdApiBundle\Mapping\ClassMetadataFactory; |
| 15 | +use Symfony\Component\Routing\RouterInterface; |
| 16 | + |
14 | 17 | /**
|
15 | 18 | * ApiDocumentation.
|
16 | 19 | *
|
17 | 20 | * @author Kévin Dunglas <[email protected]>
|
18 | 21 | */
|
19 | 22 | class ApiDocumentation
|
20 | 23 | {
|
| 24 | + /** |
| 25 | + * @var string |
| 26 | + */ |
| 27 | + const HYDRA_NS = 'http://www.w3.org/ns/hydra/core#'; |
| 28 | + |
| 29 | + /** |
| 30 | + * @var Resources |
| 31 | + */ |
| 32 | + private $resources; |
| 33 | + /** |
| 34 | + * @var RouterInterface |
| 35 | + */ |
| 36 | + private $router; |
| 37 | + /** |
| 38 | + * @var ClassMetadataFactory |
| 39 | + */ |
| 40 | + private $classMetadataFactory; |
| 41 | + /** |
| 42 | + * @var string |
| 43 | + */ |
21 | 44 | private $title;
|
| 45 | + /** |
| 46 | + * @var string |
| 47 | + */ |
22 | 48 | private $description;
|
23 |
| - private $entrypoint; |
24 |
| - private $supportedClass; |
25 |
| - private $statusCodes; |
| 49 | + |
| 50 | + /** |
| 51 | + * @param Resources $resources |
| 52 | + * @param RouterInterface $router |
| 53 | + * @param ClassMetadataFactory $classMetadataFactory |
| 54 | + * @param string $title |
| 55 | + * @param string $description |
| 56 | + */ |
| 57 | + public function __construct( |
| 58 | + Resources $resources, |
| 59 | + RouterInterface $router, |
| 60 | + ClassMetadataFactory $classMetadataFactory, |
| 61 | + $title, |
| 62 | + $description |
| 63 | + ) { |
| 64 | + $this->resources = $resources; |
| 65 | + $this->router = $router; |
| 66 | + $this->classMetadataFactory = $classMetadataFactory; |
| 67 | + $this->title = $title; |
| 68 | + $this->description = $description; |
| 69 | + } |
| 70 | + |
| 71 | + public function getDocumentation() |
| 72 | + { |
| 73 | + $doc = [ |
| 74 | + '@context' => self::HYDRA_NS, |
| 75 | + '@id' => $this->router->generate('json_ld_api_vocab'), |
| 76 | + 'hydra:title' => $this->title, |
| 77 | + 'hydra:description' => $this->description, |
| 78 | + 'hydra:entrypoint' => $this->router->generate('json_ld_api_entrypoint'), |
| 79 | + 'hydra:supportedClass' => [], |
| 80 | + ]; |
| 81 | + |
| 82 | + foreach ($this->resources as $resource) { |
| 83 | + $metadata = $this->classMetadataFactory->getMetadataFor($resource->getEntityClass()); |
| 84 | + $shortName = $resource->getShortName(); |
| 85 | + |
| 86 | + $supportedClass = [ |
| 87 | + '@id' => $shortName, |
| 88 | + '@type' => 'hydra:Class', |
| 89 | + 'hydra:title' => $resource->getTitle() ?: $resource->getShortName(), |
| 90 | + ]; |
| 91 | + |
| 92 | + $description = $resource->getDescription() ?: $metadata->getDescription(); |
| 93 | + if ($description) { |
| 94 | + $supportedClass['hydra:description'] = $description; |
| 95 | + } |
| 96 | + |
| 97 | + $attributes = $metadata->getAttributes( |
| 98 | + $resource->getNormalizationGroups(), |
| 99 | + $resource->getDenormalizationGroups(), |
| 100 | + $resource->getValidationGroups() |
| 101 | + ); |
| 102 | + |
| 103 | + $supportedClass['hydra:supportedProperty'] = []; |
| 104 | + foreach ($attributes as $name => $details) { |
| 105 | + $supportedProperty = [ |
| 106 | + '@type' => 'hydra:SupportedProperty', |
| 107 | + 'hydra:property' => sprintf('%s/%s', $shortName, $name), |
| 108 | + 'hydra:title' => $name, |
| 109 | + 'hydra:required' => $details['required'], |
| 110 | + 'hydra:readable' => $details['readable'], |
| 111 | + 'hydra:writable' => $details['writable'], |
| 112 | + ]; |
| 113 | + |
| 114 | + if ($details['description']) { |
| 115 | + $supportedProperty['hydra:description'] = $details['description']; |
| 116 | + } |
| 117 | + |
| 118 | + $supportedClass['hydra:supportedProperty'][] = $supportedProperty; |
| 119 | + } |
| 120 | + |
| 121 | + $supportedClass['hydra:supportedOperation'] = []; |
| 122 | + foreach ($resource->getCollectionOperations() as $operation) { |
| 123 | + $supportedOperation = []; |
| 124 | + |
| 125 | + if('POST' === $operation['hydra:method']) { |
| 126 | + $supportedOperation['@type'] = 'hydra:CreateResourceOperation'; |
| 127 | + $supportedOperation['hydra:title'] = sprintf('Creates a %s resource.', $shortName); |
| 128 | + $supportedOperation['hydra:expects'] = $shortName; |
| 129 | + $supportedOperation['hydra:returns'] = $shortName; |
| 130 | + } else { |
| 131 | + $supportedOperation['@type'] = 'hydra:Operation'; |
| 132 | + if ('GET' === $operation['hydra:method']) { |
| 133 | + $supportedOperation['hydra:title'] = sprintf('Retrieves the collection of %s resources.', $shortName); |
| 134 | + $supportedOperation['hydra:returns'] = 'hydra:PagedCollection'; |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + $this->populateSupportedOperation($supportedOperation, $operation); |
| 139 | + |
| 140 | + $supportedClass['hydra:supportedOperation'][] = $supportedOperation; |
| 141 | + } |
| 142 | + |
| 143 | + foreach ($resource->getItemOperations() as $operation) { |
| 144 | + $supportedOperation = []; |
| 145 | + |
| 146 | + if ('PUT' === $operation['hydra:method']) { |
| 147 | + $supportedOperation['@type'] = 'hydra:ReplaceResourceOperation'; |
| 148 | + $supportedOperation['hydra:title'] = sprintf('Replaces the %s resource.', $shortName); |
| 149 | + $supportedOperation['hydra:expects'] = $shortName; |
| 150 | + $supportedOperation['hydra:returns'] = $shortName; |
| 151 | + } elseif ('DELETE' === $operation['hydra:method']) { |
| 152 | + $supportedOperation['@type'] = 'hydra:Operation'; |
| 153 | + $supportedOperation['hydra:title'] = sprintf('Deletes the %s resource.', $shortName); |
| 154 | + $supportedOperation['hydra:expects'] = $shortName; |
| 155 | + } else { |
| 156 | + if ('GET' === $operation['hydra:method']) { |
| 157 | + $supportedOperation['@type'] = 'hydra:Operation'; |
| 158 | + $supportedOperation['hydra:title'] = sprintf('Retrieves %s resource.', $shortName); |
| 159 | + $supportedOperation['hydra:returns'] = $shortName; |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + $this->populateSupportedOperation($supportedOperation, $operation); |
| 164 | + |
| 165 | + $supportedClass['hydra:supportedOperation'][] = $supportedOperation; |
| 166 | + } |
| 167 | + |
| 168 | + $doc['hydra:supportedClass'][] = $supportedClass; |
| 169 | + } |
| 170 | + |
| 171 | + return $doc; |
| 172 | + } |
| 173 | + |
| 174 | + /** |
| 175 | + * Copy data from $operation to $supportedOperation except when the key start with "!". |
| 176 | + * |
| 177 | + * @param array $supportedOperation |
| 178 | + * @param array $operation |
| 179 | + */ |
| 180 | + private function populateSupportedOperation(array &$supportedOperation, array $operation) |
| 181 | + { |
| 182 | + foreach ($operation as $key => $value) { |
| 183 | + if (isset($key[0]) && '!' !== $key[0]) { |
| 184 | + $supportedOperation[$key] = $value; |
| 185 | + } |
| 186 | + } |
| 187 | + } |
26 | 188 | }
|
0 commit comments