1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Zikula package. |
7
|
|
|
* |
8
|
|
|
* Copyright Zikula Foundation - https://ziku.la/ |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Zikula\Bundle\CoreBundle\Maker; |
15
|
|
|
|
16
|
|
|
use InvalidArgumentException; |
17
|
|
|
|
18
|
|
|
class Validators |
19
|
|
|
{ |
20
|
|
|
public static function validateBundleNamespace(string $namespace): string |
21
|
|
|
{ |
22
|
|
|
$namespace = trim(str_replace('/', '\\', $namespace)); |
23
|
|
|
if (!preg_match('/^(?:[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*\\\?)+$/', $namespace)) { |
24
|
|
|
throw new InvalidArgumentException('The namespace contains invalid characters.'); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
// validate reserved keywords |
28
|
|
|
$reserved = self::getReservedWords(); |
29
|
|
|
foreach (explode('\\', $namespace) as $word) { |
30
|
|
|
if (in_array(mb_strtolower($word), $reserved, true)) { |
31
|
|
|
throw new InvalidArgumentException(sprintf('The namespace cannot contain reserved words ("%s").', $word)); |
32
|
|
|
} |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
foreach (['module', 'theme', 'bundle'] as $word) { |
36
|
|
|
if (false !== mb_strpos(mb_strtolower($namespace), $word)) { |
37
|
|
|
throw new InvalidArgumentException(sprintf('The namespace cannot contain "%s".', $word)); |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
// validate that the namespace is at least one level deep |
42
|
|
|
if (false === mb_strpos($namespace, '\\')) { |
43
|
|
|
throw new InvalidArgumentException(sprintf('The namespace must contain a vendor namespace (e.g. "VendorName\%s" instead of simply "%s").', $namespace, $namespace)); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
if (mb_substr_count($namespace, '\\') > 1) { |
47
|
|
|
throw new InvalidArgumentException(sprintf('The namespace must contain only a vendor and BundleName (e.g. "VendorName\BundleName" instead of "%s").', $namespace)); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
return $namespace; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public static function getReservedWords(): array |
54
|
|
|
{ |
55
|
|
|
return [ |
56
|
|
|
'abstract', |
57
|
|
|
'and', |
58
|
|
|
'array', |
59
|
|
|
'as', |
60
|
|
|
'break', |
61
|
|
|
'case', |
62
|
|
|
'catch', |
63
|
|
|
'class', |
64
|
|
|
'clone', |
65
|
|
|
'const', |
66
|
|
|
'continue', |
67
|
|
|
'declare', |
68
|
|
|
'default', |
69
|
|
|
'do', |
70
|
|
|
'else', |
71
|
|
|
'elseif', |
72
|
|
|
'enddeclare', |
73
|
|
|
'endfor', |
74
|
|
|
'endforeach', |
75
|
|
|
'endif', |
76
|
|
|
'endswitch', |
77
|
|
|
'endwhile', |
78
|
|
|
'extends', |
79
|
|
|
'final', |
80
|
|
|
'for', |
81
|
|
|
'foreach', |
82
|
|
|
'function', |
83
|
|
|
'global', |
84
|
|
|
'goto', |
85
|
|
|
'if', |
86
|
|
|
'implements', |
87
|
|
|
'interface', |
88
|
|
|
'instanceof', |
89
|
|
|
'namespace', |
90
|
|
|
'new', |
91
|
|
|
'or', |
92
|
|
|
'private', |
93
|
|
|
'protected', |
94
|
|
|
'public', |
95
|
|
|
'static', |
96
|
|
|
'switch', |
97
|
|
|
'throw', |
98
|
|
|
'try', |
99
|
|
|
'use', |
100
|
|
|
'var', |
101
|
|
|
'while', |
102
|
|
|
'xor', |
103
|
|
|
'__CLASS__', |
104
|
|
|
'__DIR__', |
105
|
|
|
'__FILE__', |
106
|
|
|
'__LINE__', |
107
|
|
|
'__FUNCTION__', |
108
|
|
|
'__METHOD__', |
109
|
|
|
'__NAMESPACE__', |
110
|
|
|
'die', |
111
|
|
|
'echo', |
112
|
|
|
'empty', |
113
|
|
|
'exit', |
114
|
|
|
'eval', |
115
|
|
|
'include', |
116
|
|
|
'include_once', |
117
|
|
|
'isset', |
118
|
|
|
'list', |
119
|
|
|
'require', |
120
|
|
|
'require_once', |
121
|
|
|
'return', |
122
|
|
|
'print', |
123
|
|
|
'unset' |
124
|
|
|
]; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|