From db12f2a929348b45df757e60ae042926b5d01b6f Mon Sep 17 00:00:00 2001 From: Prajapati Kaushik Date: Sat, 6 Aug 2022 13:37:04 +0100 Subject: [PATCH] perf: symfony6 demo app support enhancement #6495 --- Controller/WikiController.php | 25 +++++++++----------- Controller/WikiPageController.php | 36 ++++++++++++++--------------- Resources/views/base.wiki.html.twig | 2 +- Services/WikiService.php | 31 ++++++++++++++++--------- composer.json | 3 ++- 5 files changed, 51 insertions(+), 46 deletions(-) diff --git a/Controller/WikiController.php b/Controller/WikiController.php index aaa5c19..c9ce9a8 100755 --- a/Controller/WikiController.php +++ b/Controller/WikiController.php @@ -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; @@ -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; } /** @@ -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'); @@ -180,9 +182,8 @@ 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( @@ -190,7 +191,7 @@ protected function getEditForm(Request $request, Wiki $wiki, WikiEventService $w $wiki->getId(), json_encode([ 'createdAt' => time(), - 'createdBy' => $this->getUser()->getUsername(), + 'createdBy' => $this->getUser() ? $this->getUser()->getUsername() : '', 'name' => $wiki->getName(), 'description' => $wiki->getDescription(), ]) @@ -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(), ]) @@ -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: @@ -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: diff --git a/Controller/WikiPageController.php b/Controller/WikiPageController.php index 4593d6f..9bc5576 100755 --- a/Controller/WikiPageController.php +++ b/Controller/WikiPageController.php @@ -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; @@ -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; } /** @@ -94,8 +97,8 @@ 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; @@ -103,8 +106,6 @@ public function viewAction(Request $request, Wiki $wiki, string $pageName): Resp $data['wikiPage'] = $wikiPage; $data['wiki'] = $wiki; - - return $this->render('@Wiki/wiki_page/view.html.twig', $data); } @@ -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() @@ -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(), @@ -235,9 +234,8 @@ 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( @@ -245,7 +243,7 @@ protected function getEditForm($request, $wikiPage, WikiEventService $wikiEventS $wikiPage->getWiki()->getId(), json_encode([ 'createdAt' => time(), - 'createdBy' => $this->getUser()->getUsername(), + 'createdBy' => $this->getUser() ? $this->getUser()->getUsername() : '', 'name' => $wikiPage->getName(), ]), $wikiPage->getId() @@ -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() diff --git a/Resources/views/base.wiki.html.twig b/Resources/views/base.wiki.html.twig index 01f434e..960ecc9 100644 --- a/Resources/views/base.wiki.html.twig +++ b/Resources/views/base.wiki.html.twig @@ -87,7 +87,7 @@ List pages - {% if is_granted('ROLE_SUPERUSER') or writeRole %} + {% if is_granted('ROLE_SUPERUSER') or writeRole is defined %} New page diff --git a/Services/WikiService.php b/Services/WikiService.php index 80f1167..21f2ba1 100644 --- a/Services/WikiService.php +++ b/Services/WikiService.php @@ -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 { @@ -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(); } } @@ -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 .= '' . PHP_EOL; - $markdown .= trim($pageContent) . PHP_EOL . PHP_EOL; + $markdown .= ''.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); @@ -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; @@ -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; } } diff --git a/composer.json b/composer.json index d9810c7..282baff 100755 --- a/composer.json +++ b/composer.json @@ -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" } } \ No newline at end of file