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/php-file-iterator.
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 SebastianBergmann\FileIterator;
11
 
12
use function array_filter;
13
use function array_map;
14
use function preg_match;
15
use function realpath;
16
use function str_replace;
17
use function strlen;
18
use function strpos;
19
use function substr;
20
use FilterIterator;
21
 
22
class Iterator extends FilterIterator
23
{
24
    public const PREFIX = 0;
25
 
26
    public const SUFFIX = 1;
27
 
28
    /**
29
     * @var string
30
     */
31
    private $basePath;
32
 
33
    /**
34
     * @var array
35
     */
36
    private $suffixes = [];
37
 
38
    /**
39
     * @var array
40
     */
41
    private $prefixes = [];
42
 
43
    /**
44
     * @var array
45
     */
46
    private $exclude = [];
47
 
48
    public function __construct(string $basePath, \Iterator $iterator, array $suffixes = [], array $prefixes = [], array $exclude = [])
49
    {
50
        $this->basePath = realpath($basePath);
51
        $this->prefixes = $prefixes;
52
        $this->suffixes = $suffixes;
53
        $this->exclude  = array_filter(array_map('realpath', $exclude));
54
 
55
        parent::__construct($iterator);
56
    }
57
 
58
    public function accept(): bool
59
    {
60
        $current  = $this->getInnerIterator()->current();
61
        $filename = $current->getFilename();
62
        $realPath = $current->getRealPath();
63
 
64
        if ($realPath === false) {
65
            return false;
66
        }
67
 
68
        return $this->acceptPath($realPath) &&
69
               $this->acceptPrefix($filename) &&
70
               $this->acceptSuffix($filename);
71
    }
72
 
73
    private function acceptPath(string $path): bool
74
    {
75
        // Filter files in hidden directories by checking path that is relative to the base path.
76
        if (preg_match('=/\.[^/]*/=', str_replace($this->basePath, '', $path))) {
77
            return false;
78
        }
79
 
80
        foreach ($this->exclude as $exclude) {
81
            if (strpos($path, $exclude) === 0) {
82
                return false;
83
            }
84
        }
85
 
86
        return true;
87
    }
88
 
89
    private function acceptPrefix(string $filename): bool
90
    {
91
        return $this->acceptSubString($filename, $this->prefixes, self::PREFIX);
92
    }
93
 
94
    private function acceptSuffix(string $filename): bool
95
    {
96
        return $this->acceptSubString($filename, $this->suffixes, self::SUFFIX);
97
    }
98
 
99
    private function acceptSubString(string $filename, array $subStrings, int $type): bool
100
    {
101
        if (empty($subStrings)) {
102
            return true;
103
        }
104
 
105
        $matched = false;
106
 
107
        foreach ($subStrings as $string) {
108
            if (($type === self::PREFIX && strpos($filename, $string) === 0) ||
109
                ($type === self::SUFFIX &&
110
                 substr($filename, -1 * strlen($string)) === $string)) {
111
                $matched = true;
112
 
113
                break;
114
            }
115
        }
116
 
117
        return $matched;
118
    }
119
}