1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace WyriHaximus\TwigView\Lib; |
5
|
|
|
|
6
|
|
|
use Cake\Core\App; |
7
|
|
|
use Cake\Core\Plugin; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class RelativeScanner. |
11
|
|
|
* @package WyriHaximus\TwigView\Lib |
12
|
|
|
*/ |
13
|
|
|
final class RelativeScanner |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Return all sections (app & plugins) with an Template directory. |
17
|
|
|
* |
18
|
|
|
* @return array |
19
|
|
|
*/ |
20
|
3 |
|
public static function all(): array |
21
|
|
|
{ |
22
|
3 |
|
return static::strip(Scanner::all()); |
|
|
|
|
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Return all templates for a given plugin. |
27
|
|
|
* |
28
|
|
|
* @param string $plugin The plugin to find all templates for. |
29
|
|
|
* |
30
|
|
|
* @return mixed |
31
|
|
|
*/ |
32
|
2 |
|
public static function plugin($plugin) |
33
|
|
|
{ |
34
|
2 |
|
return static::strip([ |
|
|
|
|
35
|
2 |
|
$plugin => Scanner::plugin($plugin), |
36
|
2 |
|
])[$plugin]; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Strip the absolute path of template's paths for all given sections. |
41
|
|
|
* |
42
|
|
|
* @param string $sections Sections to iterate over. |
43
|
|
|
* |
44
|
|
|
* @return array |
45
|
|
|
*/ |
46
|
5 |
|
protected static function strip($sections): array |
47
|
|
|
{ |
48
|
5 |
|
foreach ($sections as $section => $paths) { |
|
|
|
|
49
|
5 |
|
$sections[$section] = static::stripAbsolutePath($paths, $section == 'APP' ? null : $section); |
50
|
|
|
} |
51
|
|
|
|
52
|
5 |
|
return $sections; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Strip the absolute path of template's paths. |
57
|
|
|
* |
58
|
|
|
* @param array $paths Paths to strip. |
59
|
|
|
* @param string|null $plugin Hold plugin name or null for App. |
60
|
|
|
* |
61
|
|
|
* @return array |
62
|
|
|
*/ |
63
|
5 |
|
protected static function stripAbsolutePath(array $paths, $plugin = null): array |
64
|
|
|
{ |
65
|
5 |
|
if ($plugin === null) { |
66
|
3 |
|
$allPaths = App::path('templates'); |
67
|
|
|
} else { |
68
|
4 |
|
$allPaths = [Plugin::templatePath($plugin)]; |
69
|
|
|
} |
70
|
|
|
|
71
|
5 |
|
foreach ($allPaths as $templatesPath) { |
72
|
|
|
array_walk($paths, function (&$path) use ($templatesPath) { |
73
|
5 |
|
if (substr($path, 0, strlen($templatesPath)) == $templatesPath) { |
74
|
5 |
|
$path = substr($path, strlen($templatesPath)); |
75
|
|
|
} |
76
|
5 |
|
}); |
77
|
|
|
} |
78
|
|
|
|
79
|
5 |
|
return $paths; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: