1: <?php
2:
3: namespace PHPixie\Route\Resolvers\Resolver;
4:
5: class Group implements \PHPixie\Route\Resolvers\Resolver,
6: \PHPixie\Route\Resolvers\Registry
7: {
8: protected $resolverBuilder;
9: protected $resolversConfig;
10:
11: protected $names;
12: protected $resolverMap;
13:
14: public function __construct($resolverBuilder, $configData)
15: {
16: $this->resolverBuilder = $resolverBuilder;
17: $this->resolversConfig = $configData->slice('resolvers');
18: }
19:
20: public function names()
21: {
22: $this->requireRouteNames();
23: return $this->names;
24: }
25:
26: public function get($name)
27: {
28: $this->requireRouteNames();
29:
30:
31: if(!array_key_exists($name, $this->resolverMap)) {
32: throw new \PHPixie\Route\Exception\Route();
33: }
34:
35: if($this->resolverMap[$name] === null) {
36: $configData = $this->resolversConfig->slice($name);
37: $this->resolverMap[$name] = $this->resolverBuilder->buildFromConfig($configData);
38: }
39:
40: return $this->resolverMap[$name];
41: }
42:
43: public function match($segment)
44: {
45: $match = null;
46:
47: foreach($this->names() as $name) {
48: $resolver = $this->get($name);
49: $match = $resolver->match($segment);
50: if($match !== null) {
51: $match->prependResolverPath($name);
52: break;
53: }
54: }
55:
56: return $match;
57: }
58:
59: public function generate($match, $withHost = false)
60: {
61: $name = $match->popResolverPath();
62: $resolver = $this->get($name);
63: return $resolver->generate($match, $withHost);
64: }
65:
66: protected function requireRouteNames()
67: {
68: if($this->names === null) {
69: $this->names = $this->resolversConfig->keys();
70: $this->resolverMap = array_fill_keys($this->names, null);
71: }
72: }
73: }