You are here

class Markdown in Markdown 8.2

Same name and namespace in other branches
  1. 3.0.x src/Markdown.php \Drupal\markdown\Markdown

Markdown service.

Hierarchy

Expanded class hierarchy of Markdown

2 files declare their use of Markdown
markdown.module in ./markdown.module
Markdown module.
ParserConfigurationForm.php in src/Form/ParserConfigurationForm.php
4 string references to 'Markdown'
markdown.info.yml in ./markdown.info.yml
markdown.info.yml
markdown.links.menu.yml in ./markdown.links.menu.yml
markdown.links.menu.yml
markdown.routing.yml in ./markdown.routing.yml
markdown.routing.yml
markdown.services.yml in ./markdown.services.yml
markdown.services.yml
1 service uses Markdown
markdown in ./markdown.services.yml
Drupal\markdown\Markdown

File

src/Markdown.php, line 22

Namespace

Drupal\markdown
View source
class Markdown implements MarkdownInterface {
  use StringTranslationTrait;

  /**
   * The cache backend.
   *
   * @var \Drupal\Core\Cache\CacheBackendInterface
   */
  protected $cache;

  /**
   * The Config Factory service.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

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

  /**
   * The HTTP Client service.
   *
   * @var \GuzzleHttp\ClientInterface
   */
  protected $httpClient;

  /**
   * The MarkdownParser Plugin Manager.
   *
   * @var \Drupal\markdown\PluginManager\ParserManagerInterface
   */
  protected $parserManager;

  /**
   * Markdown constructor.
   *
   * @param \Drupal\Core\Cache\CacheBackendInterface $cache
   *   The cache backend.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory
   *   The Config Factory service.
   * @param \Drupal\Core\File\FileSystemInterface $fileSystem
   *   The File System service.
   * @param \GuzzleHttp\ClientInterface $httpClient
   *   The HTTP Client service.
   * @param \Drupal\markdown\PluginManager\ParserManagerInterface $parserManager
   *   The Markdown Parser Plugin Manager service.
   */
  public function __construct(CacheBackendInterface $cache, ConfigFactoryInterface $configFactory, FileSystemInterface $fileSystem, ClientInterface $httpClient, ParserManagerInterface $parserManager) {
    $this->cache = $cache;
    $this->configFactory = $configFactory;
    $this->fileSystem = $fileSystem;
    $this->httpClient = $httpClient;
    $this->parserManager = $parserManager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container = NULL) {
    if (!isset($container)) {
      $container = \Drupal::getContainer();
    }
    return new static($container
      ->get('cache.markdown'), $container
      ->get('config.factory'), $container
      ->get('file_system'), $container
      ->get('http_client'), $container
      ->get('plugin.manager.markdown.parser'));
  }

  /**
   * {@inheritdoc}
   */
  public function load($id) {
    if ($id && ($cache = $this->cache
      ->get($id)) && $cache->data instanceof ParsedMarkdownInterface) {
      return $cache->data;
    }
  }

  /**
   * {@inheritdoc}
   */
  public function loadFile($path, $id = NULL, LanguageInterface $language = NULL) {
    $realpath = $this->fileSystem
      ->realpath($path) ?: $path;
    if (!file_exists($realpath)) {
      throw new MarkdownFileNotExistsException($realpath);
    }
    if (!$id) {
      $id = $this->fileSystem
        ->basename($realpath) . Crypt::hashBase64($realpath);
    }

    // Append the file modification time as a cache buster in case it changed.
    $id = "{$id}:" . filemtime($realpath);
    return $this
      ->load($id) ?: $this
      ->save($id, $this
      ->parse(file_get_contents($realpath) ?: '', $language));
  }

  /**
   * {@inheritdoc}
   *
   * @deprecated in markdown:8.x-2.0 and is removed from markdown:3.0.0.
   *   Use \Drupal\markdown\MarkdownInterface::loadFile instead.
   * @see https://www.drupal.org/project/markdown/issues/3142418
   */
  public function loadPath($path, $id = NULL, LanguageInterface $language = NULL) {
    return $this
      ->loadFile($path, $id, $language);
  }

  /**
   * {@inheritdoc}
   */
  public function loadUrl($url, $id = NULL, LanguageInterface $language = NULL) {
    if ($url instanceof Url) {
      $url = $url
        ->setAbsolute()
        ->toString();
    }
    else {
      $url = (string) $url;
    }
    if (!$id) {
      $id = $url;
    }
    if (!($parsed = $this
      ->load($id))) {
      $response = $this->httpClient
        ->get($url);
      if ($response
        ->getStatusCode() < 200 || $response
        ->getStatusCode() >= 400) {
        throw new MarkdownUrlNotExistsException($url);
      }
      $parsed = $this
        ->save($id, $this
        ->parse($response
        ->getBody()
        ->getContents(), $language));
    }
    return $parsed;
  }

  /**
   * {@inheritdoc}
   */
  public function parse($markdown, LanguageInterface $language = NULL) {
    return $this
      ->getParser()
      ->parse($markdown, $language);
  }

  /**
   * {@inheritdoc}
   */
  public function getParser($parserId = NULL, array $configuration = []) {
    if (empty($parserId)) {
      return $this->parserManager
        ->getDefaultParser($configuration);
    }
    return $this->parserManager
      ->createInstance($parserId, $configuration);
  }

  /**
   * {@inheritdoc}
   */
  public function save($id, ParsedMarkdownInterface $parsed) {
    $this->cache
      ->set($id, $parsed, $parsed
      ->getExpire(), $parsed
      ->getCacheTags());
    return $parsed;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Markdown::$cache protected property The cache backend.
Markdown::$configFactory protected property The Config Factory service.
Markdown::$fileSystem protected property The File System service.
Markdown::$httpClient protected property The HTTP Client service.
Markdown::$parserManager protected property The MarkdownParser Plugin Manager.
Markdown::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create
Markdown::getParser public function Retrieves a MarkdownParser plugin. Overrides MarkdownInterface::getParser
Markdown::load public function Loads a cached ParsedMarkdown object. Overrides MarkdownInterface::load
Markdown::loadFile public function Loads a cached ParsedMarkdown object based on system file. Overrides MarkdownInterface::loadFile
Markdown::loadPath Deprecated public function Overrides MarkdownInterface::loadPath
Markdown::loadUrl public function Loads a cached ParsedMarkdown object based on a URL. Overrides MarkdownInterface::loadUrl
Markdown::parse public function Parses markdown into HTML. Overrides MarkdownInterface::parse
Markdown::save public function Saves a parsed markdown object. Overrides MarkdownInterface::save
Markdown::__construct public function Markdown constructor.
MarkdownInterface::DOCUMENTATION_URL constant The base URL for Markdown documentation.
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.