-
Notifications
You must be signed in to change notification settings - Fork 0
/
FlatFileContentController.php
64 lines (52 loc) · 1.86 KB
/
FlatFileContentController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
namespace Anax\Controller;
use Anax\Commons\ContainerInjectableInterface;
use Anax\Commons\ContainerInjectableTrait;
/**
* A controller for flat file markdown content.
*/
class FlatFileContentController implements ContainerInjectableInterface
{
use ContainerInjectableTrait;
/**
* Render a page using flat file content.
*
* @param array $args as a variadic to catch all arguments.
*
* @return mixed as null when flat file is not found and otherwise a
* complete response object with content to render.
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function catchAll(...$args)
{
// Get the current route and see if it matches a content/file
$path = $this->di->get("request")->getRoute();
$file1 = ANAX_INSTALL_PATH . "/content/{$path}.md";
$file2 = ANAX_INSTALL_PATH . "/content/{$path}/index.md";
$file = is_file($file1) ? $file1 : null;
$file = is_file($file2) ? $file2 : $file;
if (!$file) {
return;
}
// Check that file is really in the right place
$real = realpath($file);
$base = realpath(ANAX_INSTALL_PATH . "/content/");
if (strncmp($base, $real, strlen($base))) {
return;
}
// Get content from markdown file
$content = file_get_contents($file);
$content = $this->di->get("textfilter")->parse(
$content,
["frontmatter", "variable", "shortcode", "markdown", "titlefromheader"]
);
// Add content as a view and then render the page
$page = $this->di->get("page");
$page->add("anax/v2/article/default", [
"content" => $content->text,
"frontmatter" => $content->frontmatter,
]);
return $page->render($content->frontmatter);
}
}