Skip to content

Commit a1490b2

Browse files
committed
updated index content negotiation, refactor
Signed-off-by: bidi47 <bidi@apidemia.com>
1 parent d1a034e commit a1490b2

30 files changed

Lines changed: 759 additions & 134 deletions

‎src/App/src/Factory/GetIndexViewHandlerFactory.php‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Psr\Container\NotFoundExceptionInterface;
1313

1414
use function assert;
15+
use function getcwd;
1516

1617
class GetIndexViewHandlerFactory
1718
{
@@ -26,6 +27,6 @@ public function __invoke(ContainerInterface $container, string $requestedName):
2627
$template = $container->get(TemplateRendererInterface::class);
2728
assert($template instanceof TemplateRendererInterface);
2829

29-
return new GetIndexViewHandler($template, $postRepository);
30+
return new GetIndexViewHandler($template, $postRepository, getcwd() . '/public/md-pages');
3031
}
3132
}

‎src/App/src/Handler/GetIndexViewHandler.php‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,41 @@
44

55
namespace Light\App\Handler;
66

7+
use Fig\Http\Message\StatusCodeInterface;
78
use Laminas\Diactoros\Response\HtmlResponse;
9+
use Laminas\Diactoros\Response\TextResponse;
810
use Light\Blog\Repository\PostRepository;
911
use Mezzio\Template\TemplateRendererInterface;
1012
use Psr\Http\Message\ResponseInterface;
1113
use Psr\Http\Message\ServerRequestInterface;
1214
use Psr\Http\Server\RequestHandlerInterface;
1315

16+
use function file_get_contents;
17+
use function is_file;
18+
use function str_contains;
19+
1420
class GetIndexViewHandler implements RequestHandlerInterface
1521
{
1622
public function __construct(
1723
protected TemplateRendererInterface $template,
1824
protected PostRepository $postRepository,
25+
protected string $mdPagesPath,
1926
) {
2027
}
2128

2229
public function handle(ServerRequestInterface $request): ResponseInterface
2330
{
31+
if (str_contains($request->getHeaderLine('Accept'), 'text/markdown')) {
32+
$markdownFile = $this->mdPagesPath . '/index.md';
33+
if (is_file($markdownFile)) {
34+
return new TextResponse(
35+
(string) file_get_contents($markdownFile),
36+
StatusCodeInterface::STATUS_OK,
37+
['Content-Type' => 'text/markdown; charset=utf-8'],
38+
);
39+
}
40+
}
41+
2442
$posts = $this->postRepository->getRecentPosts(3);
2543
return new HtmlResponse(
2644
$this->template->render('app::index', ['posts' => $posts])

‎src/Blog/src/ConfigProvider.php‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Light\Blog\Factory\Author\AuthorCollectionHandlerFactory;
1010
use Light\Blog\Factory\Author\AuthorResourceHandlerFactory;
1111
use Light\Blog\Factory\Author\AuthorResourceRepositoryFactory;
12+
use Light\Blog\Factory\BlogServiceFactory;
1213
use Light\Blog\Factory\Category\CategoryCollectionHandlerFactory;
1314
use Light\Blog\Factory\Category\CategoryCollectionRepositoryFactory;
1415
use Light\Blog\Factory\Category\CategoryResourceHandlerFactory;
@@ -28,6 +29,8 @@
2829
use Light\Blog\Repository\CategoryRepository;
2930
use Light\Blog\Repository\PostRepository;
3031
use Light\Blog\Repository\TagRepository;
32+
use Light\Blog\Service\BlogService;
33+
use Light\Blog\Service\BlogServiceInterface;
3134
use Mezzio\Application;
3235

3336
class ConfigProvider
@@ -51,6 +54,7 @@ public function __invoke(): array
5154
* @return array{
5255
* delegators: array<class-string, array<class-string>>,
5356
* factories: array<class-string, class-string>,
57+
* aliases: array<class-string, class-string>,
5458
* }
5559
*/
5660
private function getDependencies(): array
@@ -73,6 +77,10 @@ private function getDependencies(): array
7377
GetAuthorCollectionHandler::class => AuthorCollectionHandlerFactory::class,
7478
TagRepository::class => TagResourceRepositoryFactory::class,
7579
GetTagResourceHandler::class => TagResourceHandlerFactory::class,
80+
BlogService::class => BlogServiceFactory::class,
81+
],
82+
'aliases' => [
83+
BlogServiceInterface::class => BlogService::class,
7684
],
7785
];
7886
}

‎src/Blog/src/Factory/Author/AuthorResourceHandlerFactory.php‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Light\Blog\Repository\AuthorRepository;
99
use Light\Blog\Repository\CategoryRepository;
1010
use Light\Blog\Repository\PostRepository;
11+
use Light\Blog\Service\BlogServiceInterface;
1112
use Mezzio\Template\TemplateRendererInterface;
1213
use Psr\Container\ContainerExceptionInterface;
1314
use Psr\Container\ContainerInterface;
@@ -27,11 +28,13 @@ public function __invoke(ContainerInterface $container, string $requestedName):
2728
$template = $container->get(TemplateRendererInterface::class);
2829
$postRepository = $container->get(PostRepository::class);
2930
$categoryRepository = $container->get(CategoryRepository::class);
31+
$blogService = $container->get(BlogServiceInterface::class);
3032

3133
assert($repository instanceof AuthorRepository);
3234
assert($template instanceof TemplateRendererInterface);
3335
assert($postRepository instanceof PostRepository);
36+
assert($blogService instanceof BlogServiceInterface);
3437

35-
return new GetAuthorResourceHandler($template, $repository, $postRepository, $categoryRepository);
38+
return new GetAuthorResourceHandler($template, $repository, $postRepository, $categoryRepository, $blogService);
3639
}
3740
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Light\Blog\Factory;
6+
7+
use Light\Blog\Service\BlogService;
8+
use Light\Blog\Service\BlogServiceInterface;
9+
use Mezzio\Template\TemplateRendererInterface;
10+
use Psr\Container\ContainerExceptionInterface;
11+
use Psr\Container\ContainerInterface;
12+
use Psr\Container\NotFoundExceptionInterface;
13+
14+
use function assert;
15+
use function getcwd;
16+
17+
class BlogServiceFactory
18+
{
19+
/**
20+
* @throws ContainerExceptionInterface
21+
* @throws NotFoundExceptionInterface
22+
*/
23+
public function __invoke(ContainerInterface $container, string $requestedName): BlogServiceInterface
24+
{
25+
$template = $container->get(TemplateRendererInterface::class);
26+
assert($template instanceof TemplateRendererInterface);
27+
28+
return new BlogService($template, getcwd() . '/public/md-articles');
29+
}
30+
}

‎src/Blog/src/Factory/Category/CategoryResourceHandlerFactory.php‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Light\Blog\Handler\GetCategoryResourceHandler;
88
use Light\Blog\Repository\CategoryRepository;
9+
use Light\Blog\Service\BlogServiceInterface;
910
use Mezzio\Template\TemplateRendererInterface;
1011
use Psr\Container\ContainerExceptionInterface;
1112
use Psr\Container\ContainerInterface;
@@ -21,12 +22,14 @@ class CategoryResourceHandlerFactory
2122
*/
2223
public function __invoke(ContainerInterface $container, string $requestedName): GetCategoryResourceHandler
2324
{
24-
$repository = $container->get(CategoryRepository::class);
25-
$template = $container->get(TemplateRendererInterface::class);
25+
$repository = $container->get(CategoryRepository::class);
26+
$template = $container->get(TemplateRendererInterface::class);
27+
$blogService = $container->get(BlogServiceInterface::class);
2628

2729
assert($repository instanceof CategoryRepository);
2830
assert($template instanceof TemplateRendererInterface);
31+
assert($blogService instanceof BlogServiceInterface);
2932

30-
return new GetCategoryResourceHandler($template, $repository);
33+
return new GetCategoryResourceHandler($template, $repository, $blogService);
3134
}
3235
}

‎src/Blog/src/Factory/Post/PostResourceHandlerFactory.php‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Light\Blog\Handler\GetPostResourceHandler;
88
use Light\Blog\Repository\CategoryRepository;
99
use Light\Blog\Repository\PostRepository;
10+
use Light\Blog\Service\BlogServiceInterface;
1011
use Mezzio\Template\TemplateRendererInterface;
1112
use Psr\Container\ContainerExceptionInterface;
1213
use Psr\Container\ContainerInterface;
@@ -25,10 +26,17 @@ public function __invoke(ContainerInterface $container, string $requestedName):
2526
$repository = $container->get(PostRepository::class);
2627
$categoryRepository = $container->get(CategoryRepository::class);
2728
$template = $container->get(TemplateRendererInterface::class);
29+
$blogService = $container->get(BlogServiceInterface::class);
2830

2931
assert($repository instanceof PostRepository);
3032
assert($template instanceof TemplateRendererInterface);
33+
assert($blogService instanceof BlogServiceInterface);
3134

32-
return new GetPostResourceHandler($template, $repository, $categoryRepository);
35+
return new GetPostResourceHandler(
36+
$template,
37+
$repository,
38+
$categoryRepository,
39+
$blogService
40+
);
3341
}
3442
}

‎src/Blog/src/Factory/Tag/TagResourceHandlerFactory.php‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Light\Blog\Handler\GetTagResourceHandler;
88
use Light\Blog\Repository\CategoryRepository;
99
use Light\Blog\Repository\TagRepository;
10+
use Light\Blog\Service\BlogServiceInterface;
1011
use Mezzio\Template\TemplateRendererInterface;
1112
use Psr\Container\ContainerExceptionInterface;
1213
use Psr\Container\ContainerInterface;
@@ -25,11 +26,13 @@ public function __invoke(ContainerInterface $container, string $requestedName):
2526
$repository = $container->get(TagRepository::class);
2627
$template = $container->get(TemplateRendererInterface::class);
2728
$categoryRepository = $container->get(CategoryRepository::class);
29+
$blogService = $container->get(BlogServiceInterface::class);
2830

2931
assert($repository instanceof TagRepository);
3032
assert($template instanceof TemplateRendererInterface);
3133
assert($categoryRepository instanceof CategoryRepository);
34+
assert($blogService instanceof BlogServiceInterface);
3235

33-
return new GetTagResourceHandler($template, $repository, $categoryRepository);
36+
return new GetTagResourceHandler($template, $repository, $categoryRepository, $blogService);
3437
}
3538
}

‎src/Blog/src/Handler/GetAuthorResourceHandler.php‎

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,12 @@
44

55
namespace Light\Blog\Handler;
66

7-
use Fig\Http\Message\StatusCodeInterface;
87
use Laminas\Diactoros\Response\HtmlResponse;
98
use Light\App\Helper\Paginator;
10-
use Light\Blog\Entity\Author;
119
use Light\Blog\Repository\AuthorRepository;
1210
use Light\Blog\Repository\CategoryRepository;
1311
use Light\Blog\Repository\PostRepository;
12+
use Light\Blog\Service\BlogServiceInterface;
1413
use Mezzio\Template\TemplateRendererInterface;
1514
use Psr\Http\Message\ResponseInterface;
1615
use Psr\Http\Message\ServerRequestInterface;
@@ -23,6 +22,7 @@ public function __construct(
2322
protected AuthorRepository $authorRepository,
2423
protected PostRepository $postRepository,
2524
protected CategoryRepository $categoryRepository,
25+
protected BlogServiceInterface $blogService,
2626
) {
2727
}
2828

@@ -31,7 +31,7 @@ public function handle(ServerRequestInterface $request): ResponseInterface
3131
$authorSlug = $request->getAttribute('slug');
3232
$author = $this->authorRepository->getAuthorResource($authorSlug);
3333
if (! $author) {
34-
return $this->notFound($this->authorRepository->getAuthorsWithPublishedPosts());
34+
return $this->blogService->authorNotFound($this->authorRepository->getAuthorsWithPublishedPosts());
3535
}
3636
$categories = $this->categoryRepository->getCategories();
3737

@@ -51,17 +51,4 @@ public function handle(ServerRequestInterface $request): ResponseInterface
5151
])
5252
);
5353
}
54-
55-
/**
56-
* @param Author[] $authors
57-
*/
58-
private function notFound(array $authors): HtmlResponse
59-
{
60-
return new HtmlResponse(
61-
$this->template->render('error::404', [
62-
'authors' => $authors,
63-
]),
64-
StatusCodeInterface::STATUS_NOT_FOUND
65-
);
66-
}
6754
}

‎src/Blog/src/Handler/GetCategoryResourceHandler.php‎

Lines changed: 5 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@
44

55
namespace Light\Blog\Handler;
66

7-
use Fig\Http\Message\StatusCodeInterface;
87
use Laminas\Diactoros\Response\HtmlResponse;
98
use Light\App\Helper\Paginator;
10-
use Light\Blog\Entity\Category;
119
use Light\Blog\Repository\CategoryRepository;
10+
use Light\Blog\Service\BlogServiceInterface;
1211
use Mezzio\Template\TemplateRendererInterface;
1312
use Psr\Http\Message\ResponseInterface;
1413
use Psr\Http\Message\ServerRequestInterface;
@@ -20,6 +19,7 @@ class GetCategoryResourceHandler implements RequestHandlerInterface
2019
public function __construct(
2120
protected TemplateRendererInterface $template,
2221
protected CategoryRepository $categoryRepository,
22+
protected BlogServiceInterface $blogService,
2323
) {
2424
}
2525

@@ -29,10 +29,10 @@ public function handle(ServerRequestInterface $request): ResponseInterface
2929
$categories = $this->categoryRepository->getCategories();
3030
$category = $this->categoryRepository->getCategoryResource($categorySlug);
3131
if ($category === null) {
32-
return $this->notFound($categories);
32+
return $this->blogService->notFound($categories);
3333
}
3434
if (! $category->isVisible()) {
35-
return $this->gone($categories);
35+
return $this->blogService->gone($categories);
3636
}
3737
$meta = $category;
3838

@@ -55,33 +55,7 @@ public function handle(ServerRequestInterface $request): ResponseInterface
5555
);
5656
return new HtmlResponse($html);
5757
} catch (Throwable $e) {
58-
return $this->notFound($categories);
58+
return $this->blogService->notFound($categories);
5959
}
6060
}
61-
62-
/**
63-
* @param Category[] $categories
64-
*/
65-
private function notFound(array $categories): HtmlResponse
66-
{
67-
return new HtmlResponse(
68-
$this->template->render('error::404', [
69-
'categories' => $categories,
70-
]),
71-
StatusCodeInterface::STATUS_NOT_FOUND
72-
);
73-
}
74-
75-
/**
76-
* @param Category[] $categories
77-
*/
78-
private function gone(array $categories): HtmlResponse
79-
{
80-
return new HtmlResponse(
81-
$this->template->render('error::410', [
82-
'categories' => $categories,
83-
]),
84-
StatusCodeInterface::STATUS_GONE
85-
);
86-
}
8761
}

0 commit comments

Comments
 (0)