Completed
Push — master ( bb1ab9...4cd17e )
by Sinnarasa
02:25
created

SmartMatch::setDispatcher()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace JetFire\Routing\Match;
4
5
use JetFire\Routing\Router;
6
7
/**
8
 * Class SmartMatch
9
 * @package JetFire\Routing\Match
10
 */
11
class SmartMatch implements MatcherInterface
12
{
13
14
    /**
15
     * @var
16
     */
17
    private $router;
18
19
20
    /**
21
     * @var array
22
     */
23
    private $matcher = ['matchController','matchTemplate'];
24
25
    private $dispatcher = [
26
        'matchTemplate' => 'JetFire\Routing\Dispatcher\TemplateDispatcher',
27
        'matchController' => 'JetFire\Routing\Dispatcher\ControllerDispatcher'
28
    ];
29
30
    /**
31
     * @param Router $router
32
     */
33
    public function __construct(Router $router)
34
    {
35
        $this->router = $router;
36
    }
37
38
    /**
39
     * @param string $matcher
40
     */
41
    public function addMatcher($matcher){
42
        $this->matcher[] = $matcher;
43
    }
44
45
    /**
46
     * @return array
47
     */
48
    public function getMatcher()
49
    {
50
        return $this->matcher;
51
    }
52
53
    /**
54
     * @param array $dispatcher
55
     */
56
    public function setDispatcher($dispatcher = [])
57
    {
58
        $this->dispatcher = array_merge($dispatcher,$this->dispatcher);
59
    }
60
61
    /**
62
     * @return bool
63
     */
64
    public function match()
65
    {
66
        foreach($this->matcher as $matcher){
67
            if(call_user_func([$this,$matcher])) {
68
                $this->router->response->setStatusCode(202);
69
                return true;
70
            }
71
        }
72
        return false;
73
    }
74
75
    /**
76
     * @return bool
77
     */
78
    public function matchTemplate()
79
    {
80
        foreach ($this->router->getConfig()['viewExtension'] as $extension) {
81
            for ($i = 0; $i < $this->router->collection->countRoutes; ++$i) {
82
                $url = explode('/', str_replace($this->router->collection->getRoutes('prefix_' . $i), '',$this->router->route->getUrl()));
83
                $end = array_pop($url);
84
                $url = implode('/', array_map('ucwords', $url)).'/'.$end;
85
                if (is_file(($template = rtrim($this->router->collection->getRoutes('path_' . $i), '/') . $url . $extension))) {
86
                    $this->router->route->setTarget([
87
                        'dispatcher' => $this->dispatcher['matchTemplate'],
88
                        'template' => $template,
89
                        'extension' => str_replace('.', '', $extension),
90
                        'callback' => $this->router->getConfig()['viewCallback']
91
                    ]);
92
                    $this->router->route->addDetail('block', $this->router->collection->getRoutes('path_' . $i));
93
                    return true;
94
                }
95
            }
96
        }
97
        return false;
98
    }
99
100
    /**
101
     * @return bool
102
     */
103
    public function matchController()
104
    {
105
        $routes = array_slice(explode('/', $this->router->route->getUrl()), 1);
106
        $i = 0;
107
        do{
108
            $route =  ('/' . $routes[0] == $this->router->collection->getRoutes('prefix_' . $i)) ? array_slice($routes, 1) : $routes;
109
            if (isset($route[0])) {
110
                $class =  (class_exists($this->router->collection->getRoutes('namespace_' . $i). ucfirst($route[0]) . 'Controller'))
111
                    ? $this->router->collection->getRoutes('namespace_' . $i). ucfirst($route[0]) . 'Controller'
112
                    : ucfirst($route[0]) . 'Controller';
113
                if (isset($route[1]) && method_exists($class, $route[1])) {
114
                    $this->router->route->setTarget([
115
                        'dispatcher' => $this->dispatcher['matchController'],
116
                        'di' => $this->router->getConfig()['di'],
117
                        'controller' => $class,
118
                        'action' => $route[1]
119
                    ]);
120
                    $this->router->route->addDetail('parameters', array_slice($route, 2));
121
                    $this->router->route->addDetail('block', $this->router->collection->getRoutes('path_' . $i));
122
                    return true;
123
                }
124
            }
125
            ++$i;
126
        }while($i < $this->router->collection->countRoutes);
127
        return false;
128
    }
129
130
}
131