Completed
Push — master ( 5eac61...3c754e )
by Kanto
01:27
created

Application::setNotFound()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 4
rs 10
1
<?php
2
namespace Kore;
3
4
class Application
5
{
6
    /**
7
     * @var string
8
     */
9
    protected $basePath = '';
10
    /**
11
     * @var string
12
     */
13
    protected $defaultController = 'index';
14
    /**
15
     * @var callable
16
     */
17
    protected $notFound;
18
19
    /**
20
     * @param string $basePath ex. 'mybasepath/'
21
     * @return void
22
     */
23
    public function setBasePath($basePath)
24
    {
25
        $this->basePath = $basePath;
26
    }
27
28
    /**
29
     * @param string $defaultController
30
     * @return void
31
     */
32
    public function setDefaultController($defaultController)
33
    {
34
        $this->defaultController = $defaultController;
35
    }
36
37
    /**
38
     * @param callable $notFound
39
     * @return void
40
     */
41
    public function setNotFound($notFound)
42
    {
43
        if (is_callable($notFound)) {
44
            $this->notFound = $notFound;
45
        }
46
    }
47
    
48
    /**
49
     * @param string|null $path
50
     * @return void
51
     */
52
    public function run($path = null)
53
    {
54
        if ($path === null) {
55
            $path = $_SERVER['REQUEST_URI'];
56
        }
57
58
        list($class, $controller, $args) = $this->parseController($path);
59
60
        if ($class === null) {
61
            if (is_callable($this->notFound)) {
62
                call_user_func($this->notFound);
63
            }
64
            http_response_code(404);
65
            return;
66
        }
67
    
68
        $class->main($controller, $args);
69
    }
70
71
    /**
72
     * @param string $path
73
     * @return array<mixed>
74
     */
75
    protected function parseController($path)
76
    {
77
        if (false !== $pos = strpos($path, '?')) {
78
            $path = substr($path, 0, $pos);
79
        }
80
        $path = rawurldecode($path);
81
        $path = trim($path, '/');
82
83
        if (!empty($this->basePath) && strpos($path, $this->basePath) === 0) {
84
            $path = substr($path, strlen($this->basePath));
85
        }
86
    
87
        $parray = explode('/', $path);
88
    
89
        $class = null;
90
        $controller = $this->defaultController;
91
        $args = [];
92
        foreach ($parray as $i => $p) {
93
            $tmpController = implode('\\', array_slice($parray, 0, $i+1));
94
            if ($tmpController !== '') {
95
                $controller = $tmpController;
96
            }
97
            $controller = CONTROLLERS_NS.'\\'.$controller;
98
            if (class_exists($controller)) {
99
                $class = new $controller();
100
                $args = array_slice($parray, $i+1);
101
                break;
102
            }
103
        }
104
        return [$class, $controller, $args];
105
    }
106
107
    /**
108
     * @param array<string> $argv
109
     * @return void
110
     */
111
    public function runCmd($argv)
112
    {
113
        list($class, $command, $args, $opts) = $this->parseCommand($argv);
114
115
        $class->main($command, $args, $opts);
116
    }
117
118
    /**
119
     * @param array<string> $argv
120
     * @return array<mixed>
121
     */
122
    protected function parseCommand($argv)
123
    {
124
        if (!isset($argv[1])) {
125
            throw new \Exception('Unable to find command name');
126
        }
127
        
128
        $command = COMMANDS_NS.'\\'.str_replace('/', '\\', $argv[1]);
129
        if (!class_exists($command)) {
130
            throw new \Exception('Unable to load command class ->' . $command);
131
        }
132
        
133
        $class = new $command();
134
135
        $args = [];
136
        $opts = [];
137
        foreach ($argv as $key => $value) {
138
            if ($key > 1) {
139
                if (preg_match('/^--[a-zA-Z0-9]+=[a-zA-Z0-9]+$/', $value)) {
140
                    $params = explode('=', $value);
141
                    $name = str_replace('--', '', $params[0]);
142
                    $opts[$name] = $params[1];
143
                } else {
144
                    $args[] = $value;
145
                }
146
            }
147
        }
148
        return [$class, $command, $args, $opts];
149
    }
150
}
151