1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* You may not change or alter any portion of this comment or credits |
4
|
|
|
* of supporting developers from this source code or any supporting source code |
5
|
|
|
* which is considered copyrighted (c) material of the original comment or credit authors. |
6
|
|
|
* |
7
|
|
|
* This program is distributed in the hope that it will be useful, |
8
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
9
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Xoops\Upgrade; |
13
|
|
|
|
14
|
|
|
use Xmf\Assert; |
15
|
|
|
use InvalidArgumentException; |
16
|
|
|
use RecursiveIteratorIterator; |
17
|
|
|
use RecursiveDirectoryIterator; |
18
|
|
|
use SplFileInfo; |
19
|
|
|
use SplFileObject; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* XOOPS Upgrade ScannerWalker |
23
|
|
|
* |
24
|
|
|
* Scan for files in specified directories with one of the specified file extensions. |
25
|
|
|
* For each matching file, invoke $this->process->inspectFile() |
26
|
|
|
* |
27
|
|
|
* @category Xoops\Upgrade |
28
|
|
|
* @package Xoops |
29
|
|
|
* @author Richard Griffith <[email protected]> |
30
|
|
|
* @copyright 2023 XOOPS Project (https://xoops.org) |
31
|
|
|
* @license GNU GPL 2 or later (https://www.gnu.org/licenses/gpl-2.0.html) |
32
|
|
|
* @link https://xoops.org |
33
|
|
|
*/ |
34
|
|
|
/** |
35
|
|
|
* Scan for files with one of the specified file extensions in the specified directories. |
36
|
|
|
* For each matching file, invoke $this->process->inspectFile() |
37
|
|
|
*/ |
38
|
|
|
|
39
|
|
|
class ScannerWalker |
40
|
|
|
{ |
41
|
|
|
/** |
42
|
|
|
* @var string[] $directories |
43
|
|
|
*/ |
44
|
|
|
protected $directories = array(); |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var string[] $directories |
48
|
|
|
*/ |
49
|
|
|
|
50
|
|
|
protected $extList = array(); |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var ScannerProcess |
54
|
|
|
*/ |
55
|
|
|
private $process; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @var ScannerOutput |
59
|
|
|
*/ |
60
|
|
|
private $output; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* ScannerWalker |
64
|
|
|
* |
65
|
|
|
* @param ScannerProcess $process used to examine or manipulate matching files |
66
|
|
|
* @param ScannerOutput $output handle output from the scanning process |
67
|
|
|
*/ |
68
|
|
|
public function __construct(ScannerProcess $process, ScannerOutput $output) |
69
|
|
|
{ |
70
|
|
|
$this->output = $output; |
71
|
|
|
$this->output->outputStart(); |
72
|
|
|
$this->process = $process; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Add a directory to be scanned |
77
|
|
|
* |
78
|
|
|
* @param string $directory |
79
|
|
|
* @return void |
80
|
|
|
* @throws InvalidArgumentException |
81
|
|
|
*/ |
82
|
|
|
public function addDirectory($directory) |
83
|
|
|
{ |
84
|
|
|
Assert::stringNotEmpty($directory, 'Directory must be a string value'); |
85
|
|
|
Assert::directory($directory, 'Directory must be a directory'); |
86
|
|
|
$this->directories[] = $directory; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Add a file extension to be scanned |
91
|
|
|
* |
92
|
|
|
* @param string $ext |
93
|
|
|
* @return void |
94
|
|
|
* @throws InvalidArgumentException |
95
|
|
|
*/ |
96
|
|
|
public function addExtension($ext) |
97
|
|
|
{ |
98
|
|
|
Assert::stringNotEmpty($ext, 'Extension must be a nonempty string'); |
99
|
|
|
$this->extList[] = $ext; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* ScannerWalker::runScan() walks the directories looking for matching extensions. |
104
|
|
|
* Any matching files will be passed to the ScannerProcess specified for this instance. |
105
|
|
|
*/ |
106
|
|
|
public function runScan() |
107
|
|
|
{ |
108
|
|
|
foreach ($this->directories as $workingDirectory) { |
109
|
|
|
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($workingDirectory)); |
110
|
|
|
/** @var SplFileInfo $fileInfo */ |
111
|
|
|
foreach ($iterator as $fileInfo) { |
112
|
|
|
if ($fileInfo->isDir() || !$fileInfo->isReadable()) { |
113
|
|
|
continue; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
$ext = $fileInfo->getExtension(); |
117
|
|
|
if (in_array($ext, $this->extList, true)) { |
118
|
|
|
$this->output->addToCount('checked'); |
|
|
|
|
119
|
|
|
/** @var SplFileObject $file */ |
120
|
|
|
$length = $fileInfo->getSize(); |
121
|
|
|
if ($length > 0) { |
122
|
|
|
$this->process->inspectFile($fileInfo); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
$this->output->outputWrapUp(); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|