1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Andrew Coulton <[email protected]> |
4
|
|
|
* @copyright 2015 inGenerator Ltd |
5
|
|
|
* @license http://kohanaframework.org/license |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Ingenerator\KohanaView\TemplateManager; |
9
|
|
|
|
10
|
|
|
use Ingenerator\KohanaView\Exception\TemplateCacheException; |
11
|
|
|
use Ingenerator\KohanaView\Exception\TemplateNotFoundException; |
12
|
|
|
use Ingenerator\KohanaView\TemplateCompiler; |
13
|
|
|
use Ingenerator\KohanaView\TemplateManager; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Manages compilation of templates from view files located within the cascading file system. This allows extension |
17
|
|
|
* modules or applications to provide their own templates that are used in place of defaults provided by other modules. |
18
|
|
|
* |
19
|
|
|
* Templates will be dynamically compiled and cached to disk: |
20
|
|
|
* * If the recompile_always option is TRUE, then once for every execution |
21
|
|
|
* * If the recompile_always option is FALSE, then only if the compiled template does not yet exist |
22
|
|
|
* |
23
|
|
|
* @package Ingenerator\KohanaView\TemplateManager |
24
|
|
|
*/ |
25
|
|
|
class CFSTemplateManager implements TemplateManager |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected $cache_dir; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var CFSWrapper |
34
|
|
|
*/ |
35
|
|
|
protected $cascading_files; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var array |
39
|
|
|
*/ |
40
|
|
|
protected $compiled_paths = []; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var TemplateCompiler |
44
|
|
|
*/ |
45
|
|
|
protected $compiler; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var boolean |
49
|
|
|
*/ |
50
|
|
|
protected $recompile_always; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Valid options: |
54
|
|
|
* * cache_dir => the path where compiled templates will be cached |
55
|
|
|
* * recompile_always => whether to recompile each template on every execution, |
56
|
|
|
* |
57
|
|
|
* @param TemplateCompiler $compiler |
58
|
|
|
* @param array $options |
59
|
|
|
* @param CFSWrapper $cascading_files |
60
|
|
|
*/ |
61
|
|
|
public function __construct(TemplateCompiler $compiler, array $options, CFSWrapper $cascading_files = NULL) |
62
|
|
|
{ |
63
|
|
|
$this->cascading_files = $cascading_files ?: new CFSWrapper; |
64
|
|
|
$this->compiler = $compiler; |
65
|
|
|
$this->cache_dir = rtrim($options['cache_dir'], '/'); |
66
|
|
|
$this->recompile_always = \Arr::get($options, 'recompile_always', FALSE); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* {@inheritdoc} |
71
|
|
|
*/ |
72
|
|
|
public function getPath($template_name) |
73
|
|
|
{ |
74
|
|
|
$compiled_path = $this->cache_dir.'/'.$template_name.'.php'; |
75
|
|
|
|
76
|
|
|
if ($this->isCompileRequired($compiled_path)) { |
77
|
|
|
$source = $this->requireSourceFileContent($template_name); |
78
|
|
|
$compiled = $this->compiler->compile($source); |
79
|
|
|
$this->writeFile($compiled_path, $compiled); |
80
|
|
|
$this->compiled_paths[$compiled_path] = TRUE; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return $compiled_path; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param string $compiled_path |
88
|
|
|
* |
89
|
|
|
* @return bool |
90
|
|
|
*/ |
91
|
|
|
protected function isCompileRequired($compiled_path) |
92
|
|
|
{ |
93
|
|
|
if ($this->recompile_always AND ! isset($this->compiled_paths[$compiled_path])) { |
|
|
|
|
94
|
|
|
return TRUE; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return ! file_exists($compiled_path); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param string $template_name |
102
|
|
|
* |
103
|
|
|
* @return string |
104
|
|
|
*/ |
105
|
|
|
protected function requireSourceFileContent($template_name) |
106
|
|
|
{ |
107
|
|
|
if ( ! $source_file = $this->cascading_files->find_file('views', $template_name)) { |
108
|
|
|
throw TemplateNotFoundException::forSourcePath('views/'.$template_name); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return file_get_contents($source_file); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param string $compiled_path |
116
|
|
|
* @param string $compiled |
117
|
|
|
*/ |
118
|
|
|
protected function writeFile($compiled_path, $compiled) |
119
|
|
|
{ |
120
|
|
|
$this->ensureWriteableDirectory(dirname($compiled_path)); |
121
|
|
|
file_put_contents($compiled_path, $compiled); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @param string $path |
126
|
|
|
*/ |
127
|
|
|
protected function ensureWriteableDirectory($path) |
128
|
|
|
{ |
129
|
|
|
if (is_dir($path)) { |
130
|
|
|
if ( ! is_writeable($path)) { |
131
|
|
|
throw TemplateCacheException::pathNotWriteable($path); |
132
|
|
|
} |
133
|
|
|
} else { |
134
|
|
|
if ( ! mkdir($path, 0777, TRUE)) { |
135
|
|
|
throw TemplateCacheException::cannotCreateDirectory($path); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
} |
141
|
|
|
|
PHP has two types of connecting operators (logical operators, and boolean operators):
and
&&
or
||
The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like
&&
, or||
.Let’s take a look at a few examples:
Logical Operators are used for Control-Flow
One case where you explicitly want to use logical operators is for control-flow such as this:
Since
die
introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined withthrow
at this point:These limitations lead to logical operators rarely being of use in current PHP code.