1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* The MIT License (MIT) |
7
|
|
|
* |
8
|
|
|
* Copyright (c) 2014-2017 Spomky-Labs |
9
|
|
|
* |
10
|
|
|
* This software may be modified and distributed under the terms |
11
|
|
|
* of the MIT license. See the LICENSE file for details. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace OAuth2Framework\Bundle\Server\DependencyInjection\Source\Scope; |
15
|
|
|
|
16
|
|
|
use Fluent\PhpConfigFileLoader; |
17
|
|
|
use OAuth2Framework\Bundle\Server\DependencyInjection\Source\ActionableSource; |
18
|
|
|
use Symfony\Component\Config\FileLocator; |
19
|
|
|
use Symfony\Component\Config\Definition\Builder\NodeDefinition; |
20
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
21
|
|
|
|
22
|
|
|
final class ScopePolicySource extends ActionableSource |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* ScopePolicySource constructor. |
26
|
|
|
*/ |
27
|
|
|
public function __construct() |
28
|
|
|
{ |
29
|
|
|
$this->addSubSource(new ScopePolicyErrorSource()); |
30
|
|
|
$this->addSubSource(new ScopePolicyDefaultSource()); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* {@inheritdoc} |
35
|
|
|
*/ |
36
|
|
|
protected function name(): string |
37
|
|
|
{ |
38
|
|
|
return 'policy'; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* {@inheritdoc} |
43
|
|
|
*/ |
44
|
|
|
protected function continueLoading(string $path, ContainerBuilder $container, array $config) |
45
|
|
|
{ |
46
|
|
|
foreach (['by_default'] as $k) { |
47
|
|
|
$container->setParameter($path.'.'.$k, $config[$k]); |
48
|
|
|
} |
49
|
|
|
$loader = new PhpConfigFileLoader($container, new FileLocator(__DIR__.'/../../../Resources/config/scope')); |
50
|
|
|
$loader->load('policy.php'); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* {@inheritdoc} |
55
|
|
|
*/ |
56
|
|
|
protected function continueConfiguration(NodeDefinition $node) |
57
|
|
|
{ |
58
|
|
|
parent::continueConfiguration($node); |
59
|
|
|
$node |
|
|
|
|
60
|
|
|
->validate() |
61
|
|
|
->ifTrue(function ($config) { |
62
|
|
|
return true === $config['enabled'] && empty($config['by_default']); |
63
|
|
|
}) |
64
|
|
|
->thenInvalid('The option "repository" must be set.') |
65
|
|
|
->end() |
66
|
|
|
->children() |
67
|
|
|
->scalarNode('by_default') |
68
|
|
|
->info('Default scope policy.') |
69
|
|
|
->defaultValue('none') |
70
|
|
|
->end() |
71
|
|
|
->end(); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
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: