Skip to content

Commit c763e59

Browse files
committed
Make examples easier to understand
1 parent 5bc603d commit c763e59

File tree

4 files changed

+69
-27
lines changed

4 files changed

+69
-27
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
examples/dbconf.php
12
api
23
coverage
34
vendor

examples/config.php

Lines changed: 0 additions & 27 deletions
This file was deleted.

examples/config.phtml

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
/* This example shows how to set up a simple page that asks user for some
4+
configuration options, such as a database configuration, and stores that into
5+
a PHP file, which can, for example, later be used in an application. */
6+
7+
require __DIR__ . '/../src/autoload.php';
8+
9+
$configFile = __DIR__ . '/dbconf.php';
10+
$config = [
11+
'hostname' => '',
12+
'database' => '',
13+
'username' => '',
14+
'password' => '',
15+
];
16+
17+
if (isset($_POST['config'])) {
18+
$store = array_intersect_key($_POST['config'], $config);
19+
$encoder = new \Riimu\Kit\PHPEncoder\PHPEncoder();
20+
21+
file_put_contents($configFile, sprintf(
22+
'<?php return %s;' . PHP_EOL,
23+
$encoder->encode(array_map('strval', $store))
24+
));
25+
}
26+
27+
if (file_exists($configFile)) {
28+
$config = (require $configFile) + $config;
29+
}
30+
31+
?>
32+
<!DOCTYPE html>
33+
<html>
34+
<head>
35+
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
36+
<title>Database configuration</title>
37+
</head>
38+
<body>
39+
<h1>Database configuration</h1>
40+
<?php
41+
42+
if (isset($_POST['config'])) {
43+
echo ' <p>Configuration saved!</p>' . PHP_EOL;
44+
}
45+
46+
?>
47+
<form method="post">
48+
<table>
49+
<?php
50+
51+
foreach ($config as $name => $value) {
52+
printf(
53+
' <tr><td>%s</td><td><input type="text" name="config[%s]" value="%s" /></td></tr>' . PHP_EOL,
54+
ucfirst($name),
55+
$name,
56+
htmlspecialchars($value, ENT_QUOTES | ENT_HTML5, 'UTF-8')
57+
);
58+
}
59+
60+
?>
61+
</table>
62+
<div><input type="submit" value="Save Configuration" /></div>
63+
</form>
64+
</body>
65+
</html>

examples/writing.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
<?php
22

3+
/* This simple example demonstrates how to encode a simple array using default
4+
settings and while using minimal whitespace */
5+
36
require __DIR__ . '/../src/autoload.php';
47

58
$encoder = new \Riimu\Kit\PHPEncoder\PHPEncoder();

0 commit comments

Comments
 (0)