1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of phpDocumentor. |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
* |
9
|
|
|
* @author Mike van Riel <[email protected]> |
10
|
|
|
* @copyright 2010-2018 Mike van Riel / Naenius (http://www.naenius.com) |
11
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT |
12
|
|
|
* @link http://phpdoc.org |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace phpDocumentor\Transformer\Writer\Twig; |
16
|
|
|
|
17
|
|
|
use phpDocumentor\Descriptor\ProjectDescriptor; |
18
|
|
|
use phpDocumentor\Transformer\Router\Renderer; |
19
|
|
|
use phpDocumentor\Transformer\Transformation; |
20
|
|
|
use Twig\Environment; |
21
|
|
|
use Twig\Extension\DebugExtension; |
22
|
|
|
use Twig\Loader\FilesystemLoader; |
23
|
|
|
|
24
|
|
|
final class EnvironmentFactory |
25
|
|
|
{ |
26
|
|
|
private $baseEnvironment; |
27
|
|
|
/** |
28
|
|
|
* @var Renderer |
29
|
|
|
*/ |
30
|
|
|
private $renderer; |
31
|
|
|
|
32
|
|
|
public function __construct(Environment $baseEnvironment, Renderer $renderer) |
33
|
|
|
{ |
34
|
|
|
$this->baseEnvironment = $baseEnvironment; |
35
|
|
|
$this->renderer = $renderer; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function create( |
39
|
|
|
ProjectDescriptor $project, |
40
|
|
|
Transformation $transformation, |
41
|
|
|
string $destination |
42
|
|
|
): Environment { |
43
|
|
|
$callingTemplatePath = $this->getTemplatePath($transformation); |
44
|
|
|
|
45
|
|
|
$baseTemplatesPath = $transformation->getTransformer()->getTemplates()->getTemplatesPath(); |
46
|
|
|
|
47
|
|
|
$templateFolders = [ |
48
|
|
|
$baseTemplatesPath . '/..' . DIRECTORY_SEPARATOR . $callingTemplatePath, |
49
|
|
|
// http://twig.sensiolabs.org/doc/recipes.html#overriding-a-template-that-also-extends-itself |
50
|
|
|
$baseTemplatesPath, |
51
|
|
|
]; |
52
|
|
|
|
53
|
|
|
// get all invoked template paths, they overrule the calling template path |
54
|
|
|
/** @var \phpDocumentor\Transformer\Template $template */ |
55
|
|
|
foreach ($transformation->getTransformer()->getTemplates() as $template) { |
56
|
|
|
$path = $baseTemplatesPath . DIRECTORY_SEPARATOR . $template->getName(); |
57
|
|
|
array_unshift($templateFolders, $path); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
// Clone twig because otherwise we cannot re-set the extensions on this twig environment on every run of this |
61
|
|
|
// writer |
62
|
|
|
$env = new Environment(new FilesystemLoader($templateFolders)); |
63
|
|
|
|
64
|
|
|
$this->addPhpDocumentorExtension($project, $transformation, $destination, $env); |
65
|
|
|
$this->addExtensionsFromTemplateConfiguration($transformation, $project, $env); |
66
|
|
|
|
67
|
|
|
return $env; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Adds the phpDocumentor base extension to the Twig Environment. |
72
|
|
|
*/ |
73
|
|
|
private function addPhpDocumentorExtension( |
74
|
|
|
ProjectDescriptor $project, |
75
|
|
|
Transformation $transformation, |
76
|
|
|
string $destination, |
77
|
|
|
Environment $twigEnvironment |
78
|
|
|
): void { |
79
|
|
|
$base_extension = new Extension($project, $transformation, $this->renderer); |
80
|
|
|
$base_extension->setDestination( |
81
|
|
|
substr($destination, strlen($transformation->getTransformer()->getTarget()) + 1) |
82
|
|
|
); |
83
|
|
|
$twigEnvironment->addExtension($base_extension); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Tries to add any custom extensions that have been defined in the template or the transformation's configuration. |
88
|
|
|
* |
89
|
|
|
* This method will read the `twig-extension` parameter of the transformation (which inherits the template's |
90
|
|
|
* parameter set) and try to add those extensions to the environment. |
91
|
|
|
* |
92
|
|
|
* @throws \InvalidArgumentException if a twig-extension should be loaded but it could not be found. |
93
|
|
|
*/ |
94
|
|
|
private function addExtensionsFromTemplateConfiguration( |
95
|
|
|
Transformation $transformation, |
96
|
|
|
ProjectDescriptor $project, |
97
|
|
|
Environment $twigEnvironment |
98
|
|
|
): void { |
99
|
|
|
$isDebug = $transformation->getParameter('twig-debug') |
|
|
|
|
100
|
|
|
? $transformation->getParameter('twig-debug')->getValue() |
101
|
|
|
: false; |
102
|
|
|
// if ($isDebug === 'true') { |
103
|
|
|
$twigEnvironment->enableDebug(); |
104
|
|
|
$twigEnvironment->enableAutoReload(); |
105
|
|
|
$twigEnvironment->addExtension(new DebugExtension()); |
106
|
|
|
// } |
107
|
|
|
|
108
|
|
|
/** @var \phpDocumentor\Transformer\Template\Parameter $extension */ |
109
|
|
|
foreach ($transformation->getParametersWithKey('twig-extension') as $extension) { |
110
|
|
|
$extensionValue = $extension->getValue(); |
111
|
|
|
if (!class_exists($extensionValue)) { |
112
|
|
|
throw new \InvalidArgumentException('Unknown twig extension: ' . $extensionValue); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
// to support 'normal' Twig extensions we check the interface to determine what instantiation to do. |
116
|
|
|
$implementsInterface = in_array( |
117
|
|
|
'phpDocumentor\Transformer\Writer\Twig\ExtensionInterface', |
118
|
|
|
class_implements($extensionValue), |
119
|
|
|
true |
120
|
|
|
); |
121
|
|
|
|
122
|
|
|
$twigEnvironment->addExtension( |
123
|
|
|
$implementsInterface ? new $extensionValue($project, $transformation) : new $extensionValue() |
124
|
|
|
); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Returns the path belonging to the template. |
130
|
|
|
*/ |
131
|
|
|
private function getTemplatePath(Transformation $transformation): string |
132
|
|
|
{ |
133
|
|
|
$parts = preg_split('[\\\\|/]', $transformation->getSource()); |
134
|
|
|
|
135
|
|
|
return $parts[0] . DIRECTORY_SEPARATOR . $parts[1]; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.