summaryrefslogtreecommitdiffstats
path: root/src/View/PHP.php
blob: 5ea9559331c696ecbb07068983a0c92de648a84f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php

namespace Jasny\View;

use Jasny\ViewInterface;
use Psr\Http\Message\ResponseInterface;

/**
 * Abstraction use PHP templates as views with PSR-7
 */
class PHP implements ViewInterface
{
    /**
     * @var string
     */
    protected $path;

    /**
     * @var string
     */
    protected $ext = 'php';
    
    /**
     * Class constructor
     * 
     * @param array $options
     */
    public function __construct(array $options)
    {
        if (!isset($options['path'])) {
            throw new \BadMethodCallException("'path' option is required");
        }
            
        $this->path = $options['path'];
        
        if (isset($options['ext'])) {
            $this->ext = $options['ext'];
        }
    }
    
    /**
     * Get directory path.
     * 
     * @return string
     */
    public function getPath()
    {
       return $this->path; 
    }
    
    /**
     * Get the default file extension
     * 
     * @return string
     */
    public function getExt()
    {
        return $this->ext;
    }
    
    
    /**
     * Expose a function to the view.
     *
     * @param string        $name      Function name in template
     * @param callable|null $function
     * @return $this
     * @throws \BadMethodCallException if function can't be exposed
     */
    public function expose($name, $function = null)
    {
        if (function_exists($name) && (!isset($function) || $name === $function)) {
            return $this;
        }
        
        throw new \BadMethodCallException("Exposing functions under an alias isn't supported with PHP views");
    }
    
    
    /**
     * Get the path to a view file
     * 
     * @param string $name
     * @return string
     */
    protected function getFilePath($name)
    {
        if (pathinfo($name, PATHINFO_EXTENSION) === '') {
            $name .= (substr($name, -1) === '/' ? 'index' : '') . '.' . $this->getExt();
        }
        
        return rtrim($this->path, '/') . '/' . ltrim($name, '/');
    }
    
    /**
     * Assert the filename and that the file exists
     * 
     * @param string $name
     * @throws \InvalidArgumentException
     */
    protected function assertFile($name)
    {
        if (strpos($name, '..') !== false) {
            throw new \InvalidArgumentException("File name '$name' isn't valid");
        }
        
        $file = $this->getFilePath($name);
        
        if (!file_exists($file)) {
            throw new \RuntimeException("View file '$file' doesn't exist");
        }
    }
    
    /**
     * Run the PHP script
     * 
     * @param string $name
     * @param array  $context
     */
    protected function runScript($name, array $context)
    {
        // Prevent $name and $context being available in the view script
        $___ = $this->getFilePath($name); 
        $_context_ = $context;
        unset($name, $context);
        
        /** @see http://php.net/extract */
        extract($_context_);
        unset($_context_);
        
        include $___;
    }
    
    /**
     * Render and output template
     *
     * @param ResponseInterface $response
     * @param string            $name      Template name
     * @param array             $context   Template context as associated array
     * @return ResponseInterface
     */
    public function render(ResponseInterface $response, $name, array $context = [])
    {
        $this->assertFile($name);

        if (!$response->hasHeader('Content-Type')) {
            $response = $response->withHeader('Content-Type', 'text/html');
        }
        
        $body = $response->getBody();
        
        ob_start(function ($buffer) use ($body) {
            $body->write($buffer);
        });
        
        try {
            $this->runScript($name, $context);
        } finally {
            ob_end_clean();
        }
        
        return $response;
    }
}