Newer
Older
<?php
/**
* @author <sebastian@phpunit.de>
* @author <mlunzena@uos.de>
* @copyright 2001-2013 Sebastian Bergmann <sebastian@phpunit.de>
* @license http://www.opensource.org/licenses/BSD-3-Clause The BSD 3-Clause License
*/
class StudipFileloader
{
/**
* Loads a PHP sourcefile and transfers all therein defined
* variables into a specified container.
* Optionally you may inject more bindings into the scope, if the
* sourcefile requires them.
*
* @param string $_filename which file to load
* @param array $_container where to put the new variables into
* @param array $_injected optional bindings, to inject into
* the scope before loading
* @param bool $_allow_overwrite allow overwriting of injected
* @return array names of newly added variables
*/
public static function load($_filename, &$_container, $_injected = [], $_allow_overwrite = false)
{
extract($_injected);
$_oldVariableNames = array_keys(get_defined_vars());
foreach (preg_split('/ /', $_filename, -1, PREG_SPLIT_NO_EMPTY) as $file) {
if (
!file_exists($file)
&& !stream_resolve_include_path($file)
) {
continue;
}
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
include $file;
}
unset($file);
$newVariables = get_defined_vars();
unset($newVariables['_filename']);
unset($newVariables['_container']);
unset($newVariables['_injected']);
unset($newVariables['_allow_overwrite']);
unset($newVariables['_oldVariableNames']);
if ($_allow_overwrite) {
$newVariableNames = array_keys($newVariables);
} else {
$newVariableNames = array_diff(
array_keys($newVariables),
$_oldVariableNames
);
}
foreach ($newVariableNames as $variableName) {
$_container[$variableName] = $newVariables[$variableName];
}
foreach ($_injected as $key => $value) {
if ($$key === $value) {
$newVariableNames = array_diff($newVariableNames, [$key]);
}
}
return $newVariableNames;
}
}