Skip to content

Commit

Permalink
PHPStan Pro - configurable DNS servers
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Mar 30, 2023
1 parent d892c34 commit f3711a2
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
7 changes: 7 additions & 0 deletions conf/config.neon
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,9 @@ parameters:
editorUrl: null
editorUrlTitle: null
errorFormat: null
pro:
dnsServers:
- '1.1.1.1'
__validate: true

extensions:
Expand Down Expand Up @@ -413,6 +416,9 @@ parametersSchema:
editorUrl: schema(string(), nullable())
editorUrlTitle: schema(string(), nullable())
errorFormat: schema(string(), nullable())
pro: structure([
dnsServers: schema(listOf(string()), min(1)),
])

# irrelevant Nette parameters
debugMode: bool()
Expand Down Expand Up @@ -698,6 +704,7 @@ services:
analysedPaths: %analysedPaths%
currentWorkingDirectory: %currentWorkingDirectory%
fixerTmpDir: %fixerTmpDir%
dnsServers: %pro.dnsServers%

-
class: PHPStan\Dependency\DependencyResolver
Expand Down
1 change: 1 addition & 0 deletions src/Analyser/ResultCache/ResultCacheManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,7 @@ private function getMeta(array $allAnalysedFiles, ?array $projectConfigArray): a
unset($projectConfigArray['parameters']['internalErrorsCountLimit']);
unset($projectConfigArray['parameters']['cache']);
unset($projectConfigArray['parameters']['memoryLimitFile']);
unset($projectConfigArray['parameters']['pro']);
unset($projectConfigArray['parametersSchema']);
}

Expand Down
28 changes: 23 additions & 5 deletions src/Command/FixerApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use PHPStan\ShouldNotHappenException;
use Psr\Http\Message\ResponseInterface;
use React\ChildProcess\Process;
use React\Dns\Config\Config;
use React\EventLoop\Loop;
use React\EventLoop\LoopInterface;
use React\EventLoop\StreamSelectLoop;
Expand Down Expand Up @@ -74,6 +75,7 @@ class FixerApplication

/**
* @param string[] $analysedPaths
* @param list<string> $dnsServers
*/
public function __construct(
private FileMonitor $fileMonitor,
Expand All @@ -82,6 +84,7 @@ public function __construct(
private array $analysedPaths,
private string $currentWorkingDirectory,
private string $fixerTmpDir,
private array $dnsServers,
)
{
}
Expand Down Expand Up @@ -207,8 +210,7 @@ private function getFixerProcess(OutputInterface $output, int $serverPort): Proc
$this->downloadPhar($output, $pharPath, $infoPath);
} catch (RuntimeException $e) {
if (!is_file($pharPath)) {
$output->writeln('<fg=red>Could not download the PHPStan Pro executable.</>');
$output->writeln($e->getMessage());
$this->printDownloadError($output, $e);

throw new FixerProcessException();
}
Expand Down Expand Up @@ -293,14 +295,17 @@ private function downloadPhar(
$output->writeln('<fg=green>Checking if there\'s a new PHPStan Pro release...</>');
}

$dnsConfig = new Config();
$dnsConfig->nameservers = $this->dnsServers;

$client = new Browser(
new Connector(
[
'timeout' => 5,
'tls' => [
'cafile' => CaBundle::getBundledCaBundlePath(),
],
'dns' => '1.1.1.1',
'dns' => $dnsConfig,
],
),
);
Expand Down Expand Up @@ -339,8 +344,8 @@ private function downloadPhar(
fwrite($pharPathResource, $chunk);
$progressBar->setProgress($bytes);
});
}, static function (Throwable $e) use ($output): void {
$output->writeln(sprintf('<fg=red>Could not download the PHPStan Pro executable:</> %s', $e->getMessage()));
}, function (Throwable $e) use ($output): void {
$this->printDownloadError($output, $e);
});

Loop::run();
Expand All @@ -354,6 +359,19 @@ private function downloadPhar(
$this->writeInfoFile($infoPath, $latestInfo['version']);
}

private function printDownloadError(OutputInterface $output, Throwable $e): void
{
$output->writeln(sprintf('<fg=red>Could not download the PHPStan Pro executable:</> %s', $e->getMessage()));
$output->writeln('');
$output->writeln('Try different DNS servers in your configuration file:');
$output->writeln('');
$output->writeln('parameters:');
$output->writeln("\tpro:");
$output->writeln("\t\tdnsServers!:");
$output->writeln("\t\t\t- '8.8.8.8'");
$output->writeln('');
}

private function writeInfoFile(string $infoPath, string $version): void
{
FileWriter::write($infoPath, Json::encode([
Expand Down

0 comments on commit f3711a2

Please sign in to comment.