These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | /** |
||
3 | * phpDocumentor |
||
4 | * |
||
5 | * PHP Version 5.3 |
||
6 | * |
||
7 | * @copyright 2010-2018 Mike van Riel / Naenius (http://www.naenius.com) |
||
8 | * @license http://www.opensource.org/licenses/mit-license.php MIT |
||
9 | * @link http://phpdoc.org |
||
10 | */ |
||
11 | |||
12 | namespace phpDocumentor\Plugin\Scrybe\Template; |
||
13 | |||
14 | use InvalidArgumentException; |
||
15 | |||
16 | /** |
||
17 | * Template class to use Twig to generate templates. |
||
18 | */ |
||
19 | class Twig implements TemplateInterface |
||
20 | { |
||
21 | /** @var string The base location for templates */ |
||
22 | protected $path = ''; |
||
23 | |||
24 | /** @var string The name of the containing template folder */ |
||
25 | protected $name = 'default'; |
||
26 | |||
27 | /** @var string The extension used to select the correct template file */ |
||
28 | protected $extension = 'html'; |
||
29 | |||
30 | /** |
||
31 | * Constructs the twig template and sets the default values. |
||
32 | * |
||
33 | * @param string $templatePath the base location for templates. |
||
34 | */ |
||
35 | 23 | public function __construct(string $templatePath) |
|
36 | { |
||
37 | 23 | $this->path = $templatePath; |
|
38 | 23 | } |
|
39 | |||
40 | /** |
||
41 | * Sets the name for this template. |
||
42 | * |
||
43 | * @param string $name A template name that may be composed of alphanumeric characters, underscores and/or hyphens. |
||
44 | * |
||
45 | * @throws InvalidArgumentException if the name does not match the prescribed format. |
||
46 | */ |
||
47 | 10 | public function setName(string $name): void |
|
48 | { |
||
49 | 10 | if (!preg_match('/^[0-9a-zA-Z\-\_]{3,}$/', $name)) { |
|
50 | 5 | throw new InvalidArgumentException( |
|
51 | 'A template name may only be composed of alphanumeric ' |
||
52 | . 'characters, underscores or hyphens and have at least 3 ' |
||
53 | 5 | . 'characters.' |
|
54 | ); |
||
55 | } |
||
56 | |||
57 | 5 | $this->name = $name; |
|
58 | 5 | } |
|
59 | |||
60 | /** |
||
61 | * Returns the name of this template. |
||
62 | * |
||
63 | * @see Twig::setName() for a specification of the format. |
||
64 | */ |
||
65 | 1 | public function getName(): string |
|
66 | { |
||
67 | 1 | return $this->name; |
|
68 | } |
||
69 | |||
70 | /** |
||
71 | * Sets the base path where the templates are stored. |
||
72 | * |
||
73 | * @throws InvalidArgumentException |
||
74 | */ |
||
75 | 3 | public function setPath(string $path): void |
|
76 | { |
||
77 | 3 | if (!file_exists($path) || !is_dir($path)) { |
|
78 | 2 | throw new InvalidArgumentException( |
|
79 | 2 | 'Expected the template path to be an existing directory, received: ' . $path |
|
80 | ); |
||
81 | } |
||
82 | |||
83 | 1 | $this->path = $path; |
|
84 | 1 | } |
|
85 | |||
86 | /** |
||
87 | * Returns the base path where the templates are stored. |
||
88 | */ |
||
89 | 1 | public function getPath(): string |
|
90 | { |
||
91 | 1 | return $this->path; |
|
92 | } |
||
93 | |||
94 | /** |
||
95 | * Sets the file extension used to determine the template filename. |
||
96 | * |
||
97 | * The file extension of the destination format needs to be set. This is used to retrieve the correct template. |
||
98 | * |
||
99 | * @param string $extension an extension (thus only containing alphanumeric characters and be between 2 and 4 |
||
100 | * characters in size). |
||
101 | * |
||
102 | * @throws InvalidArgumentException if the extension does not match the validation restrictions mentioned above. |
||
103 | */ |
||
104 | 8 | public function setExtension(string $extension): void |
|
105 | { |
||
106 | 8 | if (!preg_match('/^[a-zA-Z0-9]{2,4}$/', $extension)) { |
|
107 | 4 | throw new InvalidArgumentException( |
|
108 | 'Extension should be only be composed of alphanumeric characters' |
||
109 | 4 | . ' and should be at least 2 but no more than 4 characters' |
|
110 | ); |
||
111 | } |
||
112 | |||
113 | 4 | $this->extension = $extension; |
|
114 | 4 | } |
|
115 | |||
116 | /** |
||
117 | * Returns the extension of the destination file extension. |
||
118 | * |
||
119 | * @see Twig::setExtension() for more information and the format of the extension. |
||
120 | */ |
||
121 | 1 | public function getExtension(): string |
|
122 | { |
||
123 | 1 | return $this->extension; |
|
124 | } |
||
125 | |||
126 | /** |
||
127 | * Applies the relevant template upon the given content. |
||
128 | * |
||
129 | * This method takes the combines the template with the given contents and generates a final piece of text |
||
130 | * from that. |
||
131 | * |
||
132 | * The user may add additional options that are set as parameters in the template. |
||
133 | * |
||
134 | * @param string[] $options |
||
135 | * |
||
136 | * @see Twig::getTemplateFilename() how the filename is assembled |
||
137 | */ |
||
138 | 2 | public function decorate(string $contents, array $options = []): string |
|
139 | { |
||
140 | 2 | return $this->getTwigEnvironment()->render( |
|
141 | 2 | $this->getTemplateFilename(), |
|
142 | 1 | array_merge(['contents' => $contents], $options) |
|
143 | ); |
||
144 | } |
||
145 | |||
146 | /** |
||
147 | * Returns a list of files that need to be copied to the destination location. |
||
148 | * |
||
149 | * Examples of assets can be: |
||
150 | * |
||
151 | * * CSS files |
||
152 | * * Javascript files |
||
153 | * * Images |
||
154 | * |
||
155 | * Assets for this template engine means every file that is contained in a subfolder of the template folder and |
||
156 | * does not end with the extension twig. |
||
157 | * |
||
158 | * Thus every file in the root of the template folder is ignored and files and directories having only twig |
||
159 | * templates (considered as being includes) are not included in this list. |
||
160 | * |
||
161 | * @return string[] |
||
162 | */ |
||
163 | public function getAssets(): array |
||
164 | { |
||
165 | //TODO implement this |
||
166 | return []; |
||
167 | // return iterator_to_array( |
||
0 ignored issues
–
show
|
|||
168 | // $finder->files() |
||
169 | // ->in($this->path . DIRECTORY_SEPARATOR . $this->name) |
||
170 | // ->depth('> 0') |
||
171 | // ->notName('*.twig') |
||
172 | // ->sortByName() |
||
173 | // ); |
||
174 | } |
||
175 | |||
176 | /** |
||
177 | * Returns the filename for the template. |
||
178 | * |
||
179 | * The filename is composed of the following components: |
||
180 | * |
||
181 | * - the template base folder |
||
182 | * - the template's name |
||
183 | * - a path separator |
||
184 | * - the literal 'layout' combined with the extension |
||
185 | * - and as final extension the literal '.twig' |
||
186 | * |
||
187 | * @throws \DomainException if the template does not exist. |
||
188 | * |
||
189 | * @return string |
||
190 | */ |
||
191 | 2 | protected function getTemplateFilename() |
|
192 | { |
||
193 | 2 | $filename = $this->name . '/layout.' . $this->extension . '.twig'; |
|
194 | |||
195 | 2 | $template_path = $this->path . DIRECTORY_SEPARATOR . $filename; |
|
196 | 2 | if (!file_exists($template_path)) { |
|
197 | 1 | throw new \DomainException('Template file "' . $template_path . '" could not be found'); |
|
198 | } |
||
199 | |||
200 | 1 | return $filename; |
|
201 | } |
||
202 | |||
203 | /** |
||
204 | * Constructs and returns the twig environment. |
||
205 | * |
||
206 | * This uses the path as defined with this class to instantiate a new Environment and disables the escaping |
||
207 | * mechanism since we use it to generate HTML; even embedded. |
||
208 | * |
||
209 | * @see Twig::$path for the template base path. |
||
210 | * |
||
211 | * @return \Twig_Environment |
||
212 | */ |
||
213 | 2 | protected function getTwigEnvironment() |
|
214 | { |
||
215 | 2 | return new \Twig_Environment(new \Twig_Loader_Filesystem($this->path), ['autoescape' => false]); |
|
216 | } |
||
217 | } |
||
218 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.