StagingSite
1 week ago
StagingSiteCreate
2 weeks ago
StagingSiteDelete
10 months ago
StagingSiteReset
2 weeks ago
StagingSiteUpdate
2 weeks ago
DataAdjustmentTask.php
11 months ago
DatabaseAdjustmentTask.php
2 weeks ago
FileAdjustmentTask.php
11 months ago
FileCopierTask.php
2 weeks ago
StagingTask.php
1 year ago
FileAdjustmentTask.php
97 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPStaging\Staging\Tasks; |
| 4 | |
| 5 | use WPStaging\Framework\Exceptions\WPStagingException; |
| 6 | use WPStaging\Framework\Filesystem\Filesystem; |
| 7 | use WPStaging\Framework\Queue\SeekableQueueInterface; |
| 8 | use WPStaging\Framework\Job\Dto\StepsDto; |
| 9 | use WPStaging\Framework\SiteInfo; |
| 10 | use WPStaging\Framework\Utils\Cache\Cache; |
| 11 | use WPStaging\Framework\Utils\Urls; |
| 12 | use WPStaging\Vendor\Psr\Log\LoggerInterface; |
| 13 | |
| 14 | abstract class FileAdjustmentTask extends DataAdjustmentTask |
| 15 | { |
| 16 | /** |
| 17 | * @var Filesystem |
| 18 | */ |
| 19 | protected $filesystem; |
| 20 | |
| 21 | /** |
| 22 | * @var SiteInfo |
| 23 | */ |
| 24 | protected $siteInfo; |
| 25 | |
| 26 | /** |
| 27 | * @param LoggerInterface $logger |
| 28 | * @param Cache $cache |
| 29 | * @param StepsDto $stepsDto |
| 30 | * @param SeekableQueueInterface $taskQueue |
| 31 | * @param Urls $urls |
| 32 | * @param Filesystem $filesystem |
| 33 | * @param SiteInfo $siteInfo |
| 34 | */ |
| 35 | public function __construct(LoggerInterface $logger, Cache $cache, StepsDto $stepsDto, SeekableQueueInterface $taskQueue, Urls $urls, Filesystem $filesystem, SiteInfo $siteInfo) |
| 36 | { |
| 37 | parent::__construct($logger, $cache, $stepsDto, $taskQueue, $urls); |
| 38 | $this->filesystem = $filesystem; |
| 39 | $this->siteInfo = $siteInfo; |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * @param string $string |
| 44 | * @return string |
| 45 | */ |
| 46 | protected function getDefineRegex(string $string): string |
| 47 | { |
| 48 | return "/define\s*\(\s*['\"]" . $string . "['\"]\s*,\s*(.*)\s*\);/"; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * @param string $file |
| 53 | * @return string |
| 54 | * @throws WPStagingException |
| 55 | */ |
| 56 | protected function readFile(string $file): string |
| 57 | { |
| 58 | $path = trailingslashit($this->jobDataDto->getStagingSitePath()) . $file; |
| 59 | if (($content = file_get_contents($path)) === false) { |
| 60 | throw new WPStagingException("Error - can't read " . $file); |
| 61 | } |
| 62 | |
| 63 | return $content; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * @param string $file |
| 68 | * @param string $content |
| 69 | * @return void |
| 70 | * @throws WPStagingException |
| 71 | */ |
| 72 | protected function writeFile(string $file, string $content) |
| 73 | { |
| 74 | $path = trailingslashit($this->jobDataDto->getStagingSitePath()) . $file; |
| 75 | if ($this->filesystem->create($path, $content) === false) { |
| 76 | throw new WPStagingException("Error - can't write to " . $file . "."); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * @return string |
| 82 | */ |
| 83 | protected function readWpConfig(): string |
| 84 | { |
| 85 | return $this->readFile('wp-config.php'); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * @param string $content |
| 90 | * @return void |
| 91 | */ |
| 92 | protected function writeWpConfig(string $content) |
| 93 | { |
| 94 | $this->writeFile('wp-config.php', $content); |
| 95 | } |
| 96 | } |
| 97 |