Skip to content

Commit 574a743

Browse files
committed
Entrypoint support. Cleanup. Bug fixes.
1 parent 0e9e6a3 commit 574a743

File tree

10 files changed

+140
-164
lines changed

10 files changed

+140
-164
lines changed

ApiDocumentation.php renamed to ApiDocumentationBuilder.php

Lines changed: 53 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515
use Symfony\Component\Routing\RouterInterface;
1616

1717
/**
18-
* ApiDocumentation.
18+
* Hydra's ApiDocumentation builder.
1919
*
2020
* @author Kévin Dunglas <[email protected]>
2121
*/
22-
class ApiDocumentation
22+
class ApiDocumentationBuilder
2323
{
2424
/**
2525
* @var string
@@ -68,28 +68,74 @@ public function __construct(
6868
$this->description = $description;
6969
}
7070

71-
public function getDocumentation()
71+
public function getApiDocumentation()
7272
{
7373
$doc = [
74-
'@context' => self::HYDRA_NS,
74+
'@context' => $this->router->generate('json_ld_api_context', ['shortName' => 'ApiDocumentation']),
7575
'@id' => $this->router->generate('json_ld_api_vocab'),
7676
'hydra:title' => $this->title,
7777
'hydra:description' => $this->description,
7878
'hydra:entrypoint' => $this->router->generate('json_ld_api_entrypoint'),
7979
'hydra:supportedClass' => [],
8080
];
8181

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
82128
foreach ($this->resources as $resource) {
83129
$metadata = $this->classMetadataFactory->getMetadataFor($resource->getEntityClass());
84130
$shortName = $resource->getShortName();
85131

86132
$supportedClass = [
87133
'@id' => $shortName,
88134
'@type' => 'hydra:Class',
89-
'hydra:title' => $resource->getTitle() ?: $resource->getShortName(),
135+
'hydra:title' => $resource->getShortName(),
90136
];
91137

92-
$description = $resource->getDescription() ?: $metadata->getDescription();
138+
$description = $metadata->getDescription();
93139
if ($description) {
94140
$supportedClass['hydra:description'] = $description;
95141
}
@@ -119,27 +165,6 @@ public function getDocumentation()
119165
}
120166

121167
$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-
143168
foreach ($resource->getItemOperations() as $operation) {
144169
$supportedOperation = [];
145170

@@ -172,7 +197,7 @@ public function getDocumentation()
172197
}
173198

174199
/**
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 "!".
176201
*
177202
* @param array $supportedOperation
178203
* @param array $operation

ApiDocumentationFactory.php

Lines changed: 0 additions & 21 deletions
This file was deleted.

Controller/DocumentationController.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class DocumentationController extends Controller
3232
*/
3333
public function entrypointAction()
3434
{
35-
return new JsonLdResponse([]);
35+
return new JsonLdResponse($this->get('dunglas_json_ld_api.entrypoint_builder')->getEntrypoint());
3636
}
3737

3838
/**
@@ -44,7 +44,7 @@ public function entrypointAction()
4444
*/
4545
public function vocabAction()
4646
{
47-
return new JsonLdResponse($this->get('dunglas_json_ld_api.api_documentation')->getDocumentation());
47+
return new JsonLdResponse($this->get('dunglas_json_ld_api.api_documentation_builder')->getApiDocumentation());
4848
}
4949

5050
/**
@@ -58,13 +58,15 @@ public function vocabAction()
5858
*/
5959
public function contextAction($shortName)
6060
{
61-
$resource = $this->get('dunglas_json_ld_api.resources')->getResourceForShortName($shortName);
62-
if (!$resource) {
63-
throw $this->createNotFoundException();
61+
if ('Entrypoint' !== $shortName || 'ApiDocumentation' !== $shortName) {
62+
$resource = $this->get('dunglas_json_ld_api.resources')->getResourceForShortName($shortName);
63+
if (!$resource) {
64+
throw $this->createNotFoundException();
65+
}
6466
}
6567

6668
return new JsonLdResponse(
67-
['@context' => $this->get('dunglas_json_ld_api.context_builder')->buildContext($resource)]
69+
['@context' => $this->get('dunglas_json_ld_api.context_builder')->buildContext()]
6870
);
6971
}
7072
}

DependencyInjection/DunglasJsonLdApiExtension.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@
1111

1212
namespace Dunglas\JsonLdApiBundle\DependencyInjection;
1313

14-
use Symfony\Component\DependencyInjection\ContainerBuilder;
1514
use Symfony\Component\Config\FileLocator;
16-
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
15+
use Symfony\Component\DependencyInjection\ContainerBuilder;
1716
use Symfony\Component\DependencyInjection\Loader;
17+
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
18+
1819

1920
/**
2021
* The extension of this bundle.

EntrypointBuilder.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the DunglasJsonLdApiBundle package.
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+
namespace Dunglas\JsonLdApiBundle;
13+
14+
use Symfony\Component\Routing\RouterInterface;
15+
16+
/**
17+
* Hydra's Entrypoint builder.
18+
*
19+
* @author Kévin Dunglas <[email protected]>
20+
*/
21+
class EntrypointBuilder
22+
{
23+
/**
24+
* @var Resources
25+
*/
26+
private $resources;
27+
/**
28+
* @var RouterInterface
29+
*/
30+
private $router;
31+
32+
public function __construct(
33+
Resources $resources,
34+
RouterInterface $router
35+
) {
36+
$this->resources = $resources;
37+
$this->router = $router;
38+
}
39+
40+
/**
41+
* Gets the entrypoint of the API.
42+
*
43+
* return array
44+
*/
45+
public function getEntrypoint()
46+
{
47+
$entrypoint = [
48+
'@context' => $this->router->generate('json_ld_api_context', ['shortName' => 'Entrypoint']),
49+
'@id' => $this->router->generate('json_ld_api_entrypoint'),
50+
'@type' => 'Entrypoint',
51+
];
52+
53+
foreach ($this->resources as $resource) {
54+
$entrypoint[$resource->getBeautifiedName()] = $this->router->generate($resource->getCollectionRoute());
55+
}
56+
57+
return $entrypoint;
58+
}
59+
}

Hydra/HydraClass.php

Lines changed: 0 additions & 25 deletions
This file was deleted.

Hydra/HydraOperation.php

Lines changed: 0 additions & 21 deletions
This file was deleted.

Hydra/HydraProperty.php

Lines changed: 0 additions & 25 deletions
This file was deleted.

0 commit comments

Comments
 (0)