Source of file VersionedFilesMigrator.php
Size: 4,163 Bytes - Last Modified: 2021-12-23T10:27:40+00:00
/var/www/docs.ssmods.com/process/src/src/Dev/VersionedFilesMigrator.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195 | <?php namespace SilverStripe\Assets\Dev; use SilverStripe\Assets\Filesystem; use SilverStripe\Assets\Storage\AssetStore; use SilverStripe\Control\Director; use SilverStripe\Control\HTTPRequest; use SilverStripe\Core\Injector\Injectable; use SilverStripe\Core\Path; use SilverStripe\Dev\BuildTask; use InvalidArgumentException; use Symfony\Component\Finder\Finder; use Symfony\Component\Finder\SplFileInfo; class VersionedFilesMigrator { use Injectable; const STRATEGY_DELETE = 'delete'; const STRATEGY_PROTECT = 'protect'; /** * @var array */ private static $dependencies = [ 'finder' => '%$' . Finder::class, ]; /** * @var Finder */ private $finder; /** * @var string */ private $basePath = ASSETS_DIR; /** * @var string */ private $strategy = self::STRATEGY_DELETE; /** * @var bool */ private $showOutput = true; /** * List of logged messages, if $showOutput is false * @var array */ private $log = []; /** * VersionedFilesMigrationTask constructor. * @param string $strategy * @param string $basePath * @param bool $output */ public function __construct($strategy = self::STRATEGY_DELETE, $basePath = ASSETS_DIR, $output = true) { if (!in_array($strategy, [self::STRATEGY_DELETE, self::STRATEGY_PROTECT])) { throw new InvalidArgumentException(sprintf( 'Invalid strategy: %s', $strategy )); } $this->basePath = $basePath; $this->strategy = $strategy; $this->showOutput = $output; } /** * @return void */ public function migrate() { if ($this->strategy === self::STRATEGY_PROTECT) { $this->doProtect(); } else { $this->doDelete(); } } /** * @return void */ private function doProtect() { foreach ($this->getVersionDirectories() as $path) { $htaccessPath = Path::join($path, '.htaccess'); if (!file_exists($htaccessPath)) { $content = "Require all denied"; @file_put_contents($htaccessPath, $content); if (file_exists($htaccessPath)) { $this->output("Added .htaccess file to $htaccessPath"); } else { $this->output("Failed to add .htaccess file to $htaccessPath"); } } } } /** * @return void */ private function doDelete() { foreach ($this->getVersionDirectories() as $path) { if (!is_dir($path)) { continue; } Filesystem::removeFolder($path); if (!is_dir($path)) { $this->output("Deleted $path"); } else { $this->output("Failed to delete $path"); } } } /** * @return array */ private function getVersionDirectories() { $results = $this ->getFinder() ->directories() ->name('_versions') ->in($this->basePath); $folders = []; /* @var SplFileInfo $result */ foreach ($results as $result) { $folders[] = $result->getPathname(); } return $folders; } /** * @return string */ private function nl() { return Director::is_cli() ? PHP_EOL : "<br />"; } /** * @param string $msg */ private function output($msg) { if ($this->showOutput) { echo $msg . $this->nl(); } else { $this->log[] = $msg; } } /** * @param Finder $finder * @return $this */ public function setFinder(Finder $finder) { $this->finder = $finder; return $this; } /** * @return Finder */ public function getFinder() { return $this->finder; } /** * @return array */ public function getLog() { return $this->log; } } |