|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Doctrine; |
| 4 | + |
| 5 | +use Composer\InstalledVersions; |
| 6 | +use Doctrine\ORM\EntityManagerInterface; |
| 7 | +use OutOfBoundsException; |
| 8 | +use PHPStan\Command\Output; |
| 9 | +use PHPStan\Diagnose\DiagnoseExtension; |
| 10 | +use PHPStan\Doctrine\Driver\DriverDetector; |
| 11 | +use PHPStan\Type\Doctrine\ObjectMetadataResolver; |
| 12 | +use function count; |
| 13 | +use function sprintf; |
| 14 | + |
| 15 | +class DoctrineDiagnoseExtension implements DiagnoseExtension |
| 16 | +{ |
| 17 | + |
| 18 | + /** @var ObjectMetadataResolver */ |
| 19 | + private $objectMetadataResolver; |
| 20 | + |
| 21 | + /** @var DriverDetector */ |
| 22 | + private $driverDetector; |
| 23 | + |
| 24 | + public function __construct( |
| 25 | + ObjectMetadataResolver $objectMetadataResolver, |
| 26 | + DriverDetector $driverDetector |
| 27 | + ) |
| 28 | + { |
| 29 | + $this->objectMetadataResolver = $objectMetadataResolver; |
| 30 | + $this->driverDetector = $driverDetector; |
| 31 | + } |
| 32 | + |
| 33 | + public function print(Output $output): void |
| 34 | + { |
| 35 | + $output->writeLineFormatted(sprintf( |
| 36 | + '<info>Doctrine\'s objectManagerLoader:</info> %s', |
| 37 | + $this->objectMetadataResolver->hasObjectManagerLoader() ? 'In use' : 'No' |
| 38 | + )); |
| 39 | + |
| 40 | + $objectManager = $this->objectMetadataResolver->getObjectManager(); |
| 41 | + if ($objectManager instanceof EntityManagerInterface) { |
| 42 | + $connection = $objectManager->getConnection(); |
| 43 | + $driver = $this->driverDetector->detect($connection); |
| 44 | + |
| 45 | + $output->writeLineFormatted(sprintf( |
| 46 | + '<info>Detected driver:</info> %s', |
| 47 | + $driver === null ? 'None' : $driver |
| 48 | + )); |
| 49 | + } |
| 50 | + |
| 51 | + $packages = []; |
| 52 | + $candidates = [ |
| 53 | + 'doctrine/dbal', |
| 54 | + 'doctrine/orm', |
| 55 | + 'doctrine/common', |
| 56 | + 'doctrine/collections', |
| 57 | + 'doctrine/persistence', |
| 58 | + ]; |
| 59 | + foreach ($candidates as $package) { |
| 60 | + try { |
| 61 | + $installedVersion = InstalledVersions::getPrettyVersion($package); |
| 62 | + } catch (OutOfBoundsException $e) { |
| 63 | + continue; |
| 64 | + } |
| 65 | + |
| 66 | + if ($installedVersion === null) { |
| 67 | + continue; |
| 68 | + } |
| 69 | + |
| 70 | + $packages[$package] = $installedVersion; |
| 71 | + } |
| 72 | + |
| 73 | + if (count($packages) > 0) { |
| 74 | + $output->writeLineFormatted('<info>Installed Doctrine packages:</info>'); |
| 75 | + foreach ($packages as $package => $version) { |
| 76 | + $output->writeLineFormatted(sprintf('%s: %s', $package, $version)); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + $output->writeLineFormatted(''); |
| 81 | + } |
| 82 | + |
| 83 | +} |
0 commit comments