1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Inji\Router; |
4
|
|
|
class Folder { |
5
|
|
|
public $folder, $prefix, $priority, $moduleIndex, $moduleDirs; |
|
|
|
|
6
|
|
|
|
7
|
|
|
public function __construct($folder, $prefix, $priority, $moduleIndex, $moduleDirs) { |
8
|
|
|
$this->folder = $folder; |
9
|
|
|
$this->prefix = $prefix; |
10
|
|
|
$this->priority = $priority; |
11
|
|
|
$this->moduleIndex = $moduleIndex; |
12
|
|
|
$this->moduleDirs = $moduleDirs; |
13
|
|
|
} |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @param $className |
17
|
|
|
* @return Path[] |
18
|
|
|
*/ |
19
|
2 |
|
public function forClass($className) { |
20
|
|
|
|
21
|
2 |
|
$paths = $this->moduleDirs($className); |
22
|
2 |
|
$paths += $this->prefix($className); |
23
|
2 |
|
$paths += $this->full($className); |
24
|
|
|
|
25
|
2 |
|
return $paths; |
26
|
|
|
} |
27
|
|
|
|
28
|
2 |
|
public function full($className) { |
29
|
2 |
|
if ($this->prefix !== '*') { |
30
|
2 |
|
return []; |
31
|
|
|
} |
32
|
|
|
$paths = []; |
33
|
|
|
$classPath = str_replace('/', '\\', $className); |
34
|
|
|
|
35
|
|
|
$paths['folderPath'] = new Path($this->folder . $classPath . '.php'); |
36
|
|
|
$paths['folderPathDir'] = new Path($this->folder . $classPath . substr($className, strrpos($className, '\\')) . '.php'); |
37
|
|
|
|
38
|
|
|
return $paths; |
39
|
|
|
} |
40
|
|
|
|
41
|
2 |
|
public function prefix($className) { |
42
|
2 |
|
if (strpos($className, $this->prefix) !== 0) { |
43
|
1 |
|
return []; |
44
|
|
|
} |
45
|
|
|
|
46
|
2 |
|
$paths = []; |
47
|
2 |
|
$cuttedPath = str_replace('\\', '/', substr($className, strlen($this->prefix))); |
48
|
|
|
|
49
|
2 |
|
$paths['folderPrefixPath'] = new Path($this->folder . $cuttedPath . '.php'); |
50
|
2 |
|
$paths['folderPrefixPathDir'] = new Path($this->folder . $cuttedPath . '/' . substr($className, strrpos($className, '\\') + 1) . '.php'); |
51
|
2 |
|
return $paths; |
52
|
|
|
} |
53
|
|
|
|
54
|
2 |
|
public function moduleDirs($className) { |
55
|
2 |
|
if ($this->moduleIndex === false || $this->moduleIndex >= substr_count($className, '\\')) { |
56
|
2 |
|
return []; |
57
|
|
|
} |
58
|
1 |
|
$paths = []; |
59
|
1 |
|
$classPathItems = explode('\\', $className); |
60
|
1 |
|
$moduleName = $classPathItems[$this->moduleIndex]; |
61
|
1 |
|
$classPathStart = implode('/', array_slice($classPathItems, 0, $this->moduleIndex + 1)); |
62
|
|
|
|
63
|
1 |
|
if (strpos($classPathStart, str_replace('\\', '/', $this->prefix)) === 0) { |
64
|
1 |
|
$classPathStart = substr($classPathStart, strlen($this->prefix)); |
65
|
|
|
} |
66
|
|
|
|
67
|
1 |
|
$classPathEnd = implode('/', array_slice($classPathItems, $this->moduleIndex + 1)); |
68
|
|
|
|
69
|
1 |
|
foreach ($this->moduleDirs as $moduleDir) { |
70
|
1 |
|
$dirPath = implode('/', [rtrim($this->folder, '/'), $classPathStart, $moduleDir, $classPathEnd]); |
71
|
1 |
|
$paths['folderModuleDirPath_' . $moduleDir] = new Path($dirPath . '.php', $moduleName); |
72
|
1 |
|
$paths['folderModuleDirPathDir_' . $moduleDir] = new Path($dirPath . substr($dirPath, strrpos($dirPath, '/')) . '.php', $moduleName); |
73
|
|
|
} |
74
|
1 |
|
return $paths; |
75
|
|
|
} |
76
|
|
|
} |
Only declaring a single property per statement allows you to later on add doc comments more easily.
It is also recommended by PSR2, so it is a common style that many people expect.