Skip to content
This repository was archived by the owner on Jul 22, 2025. It is now read-only.

Commit 28b1b3b

Browse files
committed
Proxy command is back!
1 parent aa5f6dd commit 28b1b3b

File tree

3 files changed

+68
-8
lines changed

3 files changed

+68
-8
lines changed

src/DefaultDumpWriter.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function __construct(string $filename)
3737
{
3838
$this->filename = $filename;
3939
$this->source = sprintf(
40-
'<?php%1$srequire_once(dirname(dirname(dirname(__DIR__))) . DIRECTORY_SEPARATOR . "autoload.php");%1$s$_SERVER[\'HTTP_HOST\'] = null;%1$s',
40+
'<?php%1$suse Imponeer\\ComposerCustomCommands\\ProxyCommand;%1$s%1$srequire_once(dirname(dirname(dirname(__DIR__))) . DIRECTORY_SEPARATOR . "autoload.php");%1$s',
4141
PHP_EOL
4242
);
4343
}
@@ -49,11 +49,12 @@ public function __construct(string $filename)
4949
*/
5050
public function writeToFile(): bool
5151
{
52-
$ret = $this->source . PHP_EOL . 'return [' . PHP_EOL;
52+
$ret = $this->source . PHP_EOL;
53+
$ret .= '$commands = array_filter([' . PHP_EOL;
5354
foreach ($this->commands as $command) {
54-
$ret .= str_repeat(' ', 4) . ' new ' . $command . '(),' . PHP_EOL;
55+
$ret .= str_repeat(' ', 4) . 'class_exists("' . $command . '") ? ' . ' new ProxyCommand(' . $command . '::class) : null,' . PHP_EOL;
5556
}
56-
$ret .= '];';
57+
$ret .= ']);' . PHP_EOL;
5758

5859
return (bool)file_put_contents($this->filename, $ret, LOCK_EX);
5960
}
@@ -76,7 +77,7 @@ public function addCommands(array $commands): void
7677
public function addBootScript(string $bootScript): void
7778
{
7879
$this->source .= sprintf(
79-
'file_exists(%1$s) && include_once(%1$s);%2$s',
80+
'try {%2$s file_exists(%1$s) && include_once(%1$s);%2$s} catch (\\Exception $ex) {%2$s}%2$s',
8081
json_encode($bootScript),
8182
PHP_EOL
8283
);

src/Plugin.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Imponeer\ComposerCustomCommands;
44

55
use Composer\Plugin\Capability\CommandProvider;
6+
use Composer\Plugin\Capable;
67
use Imponeer\ComposerCustomCommands\CommandProvider as LocalCommandProvider;
78
use Imponeer\ProjectCachedCodeGeneratorFromComposerJSONDataBase\ComposerPlugin;
89

@@ -11,7 +12,7 @@
1112
*
1213
* @package Imponeer\ComposerCustomCommand
1314
*/
14-
class Plugin extends ComposerPlugin
15+
class Plugin extends ComposerPlugin implements Capable
1516
{
1617

1718
/**
@@ -20,9 +21,9 @@ class Plugin extends ComposerPlugin
2021
* @return array
2122
*/
2223
public function getCapabilities() {
23-
return array(
24+
return [
2425
CommandProvider::class => LocalCommandProvider::class
25-
);
26+
];
2627
}
2728

2829
/**

src/ProxyCommand.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
namespace Imponeer\ComposerCustomCommands;
4+
5+
use Composer\Command\BaseCommand;
6+
use Symfony\Component\Console\Command\Command;
7+
use Symfony\Component\Console\Input\InputInterface;
8+
use Symfony\Component\Console\Output\OutputInterface;
9+
10+
class ProxyCommand extends BaseCommand
11+
{
12+
13+
/**
14+
* Linked command
15+
*
16+
* @var null|Command
17+
*/
18+
protected $realCommand = null;
19+
20+
/**
21+
* ProxyCommand constructor.
22+
*
23+
* @param string $composerCommandClass Composer command class
24+
*/
25+
public function __construct($composerCommandClass)
26+
{
27+
parent::__construct(null);
28+
29+
$this->realCommand = new $composerCommandClass();
30+
31+
$this->setAliases(
32+
$this->realCommand->getAliases()
33+
);
34+
$this->setDescription(
35+
$this->realCommand->getDescription()
36+
);
37+
$this->setDefinition(
38+
$this->realCommand->getDefinition()
39+
);
40+
}
41+
42+
/**
43+
* Execute command
44+
*
45+
* @param InputInterface $input Input
46+
* @param OutputInterface $output Output
47+
*
48+
* @return int|null
49+
*/
50+
protected function execute(InputInterface $input, OutputInterface $output)
51+
{
52+
53+
$_SERVER['HTTP_HOST'] = null;
54+
55+
return $this->realCommand->execute($input, $output);
56+
}
57+
58+
}

0 commit comments

Comments
 (0)