Skip to content

Commit ec8c673

Browse files
committed
Initial version with basic tests
1 parent 31db1fc commit ec8c673

File tree

17 files changed

+1590
-451
lines changed

17 files changed

+1590
-451
lines changed

Command/Help/DefaultController.php

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

Command/Web/ContentController.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
namespace librarianphp\Web;
4+
5+
use Librarian\Provider\TwigServiceProvider;
6+
use Librarian\Response;
7+
use Librarian\Provider\ContentServiceProvider;
8+
use Librarian\WebController;
9+
10+
/**
11+
* Class StaticController
12+
* Renders content from the data dirs
13+
* @package App\Command\Web
14+
*/
15+
class ContentController extends WebController
16+
{
17+
public function handle(): int
18+
{
19+
/** @var TwigServiceProvider $twig */
20+
$twig = $this->getApp()->twig;
21+
22+
/** @var ContentServiceProvider $content_provider */
23+
$content_provider = $this->getApp()->content;
24+
25+
$request = $this->getRequest();
26+
27+
try {
28+
$content = $content_provider->fetch($request->getRoute() . '/' . $request->getSlug());
29+
30+
if ($content === null) {
31+
$page = 1;
32+
$limit = $this->getApp()->config->posts_per_page ?? 10;
33+
$params = $this->getRequest()->getParams();
34+
35+
if (key_exists('page', $params)) {
36+
$page = $params['page'];
37+
}
38+
39+
$start = ($page * $limit) - $limit;
40+
41+
$content_list = $content_provider->fetchFrom($request->getRoute(), $start, $limit);
42+
$response = new Response($twig->render('content/listing.html.twig', [
43+
'content_list' => $content_list,
44+
'total_pages' => $content_provider->fetchTotalPages($limit),
45+
'current_page' => $page,
46+
'base_url' => $request->getRoute()
47+
]));
48+
49+
$response->output();
50+
return 0;
51+
}
52+
} catch (\Exception $e) {
53+
Response::redirect('/notfound');
54+
}
55+
56+
$output = $twig->render('content/single.html.twig', [
57+
'content' => $content
58+
]);
59+
60+
61+
$response = new Response($output);
62+
$response->output();
63+
return 0;
64+
}
65+
}

Command/Web/ErrorController.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace librarianphp\Web;
4+
5+
use Librarian\Response;
6+
use Librarian\WebController;
7+
use Twig\Environment;
8+
9+
class ErrorController extends WebController
10+
{
11+
public function handle()
12+
{
13+
/** @var Environment $twig */
14+
$twig = $this->getApp()->twig;
15+
$output = $twig->render('error/5xx.html.twig', []);
16+
17+
$response = new Response($output);
18+
19+
$response->output();
20+
}
21+
}

Command/Web/FeedController.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
namespace librarianphp\Web;
4+
5+
use Librarian\Content;
6+
use Librarian\WebController;
7+
use Librarian\Provider\ContentServiceProvider;
8+
use Suin\RSSWriter\Channel;
9+
use Suin\RSSWriter\Feed;
10+
use Suin\RSSWriter\Item;
11+
12+
class FeedController extends WebController
13+
{
14+
public function handle()
15+
{
16+
$feed = new Feed();
17+
$channel = new Channel();
18+
$channel
19+
->title($this->getApp()->config->site_name)
20+
->description($this->getApp()->config->site_description)
21+
->url($this->getApp()->config->site_url)
22+
->feedUrl($this->getApp()->config->site_url . '/feed')
23+
->language('en-US')
24+
->copyright('Copyright ' . date('Y') . ', '. $this->getApp()->config->site_name)
25+
->pubDate(strtotime(date('Y-m-d H:i:s')))
26+
->lastBuildDate(strtotime(date('Y-m-d H:i:s')))
27+
->ttl(60)
28+
->appendTo($feed);
29+
30+
/** @var ContentServiceProvider $content_provider */
31+
$content_provider = $this->getApp()->content;
32+
$content_list = $content_provider->fetchAll();
33+
34+
/** @var Content $content */
35+
foreach ($content_list as $content) {
36+
$item = new Item();
37+
$item
38+
->title($content->title ?? '')
39+
->description('<div>'.($content->description ?? '').'</div>')
40+
->contentEncoded('<div>'.($content->description ?? '').'</div>')
41+
->url($this->getApp()->config->site_url . '/' . $content->getLink())
42+
->author($this->getApp()->config->site_author)
43+
->pubDate(strtotime($content->getDate()))
44+
->guid($this->getApp()->config->site_url . '/' . $content->getLink(), true)
45+
->preferCdata(true) // By this, title and description become CDATA wrapped HTML.
46+
->appendTo($channel);
47+
}
48+
49+
header('Content-type: application/rss+xml');
50+
echo $feed;
51+
}
52+
}

Command/Web/IndexController.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
namespace librarianphp\Web;
4+
5+
use Librarian\Provider\TwigServiceProvider;
6+
use Librarian\Request;
7+
use Librarian\WebController;
8+
use Librarian\Response;
9+
use Librarian\Provider\ContentServiceProvider;
10+
11+
class IndexController extends WebController
12+
{
13+
public function handle()
14+
{
15+
/** @var TwigServiceProvider $twig */
16+
$twig = $this->getApp()->twig;
17+
/** @var ContentServiceProvider $content_provider */
18+
$content_provider = $this->getApp()->content;
19+
$request = $this->getRequest();
20+
21+
if ($this->getApp()->config->site_index !== null) {
22+
$content = $content_provider->fetch($this->getApp()->config->site_index);
23+
if ($content) {
24+
$response = new Response($twig->render('content/single.html.twig', [
25+
'content' => $content
26+
]));
27+
28+
$response->output();
29+
return 0;
30+
}
31+
}
32+
33+
$page = 1;
34+
$limit = $this->getApp()->config->posts_per_page ?? 10;
35+
$params = $request->getParams();
36+
37+
if (key_exists('page', $params)) {
38+
$page = $params['page'];
39+
}
40+
41+
$start = ($page * $limit) - $limit;
42+
43+
$content_list = $content_provider->fetchAll($start, $limit);
44+
45+
$output = $twig->render('content/listing.html.twig', [
46+
'content_list' => $content_list,
47+
'total_pages' => $content_provider->fetchTotalPages($limit),
48+
'current_page' => $page
49+
]);
50+
51+
$response = new Response($output);
52+
53+
$response->output();
54+
return 0;
55+
}
56+
}

Command/Web/NotFoundController.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace librarianphp\Web;
4+
5+
use Librarian\Response;
6+
use Librarian\WebController;
7+
use Twig\Environment;
8+
9+
class NotFoundController extends WebController
10+
{
11+
public function handle()
12+
{
13+
/** @var Environment $twig */
14+
$twig = $this->getApp()->twig;
15+
$output = $twig->render('error/404.html.twig', []);
16+
17+
$response = new Response($output);
18+
19+
$response->output();
20+
}
21+
}

Command/Web/TagController.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
namespace librarianphp\Web;
4+
5+
use Librarian\Provider\TwigServiceProvider;
6+
use Librarian\Response;
7+
use Librarian\Provider\ContentServiceProvider;
8+
use Librarian\WebController;
9+
10+
class TagController extends WebController
11+
{
12+
/**
13+
* @throws \Exception
14+
*/
15+
public function handle()
16+
{
17+
/** @var TwigServiceProvider $twig */
18+
$twig = $this->getApp()->twig;
19+
20+
/** @var ContentServiceProvider $content_provider */
21+
$content_provider = $this->getApp()->content;
22+
23+
$request = $this->getRequest();
24+
25+
if (!$request->getSlug()) {
26+
Response::redirect('/notfound');
27+
}
28+
29+
$page = 1;
30+
$limit = $this->getApp()->config->posts_per_page ?: 10;
31+
$params = $this->getRequest()->getParams();
32+
33+
if (key_exists('page', $params)) {
34+
$page = $params['page'];
35+
}
36+
37+
$start = ($page * $limit) - $limit;
38+
39+
try {
40+
$content_list = $content_provider->fetchFromTag($request->getSlug(), $start, $limit);
41+
} catch (\Exception $e) {
42+
Response::redirect('/notfound');
43+
}
44+
45+
if (!$content_list) {
46+
Response::redirect('/notfound');
47+
}
48+
49+
$output = $twig->render('content/listing.html.twig', [
50+
'content_list' => $content_list,
51+
'tag_name' => $request->getSlug(),
52+
'total_pages' => $content_provider->fetchTagTotalPages($request->getSlug(), $limit),
53+
'current_page' => $page
54+
]);
55+
56+
$response = new Response($output);
57+
$response->output();
58+
}
59+
}

README.md

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
# command-help
1+
# command-web
22

3-
Librarian's built-in help command.
3+
Librarian's web controllers are implemented as commands under the "Web" namespace.
44

5-
```shell
6-
./librarian help
7-
```

composer.json

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
{
2-
"name": "librarianphp/command-help",
2+
"name": "librarianphp/command-web",
33
"type": "library",
4-
"description": "Librarian's built-in help command",
4+
"description": "Librarian's web controllers",
55
"license": "MIT",
6-
"homepage": "https://github.com/librarianphp/command-demo",
6+
"homepage": "https://github.com/librarianphp/command-web",
77
"keywords": ["cli","command-line", "markdown"],
88
"autoload": {
99
"psr-4": {
1010
"librarianphp\\": "Command/"
1111
}
1212
},
1313
"require": {
14-
"minicli/minicli": "^3.2"
14+
"minicli/minicli": "^3.2",
15+
"librarianphp/librarian-core": "^3.0"
1516
},
1617
"require-dev": {
1718
"pestphp/pest": "^2.4",
18-
"friendsofphp/php-cs-fixer": "^3.16"
19+
"friendsofphp/php-cs-fixer": "^3.16",
20+
"mockery/mockery": "^1.5"
1921
},
2022
"scripts": {
2123
"test" : ["pest"],

0 commit comments

Comments
 (0)