You are here

abstract class AbstractYamlPatternsDeriver in UI Patterns 8

Class AbstractYamlPatternsDeriver.

Derive pattern plugin definitions stored in YAML files.

@package Drupal\ui_patterns\Deriver

Hierarchy

Expanded class hierarchy of AbstractYamlPatternsDeriver

1 file declares its use of AbstractYamlPatternsDeriver
LibraryDeriver.php in modules/ui_patterns_library/src/Plugin/Deriver/LibraryDeriver.php

File

src/Plugin/Deriver/AbstractYamlPatternsDeriver.php, line 18

Namespace

Drupal\ui_patterns\Plugin\Deriver
View source
abstract class AbstractYamlPatternsDeriver extends AbstractPatternsDeriver implements YamlPatternsDeriverInterface {

  /**
   * File system service.
   *
   * @var \Drupal\Core\File\FileSystemInterface
   */
  protected $fileSystem;

  /**
   * Constructor.
   *
   * @param string $base_plugin_id
   *   The base plugin ID.
   * @param \Drupal\Core\TypedData\TypedDataManager $typed_data_manager
   *   Typed data manager service.
   * @param \Drupal\Core\Messenger\MessengerInterface $messenger
   *   Messenger.
   * @param \Drupal\Core\File\FileSystemInterface $file_system
   *   File system service.
   */
  public function __construct($base_plugin_id, TypedDataManager $typed_data_manager, MessengerInterface $messenger, FileSystemInterface $file_system) {
    parent::__construct($base_plugin_id, $typed_data_manager, $messenger);
    $this->fileSystem = $file_system;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, $base_plugin_id) {
    return new static($base_plugin_id, $container
      ->get('typed_data_manager'), $container
      ->get('messenger'), $container
      ->get('file_system'));
  }

  /**
   * {@inheritdoc}
   */
  public function fileScanDirectory($directory) {
    if (!is_dir($directory)) {
      return [];
    }
    $options = [
      'nomask' => $this
        ->getNoMask(),
    ];
    $extensions = $this
      ->getFileExtensions();
    $extensions = array_map('preg_quote', $extensions);
    $extensions = implode('|', $extensions);
    $files = $this->fileSystem
      ->scanDirectory($directory, "/{$extensions}\$/", $options);

    // In different file systems order of files in a folder can be different
    // that can break tests. So let's sort them alphabetically manually.
    ksort($files);
    return $files;
  }

  /**
   * Returns a regular expression for directories to be excluded in a file scan.
   *
   * @return string
   *   Regular expression.
   */
  protected function getNoMask() {
    $ignore = Settings::get('file_scan_ignore_directories', []);

    // We add 'tests' directory to the ones found in settings.
    $ignore[] = 'tests';
    array_walk($ignore, function (&$value) {
      $value = preg_quote($value, '/');
    });
    return '/^' . implode('|', $ignore) . '$/';
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AbstractPatternsDeriver::$messenger protected property The messenger.
AbstractPatternsDeriver::$typedDataManager protected property Typed data manager service.
AbstractPatternsDeriver::getDerivativeDefinitions public function Gets the definition of all derivatives of a base plugin. Overrides DeriverBase::getDerivativeDefinitions
AbstractPatternsDeriver::getPatternDefinition protected function Get pattern data object.
AbstractPatternsDeriver::isValidPatternDefinition protected function Validate pattern definition.
AbstractYamlPatternsDeriver::$fileSystem protected property File system service.
AbstractYamlPatternsDeriver::create public static function Creates a new class instance. Overrides AbstractPatternsDeriver::create 1
AbstractYamlPatternsDeriver::fileScanDirectory public function Wrapper method for global function call. Overrides YamlPatternsDeriverInterface::fileScanDirectory
AbstractYamlPatternsDeriver::getNoMask protected function Returns a regular expression for directories to be excluded in a file scan.
AbstractYamlPatternsDeriver::__construct public function Constructor. Overrides AbstractPatternsDeriver::__construct 1
DeriverBase::$derivatives protected property List of derivative definitions. 1
DeriverBase::getDerivativeDefinition public function Gets the definition of a derivative plugin. Overrides DeriverInterface::getDerivativeDefinition
PatternsDeriverInterface::getPatterns public function Get pattern definition objects. 1
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
YamlPatternsDeriverInterface::getFileExtensions public function Get list of possible yaml definition file extensions. 1