port_entities 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #!/bin/php
  2. <?php
  3. define('INSTALLDIR', dirname(__DIR__));
  4. require INSTALLDIR . '/vendor/autoload.php';
  5. use App\Util\Common;
  6. error_reporting(E_ALL | E_STRICT);
  7. $delete = array_merge(glob(INSTALLDIR . '/src/Entity/*DataObject.php'),
  8. glob(INSTALLDIR . '/src/Entity/Status_network*'));
  9. foreach ($delete as $d) {
  10. system("git rm {$d}");
  11. }
  12. $files = glob(INSTALLDIR . '/src/Entity/*.php');
  13. class CatchAll
  14. {
  15. const foo = null;
  16. public function foo()
  17. {
  18. }
  19. }
  20. $classes = [];
  21. foreach ($files as $f) {
  22. system('sed -ri "' . implode('; ', [
  23. 's/(extends )?((DB|GS|Safe|Managed|Memcached)' .
  24. '_DataObject|CachingNoticeStream)/\1CatchAll/g',
  25. 's/(CatchAll::)[A-Za-z_0-9]*/\1foo/g',
  26. 's/.*(exit|die).*//g',
  27. 's/require[^sd ].*//g',
  28. ]) . '" ' . $f);
  29. require $f;
  30. $cls = get_declared_classes();
  31. $classes[] = end($cls);
  32. }
  33. $license_header = '
  34. /* {{{ License
  35. * This file is part of GNU social - https://www.gnu.org/software/social
  36. *
  37. * GNU social is free software: you can redistribute it and/or modify
  38. * it under the terms of the GNU Affero General Public License as published by
  39. * the Free Software Foundation, either version 3 of the License, or
  40. * (at your option) any later version.
  41. *
  42. * GNU social is distributed in the hope that it will be useful,
  43. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  44. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  45. * GNU Affero General Public License for more details.
  46. *
  47. * You should have received a copy of the GNU Affero General Public License
  48. * along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  49. */
  50. ';
  51. foreach ($classes as $cls) {
  52. $ref = new ReflectionClass($cls);
  53. $class_name = Common::snakeCaseToCamelCase($cls);
  54. $file = $ref->getFileName();
  55. $class_comment = $ref->getDocComment();
  56. $table_name = $ref->getDefaultProperties()['__table'];
  57. $sd = $ref->getMethod('schemaDef');
  58. $start = $sd->getStartLine();
  59. $end = $sd->getEndLine();
  60. $command = "sed -rn '" . implode('; ',
  61. [
  62. "s%(return array\\()%\\1\\
  63. \"name\" => \"{$table_name}\",%",
  64. 's%schemaDef\(\)%schemaDef(): array%',
  65. "{$start},{$end}p",
  66. ]) . "' {$file}";
  67. // echo $command . "\n";
  68. $schemaDef = [];
  69. exec($command, $schemaDef);
  70. $schemaDef = implode("\n", $schemaDef);
  71. $class = "<?php
  72. {$license_header}
  73. namespace App\\Entity;
  74. {$class_comment}
  75. class {$class_name}
  76. {
  77. // AUTOCODE BEGIN
  78. // AUTOCODE END
  79. {$schemaDef}
  80. }";
  81. $new_file = dirname($file) . '/' . $class_name . '.php';
  82. file_put_contents($file, $class);
  83. if ($file !== $new_file) {
  84. system("git mv {$file} {$new_file}");
  85. }
  86. system("git add {$new_file}");
  87. }