Skip to content

Commit

Permalink
Merge pull request #106 from tabirkeland/refactoring
Browse files Browse the repository at this point in the history
Creating Base Service Provider
  • Loading branch information
omniphx authored Jul 14, 2016
2 parents 1b15f4a + acd3858 commit d7492f6
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 119 deletions.
86 changes: 86 additions & 0 deletions src/Omniphx/Forrest/Providers/BaseServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

namespace Omniphx\Forrest\Providers;

use GuzzleHttp\Client;
use Illuminate\Support\ServiceProvider;
use Omniphx\Forrest\Providers\Laravel\LaravelCache;
use Omniphx\Forrest\Providers\Laravel\LaravelEvent;
use Omniphx\Forrest\Providers\Laravel\LaravelInput;
use Omniphx\Forrest\Providers\Laravel\LaravelRedirect;
use Omniphx\Forrest\Providers\Laravel\LaravelSession;

abstract class BaseServiceProvider extends ServiceProvider
{
/**
* Indicates if the application is laravel/lumen.
*
* @var bool
*/
protected $is_laravel = true;

/**
* Indicates if loading of the provider is deferred.
*
* @var bool
*/
protected $defer = false;

/**
* Returns the location of the package config file.
*
* @return string file location
*/
abstract protected function getConfigPath();

/**
* Bootstrap the application events.
*
* @return void
*/
public function boot()
{
if (method_exists($this, 'getConfigPath')) {
$this->publishes([
__DIR__.'/../../../../config/config.php' => $this->getConfigPath(),
]);
}
}

/**
* Register the service provider.
*
* @return void
*/
public function register()
{
$this->app->singleton('forrest', function ($app) {

// Config options
$settings = config('forrest');
$storageType = config('forrest.storage.type');
$authenticationType = config('forrest.authentication');

// Determine showing HTTP errors
$http_errors = $this->is_laravel ? true : false;

// Dependencies
$client = new Client(['http_errors' => $http_errors]);
$input = new LaravelInput();
$event = new LaravelEvent();
$redirect = new LaravelRedirect();

// Determine storage dependency
if ($storageType == 'cache') {
$storage = new LaravelCache(app('config'), app('cache'));
} else {
$storage = new LaravelSession(app('config'), app('session'));
}

// Class namespace
$forrest = "\\Omniphx\\Forrest\\Authentications\\$authenticationType";

return new $forrest($client, $event, $input, $redirect, $storage, $settings);
});
}
}
63 changes: 8 additions & 55 deletions src/Omniphx/Forrest/Providers/Laravel/ForrestServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,71 +2,24 @@

namespace Omniphx\Forrest\Providers\Laravel;

use GuzzleHttp\Client;
use Illuminate\Support\ServiceProvider;
use Omniphx\Forrest\Providers\BaseServiceProvider;

class ForrestServiceProvider extends ServiceProvider
class ForrestServiceProvider extends BaseServiceProvider
{
/**
* Indicates if loading of the provider is deferred.
* Indicates if the application is laravel/lumen.
*
* @var bool
*/
protected $defer = false;
protected $is_laravel = true;

/**
* Bootstrap the application events.
* Returns the location of the package config file.
*
* @return void
* @return string file location
*/
public function boot()
protected function getConfigPath()
{
$this->publishes([
__DIR__.'/../../../../config/config.php' => config_path('forrest.php'),
]);
}

/**
* Register the service provider.
*
* @return void
*/
public function register()
{
$this->app->singleton('forrest', function ($app) {

//Config options:
$settings = config('forrest');
$storageType = config('forrest.storage.type');
$authenticationType = config('forrest.authentication');

//Dependencies:
$client = new Client(['http_errors' => false]);
$input = new LaravelInput();
$event = new LaravelEvent();
$redirect = new LaravelRedirect();

//Determine storage dependency:
if ($storageType == 'cache') {
$storage = new LaravelCache(app('config'), app('cache'));
} else {
$storage = new LaravelSession(app('config'), app('session'));
}

//Class namespace:
$forrest = "\\Omniphx\\Forrest\\Authentications\\$authenticationType";

return new $forrest($client, $event, $input, $redirect, $storage, $settings);
});
}

/**
* Get the services provided by the provider.
*
* @return array
*/
public function provides()
{
return [];
return config_path('forrest.php');
}
}
71 changes: 7 additions & 64 deletions src/Omniphx/Forrest/Providers/Lumen/ForrestServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,80 +2,23 @@

namespace Omniphx\Forrest\Providers\Lumen;

use GuzzleHttp\Client;
use Illuminate\Support\ServiceProvider;
use Omniphx\Forrest\Providers\Laravel\LaravelCache;
use Omniphx\Forrest\Providers\Laravel\LaravelEvent;
use Omniphx\Forrest\Providers\Laravel\LaravelInput;
use Omniphx\Forrest\Providers\Laravel\LaravelRedirect;
use Omniphx\Forrest\Providers\Laravel\LaravelSession;
use Omniphx\Forrest\Providers\BaseServiceProvider;

class ForrestServiceProvider extends ServiceProvider
class ForrestServiceProvider extends BaseServiceProvider
{
/**
* Indicates if loading of the provider is deferred.
* Indicates if the application is laravel/lumen.
*
* @var bool
*/
protected $defer = false;
protected $is_laravel = false;

/**
* Bootstrap the application events.
* Returns the location of the package config file.
*
* @return void
* @return string file location
*/
public function boot()
{
$this->publishes([
__DIR__.'/../../../../config/config.php' => $this->configPath(),
]);
}

/**
* Register the service provider.
*
* @return void
*/
public function register()
{
$this->app->singleton('forrest', function ($app) {

//Config options:
$settings = config('forrest');
$storageType = config('forrest.storage.type');
$authenticationType = config('forrest.authentication');

//Dependencies:
$client = new Client();
$input = new LaravelInput();
$event = new LaravelEvent();
$redirect = new LaravelRedirect();

//Determine storage dependency:
if ($storageType == 'cache') {
$storage = new LaravelCache(app('config'), app('cache'));
} else {
$storage = new LaravelSession(app('config'), app('session'));
}

//Class namespace:
$forrest = "\\Omniphx\\Forrest\\Authentications\\$authenticationType";

return new $forrest($client, $event, $input, $redirect, $storage, $settings);
});
}

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

protected function configPath()
protected function getConfigPath()
{
return __DIR__.'/../config/forrest.php';
}
Expand Down

0 comments on commit d7492f6

Please sign in to comment.