Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fb3177f

Browse files
[Templating][String][Stopwatch] add union types
1 parent 5d23d0d commit fb3177f

8 files changed

Lines changed: 24 additions & 60 deletions

DelegatingEngine.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,15 @@ public function __construct(array $engines = [])
3636
/**
3737
* {@inheritdoc}
3838
*/
39-
public function render($name, array $parameters = [])
39+
public function render(string|TemplateReferenceInterface $name, array $parameters = [])
4040
{
4141
return $this->getEngine($name)->render($name, $parameters);
4242
}
4343

4444
/**
4545
* {@inheritdoc}
4646
*/
47-
public function stream($name, array $parameters = [])
47+
public function stream(string|TemplateReferenceInterface $name, array $parameters = [])
4848
{
4949
$engine = $this->getEngine($name);
5050
if (!$engine instanceof StreamingEngineInterface) {
@@ -57,7 +57,7 @@ public function stream($name, array $parameters = [])
5757
/**
5858
* {@inheritdoc}
5959
*/
60-
public function exists($name)
60+
public function exists(string|TemplateReferenceInterface $name)
6161
{
6262
return $this->getEngine($name)->exists($name);
6363
}
@@ -70,7 +70,7 @@ public function addEngine(EngineInterface $engine)
7070
/**
7171
* {@inheritdoc}
7272
*/
73-
public function supports($name)
73+
public function supports(string|TemplateReferenceInterface $name)
7474
{
7575
try {
7676
$this->getEngine($name);
@@ -84,13 +84,11 @@ public function supports($name)
8484
/**
8585
* Get an engine able to render the given template.
8686
*
87-
* @param string|TemplateReferenceInterface $name A template name or a TemplateReferenceInterface instance
88-
*
8987
* @return EngineInterface The engine
9088
*
9189
* @throws \RuntimeException if no engine able to work with the template is found
9290
*/
93-
public function getEngine($name)
91+
public function getEngine(string|TemplateReferenceInterface $name)
9492
{
9593
foreach ($this->engines as $engine) {
9694
if ($engine->supports($name)) {

EngineInterface.php

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,32 +33,25 @@ interface EngineInterface
3333
/**
3434
* Renders a template.
3535
*
36-
* @param string|TemplateReferenceInterface $name A template name or a TemplateReferenceInterface instance
37-
* @param array $parameters An array of parameters to pass to the template
38-
*
3936
* @return string The evaluated template as a string
4037
*
4138
* @throws \RuntimeException if the template cannot be rendered
4239
*/
43-
public function render($name, array $parameters = []);
40+
public function render(string|TemplateReferenceInterface $name, array $parameters = []);
4441

4542
/**
4643
* Returns true if the template exists.
4744
*
48-
* @param string|TemplateReferenceInterface $name A template name or a TemplateReferenceInterface instance
49-
*
5045
* @return bool true if the template exists, false otherwise
5146
*
5247
* @throws \RuntimeException if the engine cannot handle the template name
5348
*/
54-
public function exists($name);
49+
public function exists(string|TemplateReferenceInterface $name);
5550

5651
/**
5752
* Returns true if this class is able to render the given template.
5853
*
59-
* @param string|TemplateReferenceInterface $name A template name or a TemplateReferenceInterface instance
60-
*
6154
* @return bool true if this class supports the given template, false otherwise
6255
*/
63-
public function supports($name);
56+
public function supports(string|TemplateReferenceInterface $name);
6457
}

Helper/SlotsHelper.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,9 @@ public function has(string $name)
7171
/**
7272
* Gets the slot value.
7373
*
74-
* @param bool|string $default The default slot content
75-
*
7674
* @return string The slot content
7775
*/
78-
public function get(string $name, $default = false)
76+
public function get(string $name, bool|string $default = false)
7977
{
8078
return $this->slots[$name] ?? $default;
8179
}
@@ -91,11 +89,9 @@ public function set(string $name, string $content)
9189
/**
9290
* Outputs a slot.
9391
*
94-
* @param bool|string $default The default slot content
95-
*
9692
* @return bool true if the slot is defined or if a default content has been provided, false otherwise
9793
*/
98-
public function output(string $name, $default = false)
94+
public function output(string $name, bool|string $default = false)
9995
{
10096
if (!isset($this->slots[$name])) {
10197
if (false !== $default) {

Loader/FilesystemLoader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class FilesystemLoader extends Loader
2727
/**
2828
* @param string|string[] $templatePathPatterns An array of path patterns to look for templates
2929
*/
30-
public function __construct($templatePathPatterns)
30+
public function __construct(string|array $templatePathPatterns)
3131
{
3232
$this->templatePathPatterns = (array) $templatePathPatterns;
3333
}

PhpEngine.php

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public function __construct(TemplateNameParserInterface $parser, LoaderInterface
6363
*
6464
* @throws \InvalidArgumentException if the template does not exist
6565
*/
66-
public function render($name, array $parameters = [])
66+
public function render(string|TemplateReferenceInterface $name, array $parameters = [])
6767
{
6868
$storage = $this->load($name);
6969
$key = hash('sha256', serialize($storage));
@@ -94,7 +94,7 @@ public function render($name, array $parameters = [])
9494
/**
9595
* {@inheritdoc}
9696
*/
97-
public function exists($name)
97+
public function exists(string|TemplateReferenceInterface $name)
9898
{
9999
try {
100100
$this->load($name);
@@ -108,7 +108,7 @@ public function exists($name)
108108
/**
109109
* {@inheritdoc}
110110
*/
111-
public function supports($name)
111+
public function supports(string|TemplateReferenceInterface $name)
112112
{
113113
$template = $this->parser->parse($name);
114114

@@ -165,48 +165,39 @@ protected function evaluate(Storage $template, array $parameters = [])
165165
/**
166166
* Gets a helper value.
167167
*
168-
* @param string $name The helper name
169-
*
170168
* @return HelperInterface The helper value
171169
*
172170
* @throws \InvalidArgumentException if the helper is not defined
173171
*/
174-
public function offsetGet($name)
172+
public function offsetGet(mixed $name)
175173
{
176174
return $this->get($name);
177175
}
178176

179177
/**
180178
* Returns true if the helper is defined.
181179
*
182-
* @param string $name The helper name
183-
*
184180
* @return bool true if the helper is defined, false otherwise
185181
*/
186-
public function offsetExists($name)
182+
public function offsetExists(mixed $name)
187183
{
188184
return isset($this->helpers[$name]);
189185
}
190186

191187
/**
192188
* Sets a helper.
193-
*
194-
* @param HelperInterface $name The helper instance
195-
* @param string $value An alias
196189
*/
197-
public function offsetSet($name, $value)
190+
public function offsetSet(mixed $name, mixed $value)
198191
{
199192
$this->set($name, $value);
200193
}
201194

202195
/**
203196
* Removes a helper.
204197
*
205-
* @param string $name The helper name
206-
*
207198
* @throws \LogicException
208199
*/
209-
public function offsetUnset($name)
200+
public function offsetUnset(mixed $name)
210201
{
211202
throw new \LogicException(sprintf('You can\'t unset a helper (%s).', $name));
212203
}
@@ -272,8 +263,6 @@ public function get(string $name)
272263

273264
/**
274265
* Decorates the current template with another one.
275-
*
276-
* @param string $template The decorator logical name
277266
*/
278267
public function extend(string $template)
279268
{
@@ -283,11 +272,9 @@ public function extend(string $template)
283272
/**
284273
* Escapes a string by using the current charset.
285274
*
286-
* @param mixed $value A variable to escape
287-
*
288275
* @return mixed The escaped value
289276
*/
290-
public function escape($value, string $context = 'html')
277+
public function escape(mixed $value, string $context = 'html')
291278
{
292279
if (is_numeric($value)) {
293280
return $value;
@@ -356,10 +343,7 @@ public function getEscaper(string $context)
356343
return $this->escapers[$context];
357344
}
358345

359-
/**
360-
* @param mixed $value
361-
*/
362-
public function addGlobal(string $name, $value)
346+
public function addGlobal(string $name, mixed $value)
363347
{
364348
$this->globals[$name] = $value;
365349
}
@@ -466,13 +450,11 @@ public function getLoader()
466450
/**
467451
* Loads the given template.
468452
*
469-
* @param string|TemplateReferenceInterface $name A template name or a TemplateReferenceInterface instance
470-
*
471453
* @return Storage A Storage instance
472454
*
473455
* @throws \InvalidArgumentException if the template cannot be found
474456
*/
475-
protected function load($name)
457+
protected function load(string|TemplateReferenceInterface $name)
476458
{
477459
$template = $this->parser->parse($name);
478460

StreamingEngineInterface.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,8 @@ interface StreamingEngineInterface
2323
*
2424
* The implementation should output the content directly to the client.
2525
*
26-
* @param string|TemplateReferenceInterface $name A template name or a TemplateReferenceInterface instance
27-
* @param array $parameters An array of parameters to pass to the template
28-
*
2926
* @throws \RuntimeException if the template cannot be rendered
3027
* @throws \LogicException if the template cannot be streamed
3128
*/
32-
public function stream($name, array $parameters = []);
29+
public function stream(string|TemplateReferenceInterface $name, array $parameters = []);
3330
}

TemplateNameParser.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class TemplateNameParser implements TemplateNameParserInterface
2424
/**
2525
* {@inheritdoc}
2626
*/
27-
public function parse($name)
27+
public function parse(string|TemplateReferenceInterface $name)
2828
{
2929
if ($name instanceof TemplateReferenceInterface) {
3030
return $name;

TemplateNameParserInterface.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@ interface TemplateNameParserInterface
2222
/**
2323
* Convert a template name to a TemplateReferenceInterface instance.
2424
*
25-
* @param string|TemplateReferenceInterface $name A template name or a TemplateReferenceInterface instance
26-
*
2725
* @return TemplateReferenceInterface A template
2826
*/
29-
public function parse($name);
27+
public function parse(string|TemplateReferenceInterface $name);
3028
}

0 commit comments

Comments
 (0)