Completed
Push — master ( bf102e...fafcea )
by Nazar
06:46
created

Files::files_router_handler()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 4
dl 0
loc 3
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @package   CleverStyle Framework
4
 * @author    Nazar Mokrynskyi <[email protected]>
5
 * @copyright Copyright (c) 2015-2017, Nazar Mokrynskyi
6
 * @license   MIT License, see license.txt
7
 */
8
namespace cs\App\Router;
9
10
/**
11
 * @property string[] $controller_path Path that will be used by controller to render page
12
 */
13
trait Files {
14
	/**
15
	 * Include files necessary for module page rendering
16
	 *
17
	 * @param \cs\Request $Request
18
	 *
19
	 * @throws \cs\ExitException
20
	 */
21 2
	protected function files_router ($Request) {
22 2
		foreach ($this->controller_path as $index => $path) {
23 2
			$dir = $this->working_directory;
24
			/**
25
			 * Starting from index 2 (first is always `index`) we need to maintain slash-separated string that includes all paths from index 1 and till pre-last
26
			 * (last is base filename, while pre-last goes into directory path)
27
			 */
28 2
			if ($index > 1) {
29 2
				$dir .= '/'.implode('/', array_slice($this->controller_path, 1, $index - 1));
30
			}
31 2
			$next_exists = isset($this->controller_path[$index + 1]);
32 2
			$this->files_router_handler($Request, $dir, $path, !$next_exists);
33
		}
34 2
	}
35
	/**
36
	 * Include files that corresponds for specific paths in URL
37
	 *
38
	 * @param \cs\Request $Request
39
	 * @param string      $dir
40
	 * @param string      $basename
41
	 * @param bool        $required
42
	 *
43
	 * @throws \cs\ExitException
44
	 */
45 2
	protected function files_router_handler ($Request, $dir, $basename, $required = true) {
46 2
		$this->files_router_handler_internal($Request, $dir, $basename, $required);
47 2
	}
48
	/**
49
	 * @param \cs\Request $Request
50
	 * @param string      $dir
51
	 * @param string      $basename
52
	 * @param bool        $required
53
	 *
54
	 * @throws \cs\ExitException
55
	 */
56 2
	protected function files_router_handler_internal ($Request, $dir, $basename, $required) {
57 2
		$included = _include("$dir/$basename.php", false, false) !== false;
58 2
		if (!$Request->cli_path && !$Request->api_path) {
59 2
			return;
60
		}
61 2
		$request_method = strtolower($Request->method);
62 2
		$included       = _include("$dir/$basename.$request_method.php", false, false) !== false || $included;
63 2
		if ($included || !$required) {
64 2
			return;
65
		}
66 2
		$unknown_method_handler = $Request->api_path ? 'options' : 'cli';
67 2
		if (file_exists("$dir/$basename.$unknown_method_handler.php")) {
68 2
			include "$dir/$basename.$unknown_method_handler.php";
69 2
			return;
70
		}
71 2
		$this->handler_not_found(
72 2
			$this->files_router_available_methods($dir, $basename),
73 2
			$request_method,
74 2
			$Request
75
		);
76 2
	}
77
	/**
78
	 * @param string $dir
79
	 * @param string $basename
80
	 *
81
	 * @return string[]
82
	 */
83 4
	protected function files_router_available_methods ($dir, $basename) {
84 4
		$methods = get_files_list($dir, "/^$basename\\.[a-z]+\\.php$/");
85 4
		$methods = _strtoupper(_substr($methods, strlen($basename) + 1, -4));
86 4
		natcasesort($methods);
87 4
		return array_values($methods);
88
	}
89
}
90