diff --git a/src/Omniphx/Forrest/Providers/BaseServiceProvider.php b/src/Omniphx/Forrest/Providers/BaseServiceProvider.php new file mode 100644 index 0000000..340a852 --- /dev/null +++ b/src/Omniphx/Forrest/Providers/BaseServiceProvider.php @@ -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); + }); + } +} diff --git a/src/Omniphx/Forrest/Providers/Laravel/ForrestServiceProvider.php b/src/Omniphx/Forrest/Providers/Laravel/ForrestServiceProvider.php index 36f2256..55b4381 100644 --- a/src/Omniphx/Forrest/Providers/Laravel/ForrestServiceProvider.php +++ b/src/Omniphx/Forrest/Providers/Laravel/ForrestServiceProvider.php @@ -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'); } } diff --git a/src/Omniphx/Forrest/Providers/Lumen/ForrestServiceProvider.php b/src/Omniphx/Forrest/Providers/Lumen/ForrestServiceProvider.php index 4b8e582..a7328a4 100644 --- a/src/Omniphx/Forrest/Providers/Lumen/ForrestServiceProvider.php +++ b/src/Omniphx/Forrest/Providers/Lumen/ForrestServiceProvider.php @@ -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'; }