-
Notifications
You must be signed in to change notification settings - Fork 0
/
jrmvc.lib.php
146 lines (126 loc) · 4.29 KB
/
jrmvc.lib.php
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
<?php
/*
* jrMvc (JackRabbitMvc -- formerly barebonesmvc-php)
* is a one-file MVC micro-framework for PHP 5+ (supports 7)
*
* Copyright (c) 2007-2023, George M. Jempty
*
* "Perfection is attained not when there is nothing left to add,
* but when there is nothing left to take away."
* (Antoine de Saint-Exupéry)
*
* LICENSE: https://opensource.org/licenses/MIT
*
* USAGE:
*
<?php
require('jrmvc.lib.php'); // 1) require library
class DemoMTO extends JrMvcMTO { // 1a) optionally extend MTO for non-template output, e.g. JSON
function onNullTemplate() {
echo json_encode($this->model);
// Instead a binary such as an xls or pdf could be sent
}
}
class DemoController extends AbstractJrMvcController { // 2) extend Controller
function applyInputToModel() { // 3) implement only required method
// Sample demo.tpl.php content: <pre>$model: <?php print_r($model); ?></pre>
$mto = new JrMvcMTO('demo.tpl.php'); // 4) instantiate
// To output json instead use extended MTO above: $mto = new DemoMTO(JrMvcMTO::NULL_TPL);
// Or in PHP 7 you can use an inner class instead of defining DemoMTO above this class:
/*
$mto = new class(JrMvcMTO::NULL_TPL) extends JrMvcMTO {
function onNullTemplate() {
echo json_encode($this->model);
}
};
*//*
$mto->setModelValue('Su', 'Sunday'); // 5) assignments
$mto->setModelValue('Mo', 'Monday');
$mto->setModelValue('Tu', 'Tuesday');
$mto->setModelValue('We', 'Wednesday');
$mto->setModelValues(['Th'=>'Thursday', 'Fr'=>'Friday', 'Sa'=>'Saturday']);
return $mto; // 6) return MTO
}
}
DemoController::sendResponse(new DemoController()); // 7) send response
*
* OUTPUT:
*
$model: Array
(
[Su] => Sunday
[Mo] => Monday
[Tu] => Tuesday
[We] => Wednesday
[Th] => Thursday
[Fr] => Friday
[Sa] => Saturday
)
*/
interface IJrMvcController {
function setMto(IModelXfer $mto);
static function sendResponse(IJrMvcController $controller);
function applyInputToModel();
}
abstract class AbstractJrMvcController implements IJrMvcController {
protected $mto;
function setMto(IModelXfer $mto) {
$this->mto = $mto;
}
static function sendResponse(IJrMvcController $controller) {
$controller->setMto($controller->applyInputToModel());
$controller->mto->applyModelToView();
}
}
interface IModelXfer {
function setView($view);
function setModel($model);
function setModelValue($key, $value);
function applyModelToView();
}
abstract class AbstractMTO implements IModelXfer {
protected $view;
protected $model;
const NULL_TPL = null;
function setView($view) {
$this->view = $view;
}
function setModel($model) {
$this->model = $model;
}
function setModelValue($key, $value) {
$this->model[$key] = $value;
}
function setModelValues($arr) {
foreach ($arr as $key => $value) {
$this->model[$key] = $value;
}
}
protected function unsetNonSessionGlobals() {
if (array_key_exists('_SESSION', $GLOBALS)) {
$session = $GLOBALS['_SESSION'];
}
unset($GLOBALS);
if (isset($session)) {
$GLOBALS['_SESSION'] = $session;
}
}
protected function onNullTemplate() {}
}
class JrMvcMto extends AbstractMTO {
function __construct($view) {
$this->setView($view);
}
function applyModelToView() {
# Ensures view does not have access to get/post variables, thus encouraging
# all access to them to occur within controller's applyInputToModel method
$this->unsetNonSessionGlobals();
if (empty($this->view)) {
$this->onNullTemplate();
}
else {
$model = $this->model;
include($this->view);
}
}
}