ApplyFiltersTrait.php
2 months ago
ArrayableTrait.php
7 months ago
BatchSizeCalculateTrait.php
7 months ago
BearerTokenTrait.php
4 months ago
BenchmarkTrait.php
2 years ago
BooleanTransientTrait.php
5 years ago
DatabaseSearchReplaceTrait.php
2 weeks ago
DbRowsGeneratorTrait.php
3 years ago
DebugLogTrait.php
1 year ago
DeveloperTimerTrait.php
8 months ago
EndOfLinePlaceholderTrait.php
1 year ago
EventLoggerTrait.php
1 month ago
FileScanToCacheTrait.php
10 months ago
FormatTrait.php
11 months ago
HttpRequestTrait.php
8 months ago
HydrateTrait.php
1 year ago
I18nTrait.php
1 year ago
IpResolverTrait.php
10 months ago
MaintenanceTrait.php
5 months ago
MemoryExhaustTrait.php
2 years ago
MySQLRowsGeneratorTrait.php
7 months ago
NoticesTrait.php
1 day ago
PropertyConstructor.php
5 years ago
RenameTmpDirectoryTrait.php
5 months ago
ResourceTrait.php
4 months ago
RestRequestTrait.php
3 months ago
RestoreFileExclusionTrait.php
1 year ago
SerializeTrait.php
1 year ago
SetTimeLimitTrait.php
1 month ago
SlashTrait.php
1 year ago
SqlCommentTrait.php
3 months ago
TablePrefixValidator.php
3 months ago
UrlTrait.php
1 year ago
ValueGetterTrait.php
1 year ago
WindowsOsTrait.php
1 year ago
FileScanToCacheTrait.php
175 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPStaging\Framework\Traits; |
| 4 | |
| 5 | use Exception; |
| 6 | use RuntimeException; |
| 7 | use WPStaging\Core\Utils\Logger; |
| 8 | use WPStaging\Core\WPStaging; |
| 9 | use WPStaging\Framework\Filesystem\Filesystem; |
| 10 | use WPStaging\Framework\Filesystem\FilterableDirectoryIterator; |
| 11 | use WPStaging\Framework\Utils\Strings; |
| 12 | use WPStaging\Framework\Traits\EndOfLinePlaceholderTrait; |
| 13 | |
| 14 | trait FileScanToCacheTrait |
| 15 | { |
| 16 | use EndOfLinePlaceholderTrait; |
| 17 | |
| 18 | /** @var bool */ |
| 19 | protected $isExcludedWpConfig = false; |
| 20 | |
| 21 | /** @var string */ |
| 22 | protected $pathIdentifier = ''; |
| 23 | |
| 24 | /** |
| 25 | * @var Strings |
| 26 | */ |
| 27 | protected $strUtils; |
| 28 | |
| 29 | /** |
| 30 | * @var Logger |
| 31 | */ |
| 32 | protected $logger; |
| 33 | |
| 34 | /** |
| 35 | * Write contents to a file |
| 36 | * |
| 37 | * @param resource $handle File handle to write to |
| 38 | * @param string $content Content to write to the file |
| 39 | * @return int |
| 40 | * @throws Exception |
| 41 | */ |
| 42 | abstract public function write($handle, $content); |
| 43 | |
| 44 | /** |
| 45 | * Scan Recursively through DirectoryIterator as RecursiveDirectoryIterator is slow |
| 46 | * |
| 47 | * @param resource $filesHandle |
| 48 | * @param string $path |
| 49 | * @param bool $isRecursive |
| 50 | * @param array $excludePaths absolute path of dir/files to exclude |
| 51 | * @param array $excludeSizeRules exclude files by different size comparing rules |
| 52 | * @param string $wpRootPath |
| 53 | * @param bool $shouldScanEmptyDirs |
| 54 | * |
| 55 | * @return int count of files path written to cache file |
| 56 | * @throws Exception |
| 57 | */ |
| 58 | public function scanToCacheFile($filesHandle, $path, $isRecursive = false, $excludePaths = [], $excludeSizeRules = [], $wpRootPath = ABSPATH, bool $shouldScanEmptyDirs = true) |
| 59 | { |
| 60 | if (!is_readable($path)) { |
| 61 | $this->log(sprintf('Skipping! The path "%s" is not readable.', $path), Logger::TYPE_WARNING); |
| 62 | return 0; |
| 63 | } |
| 64 | |
| 65 | $filesystem = WPStaging::make(Filesystem::class); |
| 66 | $normalizedWpRoot = $filesystem->normalizePath($wpRootPath); |
| 67 | |
| 68 | if (is_file($path)) { |
| 69 | $file = str_replace($normalizedWpRoot, '', $filesystem->normalizePath($path, true)); |
| 70 | $file = $this->replaceEOLsWithPlaceholders($file) . PHP_EOL; |
| 71 | if ($this->write($filesHandle, $file)) { |
| 72 | return 1; |
| 73 | } |
| 74 | |
| 75 | return 0; |
| 76 | } |
| 77 | |
| 78 | $filesWrittenToCache = 0; |
| 79 | try { |
| 80 | $iterator = (new FilterableDirectoryIterator()) |
| 81 | ->setDirectory($filesystem->trailingSlashit($path)) |
| 82 | ->setRecursive(false) |
| 83 | ->setDotSkip() |
| 84 | ->setExcludePaths($excludePaths) |
| 85 | ->setExcludeSizeRules($excludeSizeRules) |
| 86 | ->setWpRootPath($wpRootPath) |
| 87 | ->get(); |
| 88 | |
| 89 | foreach ($iterator as $item) { |
| 90 | // Always check link first otherwise it may be treated as directory |
| 91 | $itemPath = $item->getPathname(); |
| 92 | if ($item->isLink()) { |
| 93 | // Allow copying of link if the link's source is a directory |
| 94 | if (is_dir($item->getRealPath()) && $isRecursive) { |
| 95 | $filesWrittenToCache += $this->scanToCacheFile($filesHandle, $itemPath, $isRecursive, $excludePaths, $excludeSizeRules, $wpRootPath, $shouldScanEmptyDirs); |
| 96 | } |
| 97 | |
| 98 | continue; |
| 99 | } |
| 100 | |
| 101 | if ($isRecursive && $item->isDir()) { |
| 102 | $filesWrittenToCache += $this->scanToCacheFile($filesHandle, $itemPath, $isRecursive, $excludePaths, $excludeSizeRules, $wpRootPath, $shouldScanEmptyDirs); |
| 103 | continue; |
| 104 | } |
| 105 | |
| 106 | if ($item->isFile()) { |
| 107 | $file = $filesystem->maybeNormalizePath($itemPath); |
| 108 | $file = $this->strUtils->replaceStartWith($normalizedWpRoot, '', $file); |
| 109 | // One more time with not normalized $wpRootPath in case the file path was not normalized |
| 110 | $file = $this->strUtils->replaceStartWith($wpRootPath, '', $file); |
| 111 | |
| 112 | // At the moment will only handle case where wp-config.php is present at root folder of WP |
| 113 | if ($file === '/wp-config.php') { |
| 114 | $this->setIsExcludedWpConfig(false); |
| 115 | } |
| 116 | |
| 117 | $file = $this->replaceEOLsWithPlaceholders($file) . PHP_EOL; |
| 118 | if ($this->write($filesHandle, $this->pathIdentifier . ltrim($file, '/'))) { |
| 119 | $filesWrittenToCache++; |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | } catch (Exception $e) { |
| 124 | throw new RuntimeException($e->getMessage()); |
| 125 | } |
| 126 | |
| 127 | if ($shouldScanEmptyDirs && $filesWrittenToCache === 0 && is_dir($path)) { |
| 128 | $pathPart = $this->strUtils->replaceStartWith($normalizedWpRoot, '', $path) . PHP_EOL; |
| 129 | $this->write($filesHandle, $this->pathIdentifier . ltrim($pathPart, '/')); |
| 130 | $filesWrittenToCache++; |
| 131 | } |
| 132 | |
| 133 | return $filesWrittenToCache; |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * @param bool $skipped |
| 138 | */ |
| 139 | public function setIsExcludedWpConfig($skipped = true) |
| 140 | { |
| 141 | $this->isExcludedWpConfig = $skipped; |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * @return bool |
| 146 | */ |
| 147 | public function getIsExcludedWpConfig() |
| 148 | { |
| 149 | return $this->isExcludedWpConfig; |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * @param string $pathIdentifier |
| 154 | */ |
| 155 | protected function setPathIdentifier($pathIdentifier) |
| 156 | { |
| 157 | $this->pathIdentifier = $pathIdentifier; |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * @param string $msg |
| 162 | * @param string $type |
| 163 | * |
| 164 | * @return void |
| 165 | */ |
| 166 | public function log($msg, $type = Logger::TYPE_INFO) |
| 167 | { |
| 168 | if ($this->logger === null) { |
| 169 | $this->logger = WPStaging::make(Logger::class); |
| 170 | } |
| 171 | |
| 172 | $this->logger->add($msg, $type); |
| 173 | } |
| 174 | } |
| 175 |