Skip to content

Commit 1c576f6

Browse files
committed
update some info
1 parent b37e19b commit 1c576f6

File tree

6 files changed

+39
-29
lines changed

6 files changed

+39
-29
lines changed

README.md

+11-5
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
1-
# php web library
1+
# php web tools
2+
3+
- a simple php view renderer, front assets load manage
4+
- url,html,curl helper class
25

36
## usage
47

58
### view renderer
69

710
```php
8-
$renderer = new ViewRenderer([
11+
$renderer = new \Toolkit\Web\ViewRenderer([
912
'viewsPath' => __DIR__ . '/views',
1013
'layout' => 'my-layout.php',
1114
]);
1215

13-
echo $renderer->render('home/index');
16+
echo $renderer->render('home/index', ['name' => 'inhere']);
1417
```
1518

1619
- setting page attrs and add assets
@@ -33,6 +36,9 @@ $renderer
3336
- in view template file.
3437

3538
```php
39+
/**
40+
* @var \Toolkit\Web\ViewRenderer $this
41+
*/
3642

3743
<!doctype html>
3844
<html lang="en">
@@ -42,7 +48,7 @@ $renderer
4248
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
4349
<link href="/assets/libs/bootstrap/bootstrap.min.css" rel="stylesheet">
4450
<link href="/assets/src/app.css" rel="stylesheet">
45-
<title>Hello, world!</title>
51+
<title><?= $this->getTitle('Hello, world!') ?></title>
4652
<!-- output page assets -->
4753
<?php $this->dumpTopAssets() ?>
4854
</head>
@@ -57,7 +63,7 @@ $renderer
5763
{__CONTENT__}
5864
</div>
5965
<aside class="col-md-4">
60-
sadebar ....
66+
sadebar .... my name is: <?= $name ?>
6167
</aside>
6268
</div>
6369
</main>

composer.json

+7-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
"name": "toolkit/web-utils",
33
"type": "library",
44
"description": "a simple web tool library of the php",
5-
"keywords": ["library"],
5+
"keywords": [
6+
"library",
7+
"view-renderer"
8+
],
69
"homepage": "https://github.com/php-toolkit/web-utils",
710
"license": "MIT",
811
"authors": [
@@ -13,11 +16,12 @@
1316
}
1417
],
1518
"require": {
16-
"php": ">=7.0.0"
19+
"php": ">=7.0.0",
20+
"toolkit/file-utils": "~1.0"
1721
},
1822
"autoload": {
1923
"psr-4": {
20-
"Toolkit\\Web\\" : "src/"
24+
"Toolkit\\Web\\": "src/"
2125
}
2226
},
2327
"suggest": {

src/Helper/CurlHelper.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public static function fetchImg(string $imgUrl, string $savePath, string $rename
7373
$imgData = self::execute($ch);
7474

7575
Directory::create($savePath);
76-
file_put_contents($imgFile, $imgData);
76+
\file_put_contents($imgFile, $imgData);
7777

7878
return $imgFile;
7979
}

src/Util/APIAccessChecker.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class APIAccessChecker
4141
*/
4242
public function __construct($ips, $hosts = null)
4343
{
44-
$this->hostname = \defined('HOSTNAME') ? HOSTNAME : explode('.', gethostname())[0];
44+
$this->hostname = \defined('HOSTNAME') ? \HOSTNAME : explode('.', gethostname())[0];
4545
$this->allowedIps = (array)$ips;
4646
$this->allowedHosts = (array)$hosts;
4747
}

src/Util/RequestUtil.php

+3-4
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ public static function safePostVars()
116116
if (!$_POST || !\is_array($_POST)) {
117117
$_POST = [];
118118
} else {
119-
$_POST = array_map('htmlspecialchars', $_POST);
119+
$_POST = \array_map('htmlspecialchars', $_POST);
120120
}
121121
}
122122

@@ -154,7 +154,6 @@ public static function buildQueryParams($data, $separator = '/'): array
154154
}
155155

156156
unset($arrData);
157-
158157
return $newArr;
159158
}
160159

@@ -163,7 +162,7 @@ public static function buildQueryParams($data, $separator = '/'): array
163162
* @param string $default
164163
* @return mixed
165164
*/
166-
public static function serverParam($name, $default = '')
165+
public static function serverParam(string $name, $default = '')
167166
{
168167
return self::server($name, $default);
169168
}
@@ -174,7 +173,7 @@ public static function serverParam($name, $default = '')
174173
* @param string $default
175174
* @return mixed
176175
*/
177-
public static function server($name, $default = '')
176+
public static function server(string $name, $default = '')
178177
{
179178
$name = \strtoupper($name);
180179

src/ViewRenderer.php

+16-15
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,11 @@ public function renderPartial(string $view, array $data = []): string
9999
/**
100100
* @param string $content
101101
* @param array $data
102-
* @param string|null $layout override default layout file
102+
* @param string $layout override default layout file
103103
* @return string
104104
* @throws \RuntimeException
105105
*/
106-
public function renderBody(string $content, array $data = [], $layout = null): string
106+
public function renderBody(string $content, array $data = [], string $layout = ''): string
107107
{
108108
return $this->renderContent($content, $data, $layout);
109109
}
@@ -115,7 +115,7 @@ public function renderBody(string $content, array $data = [], $layout = null): s
115115
* @return string
116116
* @throws \RuntimeException
117117
*/
118-
public function renderContent(string $content, array $data = [], $layout = null): string
118+
public function renderContent(string $content, array $data = [], string $layout = ''): string
119119
{
120120
// render layout
121121
if ($layout = $layout ?: $this->layout) {
@@ -138,11 +138,15 @@ public function renderContent(string $content, array $data = [], $layout = null)
138138
* @return string|null
139139
* @throws \RuntimeException
140140
*/
141-
public function include(string $view, array $data = [], $outputIt = true)
141+
public function include(string $view, array $data = [], $outputIt = true): string
142142
{
143+
if (!$view) {
144+
return '';
145+
}
146+
143147
if ($outputIt) {
144148
echo $this->fetch($view, $data);
145-
return null;
149+
return '';
146150
}
147151

148152
return $this->fetch($view, $data);
@@ -153,30 +157,27 @@ public function include(string $view, array $data = [], $outputIt = true)
153157
* throws RuntimeException if $viewsPath . $view does not exist
154158
* @param string $view
155159
* @param array $data
156-
* @return mixed
160+
* @return string
157161
* @throws \RuntimeException
158162
*/
159-
public function fetch(string $view, array $data = [])
163+
public function fetch(string $view, array $data = []): string
160164
{
165+
if (!$view) {
166+
return '';
167+
}
168+
161169
$file = $this->getViewFile($view);
162170

163171
if (!\is_file($file)) {
164172
throw new \RuntimeException("cannot render '$view' because the view file does not exist. File: $file");
165173
}
166174

167-
/*
168-
foreach ($data as $k=>$val) {
169-
if (in_array($k, array_keys($this->attributes))) {
170-
throw new \InvalidArgumentException("Duplicate key found in data and renderer attributes. " . $k);
171-
}
172-
}
173-
*/
174175
$data = \array_merge($this->attributes, $data);
175176

176177
try {
177178
\ob_start();
178179
$this->protectedIncludeScope($file, $data);
179-
$output = ob_get_clean();
180+
$output = \ob_get_clean();
180181
} catch (\Throwable $e) { // PHP 7+
181182
\ob_end_clean();
182183
throw new \RuntimeException("render view file [$file] is failure", -500, $e);

0 commit comments

Comments
 (0)