1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nip\Router\Router\Traits; |
4
|
|
|
|
5
|
|
|
use Nip\Router\Route\Route; |
6
|
|
|
use Nip\Router\RouteCollection; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class HasRouteCollectionTrait |
10
|
|
|
* @package Nip\Router\Router\Traits |
11
|
|
|
*/ |
12
|
|
|
trait HasRouteCollectionTrait |
13
|
|
|
{ |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @inheritdoc |
17
|
|
|
* @return RouteCollection |
18
|
|
|
*/ |
19
|
21 |
|
public function getRouteCollection() |
20
|
|
|
{ |
21
|
21 |
|
// if (null === $this->collection) { |
22
|
21 |
|
// $this->collection = $this->newRoutesCollection(); |
23
|
|
|
// } |
24
|
21 |
|
return parent::getRouteCollection(); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param $name |
29
|
|
|
* @return null|Route\Route |
30
|
|
|
*/ |
31
|
13 |
|
public function getRoute($name) |
32
|
|
|
{ |
33
|
13 |
|
return $this->getRouteCollection()->get($name); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @return RouteCollection|Route[] |
38
|
|
|
*/ |
39
|
16 |
|
public function getRoutes() |
40
|
|
|
{ |
41
|
16 |
|
return $this->getRouteCollection(); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param Route $route |
46
|
|
|
*/ |
47
|
|
|
public function addRoute($route) |
48
|
|
|
{ |
49
|
|
|
$this->getRouteCollection()->addRoute($route); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param $name |
54
|
|
|
* @return bool |
55
|
|
|
*/ |
56
|
1 |
|
public function connected($name) |
57
|
|
|
{ |
58
|
1 |
|
return $this->hasRoute($name); |
59
|
1 |
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param string $name |
63
|
|
|
* @return bool |
64
|
|
|
*/ |
65
|
|
|
public function hasRoute($name) |
66
|
|
|
{ |
67
|
|
|
return $this->getGenerator()->hasRoute($name); |
|
|
|
|
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function initRouteCollection() |
71
|
|
|
{ |
72
|
|
|
$this->collection = $this->newRoutesCollection(); |
|
|
|
|
73
|
21 |
|
} |
74
|
|
|
|
75
|
21 |
|
/** |
76
|
|
|
* @return RouteCollection |
77
|
|
|
*/ |
78
|
|
|
protected function newRoutesCollection() |
79
|
|
|
{ |
80
|
|
|
return new RouteCollection(); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.