Completed
Branch 09branch (31301e)
by Anton
02:42
created

ViewSource   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 104
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getName() 0 4 1
A getNamespace() 0 4 1
A getFilename() 0 4 1
A getCode() 0 5 1
A withCode() 0 7 1
A isFresh() 0 4 1
1
<?php
2
/**
3
 * spiral
4
 *
5
 * @author    Wolfy-J
6
 */
7
8
namespace Spiral\Views;
9
10
/**
11
 * Carries information about view.
12
 */
13
final class ViewSource
14
{
15
    /**
16
     * @var string
17
     */
18
    private $filename;
19
20
    /**
21
     * @var string
22
     */
23
    private $name;
24
25
    /**
26
     * @var string
27
     */
28
    private $namespace;
29
30
    /**
31
     * Cached or manually set source.
32
     *
33
     * @var string|null
34
     */
35
    private $code = null;
36
37
    /**
38
     * @param string $filename
39
     * @param string $name
40
     * @param string $namespace
41
     */
42
    public function __construct(string $filename, string $name, string $namespace)
43
    {
44
        $this->filename = $filename;
45
        $this->name = $name;
46
        $this->namespace = $namespace;
47
    }
48
49
    /**
50
     * Template name.
51
     *
52
     * @return string
53
     */
54
    public function getName(): string
55
    {
56
        return $this->name;
57
    }
58
59
    /**
60
     * Template namespace.
61
     *
62
     * @return string
63
     */
64
    public function getNamespace(): string
65
    {
66
        return $this->namespace;
67
    }
68
69
    /**
70
     * Template filename.
71
     *
72
     * @return string
73
     */
74
    public function getFilename(): string
75
    {
76
        return $this->filename;
77
    }
78
79
    /**
80
     * Template code.
81
     *
82
     * @return string
83
     */
84
    public function getCode(): string
85
    {
86
        //Expecting local stream
87
        return $this->code ?? file_get_contents($this->getFilename());
88
    }
89
90
    /**
91
     * Get source copy with redefined code.
92
     *
93
     * @param string $code
94
     *
95
     * @return \Spiral\Views\ViewSource
96
     */
97
    public function withCode(string $code): ViewSource
98
    {
99
        $context = clone $this;
100
        $context->code = $code;
101
102
        return $context;
103
    }
104
105
    /**
106
     * Must return true if template have not changed since given timestamp.
107
     *
108
     * @param int $timestamp
109
     *
110
     * @return bool
111
     */
112
    public function isFresh(int $timestamp): bool
113
    {
114
        return filemtime($this->getFilename()) < $timestamp;
115
    }
116
}