You are here

abstract class FrontendEnvironmentBase in Build Hooks 3.x

Same name and namespace in other branches
  1. 8.2 src/Plugin/FrontendEnvironmentBase.php \Drupal\build_hooks\Plugin\FrontendEnvironmentBase

Base class for Frontend environment plugins.

Hierarchy

Expanded class hierarchy of FrontendEnvironmentBase

6 files declare their use of FrontendEnvironmentBase
BitbucketFrontendEnvironment.php in modules/build_hooks_bitbucket/src/Plugin/FrontendEnvironment/BitbucketFrontendEnvironment.php
CircleCiFrontendEnvironment.php in modules/build_hooks_circleci/src/Plugin/FrontendEnvironment/CircleCiFrontendEnvironment.php
CircleV2.php in modules/build_hooks_circleci/src/Plugin/FrontendEnvironment/CircleV2.php
GenericFrontendEnvironment.php in src/Plugin/FrontendEnvironment/GenericFrontendEnvironment.php
NetlifyFrontendEnvironment.php in modules/build_hooks_netlify/src/Plugin/FrontendEnvironment/NetlifyFrontendEnvironment.php

... See full list

File

src/Plugin/FrontendEnvironmentBase.php, line 18

Namespace

Drupal\build_hooks\Plugin
View source
abstract class FrontendEnvironmentBase extends PluginBase implements FrontendEnvironmentInterface, PluginWithFormsInterface {
  use PluginWithFormsTrait;
  use StringTranslationTrait;

  /**
   * The transliteration service.
   *
   * @var \Drupal\Component\Transliteration\TransliterationInterface
   */
  protected $transliteration;

  /**
   * {@inheritdoc}
   */
  public function label() {
    if (!empty($this->configuration['label'])) {
      return $this->configuration['label'];
    }
  }

  /**
   * {@inheritdoc}
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this
      ->setConfiguration($configuration);
  }

  /**
   * {@inheritdoc}
   */
  public function getConfiguration() {
    return $this->configuration;
  }

  /**
   * {@inheritdoc}
   */
  public function setConfiguration(array $configuration) {
    $this->configuration = NestedArray::mergeDeep($this
      ->baseConfigurationDefaults(), $this
      ->defaultConfiguration(), $configuration);
  }

  /**
   * Returns generic default configuration for frontend environment plugins.
   *
   * @return array
   *   An associative array with the default configuration.
   */
  protected function baseConfigurationDefaults() {
    return [
      'id' => $this
        ->getPluginId(),
      'label' => '',
      'provider' => $this->pluginDefinition['provider'],
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    return [];
  }

  /**
   * {@inheritdoc}
   */
  public function setConfigurationValue($key, $value) {
    $this->configuration[$key] = $value;
  }

  /**
   * {@inheritdoc}
   */
  public function calculateDependencies() {
    $dependencies = [];
    $definition = $this
      ->getPluginDefinition();
    $dependencies['module'][] = $definition['provider'];
    return $dependencies;
  }

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $definition = $this
      ->getPluginDefinition();
    $form['provider'] = [
      '#type' => 'value',
      '#value' => $definition['provider'],
    ];

    // Add plugin-specific settings for this frontend environment type.
    $form += $this
      ->frontEndEnvironmentForm($form, $form_state);
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function frontEndEnvironmentForm($form, FormStateInterface $form_state) {
    return [];
  }

  /**
   * {@inheritdoc}
   */
  public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
    $this
      ->frontEndEnvironmentFormValidate($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function frontEndEnvironmentFormValidate($form, FormStateInterface $form_state) {
  }

  /**
   * {@inheritdoc}
   */
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
    if (!$form_state
      ->getErrors()) {
      $this->configuration['label'] = $form_state
        ->getValue('label');
      $this->configuration['provider'] = $form_state
        ->getValue('provider');
      $this
        ->frontEndEnvironmentSubmit($form, $form_state);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function frontEndEnvironmentSubmit($form, FormStateInterface $form_state) {
  }

  /**
   * {@inheritdoc}
   */
  public function getMachineNameSuggestion() {
    $definition = $this
      ->getPluginDefinition();
    $admin_label = $definition['admin_label'];
    $transliterated = $this
      ->transliteration()
      ->transliterate($admin_label, LanguageInterface::LANGCODE_DEFAULT, '_');
    $transliterated = mb_strtolower($transliterated);
    $transliterated = preg_replace('@[^a-z0-9_.]+@', '', $transliterated);
    return $transliterated;
  }

  /**
   * Wraps the transliteration service.
   *
   * @return \Drupal\Component\Transliteration\TransliterationInterface
   *   The transliteration service.
   */
  protected function transliteration() {
    if (!$this->transliteration) {
      $this->transliteration = \Drupal::transliteration();
    }
    return $this->transliteration;
  }

  /**
   * Sets the transliteration service.
   *
   * @param \Drupal\Component\Transliteration\TransliterationInterface $transliteration
   *   The transliteration service.
   */
  public function setTransliteration(TransliterationInterface $transliteration) {
    $this->transliteration = $transliteration;
  }

  /**
   * {@inheritdoc}
   */
  public function deploymentWasTriggered(ResponseInterface $response) : bool {
    return in_array($response
      ->getStatusCode(), [
      200,
      201,
    ], FALSE);
  }

  /**
   * {@inheritdoc}
   */
  public function preDeploymentTrigger(BuildTrigger $trigger) : void {

    // Nil-op.
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FrontendEnvironmentBase::$transliteration protected property The transliteration service.
FrontendEnvironmentBase::baseConfigurationDefaults protected function Returns generic default configuration for frontend environment plugins.
FrontendEnvironmentBase::buildConfigurationForm public function Form constructor. Overrides PluginFormInterface::buildConfigurationForm
FrontendEnvironmentBase::calculateDependencies public function Calculates dependencies for the configured plugin. Overrides DependentPluginInterface::calculateDependencies
FrontendEnvironmentBase::defaultConfiguration public function Gets default configuration for this plugin. Overrides ConfigurableInterface::defaultConfiguration 3
FrontendEnvironmentBase::deploymentWasTriggered public function Determine if the deployment was triggered successfully. Overrides FrontendEnvironmentInterface::deploymentWasTriggered 2
FrontendEnvironmentBase::frontEndEnvironmentForm public function 6
FrontendEnvironmentBase::frontEndEnvironmentFormValidate public function 1
FrontendEnvironmentBase::frontEndEnvironmentSubmit public function 6
FrontendEnvironmentBase::getConfiguration public function Gets this plugin's configuration. Overrides ConfigurableInterface::getConfiguration
FrontendEnvironmentBase::getMachineNameSuggestion public function
FrontendEnvironmentBase::label public function
FrontendEnvironmentBase::preDeploymentTrigger public function React before a build is triggered. Overrides FrontendEnvironmentInterface::preDeploymentTrigger 1
FrontendEnvironmentBase::setConfiguration public function Sets the configuration for this plugin instance. Overrides ConfigurableInterface::setConfiguration
FrontendEnvironmentBase::setConfigurationValue public function
FrontendEnvironmentBase::setTransliteration public function Sets the transliteration service.
FrontendEnvironmentBase::submitConfigurationForm public function Form submission handler. Overrides PluginFormInterface::submitConfigurationForm
FrontendEnvironmentBase::transliteration protected function Wraps the transliteration service.
FrontendEnvironmentBase::validateConfigurationForm public function Form validation handler. Overrides PluginFormInterface::validateConfigurationForm
FrontendEnvironmentBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides PluginBase::__construct 4
FrontendEnvironmentInterface::getAdditionalDeployFormElements public function Allows the plugin to add elements to the deployment form. 6
FrontendEnvironmentInterface::getBuildHookDetails public function Get the info to trigger the hook based on the configuration of the plugin. 6
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 2
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
PluginWithFormsTrait::getFormClass public function Implements \Drupal\Core\Plugin\PluginWithFormsInterface::getFormClass().
PluginWithFormsTrait::hasFormClass public function Implements \Drupal\Core\Plugin\PluginWithFormsInterface::hasFormClass().
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
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.