|
7 | 7 | namespace Tmdb\Laravel;
|
8 | 8 |
|
9 | 9 | 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; |
11 | 18 | use Tmdb\Client;
|
12 | 19 | use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
13 | 20 | use Symfony\Component\EventDispatcher\EventDispatcher;
|
@@ -76,20 +83,35 @@ public function register()
|
76 | 83 | // Setup default configurations for the Tmdb Client
|
77 | 84 | $this->app->singleton(Client::class, function() {
|
78 | 85 | $config = $this->provider->config();
|
79 |
| - $options = $config['options']; |
80 | 86 |
|
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); |
83 | 102 |
|
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); |
88 | 111 |
|
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; |
93 | 115 | });
|
94 | 116 | }
|
95 | 117 |
|
|
0 commit comments