Skip to content

Commit c913673

Browse files
committed
Properly configure TMDB Client
1 parent 8f9ba23 commit c913673

File tree

2 files changed

+46
-52
lines changed

2 files changed

+46
-52
lines changed

src/TmdbServiceProvider.php

+34-12
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,14 @@
77
namespace Tmdb\Laravel;
88

99
use Illuminate\Support\ServiceProvider;
10-
use Tmdb\Laravel\TmdbServiceProviderLaravel5;
10+
use Tmdb\Event\BeforeRequestEvent;
11+
use Tmdb\Event\Listener\Request\AcceptJsonRequestListener;
12+
use Tmdb\Event\Listener\Request\ApiTokenRequestListener;
13+
use Tmdb\Event\Listener\Request\ContentTypeJsonRequestListener;
14+
use Tmdb\Event\Listener\Request\UserAgentRequestListener;
15+
use Tmdb\Event\Listener\RequestListener;
16+
use Tmdb\Event\RequestEvent;
17+
use Tmdb\Laravel\TmdbServiceProviderLaravel;
1118
use Tmdb\Client;
1219
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
1320
use Symfony\Component\EventDispatcher\EventDispatcher;
@@ -76,20 +83,35 @@ public function register()
7683
// Setup default configurations for the Tmdb Client
7784
$this->app->singleton(Client::class, function() {
7885
$config = $this->provider->config();
79-
$options = $config['options'];
8086

81-
// Use an Event Dispatcher that uses the Laravel event dispatcher
82-
$options['event_dispatcher']['adapter'] = $this->app->make(EventDispatcherAdapter::class);
87+
$ed = $this->app->make(EventDispatcherAdapter::class);
88+
$client = new Client(
89+
[
90+
'api_token' => new ApiToken($config['api_key']),
91+
'event_dispatcher' =>
92+
[
93+
'adapter' => $ed
94+
],
95+
]
96+
);
97+
/**
98+
* Required event listeners and events to be registered with the PSR-14 Event Dispatcher.
99+
*/
100+
$requestListener = new RequestListener($client->getHttpClient(), $ed);
101+
$ed->addListener(RequestEvent::class, $requestListener);
83102

84-
// Register the client using the key and options from config
85-
$options['api_token'] = new ApiToken($config['api_key']);
86-
return new Client([$options]);
87-
});
103+
$apiTokenListener = new ApiTokenRequestListener($client->getToken());
104+
$ed->addListener(BeforeRequestEvent::class, $apiTokenListener);
105+
106+
$acceptJsonListener = new AcceptJsonRequestListener();
107+
$ed->addListener(BeforeRequestEvent::class, $acceptJsonListener);
108+
109+
$jsonContentTypeListener = new ContentTypeJsonRequestListener();
110+
$ed->addListener(BeforeRequestEvent::class, $jsonContentTypeListener);
88111

89-
// bind the configuration (used by the image helper)
90-
$this->app->bind(Configuration::class, function() {
91-
$configuration = $this->app->make(ConfigurationRepository::class);
92-
return $configuration->load();
112+
$userAgentListener = new UserAgentRequestListener();
113+
$ed->addListener(BeforeRequestEvent::class, $userAgentListener);
114+
return $client;
93115
});
94116
}
95117

src/config/tmdb.php

+12-40
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,12 @@
1-
<?php
2-
/**
3-
* @package php-tmdb\laravel
4-
* @author Mark Redeman <markredeman@gmail.com>
5-
* @copyright (c) 2014, Mark Redeman
6-
*/
7-
return [
8-
/*
9-
* Api key
10-
*/
11-
'api_key' => '',
12-
13-
/**
14-
* Client options
15-
*/
16-
'options' => [
17-
/**
18-
* Use https
19-
*/
20-
'secure' => true,
21-
22-
/*
23-
* Cache
24-
*/
25-
'cache' => [
26-
'enabled' => true,
27-
// Keep the path empty or remove it entirely to default to storage/tmdb
28-
'path' => storage_path('tmdb')
29-
],
30-
31-
/*
32-
* Log
33-
*/
34-
'log' => [
35-
'enabled' => true,
36-
// Keep the path empty or remove it entirely to default to storage/logs/tmdb.log
37-
'path' => storage_path('logs/tmdb.log')
38-
]
39-
],
40-
];
1+
<?php
2+
/**
3+
* @package php-tmdb\laravel
4+
* @author Mark Redeman <markredeman@gmail.com>
5+
* @copyright (c) 2014, Mark Redeman
6+
*/
7+
return [
8+
/*
9+
* Api key
10+
*/
11+
'api_key' => '',
12+
];

0 commit comments

Comments
 (0)