Download the PHP package petrelli/eloquent-consumer without Composer

On this page you can find all versions of the php package petrelli/eloquent-consumer. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package eloquent-consumer

About

Eloquent API Consumer will allow you to solve two main problems regarding API's in Laravel:

  1. Generate and execute API calls in a clear and simple way
  2. Process API responses and generate Eloquent like models and collections (including paginated ones)

You can see an example application here: Eloquent Consumer Example APP.

Development Note

This package is still on alpha state, and under heavy development. Things might change in the near future.

Motivation to create this package

Our CMS was designed to use Eloquent like models as the data source.

So we needed a way to integrate an API origin to build some listings in a seamless way, without having to modify the CMS.

This library creates models who's interface will be compatible with Eloquent, so building queries, pagination, scopes, filtering, and mostly everything related to Eloquent will be available.

With this package you will be able to manage your API endpoints, caching strategies, low level API query configuration, model attributes, scopes and functions, and basically every element involved in the process of generating a query, to getting the processed end result.

Table of contents

Overview

Let's imagine we have a Book API model and we want to read some data from the API. Let's perform some calls right now:

As you can see this syntax is very familiar.

Let's say you want to add your own Eloquent like scope to get only published elements. Just follow the same syntax as you usually do with Laravel:

This will pass a published=true parameter when performing the API call (because we simply used the rawQuery function as we saw before)

To configure this model we have to create an Endpoint class for our specific API. You can do that manually or using the following command:

Let's edit some options:

As you can see, we only defined here a $baseUri and a default TTL for caching (200 seconds).

Now that we have the endpoint configured, we should create a the actual Book Model. This model must inherit from our faux Eloquent like class.

Here we just have to configure two things:

Collection and resource are the default endpoints that the package uses. Of course you can define your own and use them later, but these are enough.

This will be enough to get you started.

If you continue reading you will learn to modify how calls are performed, and how responses will be processed.

Core concepts

You will have a default configuration file for you project defining everything we will show here. But of course, you can override these values configuring each class separately.

It's recommended to use the default namespace ApiConsumer when creating new elements.

We will mention the following entities:

Endpoint

This one will be your main entity.

You can have as many as you want per project, allowing you to use multiple API's and/or multiple configurations within the same API.

Here you can define the following options:

Keep in mind these options:

Consumer

Consumers are the clients that will execute the actual API calls. This package includes a default client that uses Guzzle.

Usually using the default one with Guzzle will be more than enough.

Creating a new Consumer class could be useful if you had to add new options like authentication headers, timeout configurations, basically, any configuration you might want supported by Guzzle or your chosen client.

Grammar

A Grammar class basically a collector that transforms all the information you gave to the Query Builder into fields that will be sent in your API request.

It's a very simple class that you can extend and adjust to your API specs.

Let's see this Grammar Class example function:

Here you see that the function search('term') will be transformed into a parameter named q.

Please refer to our default grammar class code, and you will observe there how we process all options.

Installation

  1. Install the package via composer:

  2. Export configuration file:

In there you can define default options and entities (endpoint, grammar and connection).

Configuration

Let's generate an Endpoint:

Or in case you want to use your own namespace:

Now let's add some configuration values to the endpoint:

Here you can see it's a very basic Endpoint. No grammar or consumer customized.

You can add a $grammarClass variable in case you want to extend the default one:

And we will need to create that Grammar class:

So here, we redefine compilePage which will send a page_number parameter on the API query, instead of the page default.

Usage

After everything is configured we can start using this model almost as if it was an eloquent entity.

As you can see this syntax is very familiar.

Let's say you want to add your own Eloquent like scope to get only published elements. Just follow the same syntax as you usually do with Laravel:

This will pass a published=true parameter when performing the API call (because we simply used the rawQuery function as we saw before)

Relationships

Documentation to be completed.

Transformer classes

So we generated the query, and now we are getting a correct response.

We have now to refactor this response to fit the library standards so everything works as expected.

This will be done easily with a TransformerClass.

Transformer classes have only one function: transform(). There you have available $this->response to use it and transform your responses to our proper format.

This will enable you to use any API transparently of the underline response format.

The expected format looks like the following:

So in order to be able to use full power, you will have to adapt your response to look like this one.

Extended Reference

TODO

License

The MIT License (MIT). Please see License File for more information.


All versions of eloquent-consumer with dependencies

PHP Build Version
Package Version
Requires php Version ^7.0
guzzlehttp/guzzle Version 6.3.0
jenssegers/model Version ^1.2
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package petrelli/eloquent-consumer contains the following files

Loading the files please wait ....