Skip to content

Commit

Permalink
Use singleton
Browse files Browse the repository at this point in the history
  • Loading branch information
vinkla committed Dec 22, 2015
1 parent e09c4cb commit 583d720
Showing 1 changed file with 21 additions and 16 deletions.
37 changes: 21 additions & 16 deletions src/Storage/FluentStorageServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace LucaDegasperi\OAuth2Server\Storage;

use Illuminate\Contracts\Foundation\Application;
use Illuminate\Support\ServiceProvider;
use League\OAuth2\Server\Storage\AccessTokenInterface;
use League\OAuth2\Server\Storage\AuthCodeInterface;
Expand Down Expand Up @@ -43,49 +44,51 @@ public function boot()
*/
public function register()
{
$this->registerStorageBindings();
$this->registerInterfaceBindings();
$this->registerStorageBindings($this->app);
$this->registerInterfaceBindings($this->app);
}

/**
* Bind the storage implementations to the IoC container.
*
* @param \Illuminate\Contracts\Foundation\Application $app
*
* @return void
*/
public function registerStorageBindings()
public function registerStorageBindings(Application $app)
{
$provider = $this;

$this->app->bindShared(FluentAccessToken::class, function () use ($provider) {
$app->singleton(FluentAccessToken::class, function () use ($provider) {
$storage = new FluentAccessToken($provider->app['db']);
$storage->setConnectionName($provider->getConnectionName());

return $storage;
});

$this->app->bindShared(FluentAuthCode::class, function () use ($provider) {
$app->singleton(FluentAuthCode::class, function () use ($provider) {
$storage = new FluentAuthCode($provider->app['db']);
$storage->setConnectionName($provider->getConnectionName());

return $storage;
});

$this->app->bindShared(FluentClient::class, function ($app) use ($provider) {
$app->singleton(FluentClient::class, function ($app) use ($provider) {
$limitClientsToGrants = $app['config']->get('oauth2.limit_clients_to_grants');
$storage = new FluentClient($provider->app['db'], $limitClientsToGrants);
$storage->setConnectionName($provider->getConnectionName());

return $storage;
});

$this->app->bindShared(FluentRefreshToken::class, function () use ($provider) {
$app->singleton(FluentRefreshToken::class, function () use ($provider) {
$storage = new FluentRefreshToken($provider->app['db']);
$storage->setConnectionName($provider->getConnectionName());

return $storage;
});

$this->app->bindShared(FluentScope::class, function ($app) use ($provider) {
$app->singleton(FluentScope::class, function ($app) use ($provider) {
$limitClientsToScopes = $app['config']->get('oauth2.limit_clients_to_scopes');
$limitScopesToGrants = $app['config']->get('oauth2.limit_scopes_to_grants');
$storage = new FluentScope($provider->app['db'], $limitClientsToScopes, $limitScopesToGrants);
Expand All @@ -94,7 +97,7 @@ public function registerStorageBindings()
return $storage;
});

$this->app->bindShared(FluentSession::class, function () use ($provider) {
$app->singleton(FluentSession::class, function () use ($provider) {
$storage = new FluentSession($provider->app['db']);
$storage->setConnectionName($provider->getConnectionName());

Expand All @@ -105,16 +108,18 @@ public function registerStorageBindings()
/**
* Bind the interfaces to their implementations.
*
* @param \Illuminate\Contracts\Foundation\Application $app
*
* @return void
*/
public function registerInterfaceBindings()
public function registerInterfaceBindings(Application $app)
{
$this->app->bind(ClientInterface::class, FluentClient::class);
$this->app->bind(ScopeInterface::class, FluentScope::class);
$this->app->bind(SessionInterface::class, FluentSession::class);
$this->app->bind(AuthCodeInterface::class, FluentAuthCode::class);
$this->app->bind(AccessTokenInterface::class, FluentAccessToken::class);
$this->app->bind(RefreshTokenInterface::class, FluentRefreshToken::class);
$app->bind(ClientInterface::class, FluentClient::class);
$app->bind(ScopeInterface::class, FluentScope::class);
$app->bind(SessionInterface::class, FluentSession::class);
$app->bind(AuthCodeInterface::class, FluentAuthCode::class);
$app->bind(AccessTokenInterface::class, FluentAccessToken::class);
$app->bind(RefreshTokenInterface::class, FluentRefreshToken::class);
}

/**
Expand Down

0 comments on commit 583d720

Please sign in to comment.