|
15 | 15 | use Symfony\Component\Routing\RouterInterface;
|
16 | 16 |
|
17 | 17 | /**
|
18 |
| - * ApiDocumentation. |
| 18 | + * Hydra's ApiDocumentation builder. |
19 | 19 | *
|
20 | 20 | * @author Kévin Dunglas <[email protected]>
|
21 | 21 | */
|
22 |
| -class ApiDocumentation |
| 22 | +class ApiDocumentationBuilder |
23 | 23 | {
|
24 | 24 | /**
|
25 | 25 | * @var string
|
@@ -68,28 +68,74 @@ public function __construct(
|
68 | 68 | $this->description = $description;
|
69 | 69 | }
|
70 | 70 |
|
71 |
| - public function getDocumentation() |
| 71 | + public function getApiDocumentation() |
72 | 72 | {
|
73 | 73 | $doc = [
|
74 |
| - '@context' => self::HYDRA_NS, |
| 74 | + '@context' => $this->router->generate('json_ld_api_context', ['shortName' => 'ApiDocumentation']), |
75 | 75 | '@id' => $this->router->generate('json_ld_api_vocab'),
|
76 | 76 | 'hydra:title' => $this->title,
|
77 | 77 | 'hydra:description' => $this->description,
|
78 | 78 | 'hydra:entrypoint' => $this->router->generate('json_ld_api_entrypoint'),
|
79 | 79 | 'hydra:supportedClass' => [],
|
80 | 80 | ];
|
81 | 81 |
|
| 82 | + // Entrypoint |
| 83 | + $supportedProperties = []; |
| 84 | + foreach ($this->resources as $resource) { |
| 85 | + $shortName = $resource->getShortName(); |
| 86 | + |
| 87 | + $supportedProperty = [ |
| 88 | + '@type' => 'hydra:SupportedProperty', |
| 89 | + 'hydra:property' => $resource->getBeautifiedName(), |
| 90 | + 'hydra:title' => sprintf('The collection of %s resources', $shortName), |
| 91 | + 'hydra:readable' => true, |
| 92 | + 'hydra:writable' => false, |
| 93 | + 'hydra:supportedOperation' => [], |
| 94 | + ]; |
| 95 | + |
| 96 | + foreach ($resource->getCollectionOperations() as $operation) { |
| 97 | + $supportedOperation = []; |
| 98 | + |
| 99 | + if('POST' === $operation['hydra:method']) { |
| 100 | + $supportedOperation['@type'] = 'hydra:CreateResourceOperation'; |
| 101 | + $supportedOperation['hydra:title'] = sprintf('Creates a %s resource.', $shortName); |
| 102 | + $supportedOperation['hydra:expects'] = $shortName; |
| 103 | + $supportedOperation['hydra:returns'] = $shortName; |
| 104 | + } else { |
| 105 | + $supportedOperation['@type'] = 'hydra:Operation'; |
| 106 | + if ('GET' === $operation['hydra:method']) { |
| 107 | + $supportedOperation['hydra:title'] = sprintf('Retrieves the collection of %s resources.', $shortName); |
| 108 | + $supportedOperation['hydra:returns'] = 'hydra:PagedCollection'; |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + $this->populateSupportedOperation($supportedOperation, $operation); |
| 113 | + |
| 114 | + $supportedProperty['hydra:supportedOperation'][] = $supportedOperation; |
| 115 | + } |
| 116 | + |
| 117 | + $supportedProperties[] = $supportedProperty; |
| 118 | + } |
| 119 | + |
| 120 | + $doc['hydra:supportedClass'][] = [ |
| 121 | + '@id' => 'Entrypoint', |
| 122 | + '@type' => 'hydra:class', |
| 123 | + 'hydra:title' => 'The API entrypoint', |
| 124 | + 'hydra:supportedProperty' => $supportedProperties, |
| 125 | + ]; |
| 126 | + |
| 127 | + // Resources |
82 | 128 | foreach ($this->resources as $resource) {
|
83 | 129 | $metadata = $this->classMetadataFactory->getMetadataFor($resource->getEntityClass());
|
84 | 130 | $shortName = $resource->getShortName();
|
85 | 131 |
|
86 | 132 | $supportedClass = [
|
87 | 133 | '@id' => $shortName,
|
88 | 134 | '@type' => 'hydra:Class',
|
89 |
| - 'hydra:title' => $resource->getTitle() ?: $resource->getShortName(), |
| 135 | + 'hydra:title' => $resource->getShortName(), |
90 | 136 | ];
|
91 | 137 |
|
92 |
| - $description = $resource->getDescription() ?: $metadata->getDescription(); |
| 138 | + $description = $metadata->getDescription(); |
93 | 139 | if ($description) {
|
94 | 140 | $supportedClass['hydra:description'] = $description;
|
95 | 141 | }
|
@@ -119,27 +165,6 @@ public function getDocumentation()
|
119 | 165 | }
|
120 | 166 |
|
121 | 167 | $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 | 168 | foreach ($resource->getItemOperations() as $operation) {
|
144 | 169 | $supportedOperation = [];
|
145 | 170 |
|
@@ -172,7 +197,7 @@ public function getDocumentation()
|
172 | 197 | }
|
173 | 198 |
|
174 | 199 | /**
|
175 |
| - * Copy data from $operation to $supportedOperation except when the key start with "!". |
| 200 | + * Copies data from $operation to $supportedOperation except when the key start with "!". |
176 | 201 | *
|
177 | 202 | * @param array $supportedOperation
|
178 | 203 | * @param array $operation
|
|
0 commit comments