diff --git a/package_manager/src/EventSubscriber/ExcludedPathsSubscriber.php b/package_manager/src/EventSubscriber/ExcludedPathsSubscriber.php index 5eb7e08811ea6a7cdcf07a72ba8acb66ca9a1e61..844105463c3ddbe431c7e931d562216874cc1043 100644 --- a/package_manager/src/EventSubscriber/ExcludedPathsSubscriber.php +++ b/package_manager/src/EventSubscriber/ExcludedPathsSubscriber.php @@ -18,6 +18,8 @@ use Symfony\Component\Finder\Finder; */ class ExcludedPathsSubscriber implements EventSubscriberInterface { + use PathExclusionsTrait; + /** * The current site path, relative to the Drupal root. * @@ -46,13 +48,6 @@ class ExcludedPathsSubscriber implements EventSubscriberInterface { */ protected $database; - /** - * The path locator service. - * - * @var \Drupal\package_manager\PathLocator - */ - protected $pathLocator; - /** * Constructs an ExcludedPathsSubscriber. * @@ -75,51 +70,6 @@ class ExcludedPathsSubscriber implements EventSubscriberInterface { $this->pathLocator = $path_locator; } - /** - * Flags paths to be excluded, relative to the web root. - * - * This should only be used for paths that, if they exist at all, are - * *guaranteed* to exist within the web root. - * - * @param \Drupal\package_manager\Event\PreCreateEvent|\Drupal\package_manager\Event\PreApplyEvent $event - * The event object. - * @param string[] $paths - * The paths to exclude. These should be relative to the web root, and will - * be made relative to the project root. - */ - protected function excludeInWebRoot(StageEvent $event, array $paths): void { - $web_root = $this->pathLocator->getWebRoot(); - if ($web_root) { - $web_root .= '/'; - } - - foreach ($paths as $path) { - // Make the path relative to the project root by prefixing the web root. - $event->excludePath($web_root . $path); - } - } - - /** - * Flags paths to be excluded, relative to the project root. - * - * @param \Drupal\package_manager\Event\PreCreateEvent|\Drupal\package_manager\Event\PreApplyEvent $event - * The event object. - * @param string[] $paths - * The paths to exclude. Absolute paths will be made relative to the project - * root; relative paths will be assumed to already be relative to the - * project root, and excluded as given. - */ - protected function excludeInProjectRoot(StageEvent $event, array $paths): void { - $project_root = $this->pathLocator->getProjectRoot(); - - foreach ($paths as $path) { - // Make absolute paths relative to the project root. - $path = str_replace($project_root, '', $path); - $path = ltrim($path, '/'); - $event->excludePath($path); - } - } - /** * Excludes common paths from staging operations. * diff --git a/package_manager/src/EventSubscriber/PathExclusionsTrait.php b/package_manager/src/EventSubscriber/PathExclusionsTrait.php new file mode 100644 index 0000000000000000000000000000000000000000..c928a543d180ee8ab8ba5b9449993dba396a5e73 --- /dev/null +++ b/package_manager/src/EventSubscriber/PathExclusionsTrait.php @@ -0,0 +1,64 @@ +<?php + +namespace Drupal\package_manager\EventSubscriber; + +use Drupal\package_manager\Event\StageEvent; + +/** + * Contains methods for excluding paths from staging operations. + */ +trait PathExclusionsTrait { + + /** + * The path locator service. + * + * @var \Drupal\package_manager\PathLocator + */ + protected $pathLocator; + + /** + * Flags paths to be excluded, relative to the web root. + * + * This should only be used for paths that, if they exist at all, are + * *guaranteed* to exist within the web root. + * + * @param \Drupal\package_manager\Event\PreCreateEvent|\Drupal\package_manager\Event\PreApplyEvent $event + * The event object. + * @param string[] $paths + * The paths to exclude. These should be relative to the web root, and will + * be made relative to the project root. + */ + protected function excludeInWebRoot(StageEvent $event, array $paths): void { + $web_root = $this->pathLocator->getWebRoot(); + if ($web_root) { + $web_root .= '/'; + } + + foreach ($paths as $path) { + // Make the path relative to the project root by prefixing the web root. + $event->excludePath($web_root . $path); + } + } + + /** + * Flags paths to be excluded, relative to the project root. + * + * @param \Drupal\package_manager\Event\PreCreateEvent|\Drupal\package_manager\Event\PreApplyEvent $event + * The event object. + * @param string[] $paths + * The paths to exclude. Absolute paths will be made relative to the project + * root; relative paths will be assumed to already be relative to the + * project root, and excluded as given. + */ + protected function excludeInProjectRoot(StageEvent $event, array $paths): void { + $project_root = $this->pathLocator->getProjectRoot(); + + foreach ($paths as $path) { + // Make absolute paths relative to the project root. + $path = str_replace($project_root, '', $path); + $path = ltrim($path, '/'); + $event->excludePath($path); + } + } + +}