16
16
declare (strict_types = 1 );
17
17
18
18
use GitWrapper \GitWrapper ;
19
+ use Elasticsearch \Client ;
19
20
use Elasticsearch \Util \ClientEndpoint ;
20
21
use Elasticsearch \Util \Endpoint ;
21
22
use Elasticsearch \Util \NamespaceEndpoint ;
22
23
use Elasticsearch \Tests \Utility ;
23
24
24
25
require_once dirname (__DIR__ ) . '/vendor/autoload.php ' ;
25
26
26
-
27
-
28
- removeDirectory (__DIR__ . '/../src/Elasticsearch/Endpoints ' , [
29
- __DIR__ . '/../src/Elasticsearch/Endpoints/AbstractEndpoint.php ' ,
30
- ]);
31
- removeDirectory (__DIR__ . '/../src/Elasticsearch/Namespaces ' , [
32
- __DIR__ . '/../src/Elasticsearch/Namespaces/AbstractNamespace.php ' ,
33
- __DIR__ . '/../src/Elasticsearch/Namespaces/BooleanRequestWrapper.php ' ,
34
- __DIR__ . '/../src/Elasticsearch/Namespaces/NamespaceBuilderInterface.php '
35
- ]);
36
-
37
- die ();
38
- printf ("Generating endpoints for Elasticsearch \n" );
39
- printf ("--- \n" );
40
-
41
- $ start = microtime (true );
42
-
43
27
$ client = Utility::getClient ();
44
-
45
28
$ serverInfo = $ client ->info ();
46
29
$ version = $ serverInfo ['version ' ]['number ' ];
47
30
$ buildHash = $ serverInfo ['version ' ]['build_hash ' ];
51
34
exit (1 );
52
35
}
53
36
37
+ $ backupFileName = sprintf (
38
+ "%s/backup_endpoint_namespace_%s.zip " ,
39
+ __DIR__ ,
40
+ Client::VERSION
41
+ );
42
+
43
+ printf ("Backup Endpoints and Namespaces in /src \n" );
44
+ backup ($ backupFileName );
45
+
46
+ cleanFolders ();
47
+
48
+ $ start = microtime (true );
49
+ printf ("Generating endpoints for Elasticsearch \n" );
50
+
51
+ $ success = true ;
54
52
$ gitWrapper = new GitWrapper ();
55
53
$ git = $ gitWrapper ->workingCopy (dirname (__DIR__ ) . '/util/elasticsearch ' );
56
54
63
61
]
64
62
);
65
63
$ files = explode ("\n" , $ result );
66
- $ outputDir = __DIR__ . "/output/ $ version " ;
67
-
68
- // Remove the output directory
69
- printf ("Removing %s folder \n" , $ outputDir );
70
- removeDirectory ($ outputDir );
71
- mkdir ($ outputDir );
64
+ $ outputDir = __DIR__ . "/../src/Elasticsearch/ " ;
72
65
73
66
$ endpointDir = "$ outputDir/Endpoints/ " ;
74
- printf ("Creating folder %s \n" , $ endpointDir );
75
- mkdir ($ endpointDir );
76
67
77
68
$ countEndpoint = 0 ;
78
69
$ namespaces = [];
82
73
if (empty ($ file ) || (basename ($ file ) === '_common.json ' )) {
83
74
continue ;
84
75
}
85
- printf ("Generation %s... " , basename ($ file ));
76
+ printf ("Generating %s... " , basename ($ file ));
86
77
87
- $ endpoint = new Endpoint ($ file , $ git ->run ('show ' , [': ' . trim ($ file )]), $ version );
78
+ $ endpoint = new Endpoint ($ file , $ git ->run ('show ' , [': ' . trim ($ file )]), $ version, $ buildHash );
88
79
89
80
$ dir = $ endpointDir . NamespaceEndpoint::normalizeName ($ endpoint ->namespace );
90
81
if (!file_exists ($ dir )) {
91
82
mkdir ($ dir );
92
83
}
84
+ $ outputFile = sprintf ("%s/%s.php " , $ dir , $ endpoint ->getClassName ());
93
85
file_put_contents (
94
- sprintf ( " %s/%s.php " , $ dir , $ endpoint -> getClassName ()) ,
86
+ $ outputFile ,
95
87
$ endpoint ->renderClass ()
96
88
);
89
+ if (!isValidPhpSyntax ($ outputFile )) {
90
+ printf ("Error: syntax error in %s \n" , $ outputFile );
91
+ $ success = false ;
92
+ break ;
93
+ }
97
94
98
95
printf ("done \n" );
99
96
100
97
$ namespaces [$ endpoint ->namespace ][] = $ endpoint ;
101
98
$ countEndpoint ++;
102
99
}
103
100
101
+ if (!$ success ) {
102
+ printf ("Roll back to the previous Endpoints and Namespace (ver. %s) \n" , Client::VERSION );
103
+ cleanFolders ();
104
+ restore ($ backupFileName );
105
+ exit (1 );
106
+ }
107
+
104
108
// Generate namespaces
105
109
$ namespaceDir = "$ outputDir/Namespaces/ " ;
106
- printf ("Creating folder %s \n" , $ namespaceDir );
107
- mkdir ($ namespaceDir );
108
-
109
110
$ countNamespace = 0 ;
110
111
$ clientFile = "$ outputDir/Client.php " ;
111
112
112
113
foreach ($ namespaces as $ name => $ endpoints ) {
113
114
if (empty ($ name )) {
114
- $ clientEndpoint = new ClientEndpoint (array_keys ($ namespaces ), $ version );
115
+ $ clientEndpoint = new ClientEndpoint (array_keys ($ namespaces ), $ version, $ buildHash );
115
116
foreach ($ endpoints as $ ep ) {
116
117
$ clientEndpoint ->addEndpoint ($ ep );
117
118
}
118
119
file_put_contents (
119
120
$ clientFile ,
120
121
$ clientEndpoint ->renderClass ()
121
122
);
123
+ if (!isValidPhpSyntax ($ clientFile )) {
124
+ printf ("Error: syntax error in %s \n" , $ clientFile );
125
+ $ success = false ;
126
+ break ;
127
+ }
122
128
$ countNamespace ++;
123
129
continue ;
124
130
}
125
- $ namespace = new NamespaceEndpoint ($ name , $ version );
131
+ $ namespace = new NamespaceEndpoint ($ name , $ version, $ buildHash );
126
132
foreach ($ endpoints as $ ep ) {
127
133
$ namespace ->addEndpoint ($ ep );
128
134
}
135
+ $ namespaceFile = $ namespaceDir . $ namespace ->getNamespaceName () . 'Namespace.php ' ;
129
136
file_put_contents (
130
- $ namespaceDir . $ namespace -> getNamespaceName () . ' Namespace.php ' ,
137
+ $ namespaceFile ,
131
138
$ namespace ->renderClass ()
132
139
);
140
+ if (!isValidPhpSyntax ($ namespaceFile )) {
141
+ printf ("Error: syntax error in %s \n" , $ namespaceFile );
142
+ $ success = false ;
143
+ break ;
144
+ }
133
145
$ countNamespace ++;
134
146
}
135
147
136
- # Clean
137
148
$ end = microtime (true );
138
- printf ("\nGenerated %d endpoints and %d namespaces in %.3f seconds \n. " , $ countEndpoint , $ countNamespace , $ end - $ start );
139
- printf ("--- \n" );
140
-
141
- printf ("Backup Endpoints and Namespaces in /src " );
142
-
143
- $ file = Client::Version;
144
-
145
- $ zip = new ZipArchive ();
146
- $ ret = $ zip ->open ('application.zip ' , ZipArchive::CREATE | ZipArchive::OVERWRITE );
147
- if ($ ret !== TRUE ) {
148
- printf ('Failed with code %d ' , $ ret );
149
+ if ($ success ) {
150
+ printf ("\nGenerated %d endpoints and %d namespaces in %.3f seconds \n. " , $ countEndpoint , $ countNamespace , $ end - $ start );
149
151
} else {
150
- $ options = array ('add_path ' => 'sources/ ' , 'remove_all_path ' => TRUE );
151
- $ zip ->addGlob ('*.{php,txt} ' , GLOB_BRACE , $ options );
152
- $ zip ->close ();
153
- }
154
- // End
152
+ printf ("Roll back to the previous Endpoints and Namespace (ver. %s) \n" , Client::VERSION );
153
+ cleanFolders ();
154
+ restore ($ backupFileName );
155
+ }
155
156
156
- function print_usage_msg (): void
157
- {
158
- printf ("Usage: php %s <ES_VERSION> \n" , basename (__FILE__ ));
159
- printf ("where <ES_VERSION> is the Elasticsearch version to check (version must be >= 7.4.0). \n" );
160
- }
157
+ /**
158
+ * FUNCTIONS
159
+ */
161
160
162
- // Remove directory recursively
163
- function removeDirectory ($ directory , array $ omit = [])
161
+ /**
162
+ * Remove a directory recursively
163
+ */
164
+ function removeDirectory ($ directory , array $ omit = [])
164
165
{
165
166
foreach (glob ("{$ directory }/* " ) as $ file )
166
167
{
167
168
if (is_dir ($ file )) {
168
169
if (!in_array ($ file , $ omit )) {
169
- //removeDirectory($file);
170
- printf ("Elimino directory %s \n" , $ file );
170
+ removeDirectory ($ file , $ omit );
171
171
}
172
172
} else {
173
173
if (!in_array ($ file , $ omit )) {
174
- //unlink($file);
175
- printf ("Elimino file %s \n" , $ file );
174
+ @unlink ($ file );
176
175
}
177
176
}
178
177
}
179
- if (is_dir ($ directory ) && empty ($ omit )) {
180
- //rmdir($directory);
181
- printf ("Elimino directory %s \n" , $ directory );
178
+ if (is_dir ($ directory )) {
179
+ @rmdir ($ directory );
182
180
}
183
181
}
184
182
185
- # Copy files and directory recursively
186
- function copyAll (string $ src , string $ dst ) {
187
- $ dir = opendir ($ src );
188
- @mkdir ($ dst );
189
- while (false !== ( $ file = readdir ($ dir )) ) {
190
- if (( $ file != '. ' ) && ( $ file != '.. ' )) {
191
- if ( is_dir ($ src . '/ ' . $ file ) ) {
192
- copyAll ($ src . '/ ' . $ file , $ dst . '/ ' . $ file );
193
- }
194
- else {
195
- copy ($ src . '/ ' . $ file , $ dst . '/ ' . $ file );
196
- }
197
- }
198
- }
199
- closedir ($ dir );
200
- }
183
+ /**
184
+ * Remove Endpoints, Namespaces and Client in src/Elasticsearch
185
+ */
186
+ function cleanFolders ()
187
+ {
188
+ removeDirectory (__DIR__ . '/../src/Elasticsearch/Endpoints ' , [
189
+ __DIR__ . '/../src/Elasticsearch/Endpoints/AbstractEndpoint.php ' ,
190
+ ]);
191
+ removeDirectory (__DIR__ . '/../src/Elasticsearch/Namespaces ' , [
192
+ __DIR__ . '/../src/Elasticsearch/Namespaces/AbstractNamespace.php ' ,
193
+ __DIR__ . '/../src/Elasticsearch/Namespaces/BooleanRequestWrapper.php ' ,
194
+ __DIR__ . '/../src/Elasticsearch/Namespaces/NamespaceBuilderInterface.php '
195
+ ]);
196
+ @unlink (__DIR__ . '/../src/Elasticsearch/Client.php ' );
197
+ }
198
+
199
+ /**
200
+ * Backup Endpoints, Namespaces and Client in src/Elasticsearch
201
+ */
202
+ function backup (string $ fileName )
203
+ {
204
+ $ zip = new ZipArchive ();
205
+ $ result = $ zip ->open ($ fileName , ZipArchive::CREATE | ZipArchive::OVERWRITE );
206
+ if ($ result !== true ) {
207
+ printf ("Error opening the zip file %s: %s \n" , $ fileName , $ result );
208
+ exit (1 );
209
+ } else {
210
+ $ zip ->addFile (__DIR__ . '/../src/Elasticsearch/Client.php ' , 'Client.php ' );
211
+ $ zip ->addGlob (__DIR__ . '/../src/Elasticsearch/Namespaces/*.php ' , GLOB_BRACE , [
212
+ 'remove_path ' => __DIR__ . '/../src/Elasticsearch '
213
+ ]);
214
+ // Add the Endpoints (including subfolders)
215
+ foreach (glob (__DIR__ . '/../src/Elasticsearch/Endpoints/* ' ) as $ file ) {
216
+ if (is_dir ($ file )) {
217
+ $ zip ->addGlob ("$ file/*.php " , GLOB_BRACE , [
218
+ 'remove_path ' => __DIR__ . '/../src/Elasticsearch '
219
+ ]);
220
+ } else {
221
+ $ zip ->addGlob ("$ file " , GLOB_BRACE , [
222
+ 'remove_path ' => __DIR__ . '/../src/Elasticsearch '
223
+ ]);
224
+ }
225
+ }
226
+ $ zip ->close ();
227
+ }
228
+ }
229
+
230
+ /**
231
+ * Restore Endpoints, Namespaces and Client in src/Elasticsearch
232
+ */
233
+ function restore (string $ fileName )
234
+ {
235
+ $ zip = new ZipArchive ();
236
+ $ result = $ zip ->open ($ fileName );
237
+ if ($ result !== true ) {
238
+ printf ("Error opening the zip file %s: %s \n" , $ fileName , $ result );
239
+ exit (1 );
240
+ }
241
+ $ zip ->extractTo (__DIR__ . '/../src/Elasticsearch ' );
242
+ $ zip ->close ();
243
+ }
244
+
245
+ /**
246
+ * Check if the generated code has a valid PHP syntax
247
+ */
248
+ function isValidPhpSyntax (string $ filename ): bool
249
+ {
250
+ if (file_exists ($ filename )) {
251
+ $ result = exec ("php -l $ filename " );
252
+ return false !== strpos ($ result , "No syntax errors " );
253
+ }
254
+ return false ;
255
+ }
0 commit comments