|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This is part of the webuni/front-matter package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Martin Hasoň <[email protected]> |
|
7
|
|
|
* (c) Webuni s.r.o. <[email protected]> |
|
8
|
|
|
* |
|
9
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
10
|
|
|
* file that was distributed with this source code. |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace Webuni\FrontMatter\Twig; |
|
14
|
|
|
|
|
15
|
|
|
use Twig\Loader\LoaderInterface; |
|
16
|
|
|
use Twig\Source; |
|
17
|
|
|
use Webuni\FrontMatter\FrontMatterInterface; |
|
18
|
|
|
|
|
19
|
|
|
class FrontMatterLoader implements LoaderInterface |
|
20
|
|
|
{ |
|
21
|
|
|
/** @var LoaderInterface */ |
|
22
|
|
|
private $loader; |
|
23
|
|
|
|
|
24
|
|
|
/** @var FrontMatterInterface */ |
|
25
|
|
|
private $parser; |
|
26
|
|
|
|
|
27
|
|
|
/** @var DataToTwigConvertor */ |
|
28
|
|
|
private $convertor; |
|
29
|
|
|
|
|
30
|
|
|
public function __construct(FrontMatterInterface $parser, LoaderInterface $loader, DataToTwigConvertor $convertor = null) |
|
31
|
|
|
{ |
|
32
|
|
|
$this->loader = $loader; |
|
33
|
|
|
$this->parser = $parser; |
|
34
|
|
|
$this->convertor = $convertor ?? DataToTwigConvertor::nothing(); |
|
|
|
|
|
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function getCacheKey(string $name): string |
|
38
|
|
|
{ |
|
39
|
|
|
return $this->loader->getCacheKey($name); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function isFresh(string $name, int $time): bool |
|
43
|
|
|
{ |
|
44
|
|
|
return $this->loader->isFresh($name, $time); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function exists(string $name): bool |
|
48
|
|
|
{ |
|
49
|
|
|
return $this->loader->exists($name); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function getSourceContext(string $name): Source |
|
53
|
|
|
{ |
|
54
|
|
|
$source = $this->loader->getSourceContext($name); |
|
55
|
|
|
$code = $source->getCode(); |
|
56
|
|
|
$document = $this->parser->parse($code); |
|
57
|
|
|
$content = $document->getContent(); |
|
58
|
|
|
|
|
59
|
|
|
if ($code === $content) { |
|
60
|
|
|
return $source; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
$content = $this->convertor->convert($document->getData()).$content; |
|
64
|
|
|
|
|
65
|
|
|
$lines = substr_count($code, "\n", 0, - strlen($content)); |
|
66
|
|
|
$content = "{% line $lines %}\n$content"; |
|
67
|
|
|
|
|
68
|
|
|
return new Source($content, $source->getName(), $source->getPath()); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
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
$accountIdthat can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theidproperty of an instance of theAccountclass. 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.