Commit 741b63d9 authored by Stephen Lucero's avatar Stephen Lucero
Browse files

Issue #3300299 by slucero: Replace Static Method Calls with Dependency Injection

parent b1932fc1
Loading
Loading
Loading
Loading
+8 −2
Original line number Diff line number Diff line
@@ -12,7 +12,7 @@ services:
    deprecated: 'The "%service_id%" service is deprecated. Use "patternkit.library.namespace_resolver" or "patternkit.pattern.discovery" instead.'
  patternkit.asset.library.parser.base:
    abstract: true
    arguments: [ '@serialization.json', '%app.root%', '@module_handler', '@theme.manager', '@stream_wrapper_manager', '@library.libraries_directory_file_finder', '@extension.path.resolver' ]
    arguments: [ '@serialization.json', '%app.root%', '@module_handler', '@theme.manager', '@stream_wrapper_manager', '@library.libraries_directory_file_finder', '@extension.path.resolver', '@entity_type.manager' ]
  patternkit.asset.library.parser.file:
    parent: patternkit.asset.library.parser.base
    class: Drupal\patternkit\Asset\PatternLibraryParser\FilePatternLibraryParser
@@ -24,7 +24,12 @@ services:
    class: Drupal\patternkit\Asset\PatternLibraryParser\TwigPatternLibraryParser
  patternkit.library.namespace_resolver:
    class: Drupal\patternkit\Asset\LibraryNamespaceResolver
    arguments: [ '@service_container', '@library.discovery', '@cache.discovery', '@lock' ]
    arguments:
      - '@service_container'
      - '@library.discovery'
      - '@cache.discovery'
      - '@lock'
      - '@stream_wrapper_manager'
    tags:
      - { name: needs_destruction }
  patternkit.pattern.discovery:
@@ -76,6 +81,7 @@ services:
    arguments:
      - '@patternkit.pattern.discovery'
      - '@patternkit.library.namespace_resolver'
      - '@entity_type.manager'
    public: false
  patternkit.schema.schema_walker_factory:
    class: Drupal\patternkit\Schema\SchemaWalkerFactory
+15 −5
Original line number Diff line number Diff line
@@ -6,6 +6,7 @@ use Drupal\Core\Asset\LibraryDiscoveryInterface;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Cache\CacheCollector;
use Drupal\Core\Lock\LockBackendInterface;
use Drupal\Core\StreamWrapper\StreamWrapperManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
@@ -54,6 +55,13 @@ class LibraryNamespaceResolver extends CacheCollector implements LibraryNamespac
   */
  protected LibraryDiscoveryInterface $libraryDiscovery;

  /**
   * The stream wrapper manager service.
   *
   * @var \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface
   */
  protected StreamWrapperManagerInterface $streamWrapperManager;

  /**
   * The collection of loaded extensions to search for libraries.
   *
@@ -72,15 +80,19 @@ class LibraryNamespaceResolver extends CacheCollector implements LibraryNamespac
   *   Provide a default cache.
   * @param \Drupal\Core\Lock\LockBackendInterface $lock
   *   Provide the lock backend.
   * @param \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface $streamWrapperManager
   *   The stream wrapper manager service.
   */
  public function __construct(
    ContainerInterface $container,
    LibraryDiscoveryInterface $libraryDiscovery,
    CacheBackendInterface $cache,
    LockBackendInterface $lock
    LockBackendInterface $lock,
    StreamWrapperManagerInterface $streamWrapperManager
  ) {
    $this->container = $container;
    $this->libraryDiscovery = $libraryDiscovery;
    $this->streamWrapperManager = $streamWrapperManager;

    parent::__construct(self::PERSISTENT_CACHE_ID, $cache, $lock, self::CACHE_TAGS);
  }
@@ -354,10 +366,8 @@ class LibraryNamespaceResolver extends CacheCollector implements LibraryNamespac
   */
  protected function fileValidUri($uri): bool {
    // Assert that the URI has an allowed scheme. Bare paths are not allowed.
    /** @var \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface $stream_wrapper_manager */
    $stream_wrapper_manager = \Drupal::service('stream_wrapper_manager');
    $uri_scheme = $stream_wrapper_manager::getScheme($uri);
    if (!$stream_wrapper_manager->isValidScheme($uri_scheme)) {
    $uri_scheme = $this->streamWrapperManager::getScheme($uri);
    if (!$this->streamWrapperManager->isValidScheme($uri_scheme)) {
      return FALSE;
    }
    return TRUE;
+2 −3
Original line number Diff line number Diff line
@@ -3,12 +3,11 @@
namespace Drupal\patternkit\Asset\PatternLibraryParser;

use Drupal\Core\Asset\Exception\InvalidLibraryFileException;
use Drupal\patternkit\Entity\Pattern;
use Drupal\patternkit\Asset\PatternLibraryParserBase;
use Drupal\patternkit\Entity\PatternInterface;
use Drupal\patternkit\PatternEditorConfig;
use Drupal\patternkit\PatternLibrary;
use Drupal\patternkit\PatternLibraryJSONParserTrait;
use Drupal\patternkit\Asset\PatternLibraryParserBase;

/**
 * Parses a Twig pattern library collection into usable metadata.
@@ -34,7 +33,7 @@ class TwigPatternLibraryParser extends PatternLibraryParserBase {
  public function createPattern(string $name, $data): PatternInterface {
    // Pattern schemas contain values needed for Pattern fields.
    $values = ['name' => $data['title'] ?? $name];
    return Pattern::create($values + $data);
    return $this->patternStorage->create($values + $data);
  }

  /**
+12 −3
Original line number Diff line number Diff line
@@ -5,6 +5,8 @@ namespace Drupal\patternkit\Asset;
use Drupal\Component\Serialization\SerializationInterface;
use Drupal\Core\Asset\LibrariesDirectoryFileFinder;
use Drupal\Core\Asset\LibraryDiscoveryParser;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ExtensionPathResolver;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\File\FileSystem;
@@ -31,6 +33,11 @@ abstract class PatternLibraryParserBase extends LibraryDiscoveryParser implement
   */
  protected SerializationInterface $serializer;

  /**
   * @var \Drupal\Core\Entity\EntityStorageInterface
   */
  protected EntityStorageInterface $patternStorage;

  /**
   * Constructor for PatternLibraryParser implementations.
   *
@@ -56,11 +63,13 @@ abstract class PatternLibraryParserBase extends LibraryDiscoveryParser implement
    ThemeManagerInterface $theme_manager,
    StreamWrapperManagerInterface $stream_wrapper,
    LibrariesDirectoryFileFinder $libraries_directory_file_finder,
    ExtensionPathResolver $extension_path_resolver
    ExtensionPathResolver $extension_path_resolver,
    EntityTypeManagerInterface $entity_type_manager
  ) {
    parent::__construct($root, $module_handler, $theme_manager, $stream_wrapper, $libraries_directory_file_finder, $extension_path_resolver);

    $this->serializer = $serializer;
    $this->patternStorage = $entity_type_manager->getStorage('patternkit_pattern');
  }

  /**
@@ -80,7 +89,7 @@ abstract class PatternLibraryParserBase extends LibraryDiscoveryParser implement
  public function createPattern(string $name, $data): PatternInterface {
    // Pattern schemas contain values needed for Pattern fields.
    $values = ['name' => $name];
    return Pattern::create($values + $data);
    return $this->patternStorage->create($values + $data);
  }

  /**
@@ -435,7 +444,7 @@ abstract class PatternLibraryParserBase extends LibraryDiscoveryParser implement
      }
    }

    return Pattern::create((array) $pk_obj);
    return $this->patternStorage->create((array) $pk_obj);
  }

  /**
+2 −3
Original line number Diff line number Diff line
@@ -11,7 +11,6 @@ use Drupal\Core\Plugin\Context\EntityContext;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\layout_builder\Entity\LayoutBuilderEntityViewDisplay;
use Drupal\patternkit\Asset\LibraryInterface;
use Drupal\patternkit\Entity\Pattern;
use Drupal\patternkit\Plugin\Derivative\PatternkitBlock;
use Drush\Commands\DrushCommands;

@@ -160,7 +159,7 @@ class PatternkitCommands extends DrushCommands {
          }
          try {
            /** @var \Drupal\Patternkit\entity\PatternInterface $pattern */
            $pattern = Pattern::create($pattern_asset);
            $pattern = $this->patternStorage->create($pattern_asset);
          }
          catch (\Exception $exception) {
            $logger->debug($this->t('Could not create pattern with ID @id.', ['@id' => $pattern_id]));
@@ -401,7 +400,7 @@ class PatternkitCommands extends DrushCommands {
      $logger->error($this->t("Failed to get library asset for @pattern.", ['@pattern' => $pattern_id]));
      return FALSE;
    }
    $base_pattern = Pattern::create($asset);
    $base_pattern = $this->patternStorage->create($asset);
    if ($base_pattern === NULL) {
      return FALSE;
    }
Loading