|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the ICanBoogie package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Olivier Laviale <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace ICanBoogie\Binding\Render; |
|
13
|
|
|
|
|
14
|
|
|
use ICanBoogie\Render\TemplateResolver; |
|
15
|
|
|
use ICanBoogie\Render\TemplateResolverDecorator; |
|
16
|
|
|
use ICanBoogie\Render\TemplateResolverDecoratorTrait; |
|
17
|
|
|
use ICanBoogie\Render\TemplateResolverTrait; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Decorates a template resolver and adds support for the application paths. |
|
21
|
|
|
* |
|
22
|
|
|
* @package ICanBoogie\Binding\Render |
|
23
|
|
|
*/ |
|
24
|
|
|
class ApplicationTemplateResolver implements TemplateResolverDecorator |
|
25
|
|
|
{ |
|
26
|
|
|
use TemplateResolverTrait; |
|
27
|
|
|
use TemplateResolverDecoratorTrait; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Application paths. |
|
31
|
|
|
* |
|
32
|
|
|
* @var array |
|
33
|
|
|
*/ |
|
34
|
|
|
private $paths; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param TemplateResolver $template_resolver |
|
38
|
|
|
* @param array $paths Application paths. |
|
39
|
|
|
*/ |
|
40
|
|
|
public function __construct(TemplateResolver $template_resolver, array $paths) |
|
41
|
|
|
{ |
|
42
|
|
|
$this->template_resolver = $template_resolver; |
|
43
|
|
|
$this->paths = $this->expend_paths($paths); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @inheritdoc |
|
48
|
|
|
*/ |
|
49
|
|
|
public function resolve($name, array $extensions, &$tried = []) |
|
50
|
|
|
{ |
|
51
|
|
|
if (strpos($name, '//') === 0) |
|
52
|
|
|
{ |
|
53
|
|
|
return $this->resolve_from_app(substr($name, 2), $extensions, $tried); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
$template = $this->resolve_from_app($name, $extensions, $tried); |
|
57
|
|
|
|
|
58
|
|
|
if ($template) |
|
59
|
|
|
{ |
|
60
|
|
|
return $template; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
return $this->template_resolver->resolve($name, $extensions, $tried); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Resolves a template name from the application paths. |
|
68
|
|
|
* |
|
69
|
|
|
* @param string $name |
|
70
|
|
|
* @param array $extensions |
|
71
|
|
|
* @param array $tried |
|
72
|
|
|
* |
|
73
|
|
|
* @return string|null |
|
74
|
|
|
*/ |
|
75
|
|
|
protected function resolve_from_app($name, array $extensions, &$tried) |
|
76
|
|
|
{ |
|
77
|
|
|
return $this->resolve_path($this->resolve_tries($this->paths, $name, $extensions), $tried); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Expends application paths into template paths. |
|
82
|
|
|
* |
|
83
|
|
|
* **Note:** Paths that do not have a "templates" directory are discarded. |
|
84
|
|
|
* |
|
85
|
|
|
* @param array $paths |
|
86
|
|
|
* |
|
87
|
|
|
* @return array |
|
88
|
|
|
*/ |
|
89
|
|
|
protected function expend_paths(array $paths) |
|
90
|
|
|
{ |
|
91
|
|
|
$resolved_paths = []; |
|
92
|
|
|
|
|
93
|
|
|
foreach (array_reverse($paths) as $path) |
|
94
|
|
|
{ |
|
95
|
|
|
$path .= 'templates' . DIRECTORY_SEPARATOR; |
|
96
|
|
|
|
|
97
|
|
|
if (file_exists($path)) |
|
98
|
|
|
{ |
|
99
|
|
|
$resolved_paths[] = $path; |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
return $resolved_paths; |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|