Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Workaround for constructor signature change in Symfony/Process #2766

Merged
merged 2 commits into from
Aug 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions bin/composer-script/CreateProjectScript.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Bolt\ComposerScripts;

use Composer\Script\Event;
Expand Down
4 changes: 3 additions & 1 deletion bin/composer-script/PostInstallScript.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
<?php

declare(strict_types=1);

namespace Bolt\ComposerScripts;

class PostInstallScript extends Script
{
public static function execute()
public static function execute(): void
{
parent::init('Running composer "post-install-cmd" scripts');

Expand Down
2 changes: 2 additions & 0 deletions bin/composer-script/PostUpdateScript.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Bolt\ComposerScripts;

class PostUpdateScript extends Script
Expand Down
2 changes: 2 additions & 0 deletions bin/composer-script/PrePackageUninstallScript.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Bolt\ComposerScripts;

class PrePackageUninstallScript extends Script
Expand Down
9 changes: 6 additions & 3 deletions bin/composer-script/ProjectEventHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ public static function postCreateProject(Event $event): void
CreateProjectScript::execute($event);
}

/** @placeholder */
/**
* @placeholder
*/
public static function preInstall(Event $event): void
{
}
Expand All @@ -30,7 +32,9 @@ public static function postInstall(Event $event): void
PostInstallScript::execute();
}

/** @placeholder */
/**
* @placeholder
*/
public static function preUpdate(Event $event): void
{
}
Expand All @@ -44,5 +48,4 @@ public static function prePackageUninstall(PackageEvent $event): void
{
PrePackageUninstallScript::execute();
}

}
46 changes: 42 additions & 4 deletions bin/composer-script/Script.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
<?php

declare(strict_types=1);

namespace Bolt\ComposerScripts;

use Composer\Script\Event;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Process\Process;
use Symplify\PackageBuilder\Console\Style\SymfonyStyleFactory;
Expand All @@ -12,7 +17,7 @@ class Script
/** @var SymfonyStyle */
protected static $console;

protected static function init(string $message = '')
protected static function init(string $message = ''): void
{
$consoleFactory = new SymfonyStyleFactory();
self::$console = $consoleFactory->create();
Expand All @@ -30,9 +35,42 @@ protected static function getProjectFolder(Event $event): string
/**
* Execute a command in the CLI, as a separate process.
*/
protected static function run(string $command): void
protected static function run(string $command): int
{
$process = new Process($command);
$process->run();
// Depending on the context, we're using either Symfony/Process 2.8.52 (bundled with composer 2.1.x)
// or Symfony/Process 5.3.x (if we're using our own). The signature of the Constructor changed
// from: `public function __construct(string $commandline, …)`
// to: `public function __construct(array $command, …)`
// We'll have to attempt one, and otherwise fall back to the other.

try {
$process = new Process($command);
} catch (\TypeError $e) {
$process = new Process([$command]);
}
Comment on lines +46 to +50
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Safer would be:

Suggested change
try {
$process = new Process($command);
} catch (\TypeError $e) {
$process = new Process([$command]);
}
$process = Process::fromShellCommandline($command);

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will try that out.. If that works, that'll be a lot less fugly!

Thanks for the suggestion, @stloyd!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alas, it doesn't seem to work in the context of composer create-project, because it seems that method isn't present yet in symfony/process 2.8.x, as used by Composer.

It breaks with this error:

…
…
120 packages you are using are looking for funding.
Use the `composer fund` command to find out more!

Run composer recipes at any time to see the status of your Symfony recipes.

> Bolt\ComposerScripts\ProjectEventHandler::postUpdate

 ! [NOTE] Running composer "post-update-cmd" scripts

PHP Fatal error:  Uncaught Error: Call to undefined method Symfony\Component\Process\Process::fromShellCommandline() in /Users/bob/Sites/Bolt/playground/vendor/bolt/core/bin/composer-script/Script.php:39
Stack trace:
#0 /Users/bob/Sites/Bolt/playground/vendor/bolt/core/bin/composer-script/PostUpdateScript.php(13): Bolt\ComposerScripts\Script::run('php vendor/bobd...')
#1 /Users/bob/Sites/Bolt/playground/vendor/bolt/core/bin/composer-script/ProjectEventHandler.php(44): Bolt\ComposerScripts\PostUpdateScript::execute()
#2 phar:///usr/local/bin/composer/src/Composer/EventDispatcher/EventDispatcher.php(377): Bolt\ComposerScripts\ProjectEventHandler::postUpdate(Object(Composer\Script\Event))
#3 phar:///usr/local/bin/composer/src/Composer/EventDispatcher/EventDispatcher.php(236): Composer\EventDispatcher\EventDispatcher->executeEventPhpScript('Bolt\\ComposerSc...', 'postUpdate', Object(Composer\Script\Event))
#4 phar:///usr/local/bin/composer/src/Composer/EventDispatcher/EventDispatcher.php(117): Composer\EventDispatcher\EventDispatc in /Users/bob/Sites/Bolt/playground/vendor/bolt/core/bin/composer-script/Script.php on line 39


return $process->run();
}

/**
* Create SymfonyStyle object. Taken from Symplify (which we might not
* have at our disposal inside a 'project' installation)
*/
public static function createSymfonyStyle(): SymfonyStyle
{
// to prevent missing argv indexes
if (! isset($_SERVER['argv'])) {
$_SERVER['argv'] = [];
}

$argvInput = new ArgvInput();
$consoleOutput = new ConsoleOutput();

// --debug is called
if ($argvInput->hasParameterOption('--debug')) {
$consoleOutput->setVerbosity(OutputInterface::VERBOSITY_DEBUG);
}

return new SymfonyStyle($argvInput, $consoleOutput);
}
}