Filters
7 months ago
Scanning
5 years ago
AbstractFileObject.php
1 year ago
AbstractFilesystemScanner.php
2 months ago
DebugLogReader.php
2 years ago
DirectoryListing.php
5 months ago
DiskWriteCheck.php
5 months ago
FileObject.php
1 year ago
Filesystem.php
7 months ago
FilesystemExceptions.php
5 years ago
FilesystemScanner.php
1 week ago
FilesystemScannerDto.php
1 week ago
FilterableDirectoryIterator.php
1 year ago
LegacyFileRulesTrait.php
1 week ago
LogCleanup.php
5 months ago
LogFiles.php
1 year ago
MissingFileException.php
3 years ago
OPcache.php
5 months ago
PartIdentifier.php
8 months ago
PathChecker.php
2 years ago
PathIdentifier.php
7 months ago
Permissions.php
1 week ago
WpUploadsFolderSymlinker.php
1 week ago
LegacyFileRulesTrait.php
133 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPStaging\Framework\Filesystem; |
| 4 | |
| 5 | /** |
| 6 | * Normalizes legacy file exclusion rules for filesystem scanners. |
| 7 | */ |
| 8 | trait LegacyFileRulesTrait |
| 9 | { |
| 10 | /** |
| 11 | * @param array $fileRules |
| 12 | * @return array |
| 13 | */ |
| 14 | protected function extractLegacyFileNameRules(array $fileRules): array |
| 15 | { |
| 16 | $normalizedRules = []; |
| 17 | |
| 18 | foreach ($fileRules as $fileRule) { |
| 19 | if (!is_string($fileRule)) { |
| 20 | continue; |
| 21 | } |
| 22 | |
| 23 | $fileRule = trim($fileRule); |
| 24 | if ($fileRule === '') { |
| 25 | continue; |
| 26 | } |
| 27 | |
| 28 | $fileRule = $this->reduceLegacyPathRuleToFileName($fileRule); |
| 29 | |
| 30 | if ($fileRule !== '') { |
| 31 | $normalizedRules[] = $fileRule; |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | return array_values(array_unique($normalizedRules)); |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * @param array $fileRules |
| 40 | * @return array |
| 41 | */ |
| 42 | protected function extractFileExtensions(array $fileRules): array |
| 43 | { |
| 44 | $extensions = []; |
| 45 | |
| 46 | foreach ($fileRules as $fileRule) { |
| 47 | if (!is_string($fileRule)) { |
| 48 | continue; |
| 49 | } |
| 50 | |
| 51 | $fileRule = trim($fileRule); |
| 52 | if ($this->getLegacyVcsDirectoryName($fileRule) !== '') { |
| 53 | continue; |
| 54 | } |
| 55 | |
| 56 | // Only glob extension rules (`*.log`) exclude by extension; a filename rule like |
| 57 | // `wp-staging-optimizer.php` must not turn `.php` into an ignored extension. |
| 58 | if (strpos($fileRule, '*.') !== 0) { |
| 59 | continue; |
| 60 | } |
| 61 | |
| 62 | $fileRule = ltrim($fileRule, '*'); |
| 63 | $position = strrpos($fileRule, '.'); |
| 64 | if ($position === false || $position === strlen($fileRule) - 1) { |
| 65 | continue; |
| 66 | } |
| 67 | |
| 68 | $extension = strtolower(substr($fileRule, $position + 1)); |
| 69 | if ($extension !== '') { |
| 70 | $extensions[] = $extension; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | return array_values(array_unique($extensions)); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * @param array $fileRules |
| 79 | * @return array |
| 80 | */ |
| 81 | protected function extractLegacyFolderNameRules(array $fileRules): array |
| 82 | { |
| 83 | $folderRules = []; |
| 84 | |
| 85 | foreach ($fileRules as $fileRule) { |
| 86 | if (!is_string($fileRule)) { |
| 87 | continue; |
| 88 | } |
| 89 | |
| 90 | $vcsDirectoryName = $this->getLegacyVcsDirectoryName($fileRule); |
| 91 | if ($vcsDirectoryName === '') { |
| 92 | continue; |
| 93 | } |
| 94 | |
| 95 | $folderRules[] = 'name_exact_matches ' . $vcsDirectoryName; |
| 96 | } |
| 97 | |
| 98 | return array_values(array_unique($folderRules)); |
| 99 | } |
| 100 | |
| 101 | private function getLegacyVcsDirectoryName(string $fileRule): string |
| 102 | { |
| 103 | $fileRule = trim($fileRule); |
| 104 | if ($fileRule === '') { |
| 105 | return ''; |
| 106 | } |
| 107 | |
| 108 | $fileRule = strtolower($this->reduceLegacyPathRuleToFileName($fileRule)); |
| 109 | if (strpos($fileRule, '*.') === 0) { |
| 110 | $directoryName = substr($fileRule, 2); |
| 111 | } elseif (strpos($fileRule, '.') === 0) { |
| 112 | $directoryName = substr($fileRule, 1); |
| 113 | } else { |
| 114 | return ''; |
| 115 | } |
| 116 | |
| 117 | if (!in_array($directoryName, ['git', 'svn', 'hg'], true)) { |
| 118 | return ''; |
| 119 | } |
| 120 | |
| 121 | return '.' . $directoryName; |
| 122 | } |
| 123 | |
| 124 | private function reduceLegacyPathRuleToFileName(string $fileRule): string |
| 125 | { |
| 126 | if (strpos($fileRule, '/') !== false || strpos($fileRule, '\\') !== false) { |
| 127 | return basename(str_replace('\\', '/', $fileRule)); |
| 128 | } |
| 129 | |
| 130 | return $fileRule; |
| 131 | } |
| 132 | } |
| 133 |