|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @package php-tmdb\laravel |
|
4
|
|
|
* @author Mark Redeman <[email protected]> |
|
5
|
|
|
* @copyright (c) 2014, Mark Redeman |
|
6
|
|
|
*/ |
|
7
|
|
|
namespace Tmdb\Laravel; |
|
8
|
|
|
|
|
9
|
|
|
use Illuminate\Support\ServiceProvider; |
|
10
|
|
|
use Tmdb\Laravel\TmdbServiceProviderLaravel4; |
|
11
|
|
|
use Tmdb\Laravel\TmdbServiceProviderLaravel5; |
|
12
|
|
|
use Tmdb\ApiToken; |
|
13
|
|
|
use Tmdb\Client; |
|
14
|
|
|
|
|
15
|
|
|
class TmdbServiceProvider extends ServiceProvider |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* Indicates if loading of the provider is deferred. |
|
19
|
|
|
* |
|
20
|
|
|
* @var bool |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $defer = false; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Actual provider |
|
26
|
|
|
* |
|
27
|
|
|
* @var \Illuminate\Support\ServiceProvider |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $provider; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Construct the TMDB service provider |
|
33
|
|
|
*/ |
|
34
|
|
|
public function __construct() |
|
35
|
|
|
{ |
|
36
|
|
|
// Call the parent constructor with all provided arguments |
|
37
|
|
|
$arguments = func_get_args(); |
|
38
|
|
|
call_user_func_array( |
|
39
|
|
|
[$this, 'parent::' . __FUNCTION__], |
|
40
|
|
|
$arguments |
|
41
|
|
|
); |
|
42
|
|
|
|
|
43
|
|
|
$this->registerProvider(); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Bootstrap the application events. |
|
48
|
|
|
* |
|
49
|
|
|
* @return void |
|
50
|
|
|
*/ |
|
51
|
|
|
public function boot() |
|
52
|
|
|
{ |
|
53
|
|
|
return $this->provider->boot(); |
|
|
|
|
|
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Register the service provider. |
|
58
|
|
|
* |
|
59
|
|
|
* @return void |
|
60
|
|
|
*/ |
|
61
|
|
|
public function register() |
|
62
|
|
|
{ |
|
63
|
|
|
// Configure any bindings that are version dependent |
|
64
|
|
|
$this->provider->register(); |
|
65
|
|
|
|
|
66
|
|
|
// Let the IoC container be able to make a Symfony event dispatcher |
|
67
|
|
|
$this->app->bind( |
|
68
|
|
|
'Symfony\Component\EventDispatcher\EventDispatcherInterface', |
|
69
|
|
|
'Symfony\Component\EventDispatcher\EventDispatcher' |
|
70
|
|
|
); |
|
71
|
|
|
|
|
72
|
|
|
// Setup default configurations for the Tmdb Client |
|
73
|
|
|
$this->app->singleton('Tmdb\Client', function() { |
|
74
|
|
|
$config = $this->provider->config(); |
|
|
|
|
|
|
75
|
|
|
$options = $config['options']; |
|
76
|
|
|
|
|
77
|
|
|
// Use an Event Dispatcher that uses the Laravel event dispatcher |
|
78
|
|
|
$options['event_dispatcher'] = $this->app->make('Tmdb\Laravel\Adapters\EventDispatcherAdapter'); |
|
79
|
|
|
|
|
80
|
|
|
// Register the client using the key and options from config |
|
81
|
|
|
$token = new ApiToken($config['api_key']); |
|
82
|
|
|
return new Client($token, $options); |
|
83
|
|
|
}); |
|
84
|
|
|
|
|
85
|
|
|
// bind the configuration (used by the image helper) |
|
86
|
|
|
$this->app->bind('Tmdb\Model\Configuration', function() { |
|
87
|
|
|
$configuration = $this->app->make('Tmdb\Repository\ConfigurationRepository'); |
|
88
|
|
|
return $configuration->load(); |
|
89
|
|
|
}); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Register the ServiceProvider according to Laravel version |
|
94
|
|
|
* |
|
95
|
|
|
* @return \Tmdb\Laravel\Provider\ProviderInterface |
|
96
|
|
|
*/ |
|
97
|
|
|
private function registerProvider() |
|
98
|
|
|
{ |
|
99
|
|
|
$app = $this->app; |
|
100
|
|
|
|
|
101
|
|
|
// Pick the correct service provider for the current verison of Laravel |
|
102
|
|
|
$this->provider = (version_compare($app::VERSION, '5.0', '<')) |
|
103
|
|
|
? new TmdbServiceProviderLaravel4($app) |
|
104
|
|
|
: new TmdbServiceProviderLaravel5($app); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Get the services provided by the provider. |
|
109
|
|
|
* |
|
110
|
|
|
* @return array |
|
111
|
|
|
*/ |
|
112
|
|
|
public function provides() |
|
113
|
|
|
{ |
|
114
|
|
|
return array('tmdb'); |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: