1 | <?php |
||
9 | class DiscovererSet |
||
10 | { |
||
11 | /** |
||
12 | * @var Discoverer[] |
||
13 | */ |
||
14 | private $discoverers = array(); |
||
15 | |||
16 | /** @var Filter[] */ |
||
17 | private $filters = array(); |
||
18 | |||
19 | /** |
||
20 | * @var int maximum crawl depth |
||
21 | */ |
||
22 | public $maxDepth = 3; |
||
23 | |||
24 | /** |
||
25 | * @var array the list of already visited URIs with the depth they were discovered on as value |
||
26 | */ |
||
27 | private $alreadySeenUris = array(); |
||
28 | |||
29 | public function __construct(array $discoverers = array()) |
||
30 | { |
||
31 | foreach ($discoverers as $alias => $discoverer) { |
||
32 | $this->set($discoverer, is_int($alias) ? null : $alias); |
||
33 | } |
||
34 | } |
||
35 | |||
36 | /** |
||
37 | * @param DiscoveredUri $uri |
||
38 | * |
||
39 | * Mark an Uri as already seen. |
||
40 | * |
||
41 | * If it already exists, it is not overwritten, since we want to keep the |
||
42 | * first depth it was found at. |
||
43 | */ |
||
44 | private function markSeen(DiscoveredUri $uri) |
||
45 | { |
||
46 | $uriString = $uri->normalize()->toString(); |
||
47 | if (!array_key_exists($uriString, $this->alreadySeenUris)) { |
||
48 | $this->alreadySeenUris[$uriString] = $uri->getDepthFound(); |
||
49 | } |
||
50 | } |
||
51 | |||
52 | /** |
||
53 | * @return bool Returns true if this URI was found at max depth |
||
54 | */ |
||
55 | private function isAtMaxDepth(DiscoveredUri $uri) |
||
59 | |||
60 | /** |
||
61 | * @param Resource $resource |
||
62 | * @return UriInterface[] |
||
63 | */ |
||
64 | public function discover(Resource $resource) |
||
90 | |||
91 | /** |
||
92 | * Sets a discoverer. |
||
93 | * |
||
94 | * @param discovererInterface $discoverer The discoverer instance |
||
95 | * @param string|null $alias An alias |
||
96 | */ |
||
97 | public function set(DiscovererInterface $discoverer, $alias = null) |
||
104 | |||
105 | /** |
||
106 | * @param PreFetchFilterInterface $filter |
||
107 | */ |
||
108 | public function addFilter(PreFetchFilterInterface $filter) |
||
112 | |||
113 | /** |
||
114 | * @param UriInterface[] $discoveredUris |
||
115 | */ |
||
116 | private function normalize(array &$discoveredUris) |
||
122 | |||
123 | /** |
||
124 | * @param UriInterface[] $discoveredUris |
||
125 | */ |
||
126 | private function filterAlreadySeen(array &$discoveredUris) |
||
134 | |||
135 | /** |
||
136 | * @param UriInterface[] $discoveredUris |
||
137 | */ |
||
138 | private function filter(array &$discoveredUris) |
||
148 | |||
149 | /** |
||
150 | * @param UriInterface[] $discoveredUris |
||
151 | */ |
||
152 | private function removeDuplicates(array &$discoveredUris) |
||
171 | } |
||
172 |