1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Basis\Provider; |
4
|
|
|
|
5
|
|
|
use Basis\Application; |
6
|
|
|
use Basis\Cache; |
7
|
|
|
use Basis\Config; |
8
|
|
|
use Basis\Context; |
9
|
|
|
use Basis\Converter; |
10
|
|
|
use Basis\Dispatcher; |
11
|
|
|
use Basis\Runner; |
12
|
|
|
use Basis\Filesystem; |
13
|
|
|
use Basis\Framework; |
14
|
|
|
use Basis\Http; |
15
|
|
|
use Basis\Lock; |
16
|
|
|
use Basis\Service; |
17
|
|
|
use GuzzleHttp\Client as GuzzleHttpClient; |
18
|
|
|
use League\Container\ServiceProvider\AbstractServiceProvider; |
19
|
|
|
use Predis\Client as PredisClient; |
20
|
|
|
|
21
|
|
|
class CoreProvider extends AbstractServiceProvider |
22
|
|
|
{ |
23
|
|
|
protected $provides = [ |
24
|
|
|
Cache::class, |
25
|
|
|
Config::class, |
26
|
|
|
Converter::class, |
27
|
|
|
Dispatcher::class, |
28
|
|
|
Framework::class, |
29
|
|
|
Http::class, |
30
|
|
|
Lock::class, |
31
|
|
|
Runner::class, |
32
|
|
|
]; |
33
|
|
|
|
34
|
52 |
|
public function register() |
35
|
|
|
{ |
36
|
|
|
$this->getContainer()->share(Config::class, function () { |
|
|
|
|
37
|
|
|
$app = $this->getContainer()->get(Application::class); |
38
|
|
|
$framework = $this->getContainer()->get(Framework::class); |
39
|
|
|
$fs = $this->getContainer()->get(Filesystem::class); |
40
|
|
|
$converter = $this->getContainer()->get(Converter::class); |
41
|
|
|
return new Config($app, $framework, $fs, $converter); |
42
|
|
|
}); |
43
|
|
|
|
44
|
|
|
$this->getContainer()->share(Cache::class, function () { |
|
|
|
|
45
|
|
|
$fs = $this->getContainer()->get(Filesystem::class); |
46
|
|
|
$converter = $this->getContainer()->get(Converter::class); |
47
|
|
|
return new Cache($fs, $converter); |
48
|
|
|
}); |
49
|
|
|
|
50
|
|
|
$this->getContainer()->share(Lock::class, function () { |
|
|
|
|
51
|
|
|
$redis = $this->getContainer()->get(PredisClient::class); |
52
|
|
|
return new Lock($redis); |
53
|
|
|
}); |
54
|
|
|
|
55
|
|
|
$this->getContainer()->share(Converter::class, function () { |
|
|
|
|
56
|
|
|
return new Converter(); |
57
|
|
|
}); |
58
|
|
|
|
59
|
|
|
$this->getContainer()->share(Dispatcher::class, function () { |
|
|
|
|
60
|
52 |
|
$app = $this->getContainer()->get(Application::class); |
61
|
52 |
|
return new Dispatcher($app); |
62
|
|
|
}); |
63
|
|
|
|
64
|
|
|
$this->getContainer()->share(Framework::class, function () { |
|
|
|
|
65
|
|
|
return new Framework($this->getContainer(), dirname(dirname(__DIR__))); |
|
|
|
|
66
|
|
|
}); |
67
|
|
|
|
68
|
|
|
$this->getContainer()->share(Http::class, function () { |
|
|
|
|
69
|
3 |
|
return new Http($this->getContainer()->get(Application::class)); |
70
|
|
|
}); |
71
|
|
|
|
72
|
|
|
$this->getContainer()->share(Runner::class, function () { |
|
|
|
|
73
|
52 |
|
return new Runner($this->getContainer()->get(Application::class)); |
74
|
|
|
}); |
75
|
|
|
|
76
|
|
|
$this->getContainer()->share(Context::class, function () { |
|
|
|
|
77
|
2 |
|
return new Context($this->getContainer()->get(Application::class)); |
|
|
|
|
78
|
|
|
}); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
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 implementation 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 interface: