diff options
| author | Jan-Hendrik Willms <tleilax+github@gmail.com> | 2021-07-22 16:07:19 +0200 |
|---|---|---|
| committer | Jan-Hendrik Willms <tleilax+github@gmail.com> | 2021-07-22 16:19:12 +0200 |
| commit | a3da1483a9e689846179159355badfec8073dbec (patch) | |
| tree | 770dcca6bdf5f6f2a11b0e7fcbbeda6919a3fc52 /lib/classes/StudipFileloader.php | |
current code from svn, revision 62608
Diffstat (limited to 'lib/classes/StudipFileloader.php')
| -rw-r--r-- | lib/classes/StudipFileloader.php | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/lib/classes/StudipFileloader.php b/lib/classes/StudipFileloader.php new file mode 100644 index 0000000..f499b68 --- /dev/null +++ b/lib/classes/StudipFileloader.php @@ -0,0 +1,63 @@ +<?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) { + 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; + } +} |
