Package Data | |
---|---|
Maintainer Username: | markeilander |
Maintainer Contact: | mark.eilander@outlook.com (Mark Eilander) |
Package Create Date: | 2016-02-16 |
Package Last Update: | 2018-02-09 |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-11-26 15:05:20 |
Package Statistics | |
---|---|
Total Downloads: | 351 |
Monthly Downloads: | 1 |
Daily Downloads: | 0 |
Total Stars: | 3 |
Total Watchers: | 1 |
Total Forks: | 2 |
Total Open Issues: | 0 |
An laravel implementation of the Gateway Pattern
A gateway encapsulates the semantic gap between the object-oriented domain layer and the relation-oriented persistence layer.
Further reading: http://ryantablada.com/post/two-design-patterns-that-will-make-your-applications-better
[TOC]
In your config/app.php
add Eilander\Repository\Providers\RepositoryServiceProvider:class
to the end of the providers
array:
<?php
'providers' => [
...
Eilander\Gateway\Providers\GatewayServiceProvider::class,
],
Add the package to the main composer.json
for autoloading and run composer dump-autoload
, like so:
<?php
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"App\\": "app/",
"Eilander\\Gateway\\": "../library/eilander/gateway/src/"
}
},
#!json
composer dump-autoload
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use App\Gateways\ProductGateway as Gateway;
class ProductController extends Controller
{
/**
* @var VodafoneGateway
*/
protected $gateway;
public function __construct(Gateway $gateway){
$this->gateway = $gateway;
}
public funcion create() {
return $this->gateway->createProduct();
}
....
}
<?php
namespace App\Gateways;
use Illuminate\Http\Request;
class TestGateway {
public function __construct(Request $request)
{
$this->request = $request;
}
public function createProduct()
{
// some validation
...
// screate new product
$product = App\Product::create($this->request->all());
}
}
Laravel's eloquent (single and multiple) gateways are included. An empty elasticsearch gateway is also included
Presenters function as a wrapper and renderer for objects.
Fractal Presenter
Requires Fractal.
use League\Fractal\TransformerAbstract;
class PostTransformer extends TransformerAbstract
{
public function transform(\Post $post)
{
return [
'id' => (int) $post->id,
'title' => $post->title,
'content' => $post->content
];
}
}
use Eilander\Gateway\Presenter\FractalPresenter;
class PostPresenter extends FractalPresenter {
/**
* Prepare data to present
*
* @return \League\Fractal\TransformerAbstract
*/
public function getTransformer()
{
return new PostTransformer();
}
}
<?php
namespace App\Gateways\Eloquent;
use App\Gateways\Eloquent\Contracts\GebruikerGateway as Gateways;
use App\Repositories\Eloquent\Contracts\GebruikerRepository as Repository;
use App\Validators\GebruikerValidator as Validator;
use Eilander\Gateway\Eloquent\EloquentGateway;
use App\Presenters\GebruikerPresenter as Presenter;
/**
* Class FruitRepository
*/
class GebruikerGateway extends EloquentGateway implements Gateways {
{
...
public function presenter()
{
return Presenter::class;
}
}
Use skipPresenter before any other chaining method
$posts = $this->gateway->skipPresenter()->all();