Completed
Push — master ( 6202fd...f9a3f2 )
by Sinnarasa
02:36
created

UriMatcher::matchControllerTemplate()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 11
rs 9.4285
cc 3
eloc 7
nc 3
nop 0
1
<?php
2
3
namespace JetFire\Routing\Matcher;
4
5
use JetFire\Routing\Router;
6
7
/**
8
 * Class SmartMatch
9
 * @package JetFire\Routing\Match
10
 */
11
class UriMatcher implements MatcherInterface
12
{
13
14
    /**
15
     * @var
16
     */
17
    private $router;
18
19
    /**
20
     * @var array
21
     */
22
    private $request = [];
23
24
    /**
25
     * @var array
26
     */
27
    private $matcher = ['matchControllerTemplate'];
28
29
    /**
30
     * @var array
31
     */
32
    private $dispatcher = [
33
        'matchTemplate' => 'JetFire\Routing\Dispatcher\TemplateDispatcher',
34
        'matchController' => 'JetFire\Routing\Dispatcher\ControllerDispatcher'
35
    ];
36
37
    /**
38
     * @param Router $router
39
     */
40
    public function __construct(Router $router)
41
    {
42
        $this->router = $router;
43
    }
44
45
    /**
46
     * @param string $matcher
47
     */
48
    public function addMatcher($matcher){
49
        $this->matcher[] = $matcher;
50
    }
51
52
    /**
53
     * @return array
54
     */
55
    public function getMatcher()
56
    {
57
        return $this->matcher;
58
    }
59
60
    /**
61
     * @param array $dispatcher
62
     */
63
    public function setDispatcher($dispatcher = [])
64
    {
65
        $this->dispatcher = $dispatcher;
66
    }
67
68
    /**
69
     * @param $method
70
     * @param $class
71
     * @return mixed|void
72
     */
73
    public function addDispatcher($method,$class){
74
        $this->dispatcher[$method] = $class;
75
    }
76
77
    /**
78
     * @return bool
79
     */
80
    public function match()
81
    {
82
        foreach($this->matcher as $matcher){
83
            if(is_array($target = call_user_func([$this,$matcher]))) {
84
                $this->setTarget($target);
85
                $this->router->response->setStatusCode(202);
86
                return true;
87
            }
88
        }
89
        return false;
90
    }
91
92
    /**
93
     * @param array $target
94
     */
95 View Code Duplication
    public function setTarget($target = []){
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
96
        $index = isset($this->request['collection_index']) ? $this->request['collection_index'] : 0;
97
        $this->router->route->setDetail($this->request);
98
        $this->router->route->setTarget($target);
99
        $this->router->route->addTarget('block', $this->router->collection->getRoutes('block_'.$index));
100
        $this->router->route->addTarget('view_dir', $this->router->collection->getRoutes('view_dir_'.$index));
101
    }
102
103
    /**
104
     * @return array|bool
105
     */
106
    public function matchControllerTemplate(){
107
        if(is_array($ctrl = $this->matchController())) {
108
            if (is_array($tpl = $this->matchTemplate())) {
109
                return array_merge(array_merge($ctrl, $tpl),[
110
                    'dispatcher' => [$this->dispatcher['matchController'], $this->dispatcher['matchTemplate']]
111
                ]);
112
            }
113
            return $ctrl;
114
        }
115
        return $this->matchTemplate();
116
    }
117
118
    /**
119
     * @return bool|array
120
     */
121
    public function matchTemplate()
122
    {
123
        foreach ($this->router->getConfig()['templateExtension'] as $extension) {
124
            for ($i = 0; $i < $this->router->collection->countRoutes; ++$i) {
125
                $url = explode('/', str_replace($this->router->collection->getRoutes('prefix_' . $i), '',$this->router->route->getUrl()));
126
                $end = array_pop($url);
127
                $url = implode('/', array_map('ucwords', $url)).'/'.$end;
128
                if (is_file(($template = rtrim($this->router->collection->getRoutes('view_dir_' . $i), '/') . $url . $extension))) {
129
                    $this->request['collection_index'] = $i;
130
                    return [
131
                        'dispatcher' => $this->dispatcher['matchTemplate'],
132
                        'template' => $template,
133
                        'extension' => str_replace('.', '', $extension),
134
                        'callback' => $this->router->getConfig()['templateCallback']
135
                    ];
136
                }
137
            }
138
        }
139
        return false;
140
    }
141
142
    /**
143
     * @return bool|array
144
     */
145
    public function matchController()
146
    {
147
        $routes = array_slice(explode('/', $this->router->route->getUrl()), 1);
148
        $i = 0;
149
        do{
150
            $route =  ('/' . $routes[0] == $this->router->collection->getRoutes('prefix_' . $i)) ? array_slice($routes, 1) : $routes;
151
            if (isset($route[0])) {
152
                $class =  (class_exists($this->router->collection->getRoutes('ctrl_namespace_' . $i). ucfirst($route[0]) . 'Controller'))
153
                    ? $this->router->collection->getRoutes('ctrl_namespace_' . $i). ucfirst($route[0]) . 'Controller'
154
                    : ucfirst($route[0]) . 'Controller';
155
                $route[1] = isset($route[1])?$route[1]:'index';
156
                if (method_exists($class, $route[1])) {
157
                    $this->request['parameters'] = array_slice($route, 2);
158
                    $this->request['collection_index'] = $i;
159
                    return [
160
                        'dispatcher' => $this->dispatcher['matchController'],
161
                        'di' => $this->router->getConfig()['di'],
162
                        'controller' => $class,
163
                        'action' => $route[1]
164
                    ];
165
                }
166
            }
167
            ++$i;
168
        }while($i < $this->router->collection->countRoutes);
169
        return false;
170
    }
171
172
}
173