View source
<?php
namespace Drupal\markdown;
use Drupal\Component\Utility\Crypt;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Url;
use Drupal\markdown\Exception\MarkdownFileNotExistsException;
use Drupal\markdown\Exception\MarkdownUrlNotExistsException;
use Drupal\markdown\PluginManager\ParserManagerInterface;
use Drupal\markdown\Render\ParsedMarkdownInterface;
use GuzzleHttp\ClientInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class Markdown implements MarkdownInterface {
use StringTranslationTrait;
protected $cache;
protected $configFactory;
protected $fileSystem;
protected $httpClient;
protected $parserManager;
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;
}
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'));
}
public function load($id) {
if ($id && ($cache = $this->cache
->get($id)) && $cache->data instanceof ParsedMarkdownInterface) {
return $cache->data;
}
}
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);
}
$id = "{$id}:" . filemtime($realpath);
return $this
->load($id) ?: $this
->save($id, $this
->parse(file_get_contents($realpath) ?: '', $language));
}
public function loadPath($path, $id = NULL, LanguageInterface $language = NULL) {
return $this
->loadFile($path, $id, $language);
}
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;
}
public function parse($markdown, LanguageInterface $language = NULL) {
return $this
->getParser()
->parse($markdown, $language);
}
public function getParser($parserId = NULL, array $configuration = []) {
if (empty($parserId)) {
return $this->parserManager
->getDefaultParser($configuration);
}
return $this->parserManager
->createInstance($parserId, $configuration);
}
public function save($id, ParsedMarkdownInterface $parsed) {
$this->cache
->set($id, $parsed, $parsed
->getExpire(), $parsed
->getCacheTags());
return $parsed;
}
}