1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* This file is part of phpDocumentor. |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
* |
10
|
|
|
* @author Mike van Riel <[email protected]> |
11
|
|
|
* @copyright 2010-2018 Mike van Riel / Naenius (http://www.naenius.com) |
12
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT |
13
|
|
|
* @link http://phpdoc.org |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
namespace phpDocumentor\Transformer\Template; |
17
|
|
|
|
18
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
19
|
|
|
|
20
|
|
|
class PathResolver |
21
|
|
|
{ |
22
|
|
|
private $templatePath; |
23
|
|
|
|
24
|
1 |
|
public function __construct($templatePath) |
25
|
|
|
{ |
26
|
1 |
|
$this->templatePath = $templatePath; |
27
|
1 |
|
} |
28
|
|
|
|
29
|
4 |
|
public function resolve($nameOrPath) |
30
|
|
|
{ |
31
|
4 |
|
$path = null; |
32
|
|
|
|
33
|
|
|
// if this is an absolute path; load the template into the configuration |
34
|
|
|
// Please note that this _could_ override an existing template when |
35
|
|
|
// you have a template in a subfolder with the same name as a default |
36
|
|
|
// template; we have left this in on purpose to allow people to override |
37
|
|
|
// templates should they choose to. |
38
|
4 |
|
$configPath = rtrim($nameOrPath, DIRECTORY_SEPARATOR) . '/template.xml'; |
39
|
4 |
|
if (file_exists($configPath) && is_readable($configPath)) { |
40
|
1 |
|
$path = rtrim($nameOrPath, DIRECTORY_SEPARATOR); |
41
|
1 |
|
$templateNamePart = basename($path); |
42
|
1 |
|
$cachePath = rtrim($this->templatePath, '/\\') . DIRECTORY_SEPARATOR . $templateNamePart; |
43
|
|
|
|
44
|
|
|
// move the files to a cache location and then change the path |
45
|
|
|
// variable to match the new location |
46
|
1 |
|
$filesystem = new Filesystem(); |
47
|
1 |
|
$filesystem->mirror($path, $cachePath); |
48
|
1 |
|
$path = $cachePath; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
// if we load a default template |
52
|
4 |
|
if ($path === null) { |
53
|
3 |
|
$path = rtrim($this->templatePath, '/\\') . DIRECTORY_SEPARATOR . $nameOrPath; |
54
|
|
|
} |
55
|
|
|
|
56
|
4 |
|
if (!file_exists($path) || !is_readable($path)) { |
57
|
2 |
|
throw new \InvalidArgumentException( |
58
|
2 |
|
'The given template ' . $nameOrPath . ' could not be found or is not readable' |
59
|
|
|
); |
60
|
|
|
} |
61
|
|
|
|
62
|
2 |
|
return $path; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Returns the path where all templates are stored. |
67
|
|
|
* |
68
|
|
|
* @return string |
69
|
|
|
*/ |
70
|
1 |
|
public function getTemplatePath() |
71
|
|
|
{ |
72
|
1 |
|
return $this->templatePath; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|