Skip to content

Commit 5521cac

Browse files
committed
Use GetPageViewHandler for static pages. Update routes and unit tests
1 parent 5375aea commit 5521cac

11 files changed

Lines changed: 38 additions & 105 deletions

File tree

src/App/templates/layout/default.html.twig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
<a class="nav-link px-3 fw-semibold fs-5 transition-all" target="_blank" href="https://docs.dotkernel.org/">Documentation</a>
3535
</li>
3636
<li class="nav-item">
37-
<a class="nav-link px-3 fw-semibold fs-5 transition-all" href="{{ url('page::static-page', {'static-page': 'dotkernel-packages-oss-lifecycle'}) }}">Packages Lifecycle</a>
37+
<a class="nav-link px-3 fw-semibold fs-5 transition-all" href="{{ url('page::dotkernel-packages-oss-lifecycle') }}">Packages Lifecycle</a>
3838
</li>
3939
<li class="nav-item">
4040
<a class="nav-link px-3 fw-semibold fs-5 transition-all" href="{{ url('page::category-resource', {slug: 'how-to'}) }}">How to's</a>
@@ -47,7 +47,7 @@
4747
</li>
4848
</ul>
4949
<div class="d-none d-lg-block">
50-
<a href="{{ url('page::static-page', {'static-page': 'contact'}) }}" class="btn btn-primary rounded-pill px-4">Contact</a>
50+
<a href="{{ url('page::contact') }}" class="btn btn-primary rounded-pill px-4">Contact</a>
5151
</div>
5252
</div>
5353
</div>

src/App/templates/partial/left-menu.html.twig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
</div>
1212
</div>
1313
</div>
14-
{% if data is defined %}
14+
{% if data is defined and data is iterable %}
1515
<div class="card border-0 shadow-sm rounded-3 my-2">
1616
<div class="card-body p-3">
1717
<h6 class="fw-bold text-uppercase text-muted small px-1 mb-2">Recent Articles</h6>
@@ -54,7 +54,7 @@
5454
</div>
5555
</div>
5656
</div>
57-
{% if posts is defined and posts %}
57+
{% if posts is defined and posts is iterable %}
5858
<div class="card border-0 shadow-sm rounded-3 my-2">
5959
<div class="card-body p-3">
6060
<h6 class="fw-bold text-uppercase text-muted small px-1 mb-2">Recent Articles</h6>

src/Blog/src/ConfigProvider.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ private function getDependencies(): array
6666
GetCategoryResourceHandler::class => CategoryResourceHandlerFactory::class,
6767
AuthorRepository::class => AuthorResourceRepositoryFactory::class,
6868
GetAuthorResourceHandler::class => AuthorResourceHandlerFactory::class,
69-
GetStaticPageDataHandler::class => GetStaticPageDataFactory::class,
7069
],
7170
];
7271
}

src/Blog/src/Factory/StaticPages/GetStaticPageDataFactory.php

Lines changed: 0 additions & 34 deletions
This file was deleted.

src/Blog/src/Handler/GetCategoryResourceHandler.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Light\Blog\Handler;
66

7+
use Fig\Http\Message\StatusCodeInterface;
78
use Laminas\Diactoros\Response\HtmlResponse;
89
use Light\Blog\Repository\CategoryRepository;
910
use Mezzio\Template\TemplateRendererInterface;
@@ -26,7 +27,7 @@ public function handle(ServerRequestInterface $request): ResponseInterface
2627
$meta = $category;
2728

2829
if (! $category) {
29-
return new HtmlResponse('Category not found', 404);
30+
return new HtmlResponse('Category not found', StatusCodeInterface::STATUS_NOT_FOUND);
3031
}
3132
$categories = $this->categoryRepository->getCategories();
3233
$categoryArticles = $this->categoryRepository->getCategoryPost($category);

src/Blog/src/Handler/GetPostResourceHandler.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Light\Blog\Handler;
66

7+
use Fig\Http\Message\StatusCodeInterface;
78
use Laminas\Diactoros\Response\HtmlResponse;
89
use Light\Blog\Repository\CategoryRepository;
910
use Light\Blog\Repository\PostRepository;
@@ -58,7 +59,7 @@ private function notFound(?string $categorySlug): HtmlResponse
5859
$this->template->render('error::404', [
5960
'categories' => $categories,
6061
]),
61-
404
62+
StatusCodeInterface::STATUS_NOT_FOUND
6263
);
6364
}
6465
}

src/Blog/src/Handler/GetStaticPageDataHandler.php

Lines changed: 0 additions & 55 deletions
This file was deleted.

src/Blog/src/RoutesDelegator.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
use Light\Blog\Handler\GetCategoryResourceHandler;
1010
use Light\Blog\Handler\GetPostCollectionHandler;
1111
use Light\Blog\Handler\GetPostResourceHandler;
12-
use Light\Blog\Handler\GetStaticPageDataHandler;
1312
use Mezzio\Application;
1413
use Psr\Container\ContainerInterface;
1514

@@ -26,8 +25,6 @@ public function __invoke(ContainerInterface $container, string $serviceName, cal
2625
$app->get('/categories/', [GetCategoryCollectionHandler::class], 'page::categories');
2726
$app->get('/author/{slug}/', [GetAuthorResourceHandler::class], 'page::author-resource');
2827
$app->get('/{categorySlug}/{slug}/', [GetPostResourceHandler::class], 'page::blog-resource');
29-
30-
$app->get('/{static-page}/', [GetStaticPageDataHandler::class], 'page::static-page');
3128
return $app;
3229
}
3330
}

src/Page/src/Factory/GetPageViewHandlerFactory.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace Light\Page\Factory;
66

7+
use Light\Blog\Repository\CategoryRepository;
8+
use Light\Blog\Repository\PostRepository;
79
use Light\Page\Handler\GetPageViewHandler;
810
use Mezzio\Template\TemplateRendererInterface;
911
use Psr\Container\ContainerExceptionInterface;
@@ -21,9 +23,11 @@ class GetPageViewHandlerFactory
2123
*/
2224
public function __invoke(ContainerInterface $container, string $requestedName): GetPageViewHandler
2325
{
24-
$template = $container->get(TemplateRendererInterface::class);
26+
$postRepository = $container->get(PostRepository::class);
27+
$categoryRepository = $container->get(CategoryRepository::class);
28+
$template = $container->get(TemplateRendererInterface::class);
2529
assert($template instanceof TemplateRendererInterface);
2630

27-
return new GetPageViewHandler($template);
31+
return new GetPageViewHandler($template, $categoryRepository, $postRepository);
2832
}
2933
}

src/Page/src/Handler/GetPageViewHandler.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
namespace Light\Page\Handler;
66

77
use Laminas\Diactoros\Response\HtmlResponse;
8+
use Light\Blog\Repository\CategoryRepository;
9+
use Light\Blog\Repository\PostRepository;
810
use Mezzio\Router\RouteResult;
911
use Mezzio\Template\TemplateRendererInterface;
1012
use Psr\Http\Message\ResponseInterface;
@@ -15,15 +17,21 @@ class GetPageViewHandler implements RequestHandlerInterface
1517
{
1618
public function __construct(
1719
protected TemplateRendererInterface $template,
20+
protected CategoryRepository $categoryRepository,
21+
protected PostRepository $postRepository,
1822
) {
1923
}
2024

2125
public function handle(ServerRequestInterface $request): ResponseInterface
2226
{
23-
$template = $request->getAttribute(RouteResult::class)->getMatchedRouteName();
24-
27+
$template = $request->getAttribute(RouteResult::class)->getMatchedRouteName();
28+
$posts = $this->postRepository->getRecentPosts(3);
29+
$categories = $this->categoryRepository->getCategories();
2530
return new HtmlResponse(
26-
$this->template->render($template)
31+
$this->template->render($template, [
32+
'posts' => $posts,
33+
'categories' => $categories,
34+
])
2735
);
2836
}
2937
}

0 commit comments

Comments
 (0)