Skip to content
This repository has been archived by the owner on Apr 8, 2024. It is now read-only.

Commit

Permalink
Merge branch 'release/0.2.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
jimmypuckett committed Oct 18, 2019
2 parents f7d81c5 + a889a3a commit d645344
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 90 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.1.2
0.2.0
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"extra": {
"laravel": {
"providers": [
"Spinen\\Formio\\Providers\\ClientServiceProvider",
"Spinen\\Formio\\Providers\\ServiceProvider"
]
}
Expand Down
121 changes: 121 additions & 0 deletions src/Providers/ClientServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<?php

namespace Spinen\Formio\Providers;

use Carbon\Carbon;
use GuzzleHttp\Client as Guzzle;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\Support\DeferrableProvider;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\ServiceProvider as LaravelServiceProvider;
use Spinen\Formio\Client as Formio;

/**
* Class ClientServiceProvider
*
* Since this is deferred, it only needed to deal with code that has to do with the client.
*
* @package Spinen\Formio\Providers
*/
class ClientServiceProvider extends LaravelServiceProvider implements DeferrableProvider
{
/**
* Bootstrap services.
*
* @return void
*/
public function boot()
{
//
}

/**
* Register services.
*
* @return void
*/
public function register()
{
$this->registerClient();

$this->app->alias(Formio::class, 'Formio');
}

/**
* Get the services provided by the provider.
*
* @return array
*/
public function provides()
{
return [
Formio::class,
];
}

/**
* Register the client
*
* If the Formio id or roles are null, then assume sensible values via the API
*/
protected function registerClient(): void
{
$this->app->bind(
Formio::class,
function (Application $app) {
$formio = new Formio(Config::get('formio'), $app->make(Guzzle::class));

$resourceIds = function () use ($formio) {
$id = $formio->login()
->request('form?name=user')[0]['_id'];

$formio->logout();

return $id;
};

// If the formio id is null, then get it or a cached value for the user resource
if (empty(Config::get('formio.user.form'))) {
Config::set(
'formio.user.form',
Cache::remember(
'formio.id',
Carbon::now()
->addDay(),
$resourceIds
)
);

$formio->setConfigs(Config::get('formio'));
}

$roleIds = function () use ($formio) {
$roles = (array)$formio->login()
->request('role?title=Authenticated')[0]['_id'];

$formio->logout();

return $roles;
};

// If the user roles are null, then get it or a cached value for authenticated user
if (empty(Config::get('formio.user.roles'))) {
Config::set(
'formio.user.roles',
Cache::remember(
'formio.user.roles',
Carbon::now()
->addDay(),
$roleIds
)
);

$formio->setConfigs(Config::get('formio'));
}

return $formio;
}
);
}
}
90 changes: 1 addition & 89 deletions src/Providers/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,16 @@

namespace Spinen\Formio\Providers;

use Carbon\Carbon;
use GuzzleHttp\Client as Guzzle;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\Support\DeferrableProvider;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\ServiceProvider as LaravelServiceProvider;
use Spinen\Formio\Client;
use Spinen\Formio\Client as Formio;

/**
* Class ServiceProvider
*
* @package Spinen\Formio\Providers
*/
class ServiceProvider extends LaravelServiceProvider implements DeferrableProvider
class ServiceProvider extends LaravelServiceProvider
{
/**
* Bootstrap services.
Expand All @@ -40,87 +33,6 @@ public function boot()
public function register()
{
$this->mergeConfigFrom(__DIR__ . '/../../config/formio.php', 'formio');

$this->registerClient();

$this->app->alias(Formio::class, 'Formio');
}

/**
* Get the services provided by the provider.
*
* @return array
*/
public function provides()
{
return [
Client::class,
];
}

/**
* Register the client
*
* If the Formio id or roles are null, then assume sensible values via the API
*/
protected function registerClient(): void
{
$this->app->bind(
Formio::class,
function (Application $app) {
$formio = new Formio(Config::get('formio'), $app->make(Guzzle::class));

$resourceIds = function () use ($formio) {
$id = $formio->login()
->request('form?name=user')[0]['_id'];

$formio->logout();

return $id;
};

// If the formio id is null, then get it or a cached value for the user resource
if (empty(Config::get('formio.user.form'))) {
Config::set(
'formio.user.form',
Cache::remember(
'formio.id',
Carbon::now()
->addDay(),
$resourceIds
)
);

$formio->setConfigs(Config::get('formio'));
}

$roleIds = function () use ($formio) {
$roles = (array)$formio->login()
->request('role?title=Authenticated')[0]['_id'];

$formio->logout();

return $roles;
};

// If the user roles are null, then get it or a cached value for authenticated user
if (empty(Config::get('formio.user.roles'))) {
Config::set(
'formio.user.roles',
Cache::remember(
'formio.user.roles',
Carbon::now()
->addDay(),
$roleIds
)
);

$formio->setConfigs(Config::get('formio'));
}

return $formio;
}
);
}

/**
Expand Down

0 comments on commit d645344

Please sign in to comment.