View source
<?php
namespace Drupal\automatic_updates\ReadinessChecker;
use Drupal\Component\Render\MarkupInterface;
use Drupal\Core\File\Exception\FileException;
use Drupal\Core\File\FileSystemInterface;
use Psr\Log\LoggerInterface;
class ReadOnlyFilesystem extends Filesystem {
protected $logger;
protected $fileSystem;
public function __construct($app_root, LoggerInterface $logger, FileSystemInterface $file_system) {
parent::__construct($app_root);
$this->logger = $logger;
$this->fileSystem = $file_system;
}
protected function doCheck() {
return $this
->readOnlyCheck();
}
protected function readOnlyCheck() {
$messages = [];
if ($this
->areSameLogicalDisk($this
->getRootPath(), $this
->getVendorPath())) {
$error = $this
->t('Logical disk at "@path" is read only. Updates to Drupal cannot be applied against a read only file system.', [
'@path' => $this->rootPath,
]);
$this
->doReadOnlyCheck($this
->getRootPath(), 'core/core.api.php', $messages, $error);
}
else {
$error = $this
->t('Drupal core filesystem at "@path" is read only. Updates to Drupal core cannot be applied against a read only file system.', [
'@path' => $this->rootPath . '/core',
]);
$this
->doReadOnlyCheck($this
->getRootPath(), implode(DIRECTORY_SEPARATOR, [
'core',
'core.api.php',
]), $messages, $error);
$error = $this
->t('Vendor filesystem at "@path" is read only. Updates to the site\'s code base cannot be applied against a read only file system.', [
'@path' => $this->vendorPath,
]);
$this
->doReadOnlyCheck($this
->getVendorPath(), 'composer/LICENSE', $messages, $error);
}
return $messages;
}
protected function doReadOnlyCheck($file_path, $file, array &$messages, MarkupInterface $message) {
if (!is_file($file_path . DIRECTORY_SEPARATOR . $file)) {
return;
}
try {
if ($this->fileSystem
->copy($file_path . DIRECTORY_SEPARATOR . $file, $file_path . DIRECTORY_SEPARATOR . "{$file}.automatic_updates", FileSystemInterface::EXISTS_REPLACE)) {
$this->fileSystem
->delete($file_path . DIRECTORY_SEPARATOR . "{$file}.automatic_updates");
}
else {
$this->logger
->error($message);
$messages[] = $message;
}
} catch (FileException $exception) {
$messages[] = $message;
}
}
}