sirv-8.x-3.0-beta4/src/SirvUrl.php

src/SirvUrl.php
<?php

namespace Drupal\sirv;

use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Url;

/**
 * Defines a Sirv URL service.
 */
class SirvUrl implements SirvUrlInterface {

  /**
   * The Sirv settings.
   *
   * @var array
   */
  protected $sirvSettings;

  /**
   * Constructs a new Sirv URL service.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The configuration factory.
   */
  public function __construct(ConfigFactoryInterface $config_factory) {
    $this->sirvSettings = $config_factory->get('sirv.settings')->get();
  }

  /**
   * {@inheritdoc}
   */
  public function getUrlfromUriReference($uri_reference, array $sirv_image_settings = [], $original_scheme = '') {
    $url = $uri_reference;
    $sirv_settings = $this->sirvSettings;

    if (($sirv_settings['strip_base_path'] ?? FALSE) && $original_scheme) {
      // Determine the base URL from the original stream wrapper scheme.
      $base_url = file_url_transform_relative(file_create_url($original_scheme . '://'));

      // Remove the base URL from the front of the URL.
      if (strpos($url, $base_url) === 0) {
        $url = str_replace($base_url, '', $url);
      }
    }

    // If the URL is empty, return the original URI reference.
    if (empty($url)) {
      return $uri_reference;
    }

    // Sanitize the domain and directory.
    $domain = isset($sirv_settings['domain']) ? UrlHelper::stripDangerousProtocols($sirv_settings['domain']) : '';
    $directory = isset($sirv_settings['directory']) ? UrlHelper::stripDangerousProtocols($sirv_settings['directory']) : '';

    // If the domain is empty, return the original URI reference.
    if (empty($domain)) {
      return $uri_reference;
    }

    // Parse the URL into parts.
    $url_parts = UrlHelper::parse($url);

    // Get the path.
    $path = $url_parts['path'];

    // If the path does not start with a leading slash, add one.
    if (strpos($path, '/') !== 0) {
      $path = '/' . $path;
    }

    // Prefix the path with the directory.
    if (!empty($directory)) {
      $path = '/' . $directory . $path;
    }

    $options = [];

    // Add the query to the options.
    if (isset($sirv_image_settings['options'])) {
      $options['query'] = $sirv_image_settings['options'];
    }

    // Assemble the Sirv URL.
    $uri = 'https://' . $domain . $path;
    $sirv_url = Url::fromUri($uri, $options);
    if ($sirv_url) {
      $url = $sirv_url->toString();
    }

    return $url;
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc