Page::__construct()   A
last analyzed

Complexity

Conditions 4
Paths 2

Size

Total Lines 38
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 4

Importance

Changes 6
Bugs 0 Features 0
Metric Value
cc 4
eloc 16
c 6
b 0
f 0
nc 2
nop 2
dl 0
loc 38
ccs 14
cts 14
cp 1
crap 4
rs 9.7333
1
<?php
2
/**
3
 * The page model
4
 */
5
namespace Phile\Model;
6
7
use Phile\Core\Container;
8
use Phile\Core\Router;
9
use Phile\Core\ServiceLocator;
10
use Phile\Repository\Page as Repository;
11
12
/**
13
 * the Model class for a page
14
 *
15
 * @author  Frank Nägler
16
 * @link    https://philecms.github.io
17
 * @license http://opensource.org/licenses/MIT
18
 * @package Phile\Model
19
 */
20
class Page
21
{
22
    /**
23
     * @var \Phile\Model\Meta the meta model
24
     */
25
    protected $meta;
26
27
    /**
28
     * @var string the content
29
     */
30
    protected $content;
31
32
    /**
33
     * @var string the path to the original file
34
     */
35
    protected $filePath;
36
37
    /**
38
     * @var null|string the raw file
39
     */
40
    protected $rawData;
41
42
    /**
43
     * @var \Phile\ServiceLocator\ParserInterface the parser
44
     */
45
    protected $parser;
46
47
    /**
48
     * @var string the pageId of the page
49
     */
50
    protected $pageId;
51
52
    /**
53
     * @var string The content folder, as passed to the class constructor when initiating the object.
54
     */
55
    protected $contentFolder;
56
57
    /**
58
     * @var string content extension
59
     */
60
    protected $contentExtension;
61
62
    /** @var Repository */
63
    protected $repository;
64
65
    /**
66
     * the constructor
67
     *
68
     * @param string $filePath
69
     * @param string $folder
70
     */
71 22
    public function __construct($filePath, $folder = null)
72
    {
73 22
        $settings = Container::getInstance()->get('Phile_Config')->toArray();
74 22
        $this->contentFolder = $folder ?: $settings['content_dir'];
75 22
        $this->contentExtension = $settings['content_ext'];
76 22
        $this->setFilePath($filePath);
77
78
        /**
79
         * @triggerEvent before_load_content this event is triggered before the content is loaded
80
         *
81
         * @param            string filePath the path to the file
82
         * @param \Phile\Model\Page page     the page model
83
         */
84 22
        Container::getInstance()->get('Phile_EventBus')->trigger(
85
            'before_load_content',
86 22
            ['filePath' => &$this->filePath, 'page' => &$this]
87
        );
88 22
        if (file_exists($this->filePath)) {
89 22
            $this->rawData = file_get_contents($this->filePath) ?: null;
90 22
            $this->parseRawData();
91
        }
92
        /**
93
         * @triggerEvent after_load_content this event is triggered after the content is loaded
94
         *
95
         * @param            string filePath the path to the file
96
         * @param            string rawData  the raw data
97
         * @param \Phile\Model\Page page     the page model
98
         */
99 22
        Container::getInstance()->get('Phile_EventBus')->trigger(
100
            'after_load_content',
101
            [
102 22
                'filePath' => &$this->filePath,
103 22
                'rawData' => $this->rawData,
104
                'page' => &$this
105
            ]
106
        );
107
108 22
        $this->parser = ServiceLocator::getService('Phile_Parser');
109
    }
110
111
    /**
112
     * method to get content of page, this method returned the parsed content
113
     *
114
     * @return mixed
115
     */
116 7
    public function getContent()
117
    {
118
        /**
119
         * @triggerEvent before_parse_content this event is triggered before the content is parsed
120
         *
121
         * @param            string content the raw data
122
         * @param \Phile\Model\Page page    the page model
123
         */
124 7
        Container::getInstance()->get('Phile_EventBus')->trigger(
125
            'before_parse_content',
126 7
            ['content' => $this->content, 'page' => &$this]
127
        );
128 7
        $content = $this->parser->parse($this->content);
129
        /**
130
         * @triggerEvent after_parse_content this event is triggered after the content is parsed
131
         *
132
         * @param            string content the parsed content
133
         * @param \Phile\Model\Page page    the page model
134
         */
135 7
        Container::getInstance()->get('Phile_EventBus')->trigger(
136
            'after_parse_content',
137 7
            ['content' => &$content, 'page' => &$this]
138
        );
139
140 7
        return $content;
141
    }
142
143
    /**
144
     * set content of page
145
     *
146
     * @param string $content
147
     */
148 3
    public function setContent($content)
149
    {
150 3
        $this->content = $content;
151
    }
152
153
    /**
154
     * get raw (un-parsed) page content
155
     *
156
     * @return string
157
     */
158 2
    public function getRawContent()
159
    {
160 2
        return $this->content;
161
    }
162
163
    /**
164
     * get the meta model
165
     *
166
     * @return Meta
167
     */
168 12
    public function getMeta()
169
    {
170 12
        return $this->meta;
171
    }
172
173
    /**
174
     * parse the raw content
175
     */
176 22
    protected function parseRawData()
177
    {
178 22
        $this->meta = new Meta($this->rawData);
179
180 22
        $content = '';
181 22
        if ($this->rawData !== null) {
182 21
            $rawData = trim($this->rawData);
183 21
            $fences = [
184
                'c' => ['open' => '/*', 'close' => '*/'],
185
                'html' => ['open' => '<!--', 'close' => '-->'],
186
                'yaml' => ['open' => '---', 'close' => '---']
187
            ];
188 21
            foreach ($fences as $fence) {
189 21
                if (strncmp($fence['open'], $rawData, strlen($fence['open'])) === 0) {
190 21
                    $sub = substr($rawData, strlen($fence['open']));
191 21
                    list(, $content) = explode($fence['close'], $sub, 2);
192 21
                    break;
193
                }
194
            }
195
        }
196
197 22
        $this->content = $content;
198
    }
199
200
    /**
201
     * Sets repository this page was retrieved by/belongs to
202
     *
203
     * @param Repository $repository
204
     * @return $this
205
     */
206 22
    public function setRepository(Repository $repository)
207
    {
208 22
        $this->repository = $repository;
209 22
        return $this;
210
    }
211
212
    /**
213
     * Gets repository this page belongs to
214
     *
215
     * @return Repository
216
     */
217 9
    public function getRepository()
218
    {
219 9
        if ($this->repository === null) {
220 1
            $this->repository = new Repository();
221
        }
222
223 9
        return $this->repository;
224
    }
225
226
    /**
227
     * get the title of page from meta information
228
     *
229
     * @return string|null
230
     */
231 9
    public function getTitle()
232
    {
233 9
        return $this->getMeta()->get('title');
234
    }
235
236
    /**
237
     * get Phile $pageId
238
     *
239
     * @param  string $filePath
240
     * @return string
241
     */
242 22
    protected function buildPageId($filePath)
243
    {
244 22
        $pageId = str_replace($this->contentFolder, '', $filePath);
245 22
        $pageId = str_replace($this->contentExtension, '', $pageId);
246 22
        $pageId = str_replace(DIRECTORY_SEPARATOR, '/', $pageId);
247 22
        $pageId = ltrim($pageId, '/');
248 22
        $pageId = preg_replace('/(?<=^|\/)index$/', '', $pageId);
249 22
        return $pageId;
250
    }
251
252
    /**
253
     * get the url of page
254
     *
255
     * @return string
256
     */
257 6
    public function getUrl()
258
    {
259 6
        $container = Container::getInstance();
260 6
        if ($container->has('Phile_Router')) {
261 5
            $router = $container->get('Phile_Router');
262
        } else {
263
            // BC: some old 1.x plugins may use Pages before the core is initialized
264 1
            $router = new Router;
265
        }
266 6
        return $router->urlForPage($this->pageId, false);
267
    }
268
269
    /**
270
     * set the filepath of the page
271
     *
272
     * @param string $filePath
273
     */
274 22
    public function setFilePath($filePath)
275
    {
276 22
        $this->filePath = $filePath;
277 22
        $this->pageId = $this->buildPageId($this->filePath);
278
    }
279
280
    /**
281
     * get the filepath of the page
282
     *
283
     * @return string
284
     */
285 3
    public function getFilePath()
286
    {
287 3
        return $this->filePath;
288
    }
289
290
    /**
291
     * get the folder name
292
     *
293
     * @return string
294
     */
295
    public function getFolder()
296
    {
297
        return basename(dirname($this->getFilePath()));
298
    }
299
300 7
    public function getPageId()
301
    {
302 7
        return $this->pageId;
303
    }
304
305
    /**
306
     * get the previous page if one exist
307
     *
308
     * @return null|\Phile\Model\Page
309
     */
310 1
    public function getPreviousPage()
311
    {
312 1
        return $this->getRepository()->getPageOffset($this, -1);
313
    }
314
315
    /**
316
     * get the next page if one exist
317
     *
318
     * @return null|\Phile\Model\Page
319
     */
320 1
    public function getNextPage()
321
    {
322 1
        return $this->getRepository()->getPageOffset($this, 1);
323
    }
324
}
325