Code Monkey home page Code Monkey logo

api-lite's Introduction

rekalogika/api-lite

A set of tools to simplify working with API Platform in your projects. Comes with guides, patterns, and practical examples for building API Platform-based projects.

Apply existing knowledge, experience, and patterns of working with Symfony controllers to API Platform state providers and processors. Decouple your persistence layer from the API frontend layer. Apply Domain-Driven Design (DDD) principles to your API Platform-based projects, as well as other architectural patterns and best practices, including SOLID, onion architecture, clean architecture, hexagonal architecture, and others.

'Lite' means we are refraining from using all of API Platform's features and automations in favor of better readability, simplicity, and flexibility.

Full documentation is available at rekalogika.dev/api-lite.

Motivation

API Platform documentation encourages developers to use plain old PHP objects (POPOs) or data transfer objects (DTOs) as the models for API communication, instead of using domain entities directly for this purpose. But it does not establish a practical working patterns for that approach.

Practically all the examples and demos we find on the Internet still attach ApiResource to Doctrine entities.

Sometimes API Platform can feel very rigid. It can be difficult to figure out how to accomplish things outside its conventions. There are ways around any problem, just not always immediately obvious. It can feel like that we just want to express what we need by writing a PHP code, not by figuring out the correct combination of attributes to use.

Those coming from Symfony controllers might find API Platform's approach very different, but it does not have to be.

Installation

composer require rekalogika/api-lite

Synopsis

use Doctrine\Common\Collections\Collection;
use Rekalogika\ApiLite\State\AbstractProvider;
use Rekalogika\Mapper\CollectionInterface;

#[ApiResource(
    shortName: 'Book',
    operations: [
        new Get(
            uriTemplate: '/books/{id}',
            provider: BookProvider::class
        ),
    ]
)]
class BookDto
{
    public ?Uuid $id = null;
    public ?string $title = null;
    public ?string $description = null;

    /**
     * @var ?CollectionInterface<int,ReviewDto>
     */
    public ?CollectionInterface $reviews = null;
}

/**
 * @extends AbstractProvider<BookDto>
 */
class BookProvider extends AbstractProvider
{
    public function __construct(
        private BookRepository $bookRepository
    ) {
    }

    public function provide(
        Operation $operation,
        array $uriVariables = [],
        array $context = []
    ): object|array|null {
        $book = $this->bookRepository
            ->find($uriVariables['id'] ?? null)
            ?? throw new NotFoundException('Book not found');

        $this->denyAccessUnlessGranted('view', $book);

        return $this->map($book, BookDto::class);
    }
}

To-Do List

  • Figure out & implement filtering.

Documentation

rekalogika.dev/api-lite

License

MIT

api-lite's People

Contributors

priyadi avatar dependabot[bot] avatar

Stargazers

Yassine Hamouten avatar Théo D avatar Álvaro Rosado Olmo avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.