Skip to content

Commit 8254b55

Browse files
author
Benjamin RICHARD
committed
Try to use DataProvider instead of auto-binding with Doctrine annotations
1 parent c9d119c commit 8254b55

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

config/services.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,5 @@ services:
108108
# needs to alias the current security.user.provider.concrete.in_memory to the class (seems not done in sf or security bundle)
109109
Symfony\Component\Security\Core\User\InMemoryUserProvider:
110110
alias: security.user.provider.concrete.in_memory
111+
112+
App\DataProvider\PingDataProvider: ~

src/DataProvider/PingDataProvider.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
namespace App\DataProvider;
3+
4+
5+
use ApiPlatform\Core\DataProvider\CollectionDataProviderInterface;
6+
use ApiPlatform\Core\DataProvider\ItemDataProviderInterface;
7+
use ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface;
8+
use App\Entity\Ping;
9+
10+
class PingDataProvider implements ItemDataProviderInterface, CollectionDataProviderInterface, RestrictedDataProviderInterface
11+
{
12+
public function supports(string $resourceClass, string $operationName = null, array $context = []): bool
13+
{
14+
return Ping::class === $resourceClass;
15+
}
16+
17+
public function getItem(string $resourceClass, $id, string $operationName = null, array $context = [])
18+
{
19+
$pong = new Ping();
20+
21+
return $pong;
22+
}
23+
24+
public function getCollection(string $resourceClass, string $operationName = null, array $context = [])
25+
{
26+
$pong = new Ping();
27+
28+
return [$pong];
29+
}
30+
31+
}

src/Entity/Ping.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
namespace App\Entity;
3+
4+
use ApiPlatform\Core\Annotation\ApiResource;
5+
6+
/**
7+
* Class Ping
8+
* @ApiResource(
9+
* itemOperations={
10+
* "get"={"method"="GET"}
11+
* },
12+
* collectionOperations={
13+
* "get"={"method"="GET"}
14+
* }
15+
* )
16+
* @package App\Ping
17+
*/
18+
class Ping
19+
{
20+
/**
21+
* @var string
22+
*/
23+
protected $pong = 'pong';
24+
25+
/**
26+
* @return string
27+
*/
28+
public function getPong()
29+
{
30+
return $this->pong;
31+
}
32+
}

0 commit comments

Comments
 (0)