gimmenetwork/api-platform

Unified tooling for building API

This package's canonical repository appears to be gone and the package has been frozen as a result.

0.0.9 2021-04-06 12:20 UTC

This package is auto-updated.

Last update: 2021-04-06 12:20:44 UTC


README

  1. Installation
  2. Configuration

Installation

Pretty simple:

composer require gimmenetwork/api-platform

After the bundle is installed, enable it in config/bundles.php

<?php

return [
	...
    Gimmemore\API\ApiPlatformBundle::class => ['all' => true]
]

Usage

Filters

Register compiler pass in Kernel

<?php

// src/Kernel.php

use Gimmemore\API\Filters\Filter;
use Gimmemore\API\DependencyInjection\CompilerPass\RegisterFilters;

...

protected function build(ContainerBuilder $container) : void
{
	...
    $container->registerForAutoconfiguration(Filter::class)->addTag(RegisterFilters::CONTAINTER_TAG);
    $container->addCompilerPass(new RegisterFilters());
}

Every filter implement Filter interface:

<?php

declare(strict_types=1);

namespace Gimmemore\API\Filters;

use Doctrine\ORM\QueryBuilder;

interface Filter
{
    public function applyTo(QueryBuilder $builder, string $alias): void;

    public static function getType(): string;
    public static function createFromValue($value): self;
}

If you want to filter by status, you have to register Filter that has type status.

Exception handling

Register compiler pass in Kernel

<?php

// src/Kernel.php

use Gimmemore\API\ExceptionHandling\ExceptionResponseHandler;
use Gimmemore\API\DependencyInjection\CompilerPass\RegisterResponseHandlers;

...

protected function build(ContainerBuilder $container) : void
{
	...
    $container->registerForAutoconfiguration(ExceptionResponseHandler::class)->addTag(RegisterResponseHandlers::CONTAINER_TAG);
    $container->addCompilerPass(new RegisterResponseHandlers());
}

There can be only one handler per Exception class.