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

/modules/civicrm/vendor/xkerman/restricted-unserialize/bin/generate.php

https://github.com/nysenate/Bluebird-CRM
PHP | 173 lines | 152 code | 19 blank | 2 comment | 7 complexity | 43aba4076fe1cb42cd7224ce51c6cc31 MD5 | raw file
Possible License(s): JSON, BSD-3-Clause, MPL-2.0-no-copyleft-exception, AGPL-1.0, GPL-2.0, AGPL-3.0, Apache-2.0, MIT, GPL-3.0, CC-BY-4.0, LGPL-2.1, BSD-2-Clause, LGPL-3.0
  1. <?php
  2. // see: https://github.com/nikic/PHP-Parser/blob/master/doc/2_Usage_of_basic_components.markdown
  3. require __DIR__ . '/../vendor/autoload.php';
  4. use PhpParser\BuilderFactory;
  5. use PhpParser\Comment;
  6. use PhpParser\Node;
  7. use PhpParser\Node\Expr;
  8. use PhpParser\Node\Stmt;
  9. use PhpParser\NodeTraverser;
  10. use PhpParser\NodeVisitor\NameResolver;
  11. use PhpParser\ParserFactory;
  12. use PhpParser\PrettyPrinter;
  13. class NameSpaceConverter extends \PhpParser\NodeVisitorAbstract
  14. {
  15. public function leaveNode(Node $node) {
  16. if ($node instanceof Node\Name) {
  17. return new Node\Name(str_replace('\\', '_', $node->toString()));
  18. }
  19. if ($node instanceof Stmt\Class_ ||
  20. $node instanceof Stmt\Interface_ ||
  21. $node instanceof Stmt\Function_) {
  22. $node->name = str_replace('\\', '_', $node->namespacedName->toString());
  23. }
  24. if ($node instanceof Stmt\Const_) {
  25. foreach ($node->consts as $const) {
  26. $const->name = str_replace('\\', '_', $const->namespacedName->toString());
  27. }
  28. }
  29. if ($node instanceof Stmt\Namespace_) {
  30. return $node->stmts;
  31. }
  32. if ($node instanceof Stmt\Use_) {
  33. return NodeTraverser::REMOVE_NODE;
  34. }
  35. if ($node instanceof Stmt\ClassMethod) {
  36. $doc = $node->getDocComment();
  37. if (is_null($doc)) {
  38. return $node;
  39. }
  40. $text = preg_replace('/\\\\xKerman\\\\Restricted\\\\([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff])/', 'xKerman_Restricted_$1', $doc->getText());
  41. $text = preg_replace('/ @covers ::(?:[a-zA-Z_][a-zA-Z0-9_]*)/', '', $text);
  42. $search = [
  43. '\InvalidArgumentException',
  44. ];
  45. $replace = [
  46. 'InvalidArgumentException',
  47. ];
  48. $newDoc = new Comment\Doc(
  49. str_replace($search, $replace, $text),
  50. $doc->getLine(),
  51. $doc->getFilePos()
  52. );
  53. $node->setAttribute('comments', [$newDoc]);
  54. }
  55. if ($node instanceof Expr\MethodCall) {
  56. if ($node->name->toString() === 'expectException' && $node->args[0]->value instanceof Node\Scalar\String_) {
  57. $newName = substr(str_replace('\\', '_', $node->args[0]->value->value), 1);
  58. $node->args[0] = new Node\Arg(
  59. new Node\Scalar\String_($newName)
  60. );
  61. }
  62. }
  63. }
  64. }
  65. function convert($inDir, $outDir)
  66. {
  67. $factory = new ParserFactory();
  68. $parser = $factory->create(ParserFactory::ONLY_PHP5);
  69. $traverser = new NodeTraverser();
  70. $printer = new PrettyPrinter\Standard();
  71. $traverser->addVisitor(new NameResolver());
  72. $traverser->addVisitor(new NameSpaceConverter());
  73. $files = new \RecursiveIteratorIterator(new RecursiveDirectoryIterator($inDir));
  74. $files = new \RegexIterator($files, '/\.php\z/');
  75. if (!file_exists($outDir)) {
  76. mkdir($outDir, 0755, true);
  77. }
  78. foreach ($files as $file) {
  79. try {
  80. $code = file_get_contents($file);
  81. $statements = $parser->parse($code);
  82. $statements = $traverser->traverse($statements);
  83. $sep = DIRECTORY_SEPARATOR;
  84. file_put_contents(
  85. "{$outDir}{$sep}{$file->getFileName()}",
  86. $printer->prettyPrintFile($statements)
  87. );
  88. } catch (PhpParser\Error $e) {
  89. echo 'Parsee Error: ', $e->getMessage();
  90. }
  91. }
  92. }
  93. function generateBootstrap($dir)
  94. {
  95. $code = <<<'PHPCODE'
  96. <?php
  97. function xKerman_Restricted_bootstrap($classname)
  98. {
  99. if (strpos($classname, 'xKerman_Restricted_') !== 0) {
  100. return false;
  101. }
  102. $sep = DIRECTORY_SEPARATOR;
  103. $namespace = explode('_', $classname);
  104. $filename = array_pop($namespace);
  105. $path = dirname(__FILE__) . "{$sep}{$filename}.php";
  106. if (file_exists($path)) {
  107. require_once $path;
  108. }
  109. }
  110. spl_autoload_register('xKerman_Restricted_bootstrap');
  111. $sep = DIRECTORY_SEPARATOR;
  112. require_once dirname(__FILE__) . "{$sep}function.php";
  113. PHPCODE;
  114. $sep = DIRECTORY_SEPARATOR;
  115. file_put_contents(
  116. "{$dir}{$sep}bootstrap.php",
  117. $code
  118. );
  119. }
  120. function generateBootstrapForTest($dir, $bootstrap)
  121. {
  122. $code = <<<'PHPCODE'
  123. <?php
  124. function xKerman_Restricted_Test_bootstrap($classname)
  125. {
  126. if (strpos($classname, 'xKerman_Restricted_Test') !== 0) {
  127. return false;
  128. }
  129. $sep = DIRECTORY_SEPARATOR;
  130. $namespace = explode('_', $classname);
  131. $filename = array_pop($namespace);
  132. $path = dirname(__FILE__) . "{$sep}{$filename}.php";
  133. if (file_exists($path)) {
  134. require_once $path;
  135. }
  136. }
  137. $sep = DIRECTORY_SEPARATOR;
  138. require_once %s;
  139. spl_autoload_register('xKerman_Restricted_Test_bootstrap');
  140. PHPCODE;
  141. $sep = DIRECTORY_SEPARATOR;
  142. file_put_contents(
  143. "{$dir}{$sep}bootstrap.test.php",
  144. sprintf($code, $bootstrap),
  145. );
  146. }
  147. // main
  148. convert(__DIR__ . '/../src', __DIR__ . '/../generated/src/xKerman/Restricted');
  149. convert(__DIR__ . '/../test', __DIR__ . '/../generated/test/xKerman/Restricted');
  150. generateBootstrap(__DIR__ . '/../generated/src/xKerman/Restricted');
  151. $bootstrap = 'dirname(dirname(dirname(dirname(__FILE__)))) . "{$sep}src{$sep}xKerman{$sep}Restricted{$sep}bootstrap.php"';
  152. generateBootstrapForTest(__DIR__ . '/../generated/test/xKerman/Restricted', $bootstrap);