neomerx / json-api

Framework agnostic JSON API (jsonapi.org) implementation

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to pass additional params to Schema

zion03 opened this issue · comments

I run into same problem as described #94.
I'm trying to inject dependency into Schema but it's impossible because Schema already have inner dependecies.
Using global service container looks like a bad practice.
Like option we can extend EncodingParameters with customParam to
[array $includePaths = null, array $fieldSets = null, $customParam = null] and pass param to getAttributes and getRelationships.

Is I understand for now working only DI service as global?

Using global service container looks like a bad practice.

Thank you for your support. I'm still learning PHP and the more I learn, the more I realize how much I don't know.

I'm trying to inject dependency into Schema but it's impossible because Schema already have inner dependecies.

You're probably referencing BaseSchema constructor which requires FactoryInterface as an input parameter.
Shortly you have 2 options to deal with it

  1. Create a base Schema that does not require any constructor params and use it as a base for your Schemas
abstract class MyBaseSchema extends BaseSchema {
    public function __construct() {
        parent::__construct(new \Neomerx\JsonApi\Factories\Factory());
    }
}
  1. The library nowhere requires BaseSchema or classes that extend it. You can implement \Neomerx\JsonApi\Contracts\Schema\SchemaInterface the way you like.

I run into same problem as described #94.

That issue ended up with adding support for already created Schemas. The issue starter wanted to add already created Schema instances and such feature was added.

Like option we can extend EncodingParameters with customParam to
[array $includePaths = null, array $fieldSets = null, $customParam = null] and pass param to getAttributes and getRelationships.

I didn't understand how it relates to Schemas and what the idea was about.

A sample of passing custom parameters to Schema could be found in wiki

Thanks for quick answer. Currently I decided leave my global $kernel like faster way.
Solution rewrite BaseSchema as I understand will not work, because in the end you pass SchemaFactoryInterface anyway.

And I still don't understand how to pass custom values to Schema class.

For example I have a title which contain json with translations like:
{'en': 'en title', 'es': 'es title'}

In Schema I should to know about current locale to return right value
return ['title' => $data->getTitle()]
So I need pass to schema locale or my $request service

Please have a look at the wiki link I posted above.

Something like

class ZionSchema extends BaseSchema {
    private $locales;
    private $curLocale;
    public function __construct(
        FactoryInterface $factory,
        string $curLocale,
        array $locales
    ) {
        parent::__construct($factory);
        $this->curLocale = $curLocale;
        $this->locales   = $locales;
    }

    // usual stuff for Schemas below
}

$curLocale = 'en';
$locales   = [
    'en' => ['title' => 'en title'],
    'es' => ['title' => 'es title'],
];
$schemaClosure = function (FactoryInterface $factory) use ($curLocale, $locales) {
    return new ZionSchema($factory, $curLocale, $locales);
};

$encoder = Encoder::instance([
    Zion::class => $schemaClosure,
]);

I have a feeling I've answered the question. If you have any further questions please don't hesitate to ask.