Skip to content

Commit

Permalink
enhance service provider to be deferrable with better DI support (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
klimov-paul authored and Shivella committed Jan 28, 2020
1 parent 31e96ed commit 86e8220
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 19 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"require": {
"php": ">=7.1",
"ext-json": "*",
"illuminate/support": "^5.1 || ^6.0",
"illuminate/support": "^5.8 || ^6.0",
"guzzlehttp/guzzle": "~6.0"
},
"require-dev": {
Expand Down
49 changes: 31 additions & 18 deletions src/Shivella/Bitly/BitlyServiceProvider.php
Original file line number Diff line number Diff line change
@@ -1,44 +1,57 @@
<?php
/*
* (c) Wessel Strengholt <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* (c) Wessel Strengholt <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Shivella\Bitly;

use GuzzleHttp\Client;
use Illuminate\Support\ServiceProvider as AppServiceProvider;
use Illuminate\Contracts\Support\DeferrableProvider;
use Illuminate\Support\ServiceProvider;
use Shivella\Bitly\Client\BitlyClient;

/**
* Class BitlyServiceProvider
* BitlyServiceProvider registers Bitly client as an application service.
*/
class BitlyServiceProvider extends AppServiceProvider
class BitlyServiceProvider extends ServiceProvider implements DeferrableProvider
{
/**
* Indicates if loading of the provider is deferred.
*
* @var bool
*/
protected $defer = false;

/**
* {@inheritdoc}
*/
public function register()
{
$this->app->singleton('bitly', function () {
return new BitlyClient(new Client(), config('bitly.accesstoken', ''));
return new BitlyClient(new Client(), $this->app->make('config')->get('bitly.accesstoken', ''));
});

$this->app->bind(BitlyClient::class, 'bitly');
}

/**
* {@inheritdoc}
*/
public function boot()
{
$this->publishes([__DIR__ . '/config/bitly.php' => config_path('bitly.php')]);
if ( ! $this->app->runningInConsole()) {
return;
}

$configPath = $this->app->make('path.config');

$this->publishes([__DIR__ . '/config/bitly.php' => $configPath.'/bitly.php']);
}

/**
* {@inheritdoc}
*/
public function provides()
{
return [
BitlyClient::class,
'bitly'
];
}
}

0 comments on commit 86e8220

Please sign in to comment.