|
0
|
1 |
<?php |
|
|
2 |
|
|
|
3 |
/* |
|
|
4 |
* This file is part of the Symfony package. |
|
|
5 |
* |
|
|
6 |
* (c) Fabien Potencier <fabien@symfony.com> |
|
|
7 |
* |
|
|
8 |
* For the full copyright and license information, please view the LICENSE |
|
|
9 |
* file that was distributed with this source code. |
|
|
10 |
*/ |
|
|
11 |
|
|
|
12 |
namespace Sensio\Bundle\GeneratorBundle\Command; |
|
|
13 |
|
|
|
14 |
/** |
|
|
15 |
* Validator functions. |
|
|
16 |
* |
|
|
17 |
* @author Fabien Potencier <fabien@symfony.com> |
|
|
18 |
*/ |
|
|
19 |
class Validators |
|
|
20 |
{ |
|
|
21 |
static public function validateBundleNamespace($namespace) |
|
|
22 |
{ |
|
|
23 |
if (!preg_match('/Bundle$/', $namespace)) { |
|
|
24 |
throw new \InvalidArgumentException('The namespace must end with Bundle.'); |
|
|
25 |
} |
|
|
26 |
|
|
|
27 |
$namespace = strtr($namespace, '/', '\\'); |
|
|
28 |
if (!preg_match('/^(?:[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*\\\?)+$/', $namespace)) { |
|
|
29 |
throw new \InvalidArgumentException('The namespace contains invalid characters.'); |
|
|
30 |
} |
|
|
31 |
|
|
|
32 |
// validate reserved keywords |
|
|
33 |
$reserved = self::getReservedWords(); |
|
|
34 |
foreach (explode('\\', $namespace) as $word) { |
|
|
35 |
if (in_array(strtolower($word), $reserved)) { |
|
|
36 |
throw new \InvalidArgumentException(sprintf('The namespace cannot contain PHP reserved words ("%s").', $word)); |
|
|
37 |
} |
|
|
38 |
} |
|
|
39 |
|
|
|
40 |
// validate that the namespace is at least one level deep |
|
|
41 |
if (false === strpos($namespace, '\\')) { |
|
|
42 |
$msg = array(); |
|
|
43 |
$msg[] = sprintf('The namespace must contain a vendor namespace (e.g. "VendorName\%s" instead of simply "%s").', $namespace, $namespace); |
|
|
44 |
$msg[] = 'If you\'ve specified a vendor namespace, did you forget to surround it with quotes (init:bundle "Acme\BlogBundle")?'; |
|
|
45 |
|
|
|
46 |
throw new \InvalidArgumentException(implode("\n\n", $msg)); |
|
|
47 |
} |
|
|
48 |
|
|
|
49 |
return $namespace; |
|
|
50 |
} |
|
|
51 |
|
|
|
52 |
static public function validateBundleName($bundle) |
|
|
53 |
{ |
|
|
54 |
if (!preg_match('/Bundle$/', $bundle)) { |
|
|
55 |
throw new \InvalidArgumentException('The bundle name must end with Bundle.'); |
|
|
56 |
} |
|
|
57 |
|
|
|
58 |
return $bundle; |
|
|
59 |
} |
|
|
60 |
|
|
|
61 |
static public function validateTargetDir($dir, $bundle, $namespace) |
|
|
62 |
{ |
|
|
63 |
// add trailing / if necessary |
|
|
64 |
return '/' === substr($dir, -1, 1) ? $dir : $dir.'/'; |
|
|
65 |
} |
|
|
66 |
|
|
|
67 |
static public function validateFormat($format) |
|
|
68 |
{ |
|
|
69 |
$format = strtolower($format); |
|
|
70 |
|
|
|
71 |
if (!in_array($format, array('php', 'xml', 'yml', 'annotation'))) { |
|
|
72 |
throw new \RuntimeException(sprintf('Format "%s" is not supported.', $format)); |
|
|
73 |
} |
|
|
74 |
|
|
|
75 |
return $format; |
|
|
76 |
} |
|
|
77 |
|
|
|
78 |
static public function validateEntityName($entity) |
|
|
79 |
{ |
|
|
80 |
if (false === $pos = strpos($entity, ':')) { |
|
|
81 |
throw new \InvalidArgumentException(sprintf('The entity name must contain a : ("%s" given, expecting something like AcmeBlogBundle:Blog/Post)', $entity)); |
|
|
82 |
} |
|
|
83 |
|
|
|
84 |
return $entity; |
|
|
85 |
} |
|
|
86 |
|
|
|
87 |
static public function getReservedWords() |
|
|
88 |
{ |
|
|
89 |
return array( |
|
|
90 |
'abstract', |
|
|
91 |
'and', |
|
|
92 |
'array', |
|
|
93 |
'as', |
|
|
94 |
'break', |
|
|
95 |
'case', |
|
|
96 |
'catch', |
|
|
97 |
'class', |
|
|
98 |
'clone', |
|
|
99 |
'const', |
|
|
100 |
'continue', |
|
|
101 |
'declare', |
|
|
102 |
'default', |
|
|
103 |
'do', |
|
|
104 |
'else', |
|
|
105 |
'elseif', |
|
|
106 |
'enddeclare', |
|
|
107 |
'endfor', |
|
|
108 |
'endforeach', |
|
|
109 |
'endif', |
|
|
110 |
'endswitch', |
|
|
111 |
'endwhile', |
|
|
112 |
'extends', |
|
|
113 |
'final', |
|
|
114 |
'for', |
|
|
115 |
'foreach', |
|
|
116 |
'function', |
|
|
117 |
'global', |
|
|
118 |
'goto', |
|
|
119 |
'if', |
|
|
120 |
'implements', |
|
|
121 |
'interface', |
|
|
122 |
'instanceof', |
|
|
123 |
'namespace', |
|
|
124 |
'new', |
|
|
125 |
'or', |
|
|
126 |
'private', |
|
|
127 |
'protected', |
|
|
128 |
'public', |
|
|
129 |
'static', |
|
|
130 |
'switch', |
|
|
131 |
'throw', |
|
|
132 |
'try', |
|
|
133 |
'use', |
|
|
134 |
'var', |
|
|
135 |
'while', |
|
|
136 |
'xor', |
|
|
137 |
'__CLASS__', |
|
|
138 |
'__DIR__', |
|
|
139 |
'__FILE__', |
|
|
140 |
'__LINE__', |
|
|
141 |
'__FUNCTION__', |
|
|
142 |
'__METHOD__', |
|
|
143 |
'__NAMESPACE__', |
|
|
144 |
'die', |
|
|
145 |
'echo', |
|
|
146 |
'empty', |
|
|
147 |
'exit', |
|
|
148 |
'eval', |
|
|
149 |
'include', |
|
|
150 |
'include_once', |
|
|
151 |
'isset', |
|
|
152 |
'list', |
|
|
153 |
'require', |
|
|
154 |
'require_once', |
|
|
155 |
'return', |
|
|
156 |
'print', |
|
|
157 |
'unset', |
|
|
158 |
); |
|
|
159 |
} |
|
|
160 |
} |