performance improvements (#2329)
* remove work from constructor * refactor twig extensions * upgrade theme * replace sub-request with direct template rendering
This commit is contained in:
101
src/Twig/Runtime/MarkdownExtension.php
Normal file
101
src/Twig/Runtime/MarkdownExtension.php
Normal file
@@ -0,0 +1,101 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Kimai time-tracking app.
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace App\Twig\Runtime;
|
||||
|
||||
use App\Configuration\SystemConfiguration;
|
||||
use App\Utils\Markdown;
|
||||
use Twig\Extension\RuntimeExtensionInterface;
|
||||
|
||||
final class MarkdownExtension implements RuntimeExtensionInterface
|
||||
{
|
||||
/**
|
||||
* @var Markdown
|
||||
*/
|
||||
private $markdown;
|
||||
/**
|
||||
* @var SystemConfiguration
|
||||
*/
|
||||
private $configuration;
|
||||
/**
|
||||
* @var bool|null
|
||||
*/
|
||||
private $markdownEnabled;
|
||||
|
||||
public function __construct(Markdown $parser, SystemConfiguration $configuration)
|
||||
{
|
||||
$this->markdown = $parser;
|
||||
$this->configuration = $configuration;
|
||||
}
|
||||
|
||||
private function isMarkdownEnabled(): bool
|
||||
{
|
||||
if (null === $this->markdownEnabled) {
|
||||
$this->markdownEnabled = $this->configuration->isTimesheetMarkdownEnabled();
|
||||
}
|
||||
|
||||
return $this->markdownEnabled;
|
||||
}
|
||||
|
||||
/**
|
||||
* Transforms the entities comment (customer, project, activity ...) into HTML.
|
||||
*
|
||||
* @param string|null $content
|
||||
* @param bool $fullLength
|
||||
* @return string
|
||||
*/
|
||||
public function commentContent(?string $content, bool $fullLength = false): string
|
||||
{
|
||||
if (empty($content)) {
|
||||
return '';
|
||||
}
|
||||
|
||||
if (!$fullLength && \strlen($content) > 101) {
|
||||
$content = trim(substr($content, 0, 100)) . ' …';
|
||||
}
|
||||
|
||||
if ($this->isMarkdownEnabled()) {
|
||||
$content = $this->markdown->toHtml($content, false);
|
||||
} elseif ($fullLength) {
|
||||
$content = '<p>' . nl2br($content) . '</p>';
|
||||
}
|
||||
|
||||
return $content;
|
||||
}
|
||||
|
||||
/**
|
||||
* Transforms the timesheet description content into HTML.
|
||||
*
|
||||
* @param string|null $content
|
||||
* @return string
|
||||
*/
|
||||
public function timesheetContent(?string $content): string
|
||||
{
|
||||
if (empty($content)) {
|
||||
return '';
|
||||
}
|
||||
|
||||
if ($this->isMarkdownEnabled()) {
|
||||
return $this->markdown->toHtml($content, false);
|
||||
}
|
||||
|
||||
return nl2br($content);
|
||||
}
|
||||
|
||||
/**
|
||||
* Transforms the given Markdown content into HTML
|
||||
*
|
||||
* @param string $content
|
||||
* @return string
|
||||
*/
|
||||
public function markdownToHtml(string $content): string
|
||||
{
|
||||
return $this->markdown->toHtml($content, false);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user