Skip to content
Snippets Groups Projects

Issue #3231996: Ensure file system is writeable

Compare and
3 files
+ 107
0
Compare changes
  • Side-by-side
  • Inline
Files
3
+ 84
0
<?php
namespace Drupal\automatic_updates\Validator;
use Drupal\automatic_updates\AutomaticUpdatesEvents;
use Drupal\automatic_updates\Event\UpdateEvent;
use Drupal\automatic_updates\PathLocator;
use Drupal\automatic_updates\Validation\ValidationResult;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* Validates that important file system paths are writable.
*/
class WritableFileSystemValidator implements EventSubscriberInterface {
use StringTranslationTrait;
/**
* The path locator service.
*
* @var \Drupal\automatic_updates\PathLocator
*/
protected $pathLocator;
/**
* Constructs a WriteableFileSystemValidator object.
*
* @param \Drupal\automatic_updates\PathLocator $path_locator
* The path locator service.
*/
public function __construct(PathLocator $path_locator) {
$this->pathLocator = $path_locator;
}
/**
* Validates that the file system is writable.
*
* @param \Drupal\automatic_updates\Event\UpdateEvent $event
* The event object.
*/
public function checkPermissions(UpdateEvent $event): void {
$dir = $this->pathLocator->getProjectRoot();
$message = $this->t('The project directory "@dir" is not writable.', [
'@dir' => $dir,
]);
$this->checkPath($dir, $event, $message);
$dir = $this->pathLocator->getVendorDirectory();
$message = $this->t('The vendor directory "@dir" is not writable.', [
'@dir' => $dir,
]);
$this->checkPath($dir, $event, $message);
}
/**
* Checks that a path is writable, and flags an error if it's not.
*
* @param string $path
* The path to check.
* @param \Drupal\automatic_updates\Event\UpdateEvent $event
* The event object.
* @param \Drupal\Core\StringTranslation\TranslatableMarkup $error_message
* The translated error message if the path is not writeable.
*/
protected function checkPath(string $path, UpdateEvent $event, TranslatableMarkup $error_message): void {
if (is_writable($path)) {
return;
}
$result = ValidationResult::createError([$error_message]);
$event->addValidationResult($result);
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
return [
AutomaticUpdatesEvents::READINESS_CHECK => 'checkPermissions',
];
}
}
Loading