Skip to content
Snippets Groups Projects
Commit e568c5f7 authored by Adam G-H's avatar Adam G-H
Browse files

Issue #3245846 by phenaproxima, tedbow: Move ExcludedPathsTrait to pre-create...

Issue #3245846 by phenaproxima, tedbow: Move ExcludedPathsTrait to pre-create and pre-apply events in Package Manager
parent c2c23103
No related branches found
No related tags found
No related merge requests found
......@@ -23,7 +23,12 @@ services:
- { name: event_subscriber }
automatic_updates.excluded_paths_subscriber:
class: Drupal\automatic_updates\Event\ExcludedPathsSubscriber
arguments: ['%app.root%', '%site.path%', '@file_system', '@stream_wrapper_manager']
arguments:
- '%app.root%'
- '%site.path%'
- '@file_system'
- '@stream_wrapper_manager'
- '@extension.list.module'
tags:
- { name: event_subscriber }
automatic_updates.staged_projects_validator:
......
<?php
namespace Drupal\automatic_updates\Event;
namespace Drupal\package_manager\Event;
/**
* Common functionality for events which can collect excluded paths.
......@@ -15,7 +15,7 @@ trait ExcludedPathsTrait {
protected $excludedPaths = [];
/**
* Adds an absolute path to exclude from the update operation.
* Adds an absolute path to exclude from the current operation.
*
* @todo This should only accept paths relative to the active directory.
*
......@@ -27,7 +27,7 @@ trait ExcludedPathsTrait {
}
/**
* Returns the paths to exclude from the update operation.
* Returns the paths to exclude from the current operation.
*
* @return string[]
* The paths to exclude.
......
......@@ -6,4 +6,7 @@ namespace Drupal\package_manager\Event;
* Event fired before staged changes are synced to the active directory.
*/
class PreApplyEvent extends StageEvent {
use ExcludedPathsTrait;
}
......@@ -6,4 +6,7 @@ namespace Drupal\package_manager\Event;
* Event fired before a staging area is created.
*/
class PreCreateEvent extends StageEvent {
use ExcludedPathsTrait;
}
......@@ -97,18 +97,15 @@ class Stage {
/**
* Copies the active code base into the staging area.
*
* @param array|null $exclusions
* Paths to exclude from being copied into the staging area.
*
* @todo Remove the $exclusions parameter when this method fires events.
*/
public function create(?array $exclusions = []): void {
public function create(): void {
$active_dir = $this->pathLocator->getActiveDirectory();
$stage_dir = $this->pathLocator->getStageDirectory();
$this->dispatch(new PreCreateEvent());
$this->beginner->begin($active_dir, $stage_dir, $exclusions);
$event = new PreCreateEvent();
$this->dispatch($event);
$this->beginner->begin($active_dir, $stage_dir, $event->getExcludedPaths());
$this->dispatch(new PostCreateEvent());
}
......@@ -129,18 +126,15 @@ class Stage {
/**
* Applies staged changes to the active directory.
*
* @param array|null $exclusions
* Paths to exclude from being copied into the active directory.
*
* @todo Remove the $exclusions parameter when this method fires events.
*/
public function apply(?array $exclusions = []): void {
public function apply(): void {
$active_dir = $this->pathLocator->getActiveDirectory();
$stage_dir = $this->pathLocator->getStageDirectory();
$this->dispatch(new PreApplyEvent());
$this->committer->commit($stage_dir, $active_dir, $exclusions);
$event = new PreApplyEvent();
$this->dispatch($event);
$this->committer->commit($stage_dir, $active_dir, $event->getExcludedPaths());
$this->dispatch(new PostApplyEvent());
}
......
......@@ -2,9 +2,12 @@
namespace Drupal\automatic_updates\Event;
use Drupal\Core\Extension\ModuleExtensionList;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\StreamWrapper\LocalStream;
use Drupal\Core\StreamWrapper\StreamWrapperManagerInterface;
use Drupal\package_manager\Event\PreApplyEvent;
use Drupal\package_manager\Event\PreCreateEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
......@@ -40,6 +43,13 @@ class ExcludedPathsSubscriber implements EventSubscriberInterface {
*/
protected $streamWrapperManager;
/**
* The module list service.
*
* @var \Drupal\Core\Extension\ModuleExtensionList
*/
protected $moduleList;
/**
* Constructs an UpdateSubscriber.
*
......@@ -51,21 +61,24 @@ class ExcludedPathsSubscriber implements EventSubscriberInterface {
* The file system service.
* @param \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface $stream_wrapper_manager
* The stream wrapper manager service.
* @param \Drupal\Core\Extension\ModuleExtensionList $module_list
* The module list service.
*/
public function __construct(string $app_root, string $site_path, FileSystemInterface $file_system, StreamWrapperManagerInterface $stream_wrapper_manager) {
public function __construct(string $app_root, string $site_path, FileSystemInterface $file_system, StreamWrapperManagerInterface $stream_wrapper_manager, ModuleExtensionList $module_list) {
$this->appRoot = $app_root;
$this->sitePath = $site_path;
$this->fileSystem = $file_system;
$this->streamWrapperManager = $stream_wrapper_manager;
$this->moduleList = $module_list;
}
/**
* Reacts before staged updates are committed the active directory.
*
* @param \Drupal\automatic_updates\Event\PreCommitEvent $event
* @param \Drupal\package_manager\Event\PreApplyEvent $event
* The event object.
*/
public function preCommit(PreCommitEvent $event): void {
public function preApply(PreApplyEvent $event): void {
// Don't copy anything from the staging area's sites/default.
// @todo Make this a lot smarter in https://www.drupal.org/i/3228955.
$event->excludePath('sites/default');
......@@ -79,10 +92,10 @@ class ExcludedPathsSubscriber implements EventSubscriberInterface {
/**
* Reacts to the beginning of an update process.
*
* @param \Drupal\automatic_updates\Event\PreStartEvent $event
* @param \Drupal\package_manager\Event\PreCreateEvent $event
* The event object.
*/
public function preStart(PreStartEvent $event): void {
public function preCreate(PreCreateEvent $event): void {
// Automated test site directories should never be staged.
$event->excludePath('sites/simpletest');
......@@ -100,7 +113,8 @@ class ExcludedPathsSubscriber implements EventSubscriberInterface {
}
// If this module is a git clone, exclude it.
if (is_dir(__DIR__ . '/../../.git')) {
$event->excludePath($this->fileSystem->realpath(__DIR__ . '/../..'));
$dir = $this->moduleList->getPath('automatic_updates');
$event->excludePath($dir);
}
// Exclude site-specific settings files.
......@@ -157,8 +171,8 @@ class ExcludedPathsSubscriber implements EventSubscriberInterface {
*/
public static function getSubscribedEvents() {
return [
PreStartEvent::class => 'preStart',
PreCommitEvent::class => 'preCommit',
PreCreateEvent::class => 'preCreate',
PreApplyEvent::class => 'preApply',
];
}
......
......@@ -3,6 +3,7 @@
namespace Drupal\automatic_updates\Event;
use Drupal\package_manager\ComposerUtility;
use Drupal\package_manager\Event\ExcludedPathsTrait;
/**
* Event fired before staged changes are copied into the active site.
......
......@@ -3,6 +3,7 @@
namespace Drupal\automatic_updates\Event;
use Drupal\package_manager\ComposerUtility;
use Drupal\package_manager\Event\ExcludedPathsTrait;
/**
* Event fired before an update begins.
......
......@@ -117,28 +117,11 @@ class Updater {
$packages[$package] = $project_versions['drupal'];
}
$stage_key = $this->createActiveStage($packages);
/** @var \Drupal\automatic_updates\Event\PreStartEvent $event */
$event = $this->dispatchUpdateEvent(new PreStartEvent($composer, $packages));
$this->stage->create($this->getExclusions($event));
$this->dispatchUpdateEvent(new PreStartEvent($composer, $packages));
$this->stage->create();
return $stage_key;
}
/**
* Gets the excluded paths collected by an event object.
*
* @param \Drupal\automatic_updates\Event\PreStartEvent|\Drupal\automatic_updates\Event\PreCommitEvent $event
* The event object.
*
* @return string[]
* The paths to exclude, relative to the active directory.
*/
private function getExclusions(UpdateEvent $event): array {
$make_relative = function (string $path): string {
return str_replace($this->pathLocator->getActiveDirectory() . '/', '', $path);
};
return array_map($make_relative, $event->getExcludedPaths());
}
/**
* Stages the update.
*/
......@@ -157,9 +140,8 @@ class Updater {
$stage_dir = $this->pathLocator->getStageDirectory();
$stage_composer = ComposerUtility::createForDirectory($stage_dir);
/** @var \Drupal\automatic_updates\Event\PreCommitEvent $event */
$event = $this->dispatchUpdateEvent(new PreCommitEvent($active_composer, $stage_composer));
$this->stage->apply($this->getExclusions($event));
$this->dispatchUpdateEvent(new PreCommitEvent($active_composer, $stage_composer));
$this->stage->apply();
$this->dispatchUpdateEvent(new PostCommitEvent($active_composer));
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment