mapado/rest-client-sdk-bundle

Rest Client SDK Bundle for hydra API

v0.24.1 2018-04-25 08:39 UTC

README

Symfony bundle for mapado/rest-client-sdk

Installation

composer require mapado/rest-client-sdk-bundle

Symfony flex

Add it to your config/bundle.php

return [
    // ...
    Mapado\RestClientSdkBundle\MapadoRestClientSdkBundle::class => ['all' => true],
];

Without flex

Add it to your AppKernel.php

    public function registerBundles()
    {
        $bundles = array(
            // ...
            new Mapado\RestClientSdkBundle\MapadoRestClientSdkBundle(),
        )

        // ...

Usage

Add this in your configuration file :

Symfony Flex: config/packages/mapado_rest_client_sdk.yaml, not flex: app/config/config.yml

mapado_rest_client_sdk:
    # debug: %kernel.debug%
    # cache_dir: '%kernel.cache_dir%/mapado/rest_client_sdk_bundle/'
    entity_managers:
        foo:
            server_url: 'http://foo.com:8080'
            request_headers:
                MyHeader: 'MyValue'
            mappings:
                prefix: /v1
                configuration:
                    collectionKey: 'items' # default is "hydra:member"
                dir: '%kernel.root_dir%/../src/Foo/Bar/Entity/'
            cache:
                cache_item_pool: 'psr6_cache_provider' # default is null
                cache_prefix: 'my_prefix' # default is null

The bundle registers one service for each entity manager that you defined (in this case just one for foo).

The name of the service will be: mapado.rest_client_sdk.<manager_name>.

As I named my entity manager foo, The service name here will be : mapado.rest_client_sdk.foo.

If you use Symfony 3.3+ autowiring feature, you may want to alias something like this:

services:
    # ...
    Mapado\RestClientSdk\SdkClient: '@mapado.rest_client_sdk.foo'

If you have multiple entity managers, Symfony documentation explains how to deal with multiple implementation of the same type.

Imagine I have the following model, as defined in the component documentation:

/**
 * @Rest\Entity(key="carts", client="Acme\Foo\Bar\CartClient")
 */
class Cart {
    // ...
}

I can now do something like this:

$cartList = $this->get('mapado.rest_client_sdk.foo')
    ->getRepository('carts')
    ->findAll(); // `carts` is the `key` defined in the model

$cart = $this->get('mapado.rest_client_sdk.foo')
    ->getRepository('carts')
    ->find(1);

For a more complete information on the usage, I recommand you to look at the component documentation

Using cache

By providing a Psr6 Psr\Cache\CacheItemPoolInterface to cache.cache_item_pool, each entity and entityList fetched will be stored in cache.

For example at Mapado, we are using the Symfony Array cache adapter like this:

services:
    cache.rest_client_sdk:
        class: 'Symfony\Component\Cache\Adapter\ArrayAdapter'
        arguments:
            - 0
            - false # avoid serializing entities

mapado_rest_client_sdk:
    entity_managers:
        foo:
            server_url: '%server.url%'
            mappings:
                prefix: /v1
                dir: '%kernel.root_dir%/../src/Mapado/Foo/Model/'
            cache:
                cache_item_pool: 'cache.rest_client_sdk' # the id of the cache service
                cache_prefix: 'mapado_rest_client_'