Skip to content

Commit

Permalink
Merge pull request #32 from prajapati-kaushik/6495-demo-symfony-6-app
Browse files Browse the repository at this point in the history
perf:  symfony6 demo app support enhancement #6495
  • Loading branch information
prajapati-kaushik authored Aug 6, 2022
2 parents c95db25 + db12f2a commit f55dacd
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 46 deletions.
25 changes: 11 additions & 14 deletions Controller/WikiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace LinkORB\Bundle\WikiBundle\Controller;

use Doctrine\ORM\EntityManagerInterface;
use LinkORB\Bundle\WikiBundle\Entity\Wiki;
use LinkORB\Bundle\WikiBundle\Form\WikiSearchType;
use LinkORB\Bundle\WikiBundle\Form\WikiType;
Expand All @@ -25,9 +26,10 @@ class WikiController extends AbstractController
{
private $wikiService;

public function __construct(WikiService $wikiService)
public function __construct(WikiService $wikiService, EntityManagerInterface $em)
{
$this->wikiService = $wikiService;
$this->em = $em;
}

/**
Expand Down Expand Up @@ -160,13 +162,13 @@ public function deleteAction(Request $request, Wiki $wiki, WikiEventService $wik
$wiki->getId(),
json_encode([
'deletedAt' => time(),
'deletedBy' => $this->getUser()->getUsername(),
'deletedBy' => $this->getUser() ? $this->getUser()->getUsername() : '',
'name' => $wiki->getName(),
])
);
$em = $this->getDoctrine()->getManager();
$em->remove($wiki);
$em->flush();

$this->em->remove($wiki);
$this->em->flush();
}

return $this->redirectToRoute('wiki_index');
Expand All @@ -180,17 +182,16 @@ protected function getEditForm(Request $request, Wiki $wiki, WikiEventService $w
$add = !$wiki->getid();

if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($wiki);
$em->flush();
$this->em->persist($wiki);
$this->em->flush();

if ($add) {
$wikiEventService->createEvent(
'wiki.created',
$wiki->getId(),
json_encode([
'createdAt' => time(),
'createdBy' => $this->getUser()->getUsername(),
'createdBy' => $this->getUser() ? $this->getUser()->getUsername() : '',
'name' => $wiki->getName(),
'description' => $wiki->getDescription(),
])
Expand All @@ -201,7 +202,7 @@ protected function getEditForm(Request $request, Wiki $wiki, WikiEventService $w
$wiki->getId(),
json_encode([
'updatedAt' => time(),
'updatedBy' => $this->getUser()->getUsername(),
'updatedBy' => $this->getUser() ? $this->getUser()->getUsername() : '',
'name' => $wiki->getName(),
'description' => $wiki->getDescription(),
])
Expand Down Expand Up @@ -288,8 +289,6 @@ public function exportSingleMarkdownAction(Wiki $wiki, WikiService $wikiService)

$markdown = $this->wikiService->renderSingleMarkdown($wiki);



$filename = $wiki->getName().'.md';
$response = new Response($markdown);
// To force download:
Expand Down Expand Up @@ -323,8 +322,6 @@ public function exportSingleHtmlAction(Wiki $wiki, WikiService $wikiService): Re
$html = $this->wikiService->processTwig($wiki, $layout, ['content' => $html]);
}



$filename = $wiki->getName().'.html';
$response = new Response($html);
// To force download:
Expand Down
36 changes: 17 additions & 19 deletions Controller/WikiPageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace LinkORB\Bundle\WikiBundle\Controller;

use Doctrine\ORM\EntityManagerInterface;
use LinkORB\Bundle\WikiBundle\Entity\Wiki;
use LinkORB\Bundle\WikiBundle\Entity\WikiPage;
use LinkORB\Bundle\WikiBundle\Form\WikiPageContentType;
Expand All @@ -24,11 +25,13 @@
class WikiPageController extends AbstractController
{
private $wikiService;
private $em;

public function __construct(WikiService $wikiService, WikiPageRepository $wikiPageRepository)
public function __construct(WikiService $wikiService, WikiPageRepository $wikiPageRepository, EntityManagerInterface $em)
{
$this->wikiPageRepository = $wikiPageRepository;
$this->wikiService = $wikiService;
$this->em = $em;
}

/**
Expand Down Expand Up @@ -94,17 +97,15 @@ public function viewAction(Request $request, Wiki $wiki, string $pageName): Resp
}
$html = $this->wikiService->markdownToHtml($wiki, $markdown);

foreach ($request->query->all() as $k=>$v) {
$html = str_replace('{{' . $k . '}}', $v, $html);
foreach ($request->query->all() as $k => $v) {
$html = str_replace('{{'.$k.'}}', $v, $html);
}

$data['contentHtml'] = $html;
$data['pageName'] = $pageName; // in case the page does not yet exist
$data['wikiPage'] = $wikiPage;
$data['wiki'] = $wiki;



return $this->render('@Wiki/wiki_page/view.html.twig', $data);
}

Expand Down Expand Up @@ -139,22 +140,21 @@ public function editAction(Request $request, Wiki $wiki, WikiEventService $wikiE
$data = json_decode($request->getContent(), true);
$wikiPage->setContent($data['content']);

$em = $this->getDoctrine()->getManager();
$em->persist($wikiPage);
$em->flush();
$this->em->persist($wikiPage);
$this->em->flush();

return new JsonResponse(['status' => 'success']);
}

if ($form->isSubmitted() && $form->isValid()) {
$this->getDoctrine()->getManager()->flush();
$this->em->flush();

$wikiEventService->createEvent(
'page.updated',
$wikiPage->getWiki()->getId(),
json_encode([
'updatedAt' => time(),
'updatedBy' => $this->getUser()->getUsername(),
'updatedBy' => $this->getUser() ? $this->getUser()->getUsername() : '',
'name' => $wikiPage->getName(),
]),
$wikiPage->getId()
Expand Down Expand Up @@ -195,15 +195,14 @@ public function deleteAction(Request $request, Wiki $wiki, WikiEventService $wik
$wikiPage->getWiki()->getId(),
json_encode([
'deletedAt' => time(),
'deletedBy' => $this->getUser()->getUsername(),
'deletedBy' => $this->getUser() ? $this->getUser()->getUsername() : '',
'name' => $wikiPage->getName(),
]),
$wikiPage->getId()
);

$em = $this->getDoctrine()->getManager();
$em->remove($wikiPage);
$em->flush();
$this->em->remove($wikiPage);
$this->em->flush();

return $this->redirectToRoute('wiki_page_index', [
'wikiName' => $wiki->getName(),
Expand Down Expand Up @@ -235,17 +234,16 @@ protected function getEditForm($request, $wikiPage, WikiEventService $wikiEventS
}
}

$em = $this->getDoctrine()->getManager();
$em->persist($wikiPage);
$em->flush();
$this->em->persist($wikiPage);
$this->em->flush();

if ($add) {
$wikiEventService->createEvent(
'page.created',
$wikiPage->getWiki()->getId(),
json_encode([
'createdAt' => time(),
'createdBy' => $this->getUser()->getUsername(),
'createdBy' => $this->getUser() ? $this->getUser()->getUsername() : '',
'name' => $wikiPage->getName(),
]),
$wikiPage->getId()
Expand All @@ -256,7 +254,7 @@ protected function getEditForm($request, $wikiPage, WikiEventService $wikiEventS
$wikiPage->getWiki()->getId(),
json_encode([
'updatedAt' => time(),
'updatedBy' => $this->getUser()->getUsername(),
'updatedBy' => $this->getUser() ? $this->getUser()->getUsername() : '',
'name' => $wikiPage->getName(),
]),
$wikiPage->getId()
Expand Down
2 changes: 1 addition & 1 deletion Resources/views/base.wiki.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
List pages
</a>

{% if is_granted('ROLE_SUPERUSER') or writeRole %}
{% if is_granted('ROLE_SUPERUSER') or writeRole is defined %}
<a href="{{ path('wiki_page_add',{'wikiName': wiki.name}) }}" class="btn btn-sm btn-success">
<i class="fa fa-plus" aria-hidden="true"></i>
New page
Expand Down
31 changes: 20 additions & 11 deletions Services/WikiService.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
namespace LinkORB\Bundle\WikiBundle\Services;

use Doctrine\ORM\EntityManagerInterface;
use League\CommonMark\CommonMarkConverter;
use LinkORB\Bundle\WikiBundle\Entity\Wiki;
use LinkORB\Bundle\WikiBundle\Repository\WikiPageRepository;
use LinkORB\Bundle\WikiBundle\Repository\WikiRepository;
use Proxies\__CG__\LinkORB\Bundle\WikiBundle\Entity\WikiPage;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Symfony\Component\Yaml\Yaml;
use League\CommonMark\CommonMarkConverter;

class WikiService
{
Expand Down Expand Up @@ -153,7 +153,7 @@ public function getToc(Wiki $wiki, array &$toc, $parentId = 0, $level = 0)
'page' => $page,
'level' => $level,
];
$this->getToc($wiki, $toc, $page->getId(), $level+1);
$this->getToc($wiki, $toc, $page->getId(), $level + 1);
// echo $page->getName();
}
}
Expand All @@ -166,19 +166,19 @@ public function renderSingleMarkdown(Wiki $wiki): ?string
$markdown = '';
foreach ($toc as $tocEntry) {
$page = $tocEntry['page'];
$pageContent = $page->getContent() ;
$pageContent = $page->getContent();

$markdown .= '<!-- page:' . $page->getName() . ' -->' . PHP_EOL;
$markdown .= trim($pageContent) . PHP_EOL . PHP_EOL;
$markdown .= '<!-- page:'.$page->getName().' -->'.PHP_EOL;
$markdown .= trim($pageContent).PHP_EOL.PHP_EOL;
}

$markdown = $this->processTwig($wiki, $markdown);

return $markdown;
}

public function processTwig(Wiki $wiki, string $content, array $extra = []): ?string
{

$templates = [];
$loader = new \Twig\Loader\ArrayLoader($templates);

Expand All @@ -190,20 +190,29 @@ public function processTwig(Wiki $wiki, string $content, array $extra = []): ?st
$variables = [
'data' => $config['data'] ?? [],
];
foreach ($extra as $k=>$v) {
foreach ($extra as $k => $v) {
$variables[$k] = $v;
}
// print_r($variables);
$content = $template->render($variables);

return $content;
}


public function getWikiPermission(Wiki $wiki)
{
$wikiRoles = ['readRole' => false, 'writeRole' => false];
$flag = false;

// https://symfony.com/doc/current/security.html#allowing-unsecured-access-i-e-anonymous-users
// unauthenticated/visitor user assign role.
if ($this->authorizationChecker->isGranted('PUBLIC_ACCESS')) {
$wikiRoles['readRole'] = true;
$wikiRoles['writeRole'] = true;

return $wikiRoles;
}

if ($this->authorizationChecker->isGranted('ROLE_SUPERUSER')) {
$wikiRoles['readRole'] = true;
$wikiRoles['writeRole'] = true;
Expand Down Expand Up @@ -262,9 +271,9 @@ public function markdownToHtml(Wiki $wiki, ?string $markdown): ?string
'html_input' => 'strip',
'allow_unsafe_links' => false,
]);

$html = $converter->convert($markdown);

$html = $converter->convert($markdown ?? '');

return $html;
}
}
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"sensio/framework-extra-bundle": "^5.1 || ^6.0",
"symfony/framework-bundle": "^4.0 || ^5.1 || ^6.0",
"symfony/security-bundle": "^4.0 || ^5.1 || ^6.0",
"twig/markdown-extra": "^3.3"
"twig/markdown-extra": "^3.3",
"league/commonmark": "^2.0"
}
}

0 comments on commit f55dacd

Please sign in to comment.