Completed
Push — master ( 220ceb...ab9a78 )
by Sinnarasa
02:32
created

SmartMatch::addMatcher()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
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 = ['matchTemplate','matchController'];
24
25
    /**
26
     * @param Router $router
27
     */
28
    public function __construct(Router $router)
29
    {
30
        $this->router = $router;
31
    }
32
33
    /**
34
     * @param string $matcher
35
     */
36
    public function addMatcher($matcher){
37
        $this->matcher[] = $matcher;
38
    }
39
40
    /**
41
     * @return array
42
     */
43
    public function getMatcher()
44
    {
45
        return $this->matcher;
46
    }
47
48
    /**
49
     * @return bool
50
     */
51
    public function match()
52
    {
53
        foreach($this->matcher as $matcher){
54
            if(call_user_func([$this,$matcher])) {
55
                $this->router->route->setResponse(['code' => 202, 'message' => 'Accepted']);
56
                return true;
57
            }
58
        }
59
        return false;
60
    }
61
62
    /**
63
     * @return bool
64
     */
65
    public function matchTemplate()
66
    {
67
        foreach ($this->router->getConfig()['viewExtension'] as $extension) {
68
            for ($i = 0; $i < $this->router->collection->countRoutes; ++$i) {
69
                $url = explode('/', str_replace($this->router->collection->getRoutes('prefix_' . $i), '',$this->router->route->getUrl()));
70
                $end = array_pop($url);
71
                $url = implode('/', array_map('ucwords', $url)).'/'.$end;
72
                if (is_file(($template = rtrim($this->router->collection->getRoutes('path_' . $i), '/') . $url . $extension))) {
73
                    $this->router->route->setTarget([
74
                        'dispatcher' => 'JetFire\Routing\Dispatcher\TemplateDispatcher',
75
                        'template' => $template,
76
                        'extension' => str_replace('.', '', $extension),
77
                        'callback' => $this->router->getConfig()['viewCallback']
78
                    ]);
79
                    $this->router->route->addDetail('block', $this->router->collection->getRoutes('path_' . $i));
80
                    return true;
81
                }
82
            }
83
        }
84
        return false;
85
    }
86
87
    /**
88
     * @return bool
89
     */
90
    public function matchController()
91
    {
92
        $routes = array_slice(explode('/', $this->router->route->getUrl()), 1);
93
        $i = 0;
94
        do{
95
            $route =  ('/' . $routes[0] == $this->router->collection->getRoutes('prefix_' . $i))
96
                ? array_slice($routes, 1) : $routes;
97
            if (isset($route[0])) {
98
                $class =  (class_exists($this->router->collection->getRoutes('namespace_' . $i). ucfirst($route[0]) . 'Controller'))
99
                    ? $this->router->collection->getRoutes('namespace_' . $i). ucfirst($route[0]) . 'Controller'
100
                    : ucfirst($route[0]) . 'Controller';
101
                if (isset($route[1]) && method_exists($class, $route[1])) {
102
                    $this->router->route->setTarget([
103
                        'dispatcher' => 'JetFire\Routing\Dispatcher\ControllerDispatcher',
104
                        'di' => $this->router->getConfig()['di'],
105
                        'controller' => $class,
106
                        'action' => $route[1]
107
                    ]);
108
                    $this->router->route->addDetail('parameters', array_slice($route, 2));
109
                    return true;
110
                }
111
            }
112
            ++$i;
113
        }while($i < $this->router->collection->countRoutes);
114
        return false;
115
    }
116
117
}
118