Skip to content

Commit

Permalink
temp
Browse files Browse the repository at this point in the history
  • Loading branch information
soyuka committed Sep 14, 2023
1 parent df6e547 commit 01d79bb
Show file tree
Hide file tree
Showing 12 changed files with 192 additions and 551 deletions.
5 changes: 3 additions & 2 deletions docs/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
}
],
"require": {
"api-platform/core": "^3.1",
"api-platform/core": "dev-main",
"symfony/expression-language": "6.2.*",
"nelmio/cors-bundle": "^2.2",
"phpstan/phpdoc-parser": "^1.15",
Expand Down Expand Up @@ -43,5 +43,6 @@
},
"require-dev": {
"phpunit/phpunit": "^10"
}
},
"minimum-stability": "dev"
}
2 changes: 1 addition & 1 deletion docs/config/packages/doctrine.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
doctrine:
dbal:
url: 'sqlite:///%kernel.project_dir%/var/data.db'
url: 'sqlite:///%kernel.project_dir%/var/%guide%.db'
orm:
auto_generate_proxy_classes: true
naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
Expand Down
19 changes: 11 additions & 8 deletions docs/config/packages/framework.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
when@test:
framework:
test: true
framework:
test: true
api_platform:
formats:
jsonld: ['application/ld+json']
json: ['application/json']
defaults:
extraProperties:
standard_put: true
formats:
jsonld: ['application/ld+json']
json: ['application/json']
event_listeners_backward_compatibility_layer: false
keep_legacy_inflector: false
defaults:
extra_properties:
rfc_7807_compliant_errors: true
standard_put: true
1 change: 0 additions & 1 deletion docs/guides/declare-a-resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
// position: 1
// executable: true
// tags: design
// homepage: true
// ---

// # Declare a Resource
Expand Down
110 changes: 110 additions & 0 deletions docs/guides/doctrine-entity-as-resource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php
// ---
// slug: doctrine-entity-as-resource
// name: Doctrine entity as API resource
// position: 4
// tags: doctrine
// executable: true
// ---

// API Platform is compatible with [Doctrine ORM](https://www.doctrine-project.org), all we need is to declare an
// API Resource on a Doctrine Entity.
namespace App\Entity {

use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\ApiFilter;
use ApiPlatform\Doctrine\Orm\Filter\OrderFilter;
use Doctrine\ORM\Mapping as ORM;

// When an ApiResource is declared on an ORM\Entity we have access to [Doctrine filters](https://api-platform.com/docs/core/filters/).
#[ApiResource]
#[ApiFilter(OrderFilter::class)]
#[ORM\Entity]
class Book
{
#[ORM\Id, ORM\Column, ORM\GeneratedValue]
private ?int $id = null;

#[ORM\Column]
public ?string $title = null;

public function getId(): ?int
{
return $this->id;
}
}
}

namespace App\Playground {

use Symfony\Component\HttpFoundation\Request;

function request(): Request
{
// Persistence is automatic, you can try to create or read data:
return Request::create('/books?order[id]=desc', 'GET');
return Request::create('/books/1', 'GET');
return Request::create(uri: '/books', method: 'POST', server: ['CONTENT_TYPE' => 'application/ld+json'], content: json_encode(['id' => 1, 'title' => 'API Platform rocks.']));
}
}

namespace DoctrineMigrations {

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

final class Migration extends AbstractMigration
{
public function up(Schema $schema): void
{
$this->addSql('CREATE TABLE book (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, title VARCHAR(255) NOT NULL)');
}
}
}

namespace App\Fixtures {

use App\Entity\Book;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Persistence\ObjectManager;
use function Zenstruck\Foundry\anonymous;
use function Zenstruck\Foundry\repository;

final class BookFixtures extends Fixture
{
public function load(ObjectManager $manager): void
{
$bookFactory = anonymous(Book::class);
if (repository(Book::class)->count()) {
return;
}

$bookFactory->many(10)->create([
'title' => 'title'
]);
}
}
}

namespace App\Tests {

use ApiPlatform\Symfony\Bundle\Test\ApiTestCase;
use ApiPlatform\Playground\Test\TestGuideTrait;

final class BookTest extends ApiTestCase
{
use TestGuideTrait;

public function testGet(): void
{
static::createClient()->request('GET', '/books.jsonld');
$this->assertResponseIsSuccessful();
}

public function testGetOne(): void
{
static::createClient()->request('GET', '/books/1.jsonld');
$this->assertResponseIsSuccessful();
}
}
}
115 changes: 0 additions & 115 deletions docs/guides/doctrine-orm-service-filter.php

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
// ---
// position: 13
// slug: use-doctrine-search-filter
// position: 10
// slug: doctrine-search-filter
// name: Doctrine ORM SearchFilter
// executable: true
// ---
Expand Down Expand Up @@ -58,10 +58,33 @@ public function up(Schema $schema): void
{
$this->addSql('CREATE TABLE book (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, title VARCHAR(255) NOT NULL, author VARCHAR(255) NOT NULL)');
}
}
}

public function down(Schema $schema): void
namespace App\Fixtures {

use App\Entity\Book;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Persistence\ObjectManager;
use function Zenstruck\Foundry\anonymous;
use function Zenstruck\Foundry\repository;
use function Zenstruck\Foundry\faker;

final class BookFixtures extends Fixture
{
public function load(ObjectManager $manager): void
{
$this->addSql('DROP TABLE book');
$bookFactory = anonymous(Book::class);
if (repository(Book::class)->count()) {
return;
}

$bookFactory->many(10)->create(fn() =>
[
'title' => faker()->name(),
'author' => faker()->firstName(),
]
);
}
}
}
Expand Down
Loading

0 comments on commit 01d79bb

Please sign in to comment.