bytic /
controllers
| 1 | <?php |
||||
| 2 | |||||
| 3 | namespace Nip\Controllers\View; |
||||
| 4 | |||||
| 5 | use Nip\Controllers\Utility\Path; |
||||
| 6 | use Nip\Utility\Traits\SingletonTrait; |
||||
| 7 | |||||
| 8 | /** |
||||
| 9 | * Class ViewPathDetector |
||||
| 10 | * @package Nip\Controllers\View |
||||
| 11 | */ |
||||
| 12 | class ViewPathDetector |
||||
| 13 | { |
||||
| 14 | use SingletonTrait; |
||||
| 15 | |||||
| 16 | /** |
||||
| 17 | * @var array |
||||
| 18 | */ |
||||
| 19 | protected $paths = []; |
||||
| 20 | |||||
| 21 | /** |
||||
| 22 | * ViewPathDetector constructor. |
||||
| 23 | */ |
||||
| 24 | public function __construct() |
||||
| 25 | { |
||||
| 26 | $this->initFromCache(); |
||||
| 27 | } |
||||
| 28 | |||||
| 29 | /** |
||||
| 30 | * @param $controller |
||||
| 31 | * @return bool |
||||
| 32 | */ |
||||
| 33 | 3 | public static function for($controller) |
|||
| 34 | { |
||||
| 35 | 3 | return static::instance()->detectForController($controller); |
|||
| 36 | } |
||||
| 37 | |||||
| 38 | /** |
||||
| 39 | * @param $controller |
||||
| 40 | * @return bool |
||||
| 41 | */ |
||||
| 42 | 3 | protected function detectForController($controller) |
|||
| 43 | { |
||||
| 44 | 3 | $key = $this->keyForController($controller); |
|||
| 45 | 3 | if (!isset($this->paths[$key])) { |
|||
| 46 | 2 | $this->paths[$key] = $this->generateForController($controller); |
|||
| 47 | } |
||||
| 48 | |||||
| 49 | 3 | return $this->paths[$key]; |
|||
| 50 | } |
||||
| 51 | |||||
| 52 | /** |
||||
| 53 | * @param $controller |
||||
| 54 | * @return false|string |
||||
| 55 | */ |
||||
| 56 | 3 | protected function keyForController($controller) |
|||
| 57 | { |
||||
| 58 | 3 | return get_class($controller); |
|||
| 59 | } |
||||
| 60 | |||||
| 61 | /** |
||||
| 62 | * @param $controller |
||||
| 63 | * @return bool|string |
||||
| 64 | */ |
||||
| 65 | 2 | protected function generateForController($controller) |
|||
| 66 | { |
||||
| 67 | 2 | if (method_exists($controller, 'generateViewPath')) { |
|||
| 68 | 1 | return $controller->generateViewPath(); |
|||
| 69 | } |
||||
| 70 | |||||
| 71 | 1 | if (defined('MODULES_PATH')) { |
|||
| 72 | $path = MODULES_PATH . request()->getModuleName() . '/views/'; |
||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 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)); |
|||
|
0 ignored issues
–
show
It seems like
Nip\Controllers\Utility\...::basePath($controller) can also be of type false; however, parameter $path of dirname() does only seem to accept string, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 88 | 1 | $path .= DIRECTORY_SEPARATOR . 'views'; |
|||
| 89 | 1 | if (is_dir($path)) { |
|||
| 90 | 1 | return $path; |
|||
| 91 | } |
||||
| 92 | return false; |
||||
| 93 | } |
||||
| 94 | |||||
| 95 | protected function initFromCache() |
||||
| 96 | { |
||||
| 97 | // $this->paths; |
||||
| 98 | } |
||||
| 99 | } |
||||
| 100 |