laravel-workers 0.4.0 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,158 +1,158 @@
1
- <?php
2
-
3
- require_once __DIR__ . '/_root.php';
4
- $root = lw_app_root();
5
-
6
- require lw_vendor_autoload();
7
-
8
- $app = require $root . '/bootstrap/app.php';
9
-
10
- $kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);
11
- $request = Illuminate\Http\Request::create('/', 'GET');
12
- $response = $kernel->handle($request);
13
- $kernel->terminate($request, $response);
14
-
15
- use Illuminate\Support\Facades\DB;
16
-
17
- $escape = static function (string $name): string {
18
- return str_replace("'", "''", $name);
19
- };
20
-
21
- $parseColumns = static function (string $sql): array {
22
- if (!preg_match('/\((.*)\)/s', $sql, $match)) {
23
- return [];
24
- }
25
- $body = $match[1];
26
- $parts = [];
27
- $depth = 0;
28
- $buf = '';
29
- for ($i = 0; $i < strlen($body); $i++) {
30
- $ch = $body[$i];
31
- if ($ch === '(') {
32
- $depth++;
33
- $buf .= $ch;
34
- } elseif ($ch === ')') {
35
- $depth--;
36
- $buf .= $ch;
37
- } elseif ($ch === ',' && $depth === 0) {
38
- $parts[] = trim($buf);
39
- $buf = '';
40
- } else {
41
- $buf .= $ch;
42
- }
43
- }
44
- if (trim($buf) !== '') {
45
- $parts[] = trim($buf);
46
- }
47
-
48
- $columns = [];
49
- foreach ($parts as $part) {
50
- if (preg_match('/^(PRIMARY|FOREIGN|UNIQUE|CHECK|CONSTRAINT)\b/i', $part)) {
51
- continue;
52
- }
53
- if (preg_match('/^"([^"]+)"\s+([A-Za-z]+)/', $part, $m) || preg_match('/^`([^`]+)`\s+([A-Za-z]+)/', $part, $m) || preg_match('/^\[([^\]]+)\]\s+([A-Za-z]+)/', $part, $m) || preg_match('/^([A-Za-z_][A-Za-z0-9_]*)\s+([A-Za-z]+)/', $part, $m)) {
54
- $columns[] = [
55
- 'cid' => count($columns),
56
- 'name' => $m[1],
57
- 'type' => strtoupper($m[2]),
58
- 'notnull' => (bool) preg_match('/\bNOT\s+NULL\b/i', $part),
59
- 'default' => null,
60
- 'pk' => (bool) preg_match('/\bPRIMARY\s+KEY\b/i', $part),
61
- ];
62
- }
63
- }
64
- return $columns;
65
- };
66
-
67
- $tables = DB::select("SELECT name, sql FROM sqlite_master WHERE type = 'table' AND name NOT LIKE 'sqlite_%' ORDER BY name");
68
-
69
- $out = ['tables' => []];
70
-
71
- foreach ($tables as $table) {
72
- $name = (string) $table->name;
73
- $escaped = $escape($name);
74
-
75
- $columns = [];
76
- try {
77
- foreach (DB::select("SELECT * FROM pragma_table_info('$escaped')") as $column) {
78
- $columns[] = [
79
- 'cid' => (int) $column->cid,
80
- 'name' => (string) $column->name,
81
- 'type' => (string) $column->type,
82
- 'notnull' => (bool) $column->notnull,
83
- 'default' => $column->dflt_value,
84
- 'pk' => (bool) $column->pk,
85
- ];
86
- }
87
- } catch (Throwable $e) {
88
- $columns = $parseColumns((string) $table->sql);
89
- }
90
- if (!$columns) {
91
- $columns = $parseColumns((string) $table->sql);
92
- }
93
-
94
- $indexes = [];
95
- try {
96
- foreach (DB::select("SELECT * FROM pragma_index_list('$escaped')") as $index) {
97
- $indexName = (string) $index->name;
98
- $indexColumns = [];
99
- try {
100
- foreach (DB::select("SELECT * FROM pragma_index_info('" . $escape($indexName) . "')") as $info) {
101
- $indexColumns[] = (string) $info->name;
102
- }
103
- } catch (Throwable $e) {
104
- }
105
- $indexes[] = [
106
- 'name' => $indexName,
107
- 'unique' => (bool) $index->unique,
108
- 'origin' => (string) ($index->origin ?? ''),
109
- 'partial' => (bool) ($index->partial ?? false),
110
- 'columns' => $indexColumns,
111
- ];
112
- }
113
- } catch (Throwable $e) {
114
- }
115
-
116
- $foreignKeys = [];
117
- try {
118
- foreach (DB::select("SELECT * FROM pragma_foreign_key_list('$escaped')") as $fk) {
119
- $foreignKeys[] = [
120
- 'id' => (int) $fk->id,
121
- 'seq' => (int) $fk->seq,
122
- 'table' => (string) $fk->table,
123
- 'from' => (string) $fk->from,
124
- 'to' => (string) $fk->to,
125
- 'on_update' => (string) $fk->on_update,
126
- 'on_delete' => (string) $fk->on_delete,
127
- 'match' => (string) $fk->match,
128
- ];
129
- }
130
- } catch (Throwable $e) {
131
- }
132
-
133
- $out['tables'][] = [
134
- 'name' => $name,
135
- 'sql' => (string) $table->sql,
136
- 'columns' => $columns,
137
- 'indexes' => $indexes,
138
- 'foreign_keys' => $foreignKeys,
139
- ];
140
- }
141
-
142
- $json = json_encode($out, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
143
-
144
- if ($json === false) {
145
- fwrite(STDERR, "schema-dump: JSON encode failed\n");
146
- exit(1);
147
- }
148
-
149
- $outputPath = $argv[1] ?? null;
150
- if ($outputPath !== null) {
151
- $dir = dirname($outputPath);
152
- if (!is_dir($dir)) {
153
- mkdir($dir, 0777, true);
154
- }
155
- file_put_contents($outputPath, $json . "\n");
156
- } else {
157
- echo $json . "\n";
158
- }
1
+ <?php
2
+
3
+ require_once __DIR__ . '/_root.php';
4
+ $root = lw_app_root();
5
+
6
+ require lw_vendor_autoload();
7
+
8
+ $app = require $root . '/bootstrap/app.php';
9
+
10
+ $kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);
11
+ $request = Illuminate\Http\Request::create('/', 'GET');
12
+ $response = $kernel->handle($request);
13
+ $kernel->terminate($request, $response);
14
+
15
+ use Illuminate\Support\Facades\DB;
16
+
17
+ $escape = static function (string $name): string {
18
+ return str_replace("'", "''", $name);
19
+ };
20
+
21
+ $parseColumns = static function (string $sql): array {
22
+ if (!preg_match('/\((.*)\)/s', $sql, $match)) {
23
+ return [];
24
+ }
25
+ $body = $match[1];
26
+ $parts = [];
27
+ $depth = 0;
28
+ $buf = '';
29
+ for ($i = 0; $i < strlen($body); $i++) {
30
+ $ch = $body[$i];
31
+ if ($ch === '(') {
32
+ $depth++;
33
+ $buf .= $ch;
34
+ } elseif ($ch === ')') {
35
+ $depth--;
36
+ $buf .= $ch;
37
+ } elseif ($ch === ',' && $depth === 0) {
38
+ $parts[] = trim($buf);
39
+ $buf = '';
40
+ } else {
41
+ $buf .= $ch;
42
+ }
43
+ }
44
+ if (trim($buf) !== '') {
45
+ $parts[] = trim($buf);
46
+ }
47
+
48
+ $columns = [];
49
+ foreach ($parts as $part) {
50
+ if (preg_match('/^(PRIMARY|FOREIGN|UNIQUE|CHECK|CONSTRAINT)\b/i', $part)) {
51
+ continue;
52
+ }
53
+ if (preg_match('/^"([^"]+)"\s+([A-Za-z]+)/', $part, $m) || preg_match('/^`([^`]+)`\s+([A-Za-z]+)/', $part, $m) || preg_match('/^\[([^\]]+)\]\s+([A-Za-z]+)/', $part, $m) || preg_match('/^([A-Za-z_][A-Za-z0-9_]*)\s+([A-Za-z]+)/', $part, $m)) {
54
+ $columns[] = [
55
+ 'cid' => count($columns),
56
+ 'name' => $m[1],
57
+ 'type' => strtoupper($m[2]),
58
+ 'notnull' => (bool) preg_match('/\bNOT\s+NULL\b/i', $part),
59
+ 'default' => null,
60
+ 'pk' => (bool) preg_match('/\bPRIMARY\s+KEY\b/i', $part),
61
+ ];
62
+ }
63
+ }
64
+ return $columns;
65
+ };
66
+
67
+ $tables = DB::select("SELECT name, sql FROM sqlite_master WHERE type = 'table' AND name NOT LIKE 'sqlite_%' ORDER BY name");
68
+
69
+ $out = ['tables' => []];
70
+
71
+ foreach ($tables as $table) {
72
+ $name = (string) $table->name;
73
+ $escaped = $escape($name);
74
+
75
+ $columns = [];
76
+ try {
77
+ foreach (DB::select("SELECT * FROM pragma_table_info('$escaped')") as $column) {
78
+ $columns[] = [
79
+ 'cid' => (int) $column->cid,
80
+ 'name' => (string) $column->name,
81
+ 'type' => (string) $column->type,
82
+ 'notnull' => (bool) $column->notnull,
83
+ 'default' => $column->dflt_value,
84
+ 'pk' => (bool) $column->pk,
85
+ ];
86
+ }
87
+ } catch (Throwable $e) {
88
+ $columns = $parseColumns((string) $table->sql);
89
+ }
90
+ if (!$columns) {
91
+ $columns = $parseColumns((string) $table->sql);
92
+ }
93
+
94
+ $indexes = [];
95
+ try {
96
+ foreach (DB::select("SELECT * FROM pragma_index_list('$escaped')") as $index) {
97
+ $indexName = (string) $index->name;
98
+ $indexColumns = [];
99
+ try {
100
+ foreach (DB::select("SELECT * FROM pragma_index_info('" . $escape($indexName) . "')") as $info) {
101
+ $indexColumns[] = (string) $info->name;
102
+ }
103
+ } catch (Throwable $e) {
104
+ }
105
+ $indexes[] = [
106
+ 'name' => $indexName,
107
+ 'unique' => (bool) $index->unique,
108
+ 'origin' => (string) ($index->origin ?? ''),
109
+ 'partial' => (bool) ($index->partial ?? false),
110
+ 'columns' => $indexColumns,
111
+ ];
112
+ }
113
+ } catch (Throwable $e) {
114
+ }
115
+
116
+ $foreignKeys = [];
117
+ try {
118
+ foreach (DB::select("SELECT * FROM pragma_foreign_key_list('$escaped')") as $fk) {
119
+ $foreignKeys[] = [
120
+ 'id' => (int) $fk->id,
121
+ 'seq' => (int) $fk->seq,
122
+ 'table' => (string) $fk->table,
123
+ 'from' => (string) $fk->from,
124
+ 'to' => (string) $fk->to,
125
+ 'on_update' => (string) $fk->on_update,
126
+ 'on_delete' => (string) $fk->on_delete,
127
+ 'match' => (string) $fk->match,
128
+ ];
129
+ }
130
+ } catch (Throwable $e) {
131
+ }
132
+
133
+ $out['tables'][] = [
134
+ 'name' => $name,
135
+ 'sql' => (string) $table->sql,
136
+ 'columns' => $columns,
137
+ 'indexes' => $indexes,
138
+ 'foreign_keys' => $foreignKeys,
139
+ ];
140
+ }
141
+
142
+ $json = json_encode($out, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
143
+
144
+ if ($json === false) {
145
+ fwrite(STDERR, "schema-dump: JSON encode failed\n");
146
+ exit(1);
147
+ }
148
+
149
+ $outputPath = $argv[1] ?? null;
150
+ if ($outputPath !== null) {
151
+ $dir = dirname($outputPath);
152
+ if (!is_dir($dir)) {
153
+ mkdir($dir, 0777, true);
154
+ }
155
+ file_put_contents($outputPath, $json . "\n");
156
+ } else {
157
+ echo $json . "\n";
158
+ }