截流自动化的商城平台
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

Helpers.php 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. /**
  3. * This file is part of the Nette Framework (https://nette.org)
  4. * Copyright (c) 2004 David Grudl (https://davidgrudl.com)
  5. */
  6. declare(strict_types=1);
  7. namespace Nette\PhpGenerator;
  8. use Nette;
  9. /**
  10. * @internal
  11. */
  12. final class Helpers
  13. {
  14. use Nette\StaticClass;
  15. public const PHP_IDENT = '[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*';
  16. /** @deprecated use Nette\PhpGenerator\Dumper::dump() */
  17. public static function dump($var): string
  18. {
  19. return (new Dumper)->dump($var);
  20. }
  21. /** @deprecated use Nette\PhpGenerator\Dumper::format() */
  22. public static function format(string $statement, ...$args): string
  23. {
  24. return (new Dumper)->format($statement, ...$args);
  25. }
  26. /** @deprecated use Nette\PhpGenerator\Dumper::format() */
  27. public static function formatArgs(string $statement, array $args): string
  28. {
  29. return (new Dumper)->format($statement, ...$args);
  30. }
  31. public static function formatDocComment(string $content): string
  32. {
  33. if (($s = trim($content)) === '') {
  34. return '';
  35. } elseif (strpos($content, "\n") === false) {
  36. return "/** $s */\n";
  37. } else {
  38. return str_replace("\n", "\n * ", "/**\n$s") . "\n */\n";
  39. }
  40. }
  41. public static function unformatDocComment(string $comment): string
  42. {
  43. return preg_replace('#^\s*\* ?#m', '', trim(trim(trim($comment), '/*')));
  44. }
  45. public static function unindent(string $s, int $level = 1): string
  46. {
  47. return preg_replace('#^(\t|\ \ \ \ ){1,' . $level . '}#m', '', $s);
  48. }
  49. public static function isIdentifier($value): bool
  50. {
  51. return is_string($value) && preg_match('#^' . self::PHP_IDENT . '$#D', $value);
  52. }
  53. public static function isNamespaceIdentifier($value, bool $allowLeadingSlash = false): bool
  54. {
  55. $re = '#^' . ($allowLeadingSlash ? '\\\\?' : '') . self::PHP_IDENT . '(\\\\' . self::PHP_IDENT . ')*$#D';
  56. return is_string($value) && preg_match($re, $value);
  57. }
  58. public static function extractNamespace(string $name): string
  59. {
  60. return ($pos = strrpos($name, '\\')) ? substr($name, 0, $pos) : '';
  61. }
  62. public static function extractShortName(string $name): string
  63. {
  64. return ($pos = strrpos($name, '\\')) === false
  65. ? $name
  66. : substr($name, $pos + 1);
  67. }
  68. public static function tabsToSpaces(string $s, int $count = 4): string
  69. {
  70. return str_replace("\t", str_repeat(' ', $count), $s);
  71. }
  72. /** @internal */
  73. public static function createObject(string $class, array $props)
  74. {
  75. return Dumper::createObject($class, $props);
  76. }
  77. }