sauls /
widget
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * This file is part of the sauls/widget package. |
||
| 4 | * |
||
| 5 | * @author Saulius Vaičeliūnas <[email protected]> |
||
| 6 | * @link http://saulius.vaiceliunas.lt |
||
| 7 | * @copyright 2018 |
||
| 8 | * |
||
| 9 | * For the full copyright and license information, please view the LICENSE |
||
| 10 | * file that was distributed with this source code. |
||
| 11 | */ |
||
| 12 | |||
| 13 | namespace Sauls\Component\Widget\View; |
||
| 14 | |||
| 15 | |||
| 16 | class PhpFileView implements ViewInterface |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * @var array |
||
| 20 | */ |
||
| 21 | private $templatesDirectories; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * PhpFileView constructor. |
||
| 25 | * |
||
| 26 | * @param array $templatesDirectories |
||
| 27 | */ |
||
| 28 | 21 | public function __construct(array $templatesDirectories = []) |
|
| 29 | { |
||
| 30 | 21 | $this->templatesDirectories = $templatesDirectories; |
|
| 31 | 21 | } |
|
| 32 | |||
| 33 | /** |
||
| 34 | * @throws \Exception |
||
| 35 | */ |
||
| 36 | 3 | public function render(string $viewFile, array $viewData = []): string |
|
| 37 | { |
||
| 38 | try { |
||
| 39 | 3 | ob_start(); |
|
| 40 | |||
| 41 | 3 | extract($viewData, EXTR_OVERWRITE); |
|
| 42 | |||
| 43 | 3 | include $this->resolveViewFile($viewFile); |
|
| 44 | |||
| 45 | 1 | return ob_get_clean(); |
|
| 46 | 2 | } catch (\Exception $e) { |
|
| 47 | 2 | ob_get_clean(); |
|
| 48 | 2 | throw $e; |
|
| 49 | } |
||
| 50 | } |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @param string $viewFile |
||
| 54 | * |
||
| 55 | * @return string |
||
| 56 | * @throws \RuntimeException |
||
| 57 | */ |
||
| 58 | 3 | private function resolveViewFile(string $viewFile): string |
|
| 59 | { |
||
| 60 | try { |
||
| 61 | |||
| 62 | 3 | if (file_exists($viewFile)) { |
|
| 63 | 1 | return $viewFile; |
|
| 64 | } |
||
| 65 | |||
| 66 | 3 | $this->checkTemplatesDirectoryExists(); |
|
| 67 | |||
| 68 | 2 | return $this->resolveTemplateFile($viewFile); |
|
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 69 | |||
| 70 | 2 | } catch (\Exception $e) { |
|
| 71 | 1 | throw new \RuntimeException($e->getMessage()); |
|
| 72 | 1 | } catch (\Throwable $t) { |
|
| 73 | 1 | throw new \RuntimeException( |
|
| 74 | 1 | sprintf( |
|
| 75 | 1 | 'Template %s was not found. Looked in %s', |
|
| 76 | $viewFile, |
||
| 77 | 1 | implode(',', $this->templatesDirectories)) |
|
| 78 | ); |
||
| 79 | } |
||
| 80 | } |
||
| 81 | |||
| 82 | 2 | private function resolveTemplateFile(string $viewFile): ?string |
|
| 83 | { |
||
| 84 | 2 | foreach ($this->templatesDirectories as $directory) { |
|
| 85 | 2 | $template = realpath($directory.DIRECTORY_SEPARATOR.$viewFile); |
|
| 86 | 2 | if (file_exists($template)) { |
|
| 87 | 1 | return $template; |
|
| 88 | } |
||
| 89 | } |
||
| 90 | |||
| 91 | 1 | return null; |
|
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @throws \RuntimeException |
||
| 96 | */ |
||
| 97 | 3 | private function checkTemplatesDirectoryExists(): void |
|
| 98 | { |
||
| 99 | 3 | if (empty($this->templatesDirectories)) { |
|
| 100 | 1 | throw new \RuntimeException(sprintf('Missing template directories for %s', __CLASS__)); |
|
| 101 | } |
||
| 102 | 2 | } |
|
| 103 | |||
| 104 | 19 | public function getName(): string |
|
| 105 | { |
||
| 106 | 19 | return 'php'; |
|
| 107 | } |
||
| 108 | } |
||
| 109 |