add 'adding content' chapter
This commit is contained in:
parent
ab3227b75f
commit
eb20213b94
20 changed files with 884 additions and 251 deletions
|
@ -2,18 +2,15 @@
|
|||
|
||||
namespace Lubian\NoFramework\Action;
|
||||
|
||||
use Lubian\NoFramework\Template\MarkdownParser;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
|
||||
final class Other
|
||||
{
|
||||
public function someFunctionName(ResponseInterface $response): ResponseInterface
|
||||
public function someFunctionName(ResponseInterface $response, MarkdownParser $parser): ResponseInterface
|
||||
{
|
||||
$body = $response->getBody();
|
||||
|
||||
$body->write('This works too!');
|
||||
|
||||
return $response
|
||||
->withStatus(200)
|
||||
->withBody($body);
|
||||
$html = $parser->parse('This *works* **too!**');
|
||||
$response->getBody()->write($html);
|
||||
return $response->withStatus(200);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,50 +2,79 @@
|
|||
|
||||
namespace Lubian\NoFramework\Action;
|
||||
|
||||
use Lubian\NoFramework\Model\MarkdownPage;
|
||||
use Lubian\NoFramework\Repository\MarkdownPageFilesystem;
|
||||
use Lubian\NoFramework\Repository\MarkdownPageRepo;
|
||||
use Lubian\NoFramework\Exception\InternalServerError;
|
||||
use Lubian\NoFramework\Template\MarkdownParser;
|
||||
use Lubian\NoFramework\Template\Renderer;
|
||||
use Parsedown;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
|
||||
use function array_filter;
|
||||
use function array_map;
|
||||
use function array_values;
|
||||
use function file_get_contents;
|
||||
use function glob;
|
||||
use function preg_replace;
|
||||
use function str_contains;
|
||||
use function str_replace;
|
||||
use function substr;
|
||||
|
||||
class Page
|
||||
{
|
||||
public function __construct(
|
||||
private ResponseInterface $response,
|
||||
private MarkdownPageRepo $repo,
|
||||
private Parsedown $parsedown,
|
||||
private MarkdownParser $parser,
|
||||
private Renderer $renderer,
|
||||
){}
|
||||
private string $pagesPath = __DIR__ . '/../../data/pages/'
|
||||
) {
|
||||
}
|
||||
|
||||
public function show(
|
||||
string $page,
|
||||
): ResponseInterface {
|
||||
$page = $this->repo->byTitle($page);
|
||||
$content = $this->linkFilter($page->content);
|
||||
$content = $this->parsedown->parse($content);
|
||||
$html = $this->renderer->render('page', ['content' => $content, 'title' => $page->title]);
|
||||
$page = array_values(
|
||||
array_filter(
|
||||
$this->getPages(),
|
||||
fn (string $filename) => str_contains($filename, $page)
|
||||
)
|
||||
)[0];
|
||||
$markdown = file_get_contents($page);
|
||||
|
||||
// fix the next and previous buttons to work with our routing
|
||||
$markdown = preg_replace('/\(\d\d-/m', '(', $markdown);
|
||||
$markdown = str_replace('.md)', ')', $markdown);
|
||||
|
||||
$page = str_replace([$this->pagesPath, '.md'], ['', ''], $page);
|
||||
$data = [
|
||||
'title' => substr($page, 3),
|
||||
'content' => $this->parser->parse($markdown),
|
||||
];
|
||||
$html = $this->renderer->render('page/show', $data);
|
||||
$this->response->getBody()->write($html);
|
||||
return $this->response;
|
||||
}
|
||||
|
||||
public function list(): ResponseInterface
|
||||
{
|
||||
$pages = array_map(
|
||||
fn (MarkdownPage $p) => ['title' => $p->title, 'id' => $p->id],
|
||||
$this->repo->all()
|
||||
);
|
||||
$html = $this->renderer->render('pagelist', ['pages' => $pages]);
|
||||
$pages = array_map(function (string $page) {
|
||||
$page = str_replace([$this->pagesPath, '.md'], ['', ''], $page);
|
||||
return [
|
||||
'id' => substr($page, 0, 2),
|
||||
'title' => substr($page, 3),
|
||||
];
|
||||
}, $this->getPages());
|
||||
$html = $this->renderer->render('page/list', ['pages' => $pages]);
|
||||
$this->response->getBody()->write($html);
|
||||
return $this->response;
|
||||
|
||||
}
|
||||
|
||||
private function linkFilter(string $content): string
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
private function getPages(): array
|
||||
{
|
||||
$content = preg_replace('/\(\d\d-/m', '(', $content);
|
||||
return str_replace('.md)', ')', $content);
|
||||
$files = glob($this->pagesPath . '*.md');
|
||||
if ($files === false) {
|
||||
throw new InternalServerError('cannot read pages');
|
||||
}
|
||||
return $files;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,7 +1,4 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Lubian\NoFramework\Factory;
|
||||
|
||||
|
@ -13,7 +10,10 @@ use Lubian\NoFramework\Settings;
|
|||
|
||||
final class DoctrineEm
|
||||
{
|
||||
public function __construct(private Settings $settings){}
|
||||
public function __construct(private Settings $settings)
|
||||
{
|
||||
}
|
||||
|
||||
public function create(): EntityManagerInterface
|
||||
{
|
||||
$config = Setup::createConfiguration($this->settings->doctrine['devMode']);
|
||||
|
@ -29,4 +29,4 @@ final class DoctrineEm
|
|||
$config,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -20,7 +20,7 @@ final class SettingsContainerProvider implements ContainerProvider
|
|||
$dependencies = require $settings->dependenciesFile;
|
||||
$dependencies[Settings::class] = $settings;
|
||||
$builder->addDefinitions($dependencies);
|
||||
// $builder->enableCompilation('/tmp');
|
||||
// $builder->enableCompilation('/tmp');
|
||||
return $builder->build();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -19,13 +19,12 @@ final class CacheMiddleware implements MiddlewareInterface
|
|||
private CacheInterface $cache,
|
||||
private Response\Serializer $serializer,
|
||||
private Settings $settings,
|
||||
)
|
||||
{
|
||||
) {
|
||||
}
|
||||
|
||||
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
|
||||
{
|
||||
if ($request->getMethod() === 'GET' && !$this->settings->isDev()) {
|
||||
if ($request->getMethod() === 'GET' && ! $this->settings->isDev()) {
|
||||
$key = (string) $request->getUri();
|
||||
$key = base64_encode($key);
|
||||
$callback = fn () => $handler->handle($request);
|
||||
|
|
|
@ -3,17 +3,15 @@
|
|||
namespace Lubian\NoFramework\Model;
|
||||
|
||||
use Doctrine\DBAL\Types\Types;
|
||||
use Doctrine\ORM\Mapping\Column;
|
||||
use Doctrine\ORM\Mapping\Entity;
|
||||
use Doctrine\ORM\Mapping\GeneratedValue;
|
||||
use Doctrine\ORM\Mapping\Id;
|
||||
|
||||
#[Entity]
|
||||
class MarkdownPage
|
||||
{
|
||||
public function __construct(
|
||||
#[Id, Column, GeneratedValue]
|
||||
public int|null $id = null,
|
||||
#[Id,
|
||||
Column,
|
||||
GeneratedValue]
|
||||
public int |null $id = null,
|
||||
#[Column]
|
||||
public string $title,
|
||||
#[Column(type: Types::TEXT)]
|
||||
|
|
|
@ -1,7 +1,4 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Lubian\NoFramework\Repository;
|
||||
|
||||
|
@ -10,15 +7,17 @@ use Doctrine\ORM\EntityRepository;
|
|||
use Lubian\NoFramework\Exception\NotFound;
|
||||
use Lubian\NoFramework\Model\MarkdownPage;
|
||||
|
||||
use function random_int;
|
||||
use function usleep;
|
||||
|
||||
final class DoctrineMarkdownPageRepo implements MarkdownPageRepo
|
||||
{
|
||||
/**
|
||||
* @var EntityRepository<MarkdownPage>
|
||||
*/
|
||||
/** @var EntityRepository<MarkdownPage> */
|
||||
private EntityRepository $repo;
|
||||
|
||||
public function __construct(
|
||||
private EntityManagerInterface $entityManager
|
||||
){
|
||||
) {
|
||||
$this->repo = $this->entityManager->getRepository(MarkdownPage::class);
|
||||
}
|
||||
|
||||
|
@ -27,15 +26,15 @@ final class DoctrineMarkdownPageRepo implements MarkdownPageRepo
|
|||
*/
|
||||
public function all(): array
|
||||
{
|
||||
usleep(rand(500, 1500) * 1000);
|
||||
usleep(random_int(500, 1500) * 1000);
|
||||
return $this->repo->findAll();
|
||||
}
|
||||
|
||||
public function byId(int $id): MarkdownPage
|
||||
{
|
||||
usleep(rand(500, 1500) * 1000);
|
||||
usleep(random_int(500, 1500) * 1000);
|
||||
$page = $this->repo->findOneBy(['id' => $id]);
|
||||
if (!$page instanceof MarkdownPage){
|
||||
if (! $page instanceof MarkdownPage) {
|
||||
throw new NotFound;
|
||||
}
|
||||
return $page;
|
||||
|
@ -43,9 +42,9 @@ final class DoctrineMarkdownPageRepo implements MarkdownPageRepo
|
|||
|
||||
public function byTitle(string $title): MarkdownPage
|
||||
{
|
||||
usleep(rand(500, 1500) * 1000);
|
||||
usleep(random_int(500, 1500) * 1000);
|
||||
$page = $this->repo->findOneBy(['title' => $title]);
|
||||
if (!$page instanceof MarkdownPage){
|
||||
if (! $page instanceof MarkdownPage) {
|
||||
throw new NotFound;
|
||||
}
|
||||
return $page;
|
||||
|
@ -57,4 +56,4 @@ final class DoctrineMarkdownPageRepo implements MarkdownPageRepo
|
|||
$this->entityManager->flush();
|
||||
return $page;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -13,6 +13,7 @@ use function count;
|
|||
use function file_get_contents;
|
||||
use function glob;
|
||||
use function is_array;
|
||||
use function random_int;
|
||||
use function str_replace;
|
||||
use function substr;
|
||||
use function usleep;
|
||||
|
@ -31,7 +32,7 @@ final class MarkdownPageFilesystem implements MarkdownPageRepo
|
|||
$fileNames = glob($this->dataPath . '*.md');
|
||||
assert(is_array($fileNames));
|
||||
return array_map(function (string $name): MarkdownPage {
|
||||
usleep(rand(200, 500) * 1000);
|
||||
usleep(random_int(200, 500) * 1000);
|
||||
$content = file_get_contents($name);
|
||||
$name = str_replace($this->dataPath, '', $name);
|
||||
$name = str_replace('.md', '', $name);
|
||||
|
|
28
app/src/Template/GithubMarkdownRenderer.php
Normal file
28
app/src/Template/GithubMarkdownRenderer.php
Normal file
|
@ -0,0 +1,28 @@
|
|||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Lubian\NoFramework\Template;
|
||||
|
||||
use League\CommonMark\Environment\Environment;
|
||||
use League\CommonMark\Extension\CommonMark\CommonMarkCoreExtension;
|
||||
use League\CommonMark\Extension\GithubFlavoredMarkdownExtension;
|
||||
use League\CommonMark\MarkdownConverter;
|
||||
|
||||
final class GithubMarkdownRenderer implements MarkdownRenderer
|
||||
{
|
||||
private MarkdownConverter $engine;
|
||||
|
||||
public function __construct(
|
||||
CommonMarkCoreExtension $commonMarkCoreExtension,
|
||||
GithubFlavoredMarkdownExtension $githubFlavoredMarkdownExtension,
|
||||
) {
|
||||
$environment = new Environment([]);
|
||||
$environment->addExtension($commonMarkCoreExtension);
|
||||
$environment->addExtension($githubFlavoredMarkdownExtension);
|
||||
$this->engine = new MarkdownConverter($environment);
|
||||
}
|
||||
|
||||
public function render(string $markdown): string
|
||||
{
|
||||
return (string) $this->engine->convert($markdown);
|
||||
}
|
||||
}
|
8
app/src/Template/MarkdownParser.php
Normal file
8
app/src/Template/MarkdownParser.php
Normal file
|
@ -0,0 +1,8 @@
|
|||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Lubian\NoFramework\Template;
|
||||
|
||||
interface MarkdownParser
|
||||
{
|
||||
public function parse(string $markdown): string;
|
||||
}
|
17
app/src/Template/ParsedownParser.php
Normal file
17
app/src/Template/ParsedownParser.php
Normal file
|
@ -0,0 +1,17 @@
|
|||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Lubian\NoFramework\Template;
|
||||
|
||||
use Parsedown;
|
||||
|
||||
final class ParsedownParser implements MarkdownParser
|
||||
{
|
||||
public function __construct(private Parsedown $parser)
|
||||
{
|
||||
}
|
||||
|
||||
public function parse(string $markdown): string
|
||||
{
|
||||
return $this->parser->parse($markdown);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue