1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of phpDocumentor. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
* |
8
|
|
|
* @copyright 2010-2015 Mike van Riel<[email protected]> |
9
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT |
10
|
|
|
* @link http://phpdoc.org |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace phpDocumentor\Infrastructure\Renderer\Template; |
14
|
|
|
|
15
|
|
|
use phpDocumentor\DomainModel\Path; |
16
|
|
|
use phpDocumentor\DomainModel\Renderer\Template; |
17
|
|
|
use phpDocumentor\DomainModel\Renderer\Template\PathsRepository; |
18
|
|
|
|
19
|
|
|
final class LocalPathsRepository implements PathsRepository |
20
|
|
|
{ |
21
|
|
|
/** @var string[] */ |
22
|
|
|
private $templateFolders = []; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Initializes this repository with its dependencies. |
26
|
|
|
* @param string[] $templateFolders |
27
|
|
|
*/ |
28
|
|
|
public function __construct(array $templateFolders = []) |
29
|
|
|
{ |
30
|
|
|
$this->templateFolders = $templateFolders; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Lists the folders where templates can be found |
35
|
|
|
* |
36
|
|
|
* @param Template|null $template |
37
|
|
|
* @return string[] |
38
|
|
|
*/ |
39
|
|
|
public function listLocations(Template $template = null) |
40
|
|
|
{ |
41
|
|
|
$templatesFolders = $this->templateFolders; |
42
|
|
|
|
43
|
|
|
// Determine the path of the current template and prepend it to the list so that it will always be queried |
44
|
|
|
// first. |
45
|
|
|
// http://twig.sensiolabs.org/doc/recipes.html#overriding-a-template-that-also-extends-itself |
46
|
|
|
if ($template) { |
47
|
|
|
$parameters = $template->getParameters(); |
48
|
|
|
if (isset($parameters['directory']) |
49
|
|
|
&& file_exists($parameters['directory']->getValue()) |
50
|
|
|
&& is_dir($parameters['directory']->getValue()) |
51
|
|
|
) { |
52
|
|
|
array_unshift($templatesFolders, $parameters['directory']->getValue()); |
53
|
|
|
} elseif ($template->getName()) { |
54
|
|
|
foreach ($templatesFolders as $folder) { |
55
|
|
|
$currentTemplatePath = $folder . '/' . $template->getName(); |
56
|
|
|
if (file_exists($currentTemplatePath)) { |
57
|
|
|
array_unshift($templatesFolders, $currentTemplatePath); |
58
|
|
|
break; |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return $templatesFolders; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Finds a template and returns the full name and path of the view |
69
|
|
|
* |
70
|
|
|
* @param Template $template |
71
|
|
|
* @param Path $view |
72
|
|
|
* @return null|Path |
73
|
|
|
*/ |
74
|
|
|
public function findByTemplateAndPath(Template $template, Path $view) |
75
|
|
|
{ |
76
|
|
|
foreach ($this->listLocations($template) as $location) { |
77
|
|
|
$filename = (string)$location . '/' . (string)$view; |
78
|
|
|
if (file_exists($filename)) { |
79
|
|
|
if (!is_readable($filename)) { |
80
|
|
|
$message = sprintf( |
81
|
|
|
'File "%" for template "%s" was found but could not be read', |
82
|
|
|
$filename, |
83
|
|
|
$template->getName() |
84
|
|
|
); |
85
|
|
|
|
86
|
|
|
throw new \RuntimeException($message); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return new Path($filename); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return null; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Lists all available templates |
98
|
|
|
* |
99
|
|
|
* @return string[] |
100
|
|
|
*/ |
101
|
|
|
public function listTemplates() |
102
|
|
|
{ |
103
|
|
|
$templates = []; |
104
|
|
|
|
105
|
|
|
foreach ($this->templateFolders as $templateFolder) { |
106
|
|
|
$subfolders = new \RecursiveDirectoryIterator($templateFolder); |
107
|
|
|
foreach ($subfolders as $subfolder) { |
108
|
|
|
if (file_exists($subfolder . '/template.xml')) { |
109
|
|
|
$templates[] = basename($subfolder); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return $templates; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|