-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
270 lines (234 loc) · 9.98 KB
/
index.php
File metadata and controls
270 lines (234 loc) · 9.98 KB
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
<?php
/*
* Copyright (c) 2025 Bloxtor (http://bloxtor.com) and Joao Pinto (http://jplpinto.com)
*
* Multi-licensed: BSD 3-Clause | Apache 2.0 | GNU LGPL v3 | HLNC License (http://bloxtor.com/LICENSE_HLNC.md)
* Choose one license that best fits your needs.
*
* Original PHP Spring Lib Repo: https://github.com/a19836/php-spring-lib/
* Original Bloxtor Repo: https://github.com/a19836/bloxtor
*
* YOU ARE NOT AUTHORIZED TO MODIFY OR REMOVE ANY PART OF THIS NOTICE!
*/
include_once __DIR__ . "/../config.php";
include_once __DIR__ . "/config.php";
echo $style;
$BeanFactory = new BeanFactory();
//$BeanFactory->setCacheRootPath( sys_get_temp_dir() . "/cache/spring/odi/" ); //optional: set cache to be faster
$external_vars = array(
"my_name" => $my_name,
"app_path" => $app_path,
//"GLOBALS" => $GLOBALS,
//other vars... You can also add here $_GET, $_POST, $GLOBALS, etc...
);
$BeanFactory->init(array(
"file" => __DIR__ . "/assets/beans.xml",
"external_vars" => $external_vars,
"settings" => array(
array( //Bean Person
"bean" => array(
"name" => "PersonX",
"path" => "Person",
"path_prefix" => __DIR__ . "/assets/",
"constructor_args" => array(
array("value" => "Female"),
//other arguments
),
"properties" => array(
array("name" => "age", "value" => "31"),
//other properties
),
"functions" => array(
array("name" => "eat", "parameters" => array(
array("value" => "orange")
)),
//other functions
)
)
)
//other beans...
)
));
//append new objects to existing factory
$BeanFactory->addObjects(array(
"Product" => (object) array('name' => 'Coca Cola', 'type' => 'Light', 'flavour' => 'Lemon'),
//other objects...
));
echo '<h1>PHP Spring Lib - ODI</h1>
<p>PHP ODI - Object Dependency Injection.</p>
<div class="note">
<span>
Learn how to use ODI - Object Dependency Injection - in PHP, through this library, by creating xml beans that point to PHP classes.<br/>
For more details please check the files in \'examples/odi\' folder.
</span>
<div class="features">
<h2>Features:</h2>
<ul>
<li>Map XML beans to PHP classes - also including namespaces.</li>
<li>Organize beans into multiple XML files across different folders.</li>
<li>Include bean definition files inside other bean files.</li>
<li>Group beans by application layer, placing each layer in its own XML file, and load only the beans required for that layer, when necessary.</li>
<li>Extend beans based on other bean definitions.</li>
<li>Define variables in XML that can be passed as constructor arguments, class properties, or method parameters.</li>
<li>Inject other beans into constructor arguments, class properties, or method parameters within your bean definitions.</li>
<li>Set object properties using other beans or XML-defined variables.</li>
<li>Create dynamic variables whose values are generated by PHP code.</li>
<li>Call object methods automatically after a bean is instantiated.</li>
<li>Invoke standalone PHP functions or bean methods directly from XML.</li>
<li>Instantiate all beans from a file or by group or only specific ones as needed.</li>
<li>Retrieve beans or instantiated objects by group.</li>
</ul>
</div>
</div>';
echo '
<h3>Some Available BeanFactory Methods:</h3>
<div class="code input">
<textarea readonly>
$BeanFactory = new BeanFactory();
$BeanFactory->setCacheRootPath( sys_get_temp_dir() . "/cache/spring/" ); //optional: set cache to be faster
$data = array(
"external_vars" => null, //optional associative array with key-value items
"file" => "bean.xml", //file path with xml beans
"settings" => null, //optional array with associative arrays inside, with the following keys: "import", "bean", "var" and "function".
);
$BeanFactory->init($data); //init factory based in $data
$BeanFactory->add($data); //add to factory based in $data
$BeanFactory->reset(); //clean saved data
$BeanFactory->getSettingsFromFile($file_path); //return settings after parsing xml file
$BeanFactory->getBeansFromSettings($settings, &$sort_elements = false); //return beans after parsing xml file
$BeanFactory->initObjects(); //init all parsed objects
$BeanFactory->initObjectsByGroup($group); //init parsed objects that contains a specific group
$BeanFactory->initObject($bean_name, $launch_exception = true); //init a specific object
$BeanFactory->initFunction($function, $launch_exception = true); //init a specific function
$BeanFactory->addBeans($beans); //add new beans
$BeanFactory->getBeans(); //get parsed beans
$BeanFactory->getBean($bean_name); //get a specific beans
$BeanFactory->getBeansByGroup($group); //get parsed beans by group
$BeanFactory->addObjects($objs); //add new objects
$BeanFactory->getObjects(); //get initialized objects
$BeanFactory->getObject($obj_name); //get a specific initialized object
$BeanFactory->getObjectsByGroup($group); //get initialized objects by group
$BeanFactory->setCacheRootPath($dir_path); //set cache folder path, so next time it does not need to parse the xml file
$BeanFactory->setCacheHandler(XmlSettingsCacheHandler $XmlSettingsCacheHandler); //set a different cache engine
</textarea>
</div>
<h3>Examples:</h3>';
//print initial code
$code = '$BeanFactory = new BeanFactory();
$BeanFactory->setCacheRootPath( sys_get_temp_dir() . "/cache/spring/" ); //optional: set cache to be faster
$BeanFactory->init(array(
"file" => __DIR__ . "/assets/beans.xml",
"external_vars" => $external_vars,
"settings" => array(
array( //Bean Person
"bean" => array(
"name" => "PersonX",
"path" => "Person",
"path_prefix" => __DIR__ . "/assets/",
"constructor_args" => array(
array("value" => "Female"),
//other arguments
),
"properties" => array(
array("name" => "age", "value" => "31"),
//other properties
),
"functions" => array(
array("name" => "eat", "parameters" => array(
array("value" => "orange")
)),
//other functions
)
)
)
//other beans...
)
));
//append new objects to existing factory
$BeanFactory->addObjects(array(
"Product" => (object) array("name" => "Coca Cola", "type" => "Light", "flavour" => "Lemon"),
//other objects...
));';
echo '<div>
<h4>Start Bean Factory:</h4>
<div class="code input">
<textarea readonly>' . $code . '</textarea>
</div>
<div class="code xml">
<textarea readonly>' . file_get_contents(__DIR__ . "/assets/beans.xml") . '</textarea>
</div>
</div>
<h4>Get Beans Objects:</h4>';
//init individual objects and print them
$BeanFactory->initObject("PersonX", $launch_exception = true);
$Person = $BeanFactory->getObject("PersonX");
$code = '$BeanFactory->initObject("PersonX", $launch_exception = true);
$Person = $BeanFactory->getObject("PersonX");
echo $Person->toString();';
printTest("PersonX", $code, $Person->toString());
$BeanFactory->initObject("Person1");
$Person = $BeanFactory->getObject("Person1");
$code = '$BeanFactory->initObject("Person1");
$Person = $BeanFactory->getObject("Person1");
echo $Person->toString();';
printTest("Person1", $code, $Person->toString());
$BeanFactory->initObject("Person2");
$Person = $BeanFactory->getObject("Person2");
$code = '$BeanFactory->initObject("Person2");
$Person = $BeanFactory->getObject("Person2");
echo $Person->toString();';
printTest("Person2", $code, $Person->toString());
//init all objects
$BeanFactory->initObjects();
printCode('Init all objects', '$BeanFactory->initObjects();
//we do NOT need to call anymore the \'$BeanFactory->initObject("XXX")\'');
//print other objects
$Person = $BeanFactory->getObject("Person3");
$code = '$Person = $BeanFactory->getObject("Person3");
echo $Person->toString();';
printTest("Person3", $code, $Person->toString(), "short", "");
$Person = $BeanFactory->getObject("Person4");
$code = '$Person = $BeanFactory->getObject("Person4");
echo $Person->toString();';
printTest("Person4", $code, $Person->toString(), "short", "");
$Person = $BeanFactory->getObject("Person5");
$code = '$Person = $BeanFactory->getObject("Person5");
echo $Person->toString();';
printTest("Person5", $code, $Person->toString());
$Child = $BeanFactory->getObject("Child1");
$code = '$Child = $BeanFactory->getObject("Child1");
echo $Child->toString();';
printTest("Child1", $code, $Child->toString());
$Child = $BeanFactory->getObject("Child2");
$code = '$Child = $BeanFactory->getObject("Child2");
echo $Child->toString();';
printTest("Child2", $code, $Child->toString(), "short", "");
$Child = $BeanFactory->getObject("Child3");
$code = '$Child = $BeanFactory->getObject("Child3");
echo $Child->toString();';
printTest("Child3", $code, $Child->toString(), "short", "");
echo '<hr style="margin-top:40px;"/>
<h4>Parsed Vars/Objects/Beans:</h4>';
//print vars
$code = 'echo $BeanFactory->getObject("female_gender");
echo $BeanFactory->getObject("male_gender");
//etc...';
$output = "- female_gender: " . $BeanFactory->getObject("female_gender") . "\n"
. "- male_gender: " . $BeanFactory->getObject("male_gender") . "\n"
. "- food: " . print_r($BeanFactory->getObject("food"), true) . "\n"
. "- props: " . print_r($BeanFactory->getObject("props"), true);
printTest('Loaded Vars from BeanFactory', $code, $output, "short", "");
//print all objects and vars
$objects = $BeanFactory->getObjects();
printTest('Loaded Objects from BeanFactory', '$objects = $BeanFactory->getObjects();', print_r($objects, true), "short", "");
//print all objects that contains the 'children' group
$objects = $BeanFactory->getObjectsByGroup('children');
printTest('Loaded Objects from BeanFactory that contains the "children" group', '$objects = $BeanFactory->getObjectsByGroup("children");', print_r($objects, true), "short", "");
//print all beans
$beans = $BeanFactory->getBeans();
printTest('Loaded Beans from BeanFactory', '$beans = $BeanFactory->getBeans();', print_r($beans, true), "short", "");
//print all beans that contains the 'children' group
$beans = $BeanFactory->getBeansByGroup('children');
printTest('Loaded Beans from BeanFactory that contains the "children" group', '$beans = $BeanFactory->getBeansByGroup("children");', print_r($beans, true), "short", "");
echo "<br/><br/>";
?>