Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php declare(strict_types=1);
2
/*
3
 * This file is part of PHPUnit.
4
 *
5
 * (c) Sebastian Bergmann <sebastian@phpunit.de>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace PHPUnit\Util;
11
 
12
use const DIRECTORY_SEPARATOR;
13
use function array_diff;
14
use function array_keys;
15
use function fopen;
16
use function get_defined_vars;
17
use function sprintf;
18
use function stream_resolve_include_path;
19
 
20
/**
21
 * @internal This class is not covered by the backward compatibility promise for PHPUnit
22
 */
23
final class FileLoader
24
{
25
    /**
26
     * Checks if a PHP sourcecode file is readable. The sourcecode file is loaded through the load() method.
27
     *
28
     * As a fallback, PHP looks in the directory of the file executing the stream_resolve_include_path function.
29
     * We do not want to load the Test.php file here, so skip it if it found that.
30
     * PHP prioritizes the include_path setting, so if the current directory is in there, it will first look in the
31
     * current working directory.
32
     *
33
     * @throws Exception
34
     */
35
    public static function checkAndLoad(string $filename): string
36
    {
37
        $includePathFilename = stream_resolve_include_path($filename);
38
 
39
        $localFile = __DIR__ . DIRECTORY_SEPARATOR . $filename;
40
 
41
        if (!$includePathFilename ||
42
            $includePathFilename === $localFile ||
43
            !self::isReadable($includePathFilename)) {
44
            throw new Exception(
45
                sprintf('Cannot open file "%s".' . "\n", $filename)
46
            );
47
        }
48
 
49
        self::load($includePathFilename);
50
 
51
        return $includePathFilename;
52
    }
53
 
54
    /**
55
     * Loads a PHP sourcefile.
56
     */
57
    public static function load(string $filename): void
58
    {
59
        $oldVariableNames = array_keys(get_defined_vars());
60
 
61
        /**
62
         * @noinspection PhpIncludeInspection
63
         *
64
         * @psalm-suppress UnresolvableInclude
65
         */
66
        include_once $filename;
67
 
68
        $newVariables = get_defined_vars();
69
 
70
        foreach (array_diff(array_keys($newVariables), $oldVariableNames) as $variableName) {
71
            if ($variableName !== 'oldVariableNames') {
72
                $GLOBALS[$variableName] = $newVariables[$variableName];
73
            }
74
        }
75
    }
76
 
77
    /**
78
     * @see https://github.com/sebastianbergmann/phpunit/pull/2751
79
     */
80
    private static function isReadable(string $filename): bool
81
    {
82
        return @fopen($filename, 'r') !== false;
83
    }
84
}