Completed
Pull Request — master (#20)
by Vladimir
02:11
created

FrontMatterObject::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 1
cts 1
cp 1
crap 1
1
<?php
2
3
namespace allejo\stakx\Object;
4
5
use allejo\stakx\FrontMatter\FrontMatterParser;
6
use allejo\stakx\FrontMatter\YamlVariableUndefinedException;
7
use allejo\stakx\System\Filesystem;
8
use Symfony\Component\Filesystem\Exception\FileNotFoundException;
9
use Symfony\Component\Filesystem\Exception\IOException;
10
use Symfony\Component\Yaml\Yaml;
11
12
abstract class FrontMatterObject
13
{
14
    /**
15
     * An array to keep track of collection or data dependencies used inside of a Twig template
16
     *
17
     * $dataDependencies['collections'] = array()
18
     * $dataDependencies['data'] = array()
19
     *
20
     * @var array
21
     */
22
    protected $dataDependencies;
23
24
    /**
25
     * A list of Front Matter values that should not be returned directly from the $frontMatter array. Values listed
26
     * here have dedicated functions that handle those Front Matter values and the respective functions should be called
27
     * instead.
28
     *
29
     * @var string[]
30
     */
31
    protected $frontMatterBlacklist;
32
33
    /**
34
     * Set to true if the front matter has already been evaluated with variable interpolation
35
     *
36
     * @var bool
37
     */
38
    protected $frontMatterEvaluated;
39
40
    /**
41
     * @var FrontMatterParser
42
     */
43
    protected $frontMatterParser;
44
45
    /**
46
     * An array containing the Yaml of the file
47
     *
48
     * @var array
49
     */
50
    protected $frontMatter;
51
52
    /**
53
     * Set to true if the body has already been parsed as markdown or any other format
54
     *
55
     * @var bool
56
     */
57
    protected $bodyContentEvaluated;
58
59
    /**
60
     * Only the body of the file, i.e. the content
61
     *
62
     * @var string
63
     */
64
    protected $bodyContent;
65
66
    /**
67
     * The extension of the file
68
     *
69
     * @var string
70
     */
71
    protected $extension;
72
73
    /**
74
     * The original file path to the ContentItem
75
     *
76
     * @var string
77
     */
78
    protected $filePath;
79
80
    /**
81
     * The permalink for this object
82
     *
83
     * @var string
84
     */
85
    protected $permalink;
86
87
    /**
88
     * A filesystem object
89
     *
90
     * @var Filesystem
91
     */
92
    protected $fs;
93
94
    /**
95
     * A list URLs that will redirect to this object
96
     *
97
     * @var string[]
98
     */
99
    private $redirects;
100
101
    /**
102
     * ContentItem constructor.
103
     *
104
     * @param string $filePath The path to the file that will be parsed into a ContentItem
105
     *
106
     * @throws FileNotFoundException The given file path does not exist
107
     * @throws IOException           The file was not a valid ContentItem. This would meam there was no front matter or
108
     *                               no body
109
     */
110 30
    public function __construct ($filePath)
111
    {
112 30
        $this->frontMatterBlacklist = array('permalink', 'redirects');
113
114 30
        $this->filePath  = $filePath;
115 30
        $this->fs        = new Filesystem();
116
117 30
        if (!$this->fs->exists($filePath))
118 30
        {
119 1
            throw new FileNotFoundException("The following file could not be found: ${filePath}");
120
        }
121
122
        $this->extension = strtolower($this->fs->getExtension($filePath));
123
124
        $this->refreshFileContent();
125
    }
126
127
    /**
128
     * The magic getter returns values from the front matter in order to make these values accessible to Twig templates
129
     * in a simple fashion
130
     *
131
     * @param  string $name The key in the front matter
132
     *
133
     * @return mixed|null
134
     */
135
    public function __get ($name)
136
    {
137 3
        return (array_key_exists($name, $this->frontMatter) ? $this->frontMatter[$name] : null);
138
    }
139
140
    /**
141
     * The magic getter returns true if the value exists in the Front Matter. This is used in conjunction with the __get
142
     * function
143
     *
144
     * @param  string $name The name of the Front Matter value being looked for
145
     *
146
     * @return bool
147
     */
148
    public function __isset ($name)
149
    {
150 1
        return (!in_array($name, $this->frontMatterBlacklist)) && array_key_exists($name, $this->frontMatter);
151
    }
152
153
    /**
154
     * Return the body of the Content Item
155
     *
156
     * @return string
157
     */
158
    abstract public function getContent ();
159
160
    /**
161
     * @param array|null $variables An array of YAML variables to use in evaluating the `$permalink` value
162
     */
163
    final public function evaluateFrontMatter ($variables = null)
164
    {
165 2
        if (!is_null($variables))
166 2
        {
167 2
            $this->frontMatter = array_merge($this->frontMatter, $variables);
168 2
            $this->handleSpecialFrontMatter();
169 2
            $this->evaluateYaml($this->frontMatter);
170 2
        }
171 2
    }
172
173
    /**
174
     * Get the Front Matter of a ContentItem as an array
175
     *
176
     * @param  bool $evaluateYaml When set to true, the YAML will be evaluated for variables
177
     *
178
     * @return array
179
     */
180
    final public function getFrontMatter ($evaluateYaml = true)
181
    {
182 7
        if (is_null($this->frontMatter))
183 7
        {
184 1
            $this->frontMatter = array();
185 1
        }
186 6
        else if (!$this->frontMatterEvaluated && $evaluateYaml)
187 6
        {
188 6
            $this->evaluateYaml($this->frontMatter);
189 5
            $this->frontMatterEvaluated = true;
190 5
        }
191
192 6
        return $this->frontMatter;
193
    }
194
195
    /**
196
     * Get the permalink of this Content Item
197
     *
198
     * @return string
199
     * @throws \Exception
200
     */
201
    final public function getPermalink ()
202
    {
203 9
        if (!is_null($this->permalink))
204 9
        {
205 2
            return $this->permalink;
206
        }
207
208 8
        if (!is_null($this->frontMatterParser) && $this->frontMatterParser->hasExpansion())
209 8
        {
210
            throw new \Exception('The permalink for this item has not been set');
211
        }
212
213 8
        $permalink = (is_array($this->frontMatter) && isset($this->frontMatter['permalink'])) ?
214 8
            $this->frontMatter['permalink'] : $this->getPathPermalink();
215
216 8
        if (is_array($permalink))
217 8
        {
218 3
            $this->permalink = $permalink[0];
219 3
            array_shift($permalink);
220 3
            $this->redirects = $permalink;
221 3
        }
222
        else
223
        {
224 5
            $this->permalink = $permalink;
225 5
            $this->redirects = array();
226
        }
227
228 8
        $this->permalink = $this->sanitizePermalink($this->permalink);
229
230 8
        return $this->permalink;
231
    }
232
233
    /**
234
     * Get an array of URLs that will redirect to
235
     *
236
     * @return string[]
0 ignored issues
show
Documentation introduced by
Should the return type not be null|string[]?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
237
     */
238
    final public function getRedirects ()
239
    {
240 1
        if (is_null($this->redirects))
241 1
        {
242
            $this->getPermalink();
243
        }
244
245 1
        return $this->redirects;
246
    }
247
248
    /**
249
     * Get the destination of where this Content Item would be written to when the website is compiled
250
     *
251
     * @return string
252
     */
253
    final public function getTargetFile ()
254
    {
255 7
        $permalink = $this->getPermalink();
256 7
        $extension = $this->fs->getExtension($permalink);
257
        $permalink = str_replace('/', DIRECTORY_SEPARATOR, $permalink);
258 7
259 7
        if (empty($extension))
260 3
        {
261 3
            $permalink = rtrim($permalink, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . 'index.html';
262
        }
263 7
264
        return ltrim($permalink, DIRECTORY_SEPARATOR);
265
    }
266
267
    /**
268
     * Get the name of the item, which is just the file name without the extension
269
     *
270
     * @return string
271
     */
272
    final public function getName ()
273 4
    {
274
        return $this->fs->getBaseName($this->filePath);
275
    }
276
277
    /**
278
     * Get the original file path
279
     *
280
     * @return string
281
     */
282
    final public function getFilePath ()
283 1
    {
284
        return $this->filePath;
285
    }
286
287
    /**
288
     * Get the relative path to this file relative to the root of the Stakx website
289
     *
290
     * @return string
291
     */
292
    final public function getRelativeFilePath ()
293 5
    {
294
        return $this->fs->getRelativePath($this->filePath);
295
    }
296
297
    /**
298
     * Returns true when the evaluated Front Matter has expanded values embeded
299
     *
300
     * @return bool
301
     */
302
    final public function hasExpandedFrontMatter ()
303 1
    {
304
        return (!is_null($this->frontMatterParser) && $this->frontMatterParser->hasExpansion());
305
    }
306
307
    /**
308
     * Read the file, and parse its contents
309
     */
310
    final public function refreshFileContent ()
311 29
    {
312
        $rawFileContents = file_get_contents($this->filePath);
313 29
314 29
        $frontMatter = array();
315
        preg_match('/---(.*?)---(.*)/s', $rawFileContents, $frontMatter);
316 29
317 29 View Code Duplication
        if (count($frontMatter) != 3)
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across 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...
318 1
        {
319 1
            throw new IOException(sprintf("'%s' is not a valid ContentItem",
320 1
                    $this->fs->getFileName($this->filePath))
321
            );
322
        }
323 28
324 28 View Code Duplication
        if (empty(trim($frontMatter[2])))
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across 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...
325 1
        {
326 1
            throw new IOException(sprintf('A ContentItem (%s) must have a body to render',
327 1
                    $this->fs->getFileName($this->filePath))
328
            );
329
        }
330 27
331 26
        $this->frontMatter = Yaml::parse($frontMatter[1]);
0 ignored issues
show
Documentation Bug introduced by
It seems like \Symfony\Component\Yaml\...:parse($frontMatter[1]) can also be of type string or object<stdClass>. However, the property $frontMatter is declared as type array. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
332
        $this->bodyContent = trim($frontMatter[2]);
333 26
334 26
        $this->frontMatterEvaluated = false;
335 26
        $this->bodyContentEvaluated = false;
336
        $this->permalink = null;
337 26
338 26
        $this->handleSpecialFrontMatter();
339 26
        $this->findTwigDataDependencies('collections');
340 26
        $this->findTwigDataDependencies('data');
341
    }
342
343
    /**
344
     * Check whether this object has a reference to a collection or data item
345
     *
346
     * @param  string $namespace 'collections' or 'data'
347
     * @param  string $needle
348
     *
349
     * @return bool
350
     */
351
    final public function hasTwigDependency ($namespace, $needle)
352
    {
353
        return (in_array($needle, $this->dataDependencies[$namespace]));
354
    }
355
356
    /**
357
     * Evaluate an array of data for FrontMatter variables. This function will modify the array in place.
358
     *
359
     * @param  array $yaml An array of data containing FrontMatter variables
360
     *
361
     * @throws YamlVariableUndefinedException A FrontMatter variable used does not exist
362
     */
363
    final protected function evaluateYaml (&$yaml)
364 8
    {
365 7
        $this->frontMatterParser = new FrontMatterParser($yaml);
366
    }
367
368
    /**
369
     * Handle special front matter values that need special treatment or have special meaning to a Content Item
370
     */
371
    private function handleSpecialFrontMatter ()
372 26
    {
373 26
        if (isset($this->frontMatter['date']))
374
        {
375
            try
376
            {
377 3
                // Coming from a string variable
378
                $itemDate = new \DateTime($this->frontMatter['date']);
379 3
            }
380
            catch (\Exception $e)
381
            {
382 1
                // YAML has parsed them to Epoch time
383
                $itemDate = \DateTime::createFromFormat('U', $this->frontMatter['date']);
384
            }
385 3
386 3
            if (!$itemDate === false)
387 2
            {
388 2
                $this->frontMatter['year']  = $itemDate->format('Y');
389 2
                $this->frontMatter['month'] = $itemDate->format('m');
390 2
                $this->frontMatter['day']   = $itemDate->format('d');
391 3
            }
392 26
        }
393
    }
394
395
    /**
396
     * Get all of the references to either DataItems or ContentItems inside of given string
397
     *
398
     * @param string $filter 'collections' or 'data'
399
     */
400
    private function findTwigDataDependencies ($filter)
401 26
    {
402 26
        $regex = '/{[{%](?:.+)?(?:' . $filter . ')(?:\.|\[\')(\w+)(?:\'\])?.+[%}]}/';
403
        $results = array();
404 26
405
        preg_match_all($regex, $this->bodyContent, $results);
406 26
407 26
        $this->dataDependencies[$filter] = array_unique($results[1]);
408
    }
409
410
    /**
411
     * Get the permalink based off the location of where the file is relative to the website. This permalink is to be
412
     * used as a fallback in the case that a permalink is not explicitly specified in the Front Matter.
413
     *
414
     * @return string
415
     */
416
    private function getPathPermalink ()
417
    {
418 3
        // Remove the protocol of the path, if there is one and prepend a '/' to the beginning
419 3
        $cleanPath = preg_replace('/[\w|\d]+:\/\//', '', $this->getRelativeFilePath());
420
        $cleanPath = ltrim($cleanPath, DIRECTORY_SEPARATOR);
421
422 3
        // Handle vfs:// paths by replacing their forward slashes with the OS appropriate directory separator
423
        if (DIRECTORY_SEPARATOR !== '/')
424 3
        {
425 3
            $cleanPath = str_replace('/', DIRECTORY_SEPARATOR, $cleanPath);
426 1
        }
427 1
428
        // Check the first folder and see if it's a data folder (starts with an underscore) intended for stakx
429 3
        $folders = explode(DIRECTORY_SEPARATOR, $cleanPath);
430
431 3
        if (substr($folders[0], 0, 1) === '_')
432
        {
433
            array_shift($folders);
434
        }
435
436
        $cleanPath = implode(DIRECTORY_SEPARATOR, $folders);
437
438
        return $cleanPath;
439
    }
440
441
    /**
442
     * Sanitize a permalink to remove unsupported characters or multiple '/' and replace spaces with hyphens
443
     *
444 8
     * @param  string $permalink A permalink
445
     *
446
     * @return string $permalink The sanitized permalink
447 8
     */
448
    private function sanitizePermalink ($permalink)
449
    {
450 8
        // Remove multiple '/' together
451
        $permalink = preg_replace('/\/+/', '/', $permalink);
452
453 8
        // Replace all spaces with hyphens
454
        $permalink = str_replace(' ', '-', $permalink);
455 8
456 8
        // Remove all disallowed characters
457 3
        $permalink = preg_replace('/[^0-9a-zA-Z-_\/\\\.]/', '', $permalink);
458 3
459
        // Handle unnecessary extensions
460
        $extensionsToStrip = array('twig');
461 8
462 8
        if (in_array($this->fs->getExtension($permalink), $extensionsToStrip))
463 1
        {
464 1
            $permalink = $this->fs->removeExtension($permalink);
465
        }
466
467 8
        // Remove a special './' combination from the beginning of a path
468
        if (substr($permalink, 0, 2) === '.' . DIRECTORY_SEPARATOR)
469 8
        {
470
            $permalink = substr($permalink, 2);
471 1
        }
472
473
        // Convert permalinks to lower case
474
        $permalink = mb_strtolower($permalink, 'UTF-8');
475
476
        return $permalink;
477
    }
478
}