diff --git a/README.md b/README.md index 8e518b2..4bdb703 100644 --- a/README.md +++ b/README.md @@ -66,3 +66,15 @@ class ProviderNameExtendSocialite ## Overriding a Built-in Provider You can easily override a built-in `laravel/socialite` provider by creating a new one with exactly the same name (i.e. 'facebook'). + + +## Dynamically Passing a Config + +You can dynamically pass a config by using: +``` +$key = 'SocialiteProviders.config.'; +$config = new \SocialiteProviders\Manager\Config('key', 'secret', 'callbackUri'); +$this->app->instance($key, $config) +``` + +**You must call this before you run any Socialite methods.** diff --git a/src/Config.php b/src/Config.php index 9b0bd2a..850210d 100644 --- a/src/Config.php +++ b/src/Config.php @@ -10,13 +10,13 @@ class Config implements Contracts\Config public function __construct($key, $secret, $callbackUri) { - $this->config = [ - 'client_id' => $key, - 'client_secret' => $secret, - 'redirect' => $callbackUri - ]; + $this->config = [ + 'client_id' => $key, + 'client_secret' => $secret, + 'redirect' => $callbackUri, + ]; } - + /** * @return array */ diff --git a/src/SocialiteWasCalled.php b/src/SocialiteWasCalled.php index da73b21..fedac5f 100644 --- a/src/SocialiteWasCalled.php +++ b/src/SocialiteWasCalled.php @@ -4,6 +4,7 @@ use Illuminate\Contracts\Foundation\Application as LaravelApp; use Laravel\Socialite\SocialiteManager; +use SocialiteProviders\Manager\Contracts; class SocialiteWasCalled { @@ -23,7 +24,8 @@ public function __construct(LaravelApp $app) /** * @param string $providerName 'meetup' * @param string $providerClass 'Your\Name\Space\ClassNameProvider' must extend - * either Laravel\Socialite\Two\AbstractProvider or Laravel\Socialite\One\AbstractProvider + * either Laravel\Socialite\Two\AbstractProvider or + * Laravel\Socialite\One\AbstractProvider * @param string $oauth1Server 'Your\Name\Space\ClassNameServer' must extend League\OAuth1\Client\Server\Server * * @throws InvalidArgumentException @@ -31,7 +33,7 @@ public function __construct(LaravelApp $app) public function extendSocialite($providerName, $providerClass, $oauth1Server = null) { /** @var SocialiteManager $socialite */ - $socialite = $this->app->make('Laravel\Socialite\Contracts\Factory'); + $socialite = $this->app->make(\Laravel\Socialite\Contracts\Factory::class); $provider = $this->buildProvider($socialite, $providerName, $providerClass, $oauth1Server); $socialite->extend( $providerName, @@ -43,9 +45,9 @@ function () use ($provider) { /** * @param SocialiteManager $socialite - * @param $providerName - * @param string $providerClass - * @param null|string $oauth1Server + * @param $providerName + * @param string $providerClass + * @param null|string $oauth1Server * * @return \Laravel\Socialite\One\AbstractProvider|\Laravel\Socialite\Two\AbstractProvider */ @@ -70,8 +72,8 @@ protected function buildProvider(SocialiteManager $socialite, $providerName, $pr */ protected function buildOAuth1Provider($providerClass, $oauth1Server, array $config) { - $this->classExtends($providerClass, 'Laravel\Socialite\One\AbstractProvider'); - $this->classExtends($oauth1Server, 'League\OAuth1\Client\Server\Server'); + $this->classExtends($providerClass, \Laravel\Socialite\One\AbstractProvider::class); + $this->classExtends($oauth1Server, \League\OAuth1\Client\Server\Server::class); return new $providerClass( $this->app->offsetGet('request'), new $oauth1Server($config) @@ -89,7 +91,7 @@ protected function buildOAuth1Provider($providerClass, $oauth1Server, array $con */ protected function buildOAuth2Provider(SocialiteManager $socialite, $providerClass, array $config) { - $this->classExtends($providerClass, 'Laravel\Socialite\Two\AbstractProvider'); + $this->classExtends($providerClass, \Laravel\Socialite\Two\AbstractProvider::class); return $socialite->buildProvider($providerClass, $config); } @@ -101,7 +103,18 @@ protected function buildOAuth2Provider(SocialiteManager $socialite, $providerCla */ protected function getConfig($providerName) { - return $this->app->offsetGet('config')['services.'.$providerName]; + try { + /** @var Contracts\Config $config */ + $config = $this->app->make('SocialiteProviders.config.'.$providerName); + + if (!($config instanceof Contracts\Config)) { + throw new InvalidArgumentException('Config class does not implement config contract'); + } + + return $config->get(); + } catch (\ReflectionException $e) { + return $this->app->offsetGet('config')['services.'.$providerName]; + } } /** diff --git a/tests/ConfigTest.php b/tests/ConfigTest.php index 3dcfa50..ca689e1 100644 --- a/tests/ConfigTest.php +++ b/tests/ConfigTest.php @@ -20,10 +20,10 @@ public function it_returns_a_config_array() $secret = 'secret'; $callbackUri = 'uri'; - $result = [ - 'client_id' => $key, + $result = [ + 'client_id' => $key, 'client_secret' => $secret, - 'redirect' => $callbackUri + 'redirect' => $callbackUri, ]; $config = new Config($key, $secret, $callbackUri); diff --git a/tests/ManagerTestTrait.php b/tests/ManagerTestTrait.php index 9687bb9..eab938f 100644 --- a/tests/ManagerTestTrait.php +++ b/tests/ManagerTestTrait.php @@ -17,9 +17,9 @@ protected function expectManagerInvalidArgumentException() protected function config() { return [ - 'client_id' => 'test', + 'client_id' => 'test', 'client_secret' => 'test', - 'redirect' => 'test', + 'redirect' => 'test', ]; } @@ -31,8 +31,8 @@ protected function config() protected function oauth1FormattedConfig(array $config) { return [ - 'identifier' => $config['client_id'], - 'secret' => $config['client_secret'], + 'identifier' => $config['client_id'], + 'secret' => $config['client_secret'], 'callback_uri' => $config['redirect'], ]; } @@ -42,7 +42,7 @@ protected function oauth1FormattedConfig(array $config) */ protected function appMock() { - return m::mock('Illuminate\Contracts\Foundation\Application'); + return m::mock(\Illuminate\Contracts\Foundation\Application::class); } /** @@ -50,7 +50,7 @@ protected function appMock() */ protected function socialiteMock() { - return m::mock('Laravel\Socialite\SocialiteManager'); + return m::mock(\Laravel\Socialite\SocialiteManager::class); } protected function oauth2ProviderStub() @@ -145,7 +145,7 @@ protected function providerConfigKey($providerName) */ protected function buildRequest() { - return m::mock('\Illuminate\Http\Request'); + return m::mock(\Illuminate\Http\Request::class); } /** diff --git a/tests/OAuth1ProviderTest.php b/tests/OAuth1ProviderTest.php index a771c89..d00e2c4 100644 --- a/tests/OAuth1ProviderTest.php +++ b/tests/OAuth1ProviderTest.php @@ -36,8 +36,15 @@ function ($closure) { ] ); + $config = new Config( + $this->config()['client_id'], + $this->config()['client_secret'], + $this->config()['redirect'] + ); + $app = $this->appMock(); - $app->shouldReceive('make')->andReturn($socialite); + $app->shouldReceive('make')->with(\Laravel\Socialite\Contracts\Factory::class)->andReturn($socialite); + $app->shouldReceive('make')->with('SocialiteProviders.config.'.$providerName)->andReturn($config); $app->shouldReceive('offsetGet')->with('request')->andReturn($this->buildRequest()); $app->shouldReceive('offsetGet')->with('config')->andReturn($this->servicesArray($providerName)); diff --git a/tests/OAuth2ProviderTest.php b/tests/OAuth2ProviderTest.php index 61d1adf..3775c3d 100644 --- a/tests/OAuth2ProviderTest.php +++ b/tests/OAuth2ProviderTest.php @@ -21,6 +21,38 @@ public function it_passes() $this->assertTrue(true); } + /** + * @test + * @expectedException \SocialiteProviders\Manager\InvalidArgumentException + */ + public function it_should_blow_up_if_the_config_passed_does_not_implement_config_contract() + { + $providerName = 'bar'; + + $socialite = $this->socialiteMock(); + $socialite->shouldReceive('buildProvider')->withArgs([$this->oauth2ProviderStubName(), $this->config()]) + ->andReturn($this->oauth2ProviderStub()); + $socialite->shouldReceive('extend')->withArgs( + [ + $providerName, + m::on( + function ($closure) { + $this->assertInstanceOf($this->oauth2ProviderStubName(), $closure()); + + return is_callable($closure); + } + ), + ] + ); + + $app = $this->appMock(); + $app->shouldReceive('make')->with(\Laravel\Socialite\Contracts\Factory::class)->andReturn($socialite); + $app->shouldReceive('make')->with('SocialiteProviders.config.'.$providerName)->andReturn('foobar'); + + $s = new SocialiteWasCalled($app); + $s->extendSocialite($providerName, $this->oauth2ProviderStubName()); + } + /** * @test */ @@ -44,8 +76,15 @@ function ($closure) { ] ); + $config = new Config( + $this->config()['client_id'], + $this->config()['client_secret'], + $this->config()['redirect'] + ); + $app = $this->appMock(); - $app->shouldReceive('make')->andReturn($socialite); + $app->shouldReceive('make')->with(\Laravel\Socialite\Contracts\Factory::class)->andReturn($socialite); + $app->shouldReceive('make')->with('SocialiteProviders.config.'.$providerName)->andReturn($config); $app->shouldReceive('offsetGet')->andReturn($this->servicesArray($providerName)); $s = new SocialiteWasCalled($app);