Total Complexity | 12 |
Total Lines | 85 |
Duplicated Lines | 0 % |
Coverage | 85.19% |
Changes | 1 | ||
Bugs | 0 | Features | 1 |
1 | <?php |
||
12 | class ViewPathDetector |
||
13 | { |
||
14 | use SingletonTrait; |
||
15 | |||
16 | /** |
||
17 | * @var array |
||
18 | */ |
||
19 | protected $paths = []; |
||
20 | |||
21 | /** |
||
22 | * ViewPathDetector constructor. |
||
23 | */ |
||
24 | 1 | public function __construct() |
|
25 | { |
||
26 | 1 | $this->initFromCache(); |
|
27 | 1 | } |
|
28 | |||
29 | /** |
||
30 | * @param $controller |
||
31 | * @return bool |
||
32 | */ |
||
33 | 4 | public static function for($controller) |
|
34 | { |
||
35 | 4 | return static::instance()->detectForController($controller); |
|
36 | } |
||
37 | |||
38 | /** |
||
39 | * @param $controller |
||
40 | * @return bool |
||
41 | */ |
||
42 | 4 | protected function detectForController($controller) |
|
43 | { |
||
44 | 4 | $key = $this->keyForController($controller); |
|
45 | 4 | if (!isset($this->paths[$key])) { |
|
46 | 3 | $this->paths[$key] = $this->generateForController($controller); |
|
47 | } |
||
48 | |||
49 | 4 | return $this->paths[$key]; |
|
50 | } |
||
51 | |||
52 | /** |
||
53 | * @param $controller |
||
54 | * @return false|string |
||
55 | */ |
||
56 | 4 | protected function keyForController($controller) |
|
57 | { |
||
58 | 4 | return get_class($controller); |
|
59 | } |
||
60 | |||
61 | /** |
||
62 | * @param $controller |
||
63 | * @return bool|string |
||
64 | */ |
||
65 | 3 | protected function generateForController($controller) |
|
66 | { |
||
67 | 3 | if (method_exists($controller, 'generateViewPath')) { |
|
68 | 2 | return $controller->generateViewPath(); |
|
69 | } |
||
70 | |||
71 | 1 | if (defined('MODULES_PATH')) { |
|
72 | $path = MODULES_PATH . request()->getModuleName() . '/views/'; |
||
|
|||
73 | if (is_dir($path)) { |
||
74 | return $path; |
||
75 | } |
||
76 | } |
||
77 | |||
78 | 1 | return $this->generateForControllerAutodetect($controller); |
|
79 | } |
||
80 | |||
81 | /** |
||
82 | * @param $controller |
||
83 | * @return bool|string |
||
84 | */ |
||
85 | 1 | protected function generateForControllerAutodetect($controller) |
|
86 | { |
||
87 | 1 | $path = dirname(Path::basePath($controller)); |
|
88 | 1 | $path .= DIRECTORY_SEPARATOR . 'views'; |
|
89 | 1 | if (is_dir($path)) { |
|
90 | 1 | return $path; |
|
91 | } |
||
92 | return false; |
||
93 | } |
||
94 | |||
95 | 1 | protected function initFromCache() |
|
97 | // $this->paths; |
||
98 | 1 | } |
|
99 | } |
||
100 |