Completed
Push — master ( 2e7054...27b789 )
by Mikael
08:58 queued 23s
created

src/Controller/FlatFileContentController.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Anax\Controller;
4
5
use Anax\Commons\ContainerInjectableInterface;
6
use Anax\Commons\ContainerInjectableTrait;
7
8
/**
9
 * A controller for flat file markdown content.
10
 */
11
class FlatFileContentController implements ContainerInjectableInterface
12
{
13
    use ContainerInjectableTrait;
14
15
16
17
    /**
18
     * Render a page using flat file content.
19
     *
20
     * @param array $args as a variadic to catch all arguments.
21
     *
22
     * @return mixed as null when flat file is not found and otherwise a
23
     *               complete response object with content to render.
24
     *
25
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
26
     */
27
    public function catchAll(...$args)
0 ignored issues
show
The parameter $args is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
28
    {
29
        // Get the current route and see if it matches a content/file
30
        $path = $this->di->get("request")->getRoute();
31
        $file1 = ANAX_INSTALL_PATH . "/content/{$path}.md";
32
        $file2 = ANAX_INSTALL_PATH . "/content/{$path}/index.md";
33
34
        $file = is_file($file1) ? $file1 : null;
35
        $file = is_file($file2) ? $file2 : $file;
36
        
37
        if (!$file) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $file of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
38
            return;
39
        }
40
41
        // Check that file is really in the right place
42
        $real = realpath($file);
43
        $base = realpath(ANAX_INSTALL_PATH . "/content/");
44
        if (strncmp($base, $real, strlen($base))) {
45
            return;
46
        }
47
48
        // Get content from markdown file
49
        $content = file_get_contents($file);
50
        $content = $this->di->get("textfilter")->parse(
51
            $content,
52
            ["frontmatter", "variable", "shortcode", "markdown", "titlefromheader"]
53
        );
54
55
        // Add content as a view and then render the page
56
        $page = $this->di->get("page");
57
        $page->add("anax/v2/article/default", [
58
            "content" => $content->text,
59
            "frontmatter" => $content->frontmatter,
60
        ]);
61
62
        return $page->render($content->frontmatter);
63
    }
64
}
65