-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBladeTemplateExtractor.php
More file actions
107 lines (80 loc) · 3.45 KB
/
BladeTemplateExtractor.php
File metadata and controls
107 lines (80 loc) · 3.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<?php declare(strict_types=1);
namespace IxDFCodingStandard\Sniffs\Laravel;
use BadMethodCallException;
/** phpcs:disable IxDFCodingStandard.Laravel.NonExistingBladeTemplate.TemplateNotFound */
final class BladeTemplateExtractor
{
private const INVALID_METHOD_CALL = 'Invalid method call';
// @include, @component, @extends
private const INCLUDE_BLADE_DIRECTIVE = '/@(include|component|extends)\(\'([^\']++)\'/';
// @includeIf, @includeWhen
private const CONDITIONAL_INCLUDE_BLADE_DIRECTIVE = '/@(includeIf|includeWhen)\([^,]++,\s*+\'([^\']++)\'/';
// @includeFirst, @componentFirst - takes array parameter: @includeFirst(['view1', 'view2'])
private const FIRST_BLADE_DIRECTIVE = '/@(includeFirst|componentFirst)\(\s*+\[\s*+\'([^\']++)\'/';
// @each directive: @each('view.name', $items, 'item', 'empty.view')
private const EACH_BLADE_DIRECTIVE = '/@each\(\s*+\'([^\']++)\'/';
public function isBladeIncludeDirective(string $tokenContent): bool
{
return preg_match(self::INCLUDE_BLADE_DIRECTIVE, $tokenContent) === 1;
}
public function isConditionalBladeIncludeDirective(string $tokenContent): bool
{
return preg_match(self::CONDITIONAL_INCLUDE_BLADE_DIRECTIVE, $tokenContent) === 1;
}
public function isEachBladeDirective(string $tokenContent): bool
{
return preg_match(self::EACH_BLADE_DIRECTIVE, $tokenContent) === 1;
}
public function isFirstBladeDirective(string $tokenContent): bool
{
return preg_match(self::FIRST_BLADE_DIRECTIVE, $tokenContent) === 1;
}
public function getBladeTemplateName(string $tokenContent): string
{
if (!$this->isBladeIncludeDirective($tokenContent)) {
throw new BadMethodCallException(self::INVALID_METHOD_CALL);
}
$matches = [];
preg_match(self::INCLUDE_BLADE_DIRECTIVE, $tokenContent, $matches);
if (!isset($matches[2])) {
throw new BadMethodCallException(self::INVALID_METHOD_CALL);
}
return $matches[2];
}
public function getConditionalBladeTemplateName(string $tokenContent): string
{
if (!$this->isConditionalBladeIncludeDirective($tokenContent)) {
throw new BadMethodCallException(self::INVALID_METHOD_CALL);
}
$matches = [];
preg_match(self::CONDITIONAL_INCLUDE_BLADE_DIRECTIVE, $tokenContent, $matches);
if (!isset($matches[2])) {
throw new BadMethodCallException(self::INVALID_METHOD_CALL);
}
return $matches[2];
}
public function getEachBladeTemplateName(string $tokenContent): string
{
if (!$this->isEachBladeDirective($tokenContent)) {
throw new BadMethodCallException(self::INVALID_METHOD_CALL);
}
$matches = [];
preg_match(self::EACH_BLADE_DIRECTIVE, $tokenContent, $matches);
if (!isset($matches[1])) {
throw new BadMethodCallException(self::INVALID_METHOD_CALL);
}
return $matches[1];
}
public function getFirstBladeTemplateName(string $tokenContent): string
{
if (!$this->isFirstBladeDirective($tokenContent)) {
throw new BadMethodCallException(self::INVALID_METHOD_CALL);
}
$matches = [];
preg_match(self::FIRST_BLADE_DIRECTIVE, $tokenContent, $matches);
if (!isset($matches[2])) {
throw new BadMethodCallException(self::INVALID_METHOD_CALL);
}
return $matches[2];
}
}