Passed
Pull Request — master (#190)
by Arman
04:27
created

HtmlAdapter   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
c 1
b 0
f 0
dl 0
loc 62
rs 10
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getViewFilePath() 0 6 2
A __construct() 0 5 1
A render() 0 18 3
1
<?php
2
3
/**
4
 * Quantum PHP Framework
5
 *
6
 * An open source software development framework for PHP
7
 *
8
 * @package Quantum
9
 * @author Arman Ag. <[email protected]>
10
 * @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org)
11
 * @link http://quantum.softberg.org/
12
 * @since 2.9.5
13
 */
14
15
namespace Quantum\Renderer\Adapters;
16
17
use Quantum\Libraries\Storage\Factories\FileSystemFactory;
18
use Quantum\Renderer\Contracts\TemplateRendererInterface;
19
use Quantum\Renderer\Exceptions\RendererException;
20
use Quantum\Libraries\Storage\FileSystem;
21
use Quantum\Di\Exceptions\DiException;
22
use Quantum\Exceptions\BaseException;
23
use ReflectionException;
24
25
/**
26
 * Class HtmlAdapter
27
 * @package Quantum\Renderer
28
 */
29
class HtmlAdapter implements TemplateRendererInterface
30
{
31
32
    /**
33
     * @var FileSystem
34
     */
35
    protected $fs;
36
37
    /**
38
     * @var array|null
39
     */
40
    protected $configs;
41
42
    /**
43
     * @param array|null $configs
44
     * @throws DiException
45
     * @throws ReflectionException
46
     */
47
    public function __construct(?array $configs = [])
48
    {
49
        $this->configs = $configs;
50
51
        $this->fs = FileSystemFactory::get();
52
    }
53
54
    /**
55
     * Renders the view
56
     * @param string $view
57
     * @param array $params
58
     * @return string
59
     * @throws BaseException
60
     */
61
    public function render(string $view, array $params = []): string
62
    {
63
        $filePath = $this->getViewFilePath($view);
64
65
        if (!$this->fs->exists($filePath)) {
66
            throw RendererException::fileNotFound($view);
67
        }
68
69
        ob_start();
70
        ob_implicit_flush(0);
71
72
        if (!empty($params)) {
73
            extract($params);
74
        }
75
76
        require $filePath;
77
78
        return ob_get_clean();
79
    }
80
81
    /**
82
     * @param string $view
83
     * @return string
84
     */
85
    private function getViewFilePath(string $view): string
86
    {
87
        $moduleViewPath = modules_dir() . DS . current_module() . DS . 'Views' . DS . $view . '.php';
0 ignored issues
show
Bug introduced by
The constant Quantum\Renderer\Adapters\DS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
88
        $sharedViewPath = base_dir() . DS . 'shared' . DS . 'views' . DS . $view . '.php';
89
90
        return $this->fs->exists($moduleViewPath) ? $moduleViewPath : $sharedViewPath;
91
    }
92
}