1
|
|
|
<?php |
2
|
|
|
namespace Agavi\Routing; |
3
|
|
|
|
4
|
|
|
// +---------------------------------------------------------------------------+ |
5
|
|
|
// | This file is part of the Agavi package. | |
6
|
|
|
// | Copyright (c) 2005-2011 the Agavi Project. | |
7
|
|
|
// | | |
8
|
|
|
// | For the full copyright and license information, please view the LICENSE | |
9
|
|
|
// | file that was distributed with this source code. You can also view the | |
10
|
|
|
// | LICENSE file online at http://www.agavi.org/LICENSE.txt | |
11
|
|
|
// | vi: set noexpandtab: | |
12
|
|
|
// | Local Variables: | |
13
|
|
|
// | indent-tabs-mode: t | |
14
|
|
|
// | End: | |
15
|
|
|
// +---------------------------------------------------------------------------+ |
16
|
|
|
use Agavi\User\User; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* AgaviRoutingUserSource allows you to provide an user source for the routing |
20
|
|
|
* |
21
|
|
|
* @package agavi |
22
|
|
|
* @subpackage routing |
23
|
|
|
* |
24
|
|
|
* @author Dominik del Bondio <[email protected]> |
25
|
|
|
* @copyright Authors |
26
|
|
|
* @copyright The Agavi Project |
27
|
|
|
* |
28
|
|
|
* @since 0.11.0 |
29
|
|
|
* |
30
|
|
|
* @version $Id$ |
31
|
|
|
*/ |
32
|
|
|
class RoutingUserSource implements RoutingSourceInterface |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* @var User An user instance. |
36
|
|
|
*/ |
37
|
|
|
protected $user = null; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Constructor. |
41
|
|
|
* |
42
|
|
|
* @param User $user An user instance. |
43
|
|
|
* |
44
|
|
|
* @author Dominik del Bondio <[email protected]> |
45
|
|
|
* @since 0.11.0 |
46
|
|
|
*/ |
47
|
|
|
public function __construct(User $user) |
48
|
|
|
{ |
49
|
|
|
$this->user = $user; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Retrieves the value for a given entry from the source. |
54
|
|
|
* |
55
|
|
|
* @param array $parts An array with the name parts for the entry. |
56
|
|
|
* |
57
|
|
|
* @return mixed The value. |
58
|
|
|
* |
59
|
|
|
* @author Dominik del Bondio <[email protected]> |
60
|
|
|
* @since 0.11.0 |
61
|
|
|
*/ |
62
|
|
|
public function getSource(array $parts) |
63
|
|
|
{ |
64
|
|
|
if ($parts[0] == 'authenticated') { |
65
|
|
|
return (int) $this->user->isAuthenticated(); |
|
|
|
|
66
|
|
|
} elseif ($parts[0] == 'credentials' && count($parts) > 1) { |
67
|
|
|
// throw the 'credentials' entry away and check with the parameters left |
68
|
|
|
array_shift($parts); |
69
|
|
|
return (int) $this->user->hasCredentials($parts); |
|
|
|
|
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return null; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
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: