Skip to content

Commit

Permalink
Properly configure TMDB Client
Browse files Browse the repository at this point in the history
  • Loading branch information
DariusIII committed Jan 17, 2021
1 parent 8f9ba23 commit c913673
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 52 deletions.
46 changes: 34 additions & 12 deletions src/TmdbServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,14 @@
namespace Tmdb\Laravel;

use Illuminate\Support\ServiceProvider;
use Tmdb\Laravel\TmdbServiceProviderLaravel5;
use Tmdb\Event\BeforeRequestEvent;
use Tmdb\Event\Listener\Request\AcceptJsonRequestListener;
use Tmdb\Event\Listener\Request\ApiTokenRequestListener;
use Tmdb\Event\Listener\Request\ContentTypeJsonRequestListener;
use Tmdb\Event\Listener\Request\UserAgentRequestListener;
use Tmdb\Event\Listener\RequestListener;
use Tmdb\Event\RequestEvent;
use Tmdb\Laravel\TmdbServiceProviderLaravel;
use Tmdb\Client;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\EventDispatcher;
Expand Down Expand Up @@ -76,20 +83,35 @@ public function register()
// Setup default configurations for the Tmdb Client
$this->app->singleton(Client::class, function() {
$config = $this->provider->config();
$options = $config['options'];

// Use an Event Dispatcher that uses the Laravel event dispatcher
$options['event_dispatcher']['adapter'] = $this->app->make(EventDispatcherAdapter::class);
$ed = $this->app->make(EventDispatcherAdapter::class);
$client = new Client(
[
'api_token' => new ApiToken($config['api_key']),
'event_dispatcher' =>
[
'adapter' => $ed
],
]
);
/**
* Required event listeners and events to be registered with the PSR-14 Event Dispatcher.
*/
$requestListener = new RequestListener($client->getHttpClient(), $ed);
$ed->addListener(RequestEvent::class, $requestListener);

// Register the client using the key and options from config
$options['api_token'] = new ApiToken($config['api_key']);
return new Client([$options]);
});
$apiTokenListener = new ApiTokenRequestListener($client->getToken());
$ed->addListener(BeforeRequestEvent::class, $apiTokenListener);

$acceptJsonListener = new AcceptJsonRequestListener();
$ed->addListener(BeforeRequestEvent::class, $acceptJsonListener);

$jsonContentTypeListener = new ContentTypeJsonRequestListener();
$ed->addListener(BeforeRequestEvent::class, $jsonContentTypeListener);

// bind the configuration (used by the image helper)
$this->app->bind(Configuration::class, function() {
$configuration = $this->app->make(ConfigurationRepository::class);
return $configuration->load();
$userAgentListener = new UserAgentRequestListener();
$ed->addListener(BeforeRequestEvent::class, $userAgentListener);
return $client;
});
}

Expand Down
52 changes: 12 additions & 40 deletions src/config/tmdb.php
Original file line number Diff line number Diff line change
@@ -1,40 +1,12 @@
<?php
/**
* @package php-tmdb\laravel
* @author Mark Redeman <markredeman@gmail.com>
* @copyright (c) 2014, Mark Redeman
*/
return [
/*
* Api key
*/
'api_key' => '',

/**
* Client options
*/
'options' => [
/**
* Use https
*/
'secure' => true,

/*
* Cache
*/
'cache' => [
'enabled' => true,
// Keep the path empty or remove it entirely to default to storage/tmdb
'path' => storage_path('tmdb')
],

/*
* Log
*/
'log' => [
'enabled' => true,
// Keep the path empty or remove it entirely to default to storage/logs/tmdb.log
'path' => storage_path('logs/tmdb.log')
]
],
];
<?php
/**
* @package php-tmdb\laravel
* @author Mark Redeman <markredeman@gmail.com>
* @copyright (c) 2014, Mark Redeman
*/
return [
/*
* Api key
*/
'api_key' => '',
];

0 comments on commit c913673

Please sign in to comment.