Completed
Push — master ( 7382e1...c98941 )
by Mikael
03:21
created

CFileBasedContent::useCache()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
ccs 0
cts 3
cp 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Anax\Content;
4
5
/**
6
 * Pages based on file content.
7
 */
8
class CFileBasedContent
9
{
10
    use \Anax\TConfigure,
11
        \Anax\DI\TInjectionAware;
12
13
14
15
    /**
16
     * Properties.
17
     */
18
    private $index = null;
19
    private $ignoreCache = false;
20
21
22
23
    /**
24
     * Should the cache be used or ignored.
25
     *
26
     * @param boolean $use true to use the cache or false to ignore the cache.
27
     *
28
     * @return this.
0 ignored issues
show
Documentation introduced by
The doc-type this. could not be parsed: Unknown type name "this." at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
29
     */
30
    public function useCache($use)
31
    {
32
        $this->ignoreCache = !$use;
33
34
        return $this;
35
    }
36
37
38
39
    /**
40
     * Get the index as an array.
41
     *
42
     * @return array as index.
43
     */
44
    public function getIndex()
45
    {
46
        return $this->loadIndex();
47
    }
48
49
50
51
    /**
52
     * Create the index of all content into an array.
53
     *
54
     * @return array as index.
55
     */
56 View Code Duplication
    private function loadIndex()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
57
    {
58
        if ($this->index) {
59
            return $this->index;
60
        }
61
62
        $key = $this->di->cache->createKey(__CLASS__, "index");
63
        $this->index = $this->di->cache->get($key);
64
65
        if (!$this->index || $this->ignoreCache) {
66
            $this->index = $this->createIndex();
67
            $this->di->cache->put($key, $this->index);
68
        }
69
70
        return $this->index;
71
    }
72
73
74
75
    /**
76
     * Generate an index from the directory structure.
77
     *
78
     * @return array as table of content.
79
     */
80
    private function createIndex()
81
    {
82
        $basepath   = $this->config["basepath"];
83
        $pattern    = $this->config["pattern"];
84
        $path       = "$basepath/$pattern";
85
        
86
        $index = [];
87
        foreach (glob_recursive($path) as $file) {
88
            $filepath = substr($file, strlen($basepath) + 1);
89
90
            $matches = [];
91
            preg_match("#^(\d*)_*([^\.]+)\.md$#", basename($filepath), $matches);
92
            $dirpart = dirname($filepath) . "/";
93
            if ($dirpart === "./") {
94
                $dirpart = null;
95
            }
96
            $key = $dirpart . $matches[2];
97
98
            $index[$key] = [
99
                "file"    => $filepath,
100
                "section" => $matches[1]
101
            ];
102
        }
103
104
        return $index;
105
    }
106
107
108
109
    /**
110
     * Map the route to the correct entry in the index.
111
     *
112
     * @param string $route current route used to access page.
113
     *
114
     * @return string as the title for the content.
115
     */
116
    private function mapRoute2Index($route)
117
    {
118
        if (key_exists($route, $this->index)) {
119
            return $this->index[$route];
120
        } elseif (empty($route) && key_exists("index", $this->index)) {
121
            return $this->index["index"];
122
        } elseif (key_exists($route . "/index", $this->index)) {
123
            return $this->index["$route/index"];
124
        }
125
126
        throw new \Anax\Exception\NotFoundException(t("The content does not exists in the index."));
127
    }
128
129
130
131
    /**
132
     * Map url to content if such mapping can be done.
133
     *
134
     * @throws NotFoundException when mapping can not be done.
135
     */
136
    public function contentForRoute()
137
    {
138
        // Settings from config
139
        $basepath = $this->config["basepath"];
140
        $filter   = $this->config["textfilter"];
141
142
        // Get the route
143
        $route = $this->di->request->getRoute();
144
145
        // Load index and map route to entry
146
        $this->loadIndex();
147
        $content = $this->mapRoute2Index($route);
148
149
        // Whole path to file
150
        $path = $basepath . "/" . $content["file"];
151
        $content["path"] = $path;
152
153
        // Load content from file
154
        if (!is_file($path)) {
155
            throw new \Anax\Exception\NotFoundException(t("The content does not exists as a file."));
156
        }
157
        $src = file_get_contents($path);
158
        $content["src"] = $src;
159
160
        // Get filtered content
161
        $filtered = $this->di->textFilter->parse($src, $filter);
162
        //$frontmatter = $this->di->textFilter->yamlFrontMatter($src);
163
164
        $content["frontmatter"] = $filtered->frontmatter;
165
        $content["text"]        = $filtered->text;
166
167
        return (object) $content;
168
    }
169
}
170