Skip to content

Commit

Permalink
Continuing refactoring outdated "Model" code.
Browse files Browse the repository at this point in the history
  • Loading branch information
parpalak committed May 5, 2024
1 parent 7316968 commit c64dfe2
Show file tree
Hide file tree
Showing 18 changed files with 118 additions and 71 deletions.
14 changes: 10 additions & 4 deletions _extensions/s2_blog/Controller/BlogController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

use Lang;
use S2\Cms\Framework\ControllerInterface;
use S2\Cms\Model\ArticleProvider;
use S2\Cms\Model\UrlBuilder;
use S2\Cms\Pdo\DbLayer;
use S2\Cms\Template\HtmlTemplate;
use S2\Cms\Template\HtmlTemplateProvider;
Expand All @@ -29,14 +31,16 @@ abstract public function body(Request $request, HtmlTemplate $template): ?Respon

public function __construct(
protected DbLayer $dbLayer,
protected ArticleProvider $articleProvider,
protected UrlBuilder $urlBuilder,
protected HtmlTemplateProvider $templateProvider,
protected Viewer $viewer,
protected string $tagsUrl,
protected string $blogUrl, // S2_BLOG_URL
protected string $blogTitle, // S2_BLOG_TITLE
)
{
$this->blogPath = s2_link(str_replace(urlencode('/'), '/', urlencode($this->blogUrl)) . '/'); // S2_BLOG_PATH
$this->blogPath = $this->urlBuilder->link(str_replace(urlencode('/'), '/', urlencode($this->blogUrl)) . '/'); // S2_BLOG_PATH
$this->blogTagsPath = $this->blogPath . urlencode($this->tagsUrl) . '/'; // S2_BLOG_TAGS_PATH

Lang::load('s2_blog', function () {
Expand All @@ -54,9 +58,11 @@ public function handle(Request $request): Response
$template
->putInPlaceholder('commented', 0)
->putInPlaceholder('class', 's2_blog')
->putInPlaceholder('rss_link', ['<link rel="alternate" type="application/rss+xml" title="' .
s2_htmlencode(Lang::get('RSS link title', 's2_blog')) . '" href="' .
s2_link(str_replace(urlencode('/'), '/', urlencode($this->blogUrl)) . '/rss.xml') . '" />'])
->putInPlaceholder('rss_link', [sprintf(
'<link rel="alternate" type="application/rss+xml" title="%s" href="%s" />',
s2_htmlencode(Lang::get('RSS link title', 's2_blog')),
$this->urlBuilder->link(str_replace(urlencode('/'), '/', urlencode($this->blogUrl)) . '/rss.xml')
)])
;

if ($template->hasPlaceholder('<!-- s2_blog_navigation -->')) {
Expand Down
3 changes: 1 addition & 2 deletions _extensions/s2_blog/Controller/DayPageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;


class DayPageController extends BlogController
{
public function body(Request $request, HtmlTemplate $template): ?Response
Expand Down Expand Up @@ -50,7 +49,7 @@ public function body(Request $request, HtmlTemplate $template): ?Response
->putInPlaceholder('head_title', s2_date($startTime))
;

$template->addBreadCrumb(\S2\Cms\Model\Model::main_page_title(), s2_link('/'));
$template->addBreadCrumb($this->articleProvider->mainPageTitle(), $this->urlBuilder->link('/'));
if ($this->blogUrl !== '') {
$template->addBreadCrumb(Lang::get('Blog', 's2_blog'), $this->blogPath);
}
Expand Down
13 changes: 6 additions & 7 deletions _extensions/s2_blog/Controller/FavoritePageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,15 @@
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;


class FavoritePageController extends BlogController
{
public function body (Request $request, HtmlTemplate $template): ?Response
public function body(Request $request, HtmlTemplate $template): ?Response
{
if ($request->attributes->get('slash') !== '/') {
return new RedirectResponse(s2_link($request->getPathInfo() . '/'), Response::HTTP_MOVED_PERMANENTLY);
return new RedirectResponse($this->urlBuilder->link($request->getPathInfo() . '/'), Response::HTTP_MOVED_PERMANENTLY);
}

if ($template->hasPlaceholder('<!-- s2_blog_calendar -->')) {
if ($template->hasPlaceholder('<!-- s2_blog_calendar -->')) {
$template->registerPlaceholder('<!-- s2_blog_calendar -->', Lib::calendar(date('Y'), date('m'), '0'));
}

Expand All @@ -39,8 +38,8 @@ public function body (Request $request, HtmlTemplate $template): ?Response
$template->markAsNotFound();
}

// Bread crumbs
$template->addBreadCrumb(\S2\Cms\Model\Model::main_page_title(), s2_link('/'));
// Bread crumbs
$template->addBreadCrumb($this->articleProvider->mainPageTitle(), $this->urlBuilder->link('/'));
if ($this->blogUrl !== '') {
$template->addBreadCrumb(Lang::get('Blog', 's2_blog'), $this->blogPath);
}
Expand All @@ -55,5 +54,5 @@ public function body (Request $request, HtmlTemplate $template): ?Response
$template->setLink('up', $this->blogPath);

return null;
}
}
}
13 changes: 8 additions & 5 deletions _extensions/s2_blog/Controller/MainPageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
namespace s2_extensions\s2_blog\Controller;

use Lang;
use S2\Cms\Model\ArticleProvider;
use S2\Cms\Model\UrlBuilder;
use S2\Cms\Pdo\DbLayer;
use S2\Cms\Template\HtmlTemplate;
use S2\Cms\Template\HtmlTemplateProvider;
Expand All @@ -19,19 +21,20 @@
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;


class MainPageController extends BlogController
{
public function __construct(
DbLayer $dbLayer,
ArticleProvider $articleProvider,
UrlBuilder $urlBuilder,
HtmlTemplateProvider $templateProvider,
Viewer $viewer,
string $tagsUrl,
string $blogUrl,
string $blogTitle,
private readonly int $itemsPerPage,
) {
parent::__construct($dbLayer, $templateProvider, $viewer, $tagsUrl, $blogUrl, $blogTitle);
parent::__construct($dbLayer, $articleProvider, $urlBuilder, $templateProvider, $viewer, $tagsUrl, $blogUrl, $blogTitle);
}

public function handle(Request $request): Response
Expand All @@ -45,7 +48,7 @@ public function handle(Request $request): Response
public function body(Request $request, HtmlTemplate $template): ?Response
{
if ($request->attributes->get('slash') !== '/') {
return new RedirectResponse(s2_link($request->getPathInfo() . '/'), Response::HTTP_MOVED_PERMANENTLY);
return new RedirectResponse($this->urlBuilder->link($request->getPathInfo() . '/'), Response::HTTP_MOVED_PERMANENTLY);
}

$skipLastPostsNum = (int)$request->attributes->get('page', 0);
Expand Down Expand Up @@ -90,7 +93,7 @@ public function body(Request $request, HtmlTemplate $template): ?Response

$template->putInPlaceholder('text', $output);

$template->addBreadCrumb(\S2\Cms\Model\Model::main_page_title(), s2_link('/'));
$template->addBreadCrumb($this->articleProvider->mainPageTitle(), $this->urlBuilder->link('/'));
if ($this->blogUrl !== '') {
$template->addBreadCrumb(Lang::get('Blog', 's2_blog'), $skipLastPostsNum > 0 ? $this->blogPath : null);
}
Expand All @@ -100,7 +103,7 @@ public function body(Request $request, HtmlTemplate $template): ?Response
} else {
$template->putInPlaceholder('meta_description', $this->blogTitle);
if ($this->blogUrl !== '') {
$template->setLink('up', s2_link('/'));
$template->setLink('up', $this->urlBuilder->link('/'));
}
}

Expand Down
9 changes: 6 additions & 3 deletions _extensions/s2_blog/Controller/MonthPageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
namespace s2_extensions\s2_blog\Controller;

use Lang;
use S2\Cms\Model\ArticleProvider;
use S2\Cms\Model\UrlBuilder;
use S2\Cms\Pdo\DbLayer;
use S2\Cms\Template\HtmlTemplate;
use S2\Cms\Template\HtmlTemplateProvider;
Expand All @@ -18,19 +20,20 @@
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;


class MonthPageController extends BlogController
{
public function __construct(
DbLayer $dbLayer,
ArticleProvider $articleProvider,
UrlBuilder $urlBuilder,
HtmlTemplateProvider $templateProvider,
Viewer $viewer,
string $tagsUrl,
string $blogUrl,
string $blogTitle,
private readonly string $startYear,
) {
parent::__construct($dbLayer, $templateProvider, $viewer, $tagsUrl, $blogUrl, $blogTitle);
parent::__construct($dbLayer, $articleProvider, $urlBuilder, $templateProvider, $viewer, $tagsUrl, $blogUrl, $blogTitle);
}

public function body(Request $request, HtmlTemplate $template): ?Response
Expand Down Expand Up @@ -83,7 +86,7 @@ public function body(Request $request, HtmlTemplate $template): ?Response
->putInPlaceholder('head_title', \Lang::month($month) . ', ' . $year)
;

$template->addBreadCrumb(\S2\Cms\Model\Model::main_page_title(), s2_link('/'));
$template->addBreadCrumb($this->articleProvider->mainPageTitle(), $this->urlBuilder->link('/'));
if ($this->blogUrl !== '') {
$template->addBreadCrumb(Lang::get('Blog', 's2_blog'), $this->blogPath);
}
Expand Down
8 changes: 6 additions & 2 deletions _extensions/s2_blog/Controller/PostPageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
namespace s2_extensions\s2_blog\Controller;

use Lang;
use S2\Cms\Model\ArticleProvider;
use S2\Cms\Model\UrlBuilder;
use S2\Cms\Pdo\DbLayer;
use S2\Cms\Recommendation\RecommendationProvider;
use S2\Cms\Template\HtmlTemplate;
Expand All @@ -24,6 +26,8 @@ class PostPageController extends BlogController
{
public function __construct(
DbLayer $dbLayer,
ArticleProvider $articleProvider,
UrlBuilder $urlBuilder,
private readonly RecommendationProvider $recommendationProvider,
HtmlTemplateProvider $templateProvider,
Viewer $viewer,
Expand All @@ -32,7 +36,7 @@ public function __construct(
string $blogTitle,
protected bool $showComments,
) {
parent::__construct($dbLayer, $templateProvider, $viewer, $tagsUrl, $blogUrl, $blogTitle);
parent::__construct($dbLayer, $articleProvider, $urlBuilder, $templateProvider, $viewer, $tagsUrl, $blogUrl, $blogTitle);
}

public function body(Request $request, HtmlTemplate $template): ?Response
Expand All @@ -49,7 +53,7 @@ public function body(Request $request, HtmlTemplate $template): ?Response
return $result;
}

$template->addBreadCrumb(\S2\Cms\Model\Model::main_page_title(), s2_link('/'));
$template->addBreadCrumb($this->articleProvider->mainPageTitle(), $this->urlBuilder->link('/'));
if ($this->blogUrl !== '') {
$template->addBreadCrumb(Lang::get('Blog', 's2_blog'), $this->blogPath);
}
Expand Down
22 changes: 11 additions & 11 deletions _extensions/s2_blog/Controller/TagPageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@
class TagPageController extends BlogController
{
public function __construct(
DbLayer $dbLayer,
private readonly ArticleProvider $articleProvider,
private readonly UrlBuilder $urlBuilder,
HtmlTemplateProvider $templateProvider,
Viewer $viewer,
string $tagsUrl,
string $blogUrl,
string $blogTitle,
private readonly bool $useHierarchy
DbLayer $dbLayer,
ArticleProvider $articleProvider,
UrlBuilder $urlBuilder,
HtmlTemplateProvider $templateProvider,
Viewer $viewer,
string $tagsUrl,
string $blogUrl,
string $blogTitle,
private readonly bool $useHierarchy
) {
parent::__construct($dbLayer, $templateProvider, $viewer, $tagsUrl, $blogUrl, $blogTitle);
parent::__construct($dbLayer, $articleProvider, $urlBuilder, $templateProvider, $viewer, $tagsUrl, $blogUrl, $blogTitle);
}

public function body(Request $request, HtmlTemplate $template): ?Response
Expand Down Expand Up @@ -89,7 +89,7 @@ public function body(Request $request, HtmlTemplate $template): ?Response
throw new NotFoundException();
}

$template->addBreadCrumb(\S2\Cms\Model\Model::main_page_title(), $this->urlBuilder->link('/'));
$template->addBreadCrumb($this->articleProvider->mainPageTitle(), $this->urlBuilder->link('/'));
if ($this->blogUrl !== '') {
$template->addBreadCrumb(Lang::get('Blog', 's2_blog'), $this->blogPath);
}
Expand Down
5 changes: 2 additions & 3 deletions _extensions/s2_blog/Controller/TagsPageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,12 @@
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;


class TagsPageController extends BlogController
{
public function body (Request $request, HtmlTemplate $template): ?Response
{
if ($request->attributes->get('slash') !== '/') {
return new RedirectResponse(s2_link($request->getPathInfo() . '/'), Response::HTTP_MOVED_PERMANENTLY);
return new RedirectResponse($this->urlBuilder->link($request->getPathInfo() . '/'), Response::HTTP_MOVED_PERMANENTLY);
}

$template->registerPlaceholder('<!-- s2_blog_navigation -->', '');
Expand Down Expand Up @@ -76,7 +75,7 @@ public function body (Request $request, HtmlTemplate $template): ?Response

$template->putInPlaceholder('text', $this->viewer->render('tags_list', ['tags' => $tags]));

$template->addBreadCrumb(\S2\Cms\Model\Model::main_page_title(), s2_link('/'));
$template->addBreadCrumb($this->articleProvider->mainPageTitle(), $this->urlBuilder->link('/'));
if ($this->blogUrl !== '') {
$template->addBreadCrumb(Lang::get('Blog', 's2_blog'), $this->blogPath);
}
Expand Down
8 changes: 6 additions & 2 deletions _extensions/s2_blog/Controller/YearPageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
namespace s2_extensions\s2_blog\Controller;

use Lang;
use S2\Cms\Model\ArticleProvider;
use S2\Cms\Model\UrlBuilder;
use S2\Cms\Pdo\DbLayer;
use S2\Cms\Template\HtmlTemplate;
use S2\Cms\Template\HtmlTemplateProvider;
Expand All @@ -23,14 +25,16 @@ class YearPageController extends BlogController
{
public function __construct(
DbLayer $dbLayer,
ArticleProvider $articleProvider,
UrlBuilder $urlBuilder,
HtmlTemplateProvider $templateProvider,
Viewer $viewer,
string $tagsUrl,
string $blogUrl,
string $blogTitle,
private readonly string $startYear,
) {
parent::__construct($dbLayer, $templateProvider, $viewer, $tagsUrl, $blogUrl, $blogTitle);
parent::__construct($dbLayer, $articleProvider, $urlBuilder, $templateProvider, $viewer, $tagsUrl, $blogUrl, $blogTitle);
}

public function body(Request $request, HtmlTemplate $template): ?Response
Expand Down Expand Up @@ -82,7 +86,7 @@ public function body(Request $request, HtmlTemplate $template): ?Response
'content' => $content
], 's2_blog'));

$template->addBreadCrumb(\S2\Cms\Model\Model::main_page_title(), s2_link('/'));
$template->addBreadCrumb($this->articleProvider->mainPageTitle(), $this->urlBuilder->link('/'));
if ($this->blogUrl !== '') {
$template->addBreadCrumb(Lang::get('Blog', 's2_blog'), $this->blogPath);
}
Expand Down
14 changes: 14 additions & 0 deletions _extensions/s2_blog/Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ public function buildContainer(Container $container): void
$provider = $container->get(DynamicConfigProvider::class);
return new MainPageController(
$container->get(DbLayer::class),
$container->get(ArticleProvider::class),
$container->get(UrlBuilder::class),
$container->get(HtmlTemplateProvider::class),
$container->get(Viewer::class),
$provider->get('S2_TAGS_URL'),
Expand All @@ -57,6 +59,8 @@ public function buildContainer(Container $container): void
$provider = $container->get(DynamicConfigProvider::class);
return new DayPageController(
$container->get(DbLayer::class),
$container->get(ArticleProvider::class),
$container->get(UrlBuilder::class),
$container->get(HtmlTemplateProvider::class),
$container->get(Viewer::class),
$provider->get('S2_TAGS_URL'),
Expand All @@ -69,6 +73,8 @@ public function buildContainer(Container $container): void
$provider = $container->get(DynamicConfigProvider::class);
return new MonthPageController(
$container->get(DbLayer::class),
$container->get(ArticleProvider::class),
$container->get(UrlBuilder::class),
$container->get(HtmlTemplateProvider::class),
$container->get(Viewer::class),
$provider->get('S2_TAGS_URL'),
Expand All @@ -82,6 +88,8 @@ public function buildContainer(Container $container): void
$provider = $container->get(DynamicConfigProvider::class);
return new YearPageController(
$container->get(DbLayer::class),
$container->get(ArticleProvider::class),
$container->get(UrlBuilder::class),
$container->get(HtmlTemplateProvider::class),
$container->get(Viewer::class),
$provider->get('S2_TAGS_URL'),
Expand All @@ -95,6 +103,8 @@ public function buildContainer(Container $container): void
$provider = $container->get(DynamicConfigProvider::class);
return new PostPageController(
$container->get(DbLayer::class),
$container->get(ArticleProvider::class),
$container->get(UrlBuilder::class),
$container->get(RecommendationProvider::class),
$container->get(HtmlTemplateProvider::class),
$container->get(Viewer::class),
Expand All @@ -109,6 +119,8 @@ public function buildContainer(Container $container): void
$provider = $container->get(DynamicConfigProvider::class);
return new TagsPageController(
$container->get(DbLayer::class),
$container->get(ArticleProvider::class),
$container->get(UrlBuilder::class),
$container->get(HtmlTemplateProvider::class),
$container->get(Viewer::class),
$provider->get('S2_TAGS_URL'),
Expand Down Expand Up @@ -136,6 +148,8 @@ public function buildContainer(Container $container): void
$provider = $container->get(DynamicConfigProvider::class);
return new FavoritePageController(
$container->get(DbLayer::class),
$container->get(ArticleProvider::class),
$container->get(UrlBuilder::class),
$container->get(HtmlTemplateProvider::class),
$container->get(Viewer::class),
$provider->get('S2_TAGS_URL'),
Expand Down
Loading

0 comments on commit c64dfe2

Please sign in to comment.