This package allows you to sync your data with Laravel 5.5 and higher. Contains two sections of manger and clint
Require this package in your composer.json and update composer.
composer require 4myth/api
Laravel 5.5 uses Package Auto-Discovery, so doesn't require you to manually add the ServiceProvider/Facade.
You can use the Alias for shorter code.
use Myth\Api;
Api::name();
Finally you can publish the package:
php artisan vendor:publish --tag=myth-api
-
The settings you need to setup of connection with your clients. your
name
and yourclients
.
"name" => "manager",
"clients" => [
# Client name
"client-name" => [
"secret" => "secret",
"base_uri" => "http://127.0.0.1/api/v1",
"models" => [
App\User::class => [
"uri" => "user",
"transformer" => App\UserApiTransformer::class,
],
],
"options" => [
"http" => [],
],
],
],
]
Example multiple clients:
[
"client-1" => [],
"client-2" => [],
];
Example multiple models with clients:
[
"client-1" => [
"models" => [
App\User::class => [],
Some\Name\SomeModel::class => [],
],
]
];
The key
of client array must be the client name in your application.
All options array keys: secret
,base_uri
,options
,models
secret
: Client's authentication secret, which you can obtain from your client.base_uri
: Client api url. Example:http://127.0.0.1/api/v1
options
: Array of your client options. available options:http
.http
: GuzzleHttp\Client options. See: http://docs.guzzlephp.org
models
: Array of your models, will be able tosyc
with specific client.uri
: Theurl
orprefix
of model at your client software.transformer
: This option will be used automatically when your system syncsend
model data to your client, you must make a new transformer for each client's model.
- Model.
- Model transformer.
Before you begin to use the package you must setup your model by use package trait
.
namespace App;
use Illuminate\Database\Eloquent\Model;
use Myth\Api\Traits\HasApiManager;
class User extends Model
{
use HasApiManager;
// ....
}
first to make a new model transformer we have configure in our config, we can execute artisan command:
php artisan myth:make-client-transformer TransformerName
then we will find the transformer inside app directory app\TransformerName.php
public function body(): array
{
return [];
}
The body method must be return an array data that you will send when syncing this model with Client.
Example:
public function body(): array
{
return [ "name" => "MyTh", "password" => 123 ];
}
The client will receive in body request this array.
to access your client inside your application you can use the facade
package Api
use Myth\Api;
$client = Api::client("client-name");
use Myth\Api;
$client = Api::client("client-name");
/** @var Myth\Api\ClientModelWrapper $model */
$ClientModelWrapper = $client->model(User::class);
$ClientModelWrapper = $client->model(User::find(1));
/** @var \Illuminate\Database\Eloquent\Model $model */
$model = $ClientModelWrapper->model();
you can send your data with multiple ways:
use Myth\Api;
// facade
$body = [ "name" => "MyTh", "password" => 123 ];
$response = Api::sendToClient("client-name", User::calss, $body);
// ----------------------
// client wrapper
$client = Api::client("client-name");
$user = User::find(123);
$response = $client->sendData($user);
dd($response);
$body = [ "name" => "Name", "password" => "123456789"];
$response = $client->sendData(User::class,$body);
dd($response);
// ----------------------
// by model directly
$user = User::find(2);
$response = $user->sendToClient("client-name");
dd($response);
The response using class ResponseInterface
package contains methods you can access easily in your code
request
: \GuzzleHttp\Psr7\Response See: http://docs.guzzlephp.orgresponse
: array client responseclient_id
: the unique id at client in database.
use Myth\Api;
$data = Api::clientData("client-name", User::class, $sync = true)->get();
$data = Api::clientData("client-name", User::class, $sync = false)->get();
$data = Api::clientData("client-name", User::class, $sync = null)->get();
dd($data);
$client = Api::client('client-name');
$data = $client->model(User::class)->data($sync = true)->get();
$data = $client->model(User::class)->data($sync = false)->get();
$data = $client->model(User::class)->data($sync = null)->get();
dd($data);
$data = User::clientData("client-name", $sync = true)->get();
$data = User::clientData("client-name", $sync = false)->get();
$data = User::clientData("client-name", $sync = null)->get();
dd($data);
set must sync with client
use Myth\Api;
$model = Api::syncWithClient("client-name", $client_id = 4, $model = User::find(1));
$model = Api::client("client-name")->syncModel($client_id = 5, $model = User::find(1));
$model = User::find(1)->syncWithClient("client-name", $client_id = 3);
set synced with client
use Myth\Api;
$model = Api::syncedWithClient("client-name", $client_id = 4, $model = User::find(1));
$model = Api::client("client-name")->syncedModel($client_id = 5, $model = User::find(1));
$model = User::find(1)->syncedWithClient("client-name", $client_id = 3);
unsync model with client Delete relation
use Myth\Api;
$model = Api::unsyncWithClient("client-name", $client_id = 1, $model = User::find(1));
$model = Api::client("client-name")->unsyncModel($client_id = 1, $model = User::find(1));
$model = User::find(1)->unsyncWithClient("client-name", $client_id = 1);