Completed
Push — master ( dd6614...ca1fcc )
by Alexander
51:04 queued 46:47
created

Enumerator::getFileFullPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
/*
3
 * Go! AOP framework
4
 *
5
 * @copyright Copyright 2015, Lisachenko Alexander <[email protected]>
6
 *
7
 * This source file is subject to the license that is bundled
8
 * with this source code in the file LICENSE.
9
 */
10
11
namespace Go\Instrument\FileSystem;
12
13
/**
14
 * Enumerates files in the concrete directory, applying filtration logic
15
 */
16
class Enumerator
17
{
18
19
    /**
20
     * Path to the root directory, where enumeration should start
21
     *
22
     * @var string
23
     */
24
    private $rootDirectory;
25
26
    /**
27
     * List of additional include paths, should be below rootDirectory
28
     *
29
     * @var array
30
     */
31
    private $includePaths;
32
33
    /**
34
     * List of additional exclude paths, should be below rootDirectory
35
     *
36
     * @var array
37
     */
38
    private $excludePaths;
39
40
    /**
41
     * Initializes an enumerator
42
     *
43
     * @param string $rootDirectory Path to the root directory
44
     * @param array  $includePaths  List of additional include paths
45
     * @param array  $excludePaths  List of additional exclude paths
46
     */
47 6
    public function __construct($rootDirectory, array $includePaths = [], array $excludePaths = [])
48
    {
49 6
        $this->rootDirectory = $rootDirectory;
50 6
        $this->includePaths = $includePaths;
51 6
        $this->excludePaths = $excludePaths;
52 6
    }
53
54
    /**
55
     * Returns an enumerator for files
56
     *
57
     * @return \CallbackFilterIterator|\RecursiveIteratorIterator|\SplFileInfo[]
58
     */
59 6
    public function enumerate()
60
    {
61 6
        $iterator = new \RecursiveIteratorIterator(
62 6
            new \RecursiveDirectoryIterator(
63 6
                $this->rootDirectory,
64 6
                \FilesystemIterator::SKIP_DOTS
65
            )
66
        );
67
68 6
        $callback = $this->getFilter();
69 6
        $iterator = new \CallbackFilterIterator($iterator, $callback);
70
71 6
        return $iterator;
72
    }
73
74
    /**
75
     * Returns a filter callback for enumerating files
76
     *
77
     * @return \Closure
78
     */
79 6
    public function getFilter()
80
    {
81 6
        $rootDirectory = $this->rootDirectory;
82 6
        $includePaths = $this->includePaths;
83 6
        $excludePaths = $this->excludePaths;
84
85 6
        return function (\SplFileInfo $file) use ($rootDirectory, $includePaths, $excludePaths) {
86
87 6
            if ($file->getExtension() !== 'php') {
88
                return false;
89
            };
90
91 6
            $fullPath = $this->getFileFullPath($file);
92
            // Do not touch files that not under rootDirectory
93 6
            if (strpos($fullPath, $rootDirectory) !== 0) {
94
                return false;
95
            }
96
97 6
            if (!empty($includePaths)) {
98 1
                $found = false;
99 1
                foreach ($includePaths as $includePattern) {
100 1
                    if (fnmatch("{$includePattern}*", $fullPath)) {
101 1
                        $found = true;
102 1
                        break;
103
                    }
104
                }
105 1
                if (!$found) {
106
                    return false;
107
                }
108
            }
109
110 6
            foreach ($excludePaths as $excludePattern) {
111 4
                if (fnmatch("{$excludePattern}*", $fullPath)) {
112 4
                    return false;
113
                }
114
            }
115
116 4
            return true;
117 6
        };
118
    }
119
120
    /**
121
     * Return the real path of the given file
122
     *
123
     * This is used for testing purpose with virtual file system.
124
     * In a vfs the 'realPath' methode will always return false.
125
     * So we have a chance to mock this single function to return different path.
126
     *
127
     * @param \SplFileInfo $file
128
     *
129
     * @return string
130
     */
131
    protected function getFileFullPath(\SplFileInfo $file)
132
    {
133
        return $file->getRealPath();
134
    }
135
136
}
137