Start on removing git and better integrate db
This commit is contained in:
@ -2,10 +2,10 @@
|
||||
|
||||
namespace App\Service\SnipParser\Stages;
|
||||
|
||||
use App\Repository\SnipContentRepository;
|
||||
use App\Repository\SnipRepository;
|
||||
use App\Security\Voter\SnipVoter;
|
||||
use App\Service\SnipParser\Pipeline;
|
||||
use App\Service\SnipServiceFactory;
|
||||
use League\Pipeline\StageInterface;
|
||||
use Symfony\Bundle\SecurityBundle\Security;
|
||||
use Symfony\Component\DependencyInjection\Attribute\Autowire;
|
||||
@ -13,10 +13,10 @@ use Symfony\Component\DependencyInjection\Attribute\Autowire;
|
||||
class IncludeReferenceStage implements StageInterface
|
||||
{
|
||||
public function __construct(
|
||||
#[Autowire(lazy: true)] private readonly Security $security,
|
||||
#[Autowire(lazy: true)] private readonly SnipRepository $snipRepository,
|
||||
#[Autowire(lazy: true)] private readonly SnipServiceFactory $snipServiceFactory,
|
||||
#[Autowire(lazy: true)] private readonly Pipeline $pipeline,
|
||||
#[Autowire(lazy: true)] private readonly Security $security,
|
||||
#[Autowire(lazy: true)] private readonly SnipRepository $snipRepository,
|
||||
#[Autowire(lazy: true)] private readonly SnipContentRepository $snipContentRepository,
|
||||
#[Autowire(lazy: true)] private readonly Pipeline $pipeline,
|
||||
) {}
|
||||
|
||||
public function __invoke(mixed $payload): string
|
||||
@ -26,19 +26,32 @@ class IncludeReferenceStage implements StageInterface
|
||||
|
||||
private function replaceReferences(mixed $payload): string
|
||||
{
|
||||
// replaces all references (#n) to other snips with links
|
||||
$pattern = '/\{\{(\d+)\}\}/';
|
||||
// replaces all references ({{ID}}) with the content of the snip
|
||||
$pattern = '/\{\{([A-Z0-9]+)\}\}/';
|
||||
|
||||
return preg_replace_callback($pattern, function ($matches) {
|
||||
$snip = $this->snipRepository->find($matches[1]);
|
||||
if ($snip === null) {
|
||||
return sprintf('<span title="not found">%s</span>', $matches[0]);
|
||||
$id = $matches[1];
|
||||
try {
|
||||
$content = $this->snipContentRepository->find($id);
|
||||
} catch (\Exception) {
|
||||
$content = null;
|
||||
}
|
||||
if ($content) {
|
||||
$snip = $content->getSnip();
|
||||
} else {
|
||||
$snip = $this->snipRepository->find($id);
|
||||
if ($snip) {
|
||||
$content = $this->snipContentRepository->find($snip->getActiveCommit());
|
||||
}
|
||||
}
|
||||
if ($content === null) {
|
||||
return sprintf('<span title="snip or content not found">%s</span>', $matches[0]);
|
||||
}
|
||||
if (!$this->security->isGranted(SnipVoter::VIEW, $snip)) {
|
||||
return sprintf('<span title="access denied">%s</span>', $matches[0]);
|
||||
}
|
||||
|
||||
return $this->pipeline->parse($this->snipServiceFactory->create($snip)->get());
|
||||
return $this->pipeline->parse($content->getText());
|
||||
}, $payload);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user