Skip to content

Commit

Permalink
Refactor service provider bindings.
Browse files Browse the repository at this point in the history
  • Loading branch information
irazasyed committed Sep 10, 2020
1 parent 4a3279c commit dfa4324
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 54 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
],
"require": {
"php": "^7.2",
"guzzlehttp/guzzle": "^6.1 || ^7.0",
"guzzlehttp/guzzle": "^6.3 || ^7.0.1",
"guzzlehttp/psr7": "^1.3",
"illuminate/support": "^5.5 || ^6.0 || ^7.0 || ^8.0",
"league/event": "^2.1"
Expand Down
3 changes: 2 additions & 1 deletion src/Laravel/Facades/Telegram.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Telegram\Bot\Laravel\Facades;

use Telegram\Bot\BotsManager;
use Illuminate\Support\Facades\Facade;

/**
Expand All @@ -16,6 +17,6 @@ class Telegram extends Facade
*/
protected static function getFacadeAccessor()
{
return 'telegram';
return BotsManager::class;
}
}
95 changes: 43 additions & 52 deletions src/Laravel/TelegramServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,97 +2,88 @@

namespace Telegram\Bot\Laravel;

use Illuminate\Contracts\Container\Container as Application;
use Illuminate\Foundation\Application as LaravelApplication;
use Illuminate\Support\ServiceProvider;
use Laravel\Lumen\Application as LumenApplication;
use Telegram\Bot\Api;
use Telegram\Bot\BotsManager;
use Illuminate\Support\ServiceProvider;
use Telegram\Bot\Laravel\Artisan\WebhookCommand;
use Laravel\Lumen\Application as LumenApplication;
use Illuminate\Foundation\Application as LaravelApplication;

/**
* Class TelegramServiceProvider.
*/
class TelegramServiceProvider extends ServiceProvider
{
/** @var bool Indicates if loading of the provider is deferred. */
protected $defer = true;

/** Boot the service provider. */
public function boot()
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
$this->setupConfig($this->app);
$this->configure();
$this->offerPublishing();
$this->registerBindings();
$this->registerCommands();
}

/**
* Setup the config.
*
* @param \Illuminate\Contracts\Container\Container $app
* Setup the configuration.
*/
protected function setupConfig(Application $app)
protected function configure()
{
$source = __DIR__.'/config/telegram.php';

if ($app instanceof LaravelApplication && $app->runningInConsole()) {
$this->publishes([$source => config_path('telegram.php')]);
} elseif ($app instanceof LumenApplication) {
$app->configure('telegram');
}

$this->mergeConfigFrom($source, 'telegram');
$this->mergeConfigFrom(__DIR__.'/config/telegram.php', 'telegram');
}

/**
* Register the service provider.
* Setup the resource publishing groups.
*/
public function register()
protected function offerPublishing()
{
$this->registerManager($this->app);
$this->registerBindings($this->app);
$this->commands('telegram.bot.commands.webhook');
if ($this->app instanceof LaravelApplication && $this->app->runningInConsole()) {
$this->publishes([
__DIR__.'/config/telegram.php' => config_path('telegram.php'),
], 'telegram-config');
} elseif ($this->app instanceof LumenApplication) {
$this->app->configure('telegram');
}
}

/**
* Register the manager class.
*
* @param \Illuminate\Contracts\Container\Container $app
* Register bindings in the container.
*/
protected function registerManager(Application $app)
protected function registerBindings()
{
$app->singleton('telegram', function ($app) {
$config = app('config')->get('telegram');

return (new BotsManager($config))->setContainer($app);
$this->app->bind(BotsManager::class, static function ($app) {
return (new BotsManager(config('telegram')))->setContainer($app);
});
$this->app->alias(BotsManager::class, 'telegram');

$app->alias('telegram', BotsManager::class);
$this->app->bind(Api::class, static function ($app) {
return $app[BotsManager::class]->bot();
});
$this->app->alias(Api::class, 'telegram.bot');
}

/**
* Register the bindings.
*
* @param \Illuminate\Contracts\Container\Container $app
* Register the Artisan commands.
*/
protected function registerBindings(Application $app)
protected function registerCommands()
{
$app->bind('telegram.bot', function ($app) {
$manager = $app['telegram'];

return $manager->bot();
});

$app->bind('telegram.bot.commands.webhook', WebhookCommand::class);

$app->alias('telegram.bot', Api::class);
if ($this->app->runningInConsole()) {
$this->commands([
WebhookCommand::class,
]);
}
}

/**
* Get the services provided by the provider.
*
* @return array
*/
public function provides(): array
public function provides()
{
return ['telegram', 'telegram.bot', BotsManager::class, Api::class];
return [BotsManager::class, Api::class, 'telegram', 'telegram.bot'];
}
}

0 comments on commit dfa4324

Please sign in to comment.