PageRenderTime 43ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/sensio/generator-bundle/Sensio/Bundle/GeneratorBundle/Command/Validators.php

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