3 * This file is part of PHPUnit.
5 * (c) Sebastian Bergmann <sebastian@phpunit.de>
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
12 * Utility methods to load PHP sourcefiles.
14 * @since Class available since Release 2.3.0
16 class PHPUnit_Util_Fileloader
19 * Checks if a PHP sourcefile is readable.
20 * The sourcefile is loaded through the load() method.
22 * @param string $filename
26 * @throws PHPUnit_Framework_Exception
28 public static function checkAndLoad($filename)
30 $includePathFilename = stream_resolve_include_path($filename);
32 if (!$includePathFilename || !is_readable($includePathFilename)) {
33 throw new PHPUnit_Framework_Exception(
34 sprintf('Cannot open file "%s".' . "\n", $filename)
38 self::load($includePathFilename);
40 return $includePathFilename;
44 * Loads a PHP sourcefile.
46 * @param string $filename
50 * @since Method available since Release 3.0.0
52 public static function load($filename)
54 $oldVariableNames = array_keys(get_defined_vars());
56 include_once $filename;
58 $newVariables = get_defined_vars();
59 $newVariableNames = array_diff(
60 array_keys($newVariables),
64 foreach ($newVariableNames as $variableName) {
65 if ($variableName != 'oldVariableNames') {
66 $GLOBALS[$variableName] = $newVariables[$variableName];