28
28
use Webmozarts \Console \Parallelization \Logger \DebugProgressBarFactory ;
29
29
use Webmozarts \Console \Parallelization \Logger \Logger ;
30
30
use Webmozarts \Console \Parallelization \Logger \StandardLogger ;
31
+ use Webmozarts \Console \Parallelization \Process \PhpExecutableFinder ;
32
+ use function chr ;
33
+ use function dirname ;
34
+ use function getcwd ;
35
+ use function getenv ;
36
+ use function realpath ;
31
37
32
38
/**
33
39
* Adds parallelization capabilities to console commands.
60
66
*/
61
67
trait Parallelization
62
68
{
69
+ /**
70
+ * @deprecated Deprecated since 2.0.0 and will be removed in 3.0.0. Override the method ::createErrorHandler() instead.
71
+ */
72
+ private bool $ logError = true ;
73
+
63
74
/**
64
75
* Provided by Symfony Command class.
65
76
*
66
77
* @return string The command name
67
78
*/
68
79
abstract public function getName ();
69
80
81
+ /**
82
+ * @deprecated Deprecated since 2.0.0 and will be removed in 3.0.0. Use ParallelizationInput::configureCommand() instead.
83
+ */
84
+ protected static function configureParallelization (Command $ command ): void
85
+ {
86
+ ParallelizationInput::configureCommand ($ command );
87
+ }
88
+
70
89
/**
71
90
* Fetches the items that should be processed.
72
91
*
@@ -147,11 +166,102 @@ protected function getParallelExecutableFactory(
147
166
// overriding this method, it is highly recommended you don't and just
148
167
// call `ParallelExecutorFactory::create(...func_get_args())`.
149
168
//
150
- // Configuring the factory is recommended to be done in
151
- // ::configureParallelExecutableFactory() instead which is
152
- // simpler to override, unless you _really_ need one of the
153
- // parameters passed to this method.
154
- return ParallelExecutorFactory::create (...func_get_args ())
169
+ // The only exception is if you need the whole BC layer with the API
170
+ // from 1.x.
171
+ $ factory = ParallelExecutorFactory::create (
172
+ $ fetchItems ,
173
+ $ runSingleCommand ,
174
+ $ getItemName ,
175
+ $ commandName ,
176
+ $ commandDefinition ,
177
+ $ errorHandler ,
178
+ );
179
+
180
+ $ container = $ this ->getContainer ();
181
+
182
+ $ progressSymbol = $ this ->getProgressSymbol ();
183
+ $ legacyDefaultProgressSymbol = chr (254 );
184
+ if ($ legacyDefaultProgressSymbol !== $ progressSymbol ) {
185
+ Deprecation::trigger (
186
+ 'The method ::getProgressSymbol() is deprecated and will be removed in 3.0.0. Override the ::%s() method instead to register your progress symbol to the factory. ' ,
187
+ __FUNCTION__ ,
188
+ );
189
+
190
+ $ factory = $ factory ->withProgressSymbol ($ progressSymbol );
191
+ }
192
+
193
+ $ phpExecutable = $ this ->detectPhpExecutable ();
194
+ $ legacyDefaultPhpExecutable = PhpExecutableFinder::tryToFind ();
195
+ if ($ phpExecutable !== $ legacyDefaultPhpExecutable ) {
196
+ Deprecation::trigger (
197
+ 'The method ::detectPhpExecutable() is deprecated and will be removed in 3.0.0. Override the ::%s() method instead to register your PHP executable path to the factory. ' ,
198
+ __FUNCTION__ ,
199
+ );
200
+
201
+ $ factory = $ factory ->withPhpExecutable ($ phpExecutable );
202
+ }
203
+
204
+ $ workingDirectory = $ this ->getWorkingDirectory ($ container );
205
+ $ legacyDefaultWorkingDirectory = dirname ($ container ->getParameter ('kernel.project_dir ' ));
206
+ if ($ workingDirectory !== $ legacyDefaultWorkingDirectory ) {
207
+ Deprecation::trigger (
208
+ 'The method ::getWorkingDirectory() is deprecated and will be removed in 3.0.0. Override the ::%s() method instead to register your working directory path to the factory. ' ,
209
+ __FUNCTION__ ,
210
+ );
211
+
212
+ $ factory = $ factory ->withWorkingDirectory ($ workingDirectory );
213
+ }
214
+
215
+ $ environmentVariables = $ this ->getEnvironmentVariables ($ container );
216
+ $ legacyDefaultEnvironmentVariables = [
217
+ 'PATH ' => getenv ('PATH ' ),
218
+ 'HOME ' => getenv ('HOME ' ),
219
+ 'SYMFONY_DEBUG ' => $ container ->getParameter ('kernel.debug ' ),
220
+ 'SYMFONY_ENV ' => $ container ->getParameter ('kernel.environment ' ),
221
+ ];
222
+ if ($ environmentVariables !== $ legacyDefaultEnvironmentVariables ) {
223
+ Deprecation::trigger (
224
+ 'The method ::getEnvironmentVariables() is deprecated and will be removed in 3.0.0. Override the ::%s() method instead to register your extra environment variables to the factory. ' ,
225
+ __FUNCTION__ ,
226
+ );
227
+
228
+ $ factory = $ factory ->withExtraEnvironmentVariables ($ environmentVariables );
229
+ }
230
+
231
+ $ segmentSize = $ this ->getSegmentSize ();
232
+ $ legacyDefaultSegmentSize = 50 ;
233
+ if ($ segmentSize !== $ legacyDefaultSegmentSize ) {
234
+ Deprecation::trigger (
235
+ 'The method ::getSegmentSize() is deprecated and will be removed in 3.0.0. Override the ::%s() method instead to register your segment size to the factory. ' ,
236
+ __FUNCTION__ ,
237
+ );
238
+
239
+ $ factory = $ factory ->withSegmentSize ($ segmentSize );
240
+ }
241
+
242
+ $ batchSize = $ this ->getBatchSize ();
243
+ $ legacyDefaultBatchSize = $ segmentSize ;
244
+ if ($ batchSize !== $ legacyDefaultBatchSize ) {
245
+ Deprecation::trigger (
246
+ 'The method ::getBatchSize() is deprecated and will be removed in 3.0.0. Override the ::%s() method instead to register your batch size to the factory. ' ,
247
+ __FUNCTION__ ,
248
+ );
249
+
250
+ $ factory = $ factory ->withBatchSize ($ batchSize );
251
+ }
252
+
253
+ $ consolePath = $ this ->getConsolePath ();
254
+ $ legacyDefaultConsolePath = realpath (getcwd ().'/bin/console ' );
255
+ if ($ consolePath !== $ legacyDefaultConsolePath ) {
256
+ Deprecation::trigger (
257
+ 'The method ::getConsolePath() is deprecated and will be removed in 3.0.0. Override the ::%s() method instead to register your script path to the factory. ' ,
258
+ __FUNCTION__ ,
259
+ );
260
+
261
+ $ factory = $ factory ->withScriptPath ($ consolePath );
262
+ }
263
+
264
+ return $ factory
155
265
->withRunBeforeFirstCommand ($ this ->runBeforeFirstCommand (...))
156
266
->withRunAfterLastCommand ($ this ->runAfterLastCommand (...))
157
267
->withRunBeforeBatch ($ this ->runBeforeBatch (...))
@@ -176,11 +286,21 @@ protected function configureParallelExecutableFactory(
176
286
177
287
protected function createErrorHandler (InputInterface $ input , OutputInterface $ output ): ErrorHandler
178
288
{
179
- return new LoggingErrorHandler (
180
- new ThrowableCodeErrorHandler (
181
- ResetServiceErrorHandler::forContainer ($ this ->getContainer ()),
182
- ),
289
+ $ errorHandler = new ThrowableCodeErrorHandler (
290
+ ResetServiceErrorHandler::forContainer ($ this ->getContainer ()),
183
291
);
292
+
293
+ if (!$ this ->logError ) {
294
+ Deprecation::trigger (
295
+ 'The %s#logError property is deprecated and will be removed in 3.0.0. Override the ::%s() method instead to produce the desired error handler. ' ,
296
+ self ::class,
297
+ __FUNCTION__ ,
298
+ );
299
+
300
+ return $ errorHandler ;
301
+ }
302
+
303
+ return new LoggingErrorHandler ($ errorHandler );
184
304
}
185
305
186
306
protected function createLogger (InputInterface $ input , OutputInterface $ output ): Logger
@@ -198,7 +318,7 @@ protected function getContainer(): ?ContainerInterface
198
318
// The container is required to reset the container upon failure to
199
319
// avoid things such as a broken UoW or entity manager.
200
320
//
201
- // If no such behaviour is desired, ::createErrorHandler() can be
321
+ // If no such behaviour is desired, ` ::createErrorHandler()` can be
202
322
// overridden to provide a different error handler.
203
323
$ application = $ this ->getApplication ();
204
324
@@ -209,6 +329,51 @@ protected function getContainer(): ?ContainerInterface
209
329
return null ;
210
330
}
211
331
332
+ /**
333
+ * @deprecated Deprecated since 2.0.0 and will be removed in 3.0.0. Override
334
+ * ::getParallelExecutableFactory() to register the progress symbol to the factory
335
+ * instead.
336
+ */
337
+ private static function getProgressSymbol (): string
338
+ {
339
+ return chr (254 );
340
+ }
341
+
342
+ /**
343
+ * @deprecated Deprecated since 2.0.0 and will be removed in 3.0.0. Override
344
+ * ::getParallelExecutableFactory() to register the PHP executable path to the
345
+ * factory instead.
346
+ */
347
+ private static function detectPhpExecutable (): string
348
+ {
349
+ return PhpExecutableFinder::find ();
350
+ }
351
+
352
+ /**
353
+ * @deprecated Deprecated since 2.0.0 and will be removed in 3.0.0. Override
354
+ * ::getParallelExecutableFactory() to register the working directory path to the
355
+ * factory instead.
356
+ */
357
+ private static function getWorkingDirectory (ContainerInterface $ container ): string
358
+ {
359
+ return dirname ($ container ->getParameter ('kernel.project_dir ' ));
360
+ }
361
+
362
+ /**
363
+ * @deprecated Deprecated since 2.0.0 and will be removed in 3.0.0. Override
364
+ * ::getParallelExecutableFactory() to register the working directory path to the
365
+ * factory instead.
366
+ */
367
+ protected function getEnvironmentVariables (ContainerInterface $ container ): array
368
+ {
369
+ return [
370
+ 'PATH ' => getenv ('PATH ' ),
371
+ 'HOME ' => getenv ('HOME ' ),
372
+ 'SYMFONY_DEBUG ' => $ container ->getParameter ('kernel.debug ' ),
373
+ 'SYMFONY_ENV ' => $ container ->getParameter ('kernel.environment ' ),
374
+ ];
375
+ }
376
+
212
377
protected function runBeforeFirstCommand (
213
378
InputInterface $ input ,
214
379
OutputInterface $ output
@@ -240,4 +405,34 @@ protected function runAfterBatch(
240
405
array $ items
241
406
): void {
242
407
}
408
+
409
+ /**
410
+ * @deprecated Deprecated since 2.0.0 and will be removed in 3.0.0. Override
411
+ * ::getParallelExecutableFactory() to register your segment size
412
+ * to the factory instead.
413
+ */
414
+ protected function getSegmentSize (): int
415
+ {
416
+ return 50 ;
417
+ }
418
+
419
+ /**
420
+ * @deprecated Deprecated since 2.0.0 and will be removed in 3.0.0. Override
421
+ * ::getParallelExecutableFactory() to register your batch size
422
+ * to the factory instead.
423
+ */
424
+ protected function getBatchSize (): int
425
+ {
426
+ return $ this ->getSegmentSize ();
427
+ }
428
+
429
+ /**
430
+ * @deprecated Deprecated since 2.0.0 and will be removed in 3.0.0. Override
431
+ * ::getParallelExecutableFactory() to register your console path
432
+ * to the factory instead.
433
+ */
434
+ protected function getConsolePath (): string
435
+ {
436
+ return realpath (getcwd ().'/bin/console ' );
437
+ }
243
438
}
0 commit comments