Skip to content

Commit 0cda2e5

Browse files
authored
Revert "feat!: Remove the deprecations from 2.x (#316)" (#334)
This reverts commit ef8a3bf.
1 parent bd21d49 commit 0cda2e5

File tree

6 files changed

+592
-32
lines changed

6 files changed

+592
-32
lines changed

UPGRADE.md

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,3 @@
1-
## From 2.x to 3.x
2-
3-
- Removed the deprecated `PhpExecutableFinder::tryToFind()` method. Use the `::find()` method instead.
4-
- Removed the following deprecations from the `Parallelization` trait:
5-
- `#logError: bool` property: override the `::createErrorHandler()` method instead.
6-
- `::configureParallelization()`: use ParallelizationInput::configureCommand()
7-
- `::getProgressSymbol()`: Override `::configureParallelExecutableFactory()` and use `::withProgressSymbol()` of the
8-
factory.
9-
- `::detectPhpExecutable()`: Override `::configureParallelExecutableFactory()` and use `::withPhpExecutable()` of the
10-
factory.
11-
- `::getWorkingDirectory()`: Override `::configureParallelExecutableFactory()` and use `::withWorkingDirectory()` of
12-
the factory.
13-
- `::getEnvironmentVariables()`: Override `::configureParallelExecutableFactory()` and use
14-
`::withExtraEnvironmentVariables()` of the factory.
15-
- `::getSegmentSize()`: Override `::configureParallelExecutableFactory()` and use `::withSegmentSize()` of the
16-
factory.
17-
- `::getBatchSize()`: Override `::configureParallelExecutableFactory()` and use `::withBatchSize()` of the
18-
factory.
19-
- `::getConsolePath()`: Override `::configureParallelExecutableFactory()` and use `::withScriptPath()` of the
20-
factory.
21-
22-
231
## From 1.x to 2.x
242

253
- `Parallelization::fetchItems()` now requires `OutputInterface` as a second parameter

phpstan-tests.neon.dist

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ parameters:
1818
message: '#ParallelizationIntegrationTest::\$importUnknownMoviesCountCommand#'
1919
- path: tests/Integration/ParallelizationIntegrationTest.php
2020
message: '#ParallelizationIntegrationTest::\$noSubProcessCommand#'
21+
- path: tests/Integration/ParallelizationIntegrationTest.php
22+
message: '#ParallelizationIntegrationTest::\$legacyCommand#'
2123
- path: tests/Integration/DebugChildProcessInputsTest.php
2224
message: '#DebugChildProcessInputsTest::\$command#'
2325

@@ -32,6 +34,9 @@ parameters:
3234
- path: tests/ParallelExecutorFactoryTest.php
3335
message: '#ParallelExecutorFactory::create\(\) expects Closure#'
3436

37+
- path: tests/Fixtures/Command/LegacyCommand.php
38+
message: '#Static method .* is unused#'
39+
3540
- path: tests/Input/RawInputTest.php
3641
message: '#Webmozarts\\Console\\Parallelization\\Input\\FakeInput#'
3742

src/ParallelCommand.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@
3333

3434
abstract class ParallelCommand extends Command
3535
{
36+
// TODO: simply add the Parallelization trait for 3.x where all the BC
37+
// layer of the trait is removed.
38+
3639
protected function configure(): void
3740
{
3841
ParallelizationInput::configureCommand($this);

src/Parallelization.php

Lines changed: 205 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@
2828
use Webmozarts\Console\Parallelization\Logger\DebugProgressBarFactory;
2929
use Webmozarts\Console\Parallelization\Logger\Logger;
3030
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;
3137

3238
/**
3339
* Adds parallelization capabilities to console commands.
@@ -60,13 +66,26 @@
6066
*/
6167
trait Parallelization
6268
{
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+
6374
/**
6475
* Provided by Symfony Command class.
6576
*
6677
* @return string The command name
6778
*/
6879
abstract public function getName();
6980

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+
7089
/**
7190
* Fetches the items that should be processed.
7291
*
@@ -147,11 +166,102 @@ protected function getParallelExecutableFactory(
147166
// overriding this method, it is highly recommended you don't and just
148167
// call `ParallelExecutorFactory::create(...func_get_args())`.
149168
//
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
155265
->withRunBeforeFirstCommand($this->runBeforeFirstCommand(...))
156266
->withRunAfterLastCommand($this->runAfterLastCommand(...))
157267
->withRunBeforeBatch($this->runBeforeBatch(...))
@@ -176,11 +286,21 @@ protected function configureParallelExecutableFactory(
176286

177287
protected function createErrorHandler(InputInterface $input, OutputInterface $output): ErrorHandler
178288
{
179-
return new LoggingErrorHandler(
180-
new ThrowableCodeErrorHandler(
181-
ResetServiceErrorHandler::forContainer($this->getContainer()),
182-
),
289+
$errorHandler = new ThrowableCodeErrorHandler(
290+
ResetServiceErrorHandler::forContainer($this->getContainer()),
183291
);
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);
184304
}
185305

186306
protected function createLogger(InputInterface $input, OutputInterface $output): Logger
@@ -198,7 +318,7 @@ protected function getContainer(): ?ContainerInterface
198318
// The container is required to reset the container upon failure to
199319
// avoid things such as a broken UoW or entity manager.
200320
//
201-
// If no such behaviour is desired, ::createErrorHandler() can be
321+
// If no such behaviour is desired, `::createErrorHandler()` can be
202322
// overridden to provide a different error handler.
203323
$application = $this->getApplication();
204324

@@ -209,6 +329,51 @@ protected function getContainer(): ?ContainerInterface
209329
return null;
210330
}
211331

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+
212377
protected function runBeforeFirstCommand(
213378
InputInterface $input,
214379
OutputInterface $output
@@ -240,4 +405,34 @@ protected function runAfterBatch(
240405
array $items
241406
): void {
242407
}
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+
}
243438
}

0 commit comments

Comments
 (0)