Skip to content
Snippets Groups Projects

Issue #3363938: Package Manager should ignore default.settings.php and default.services.yml

1 file
+ 73
1
Compare changes
  • Side-by-side
  • Inline
@@ -4,7 +4,12 @@ declare(strict_types = 1);
namespace Drupal\package_manager\PathExcluder;
use Drupal\Core\File\Exception\FileException;
use Drupal\Core\File\FileSystemInterface;
use Drupal\package_manager\Event\CollectPathsToExcludeEvent;
use Drupal\package_manager\Event\PostCreateEvent;
use Drupal\package_manager\Event\PreApplyEvent;
use Drupal\package_manager\PathLocator;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
@@ -22,8 +27,16 @@ class SiteConfigurationExcluder implements EventSubscriberInterface {
*
* @param string $sitePath
* The current site path, relative to the Drupal root.
* @param \Drupal\package_manager\PathLocator $pathLocator
* The path locator service.
* @param \Drupal\Core\File\FileSystemInterface $fileSystem
* The file system service.
*/
public function __construct(protected string $sitePath) {}
public function __construct(
protected string $sitePath,
private readonly PathLocator $pathLocator,
private readonly FileSystemInterface $fileSystem
) {}
/**
* Excludes site configuration files from stage operations.
@@ -53,12 +66,71 @@ class SiteConfigurationExcluder implements EventSubscriberInterface {
$event->addPathsRelativeToWebRoot($paths);
}
/**
* Makes the staged `sites/default` directory world-writable.
*
* @param \Drupal\package_manager\Event\PostCreateEvent $event
* The event being handled.
*/
public function makeDefaultSiteDirectoryWritable(PostCreateEvent $event): void {
$dir = $this->getDefaultSiteDirectoryPath($event->stage->getStageDirectory());
if (!$this->fileSystem->chmod($dir, 0777)) {
throw new FileException("Could not change permissions on '$dir'.");
}
}
/**
* Makes `sites/default` permissions the same in live and stage directories.
*
* @param \Drupal\package_manager\Event\PreApplyEvent $event
* The event being handled.
*
* @throws \Drupal\Core\File\Exception\FileException
* If the permissions of the live `sites/default` cannot be determined, or
* changed on the staged `sites/default`.
*/
public function syncDefaultSiteDirectoryPermissions(PreApplyEvent $event): void {
$live_dir = $this->getDefaultSiteDirectoryPath($this->pathLocator->getProjectRoot());
$staged_dir = $this->getDefaultSiteDirectoryPath($event->stage->getStageDirectory());
$original_permissions = fileperms($live_dir);
if ($original_permissions === FALSE) {
throw new FileException("Could not determine permissions for '$live_dir'.");
}
// @see \Symfony\Component\Filesystem\Filesystem::copy() for another use
// of the 0111 bitmask.
if (!$this->fileSystem->chmod($staged_dir, $original_permissions & 0111)) {
throw new FileException("Could not change permissions on '$staged_dir'.");
}
}
/**
* Returns the full path to `sites/default`, relative to a root directory.
*
* @param string $root_dir
* The root directory.
*
* @return string
* The full path to `sites/default` within the given root directory.
*/
private function getDefaultSiteDirectoryPath(string $root_dir): string {
$dir = [$root_dir, 'sites', 'default'];
$web_root = $this->pathLocator->getWebRoot();
if ($web_root) {
array_splice($dir, 1, 0, $web_root);
}
return implode(DIRECTORY_SEPARATOR, $dir);
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents(): array {
return [
CollectPathsToExcludeEvent::class => 'excludeSiteConfiguration',
PostCreateEvent::class => 'makeDefaultSiteDirectoryWritable',
PreApplyEvent::class => 'syncDefaultSiteDirectoryPermissions',
];
}
Loading