src/Controller/MarkdownWikiController.php line 47

Open in your IDE?
  1. <?php
  2. /*
  3.  * Copyright (C) 2021 - All rights reserved.
  4.  * https://gigadrivegroup.com
  5.  *
  6.  * This program is free software: you can redistribute it and/or modify
  7.  * it under the terms of the GNU General Public License as published by
  8.  * the Free Software Foundation, either version 3 of the License, or
  9.  * (at your option) any later version.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program. If not, see <https://gnu.org/licenses/>
  18.  */
  19. namespace App\Controller;
  20. use Gigadrive\Bundle\MarkdownWikiBundle\Service\Storage\MarkdownWikiStorageInterface;
  21. use Parsedown;
  22. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  23. use Symfony\Component\Filesystem\Filesystem;
  24. use Symfony\Component\HttpFoundation\Request;
  25. use Symfony\Component\HttpFoundation\Response;
  26. use function PHPUnit\Framework\assertDirectoryDoesNotExist;
  27. /**
  28.  * Represents a controller used for serving pages.
  29.  *
  30.  * This controller is not routed by default. You may use it in
  31.  * your project, but it mainly serves as an example
  32.  * for using this bundle.
  33.  *
  34.  * @package Gigadrive\Bundle\MarkdownWikiBundle\Controller
  35.  * @author Mehdi Baaboura <mbaaboura@gigadrivegroup.com>
  36.  */
  37. class MarkdownWikiController extends AbstractController
  38. {
  39.     public function __construct(
  40.         protected MarkdownWikiStorageInterface $storage,
  41.     ) {
  42.     }
  43.     public function indexAction(string $pathRequest $request): Response
  44.     {
  45.         $baseDocsPath $this->getParameter('kernel.project_dir') . "/mxo-docs/";
  46.         $filesystem = new Filesystem();
  47.         $filePath $baseDocsPath $path ".md";
  48.         $html "";
  49.         if ($filesystem->exists($filePath)) {
  50.             // Parse content
  51.             $html $this->parseContent($filePath);
  52.         } else {
  53.             throw $this->createNotFoundException("Page with path $baseDocsPath . $path not found.");
  54.         }
  55.         return $this->render("markdown/index.html.twig", [
  56.             "title" => "",
  57.             "description" => "",
  58.             "content" => $html
  59.         ]);
  60.     }
  61.     function parseContent($filePath): string
  62.     {
  63.          $content file_get_contents($filePath);
  64.         // Markdown-Parser initialisieren
  65.         $parser = new Parsedown();
  66.         // Markdown in HTML parsen
  67.         return $parser->text($content);
  68.     }
  69. }