1
|
|
|
<?php |
2
|
|
|
namespace Http\Html; |
3
|
|
|
|
4
|
|
|
use \Psr\Http\Message\ServerRequestInterface as Request; |
5
|
|
|
use \Psr\Http\Message\ResponseInterface as Response; |
6
|
|
|
|
7
|
|
|
require 'vendor/autoload.php'; |
8
|
|
|
|
9
|
|
|
class WebPage |
10
|
|
|
{ |
11
|
|
|
/** The webpage title */ |
12
|
|
|
public $title; |
13
|
|
|
/** An array of tags to be added to the HTML head section */ |
14
|
|
|
protected $headTags; |
15
|
|
|
/** A string represnting the body of the page */ |
16
|
|
|
public $body; |
17
|
|
|
/** A string to add to the body open tag */ |
18
|
|
|
public $body_tags; |
19
|
|
|
|
20
|
|
|
protected $request = null; |
21
|
|
|
|
22
|
|
|
public function __construct($title = '') |
23
|
|
|
{ |
24
|
|
|
$this->title = $title; |
25
|
|
|
$this->headTags = array(); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function handleRequest($request, $response, $args) |
|
|
|
|
29
|
|
|
{ |
30
|
|
|
$this->request = $request; |
31
|
|
|
$response = $this->printPage($response); |
32
|
|
|
return $response; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Add a tag to the head element |
37
|
|
|
* |
38
|
|
|
* @param string $tag The tag to add to the page header |
39
|
|
|
*/ |
40
|
|
|
public function addHeadTag($tag) |
41
|
|
|
{ |
42
|
|
|
array_push($this->headTags, $tag); |
43
|
|
|
} |
44
|
|
|
/** |
45
|
|
|
* Create a tag to be added to the document |
46
|
|
|
* |
47
|
|
|
* @param string $tagName The tag's name (i.e. the string right after the open sign |
48
|
|
|
* @param array $attribs Attributes to be added to the tag in the form key=value |
49
|
|
|
* @param boolean $selfClose Does this tag end with a close (/>)? |
50
|
|
|
* |
51
|
|
|
* @return string The tag as a string |
52
|
|
|
*/ |
53
|
|
View Code Duplication |
protected function createOpenTag($tagName, $attribs = array(), $selfClose = false) |
|
|
|
|
54
|
|
|
{ |
55
|
|
|
$tag = '<'.$tagName; |
56
|
|
|
$attribNames = array_keys($attribs); |
57
|
|
|
foreach($attribNames as $attribName) |
58
|
|
|
{ |
59
|
|
|
$tag .= ' '.$attribName; |
60
|
|
|
if($attribs[$attribName]) |
61
|
|
|
{ |
62
|
|
|
$tag .= '="'.$attribs[$attribName].'"'; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
if($selfClose) |
66
|
|
|
{ |
67
|
|
|
return $tag.'/>'; |
68
|
|
|
} |
69
|
|
|
return $tag.'>'; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Create a close tag to be added to the document |
74
|
|
|
* |
75
|
|
|
* @param string $tagName The tag's name (i.e. the string right after the open sign |
76
|
|
|
* |
77
|
|
|
* @return string The close tag as a string |
78
|
|
|
*/ |
79
|
|
|
protected function createCloseTag($tagName) |
80
|
|
|
{ |
81
|
|
|
return '</'.$tagName.'>'; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Create a link to be added to the document |
86
|
|
|
* |
87
|
|
|
* @param string $linkName The text inside the link |
88
|
|
|
* @param string $linkTarget The location the link goes to |
89
|
|
|
* |
90
|
|
|
* @return string The link |
91
|
|
|
*/ |
92
|
|
|
public function createLink($linkName, $linkTarget = '#') |
93
|
|
|
{ |
94
|
|
|
$startTag = $this->createOpenTag('a', array('href'=>$linkTarget)); |
95
|
|
|
$endTag = $this->createCloseTag('a'); |
96
|
|
|
return $startTag.$linkName.$endTag; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Print the HTML doctype header |
101
|
|
|
*/ |
102
|
|
|
protected function printDoctype($response) |
103
|
|
|
{ |
104
|
|
|
return $response->write('<!DOCTYPE html>'); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Print the opening HTML tag |
109
|
|
|
*/ |
110
|
|
|
protected function printOpenHtml($response) |
111
|
|
|
{ |
112
|
|
|
return $response->write('<HTML lang="en">'); |
113
|
|
|
} |
114
|
|
|
/** |
115
|
|
|
* Print the closing HTML tag |
116
|
|
|
*/ |
117
|
|
|
protected function printCloseHtml($response) |
118
|
|
|
{ |
119
|
|
|
return $response->write('</HTML>'); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Print the HTML HEAD section |
124
|
|
|
*/ |
125
|
|
|
protected function printHead($response) |
126
|
|
|
{ |
127
|
|
|
$head = '<HEAD><TITLE>'.$this->title.'</TITLE>'; |
128
|
|
|
$head.='<meta name="viewport" content="width=device-width, initial-scale=1.0">'; |
129
|
|
|
foreach($this->headTags as $tag) |
130
|
|
|
{ |
131
|
|
|
$head.=$tag."\n"; |
132
|
|
|
} |
133
|
|
|
$head.='</HEAD>'; |
134
|
|
|
return $response->write($head); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Print the HTML BODY section |
139
|
|
|
*/ |
140
|
|
|
protected function printBody($response) |
141
|
|
|
{ |
142
|
|
|
return $response->write('<BODY '.$this->body_tags.'>'.$this->body.'</BODY>'); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
protected function printPage($response) |
146
|
|
|
{ |
147
|
|
|
$response = $this->printDoctype($response); |
148
|
|
|
$response = $this->printOpenHtml($response); |
149
|
|
|
$response = $this->printHead($response); |
150
|
|
|
$response = $this->printBody($response); |
151
|
|
|
$response = $this->printCloseHtml($response); |
152
|
|
|
return $response; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
public function getCurrentUrl() |
156
|
|
|
{ |
157
|
|
|
if($this->request === null) |
158
|
|
|
{ |
159
|
|
|
return ''; |
160
|
|
|
} |
161
|
|
|
return $this->request->getUri(); |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.