|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Nip\View\ViewFinder; |
|
4
|
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Trait HasViewFinder |
|
9
|
|
|
* @package Nip\View\ViewFinder |
|
10
|
|
|
*/ |
|
11
|
|
|
trait HasViewFinder |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* The view finder implementation. |
|
15
|
|
|
* |
|
16
|
|
|
* @var ViewFinderInterface |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $finder = null; |
|
19
|
|
|
|
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Adds a path where templates are stored. |
|
23
|
|
|
* |
|
24
|
|
|
* @param string $path A path where to look for templates |
|
25
|
|
|
* @param string $namespace A path namespace |
|
26
|
|
|
* |
|
27
|
|
|
* @return void |
|
28
|
|
|
*/ |
|
29
|
1 |
|
public function addPath($path, $namespace = ViewFinderInterface::MAIN_NAMESPACE) |
|
30
|
|
|
{ |
|
31
|
1 |
|
$this->getFinder()->addPath($path, $namespace); |
|
32
|
1 |
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Prepends a path where templates are stored. |
|
36
|
|
|
* |
|
37
|
|
|
* @param string $path A path where to look for templates |
|
38
|
|
|
* @param string $namespace A path namespace |
|
39
|
|
|
* @return void |
|
40
|
|
|
*/ |
|
41
|
1 |
|
public function prependPath($path, $namespace = ViewFinderInterface::MAIN_NAMESPACE) |
|
42
|
|
|
{ |
|
43
|
1 |
|
$this->getFinder()->prependPath($path, $namespace); |
|
44
|
1 |
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param $view |
|
48
|
|
|
* @return bool |
|
49
|
|
|
*/ |
|
50
|
|
|
public function existPath($view) |
|
51
|
|
|
{ |
|
52
|
|
|
try { |
|
53
|
|
|
$this->getFinder()->find($view); |
|
54
|
|
|
} catch (InvalidArgumentException $exception) { |
|
55
|
|
|
return false; |
|
56
|
|
|
} |
|
57
|
|
|
return true; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Get the view finder instance. |
|
62
|
|
|
* |
|
63
|
|
|
* @return ViewFinderInterface|ViewFinder |
|
64
|
|
|
*/ |
|
65
|
6 |
|
public function getFinder() |
|
66
|
|
|
{ |
|
67
|
6 |
|
if ($this->finder === null) { |
|
68
|
6 |
|
$this->initFinder(); |
|
69
|
|
|
} |
|
70
|
6 |
|
return $this->finder; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
6 |
|
public function initFinder() |
|
74
|
|
|
{ |
|
75
|
6 |
|
$finder = new ViewFinder(); |
|
76
|
6 |
|
$finder->addPath($this->getBasePath()); |
|
|
|
|
|
|
77
|
6 |
|
$this->setFinder($finder); |
|
78
|
6 |
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Set the view finder instance. |
|
82
|
|
|
* |
|
83
|
|
|
* @param ViewFinderInterface $finder |
|
84
|
|
|
* @return void |
|
85
|
|
|
*/ |
|
86
|
6 |
|
public function setFinder(ViewFinderInterface $finder) |
|
87
|
|
|
{ |
|
88
|
6 |
|
$this->finder = $finder; |
|
89
|
6 |
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|