-
-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #106 from tabirkeland/refactoring
Creating Base Service Provider
- Loading branch information
Showing
3 changed files
with
101 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters