Skip to content
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
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
"zendframework/zend-diactoros": "~1.4"
},
"require-dev": {
"composer/composer": "^1.6",
"friendsofphp/php-cs-fixer": "^2.13",
"jean85/pretty-package-versions": "^1.2",
"monolog/monolog": "^1.3",
"php-http/curl-client": "^1.7.1",
"php-http/message": "^1.5",
Expand All @@ -37,6 +37,7 @@
"symfony/phpunit-bridge": "^4.1.6"
},
"suggest": {
"jean85/pretty-package-versions": "To use the ModulesIntegration, that lists all installed dependencies in every sent event",
"monolog/monolog": "Automatically capture Monolog events as breadcrumbs"
},
"conflict": {
Expand Down
44 changes: 11 additions & 33 deletions src/Integration/ModulesIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,43 +5,28 @@
namespace Sentry\Integration;

use Composer\Composer;
use Composer\Factory;
use Composer\IO\NullIO;
use Jean85\PrettyVersions;
use PackageVersions\Versions;
use Sentry\Event;
use Sentry\Options;
use Sentry\State\Hub;
use Sentry\State\Scope;

/**
* This integration logs with the event details all the versions of the packages
* installed with Composer, if any.
*
* @author Stefano Arlandini <[email protected]>
* installed with Composer; the root project is included too.
*/
final class ModulesIntegration implements IntegrationInterface
{
/**
* @var Options The client option
*/
private $options;

/**
* @var array The list of installed vendors
*/
private static $loadedModules = [];

/**
* Constructor.
*
* @param Options $options The Client options
*/
public function __construct(Options $options)
public function __construct()
{
if (!class_exists(Composer::class)) {
throw new \LogicException('You need the "composer/composer" package in order to use this integration.');
if (!class_exists(PrettyVersions::class)) {
throw new \LogicException('You need the "jean85/pretty-package-versions" package in order to use this integration.');
}

$this->options = $options;
}

/**
Expand All @@ -61,21 +46,14 @@ public function setupOnce(): void
}

/**
* @param ModulesIntegration $self The instance of this integration
* @param Event $event The event that will be enriched with the modules
* @param self $self The instance of this integration
* @param Event $event The event that will be enriched with the modules
*/
public static function applyToEvent(self $self, Event $event): void
{
$composerFilePath = $self->options->getProjectRoot() . \DIRECTORY_SEPARATOR . 'composer.json';

if (file_exists($composerFilePath) && 0 == \count(self::$loadedModules)) {
$composer = Factory::create(new NullIO(), $composerFilePath, true);
$locker = $composer->getLocker();

if ($locker->isLocked()) {
foreach ($locker->getLockedRepository()->getPackages() as $package) {
self::$loadedModules[$package->getName()] = $package->getVersion();
}
if (empty(self::$loadedModules)) {
foreach (Versions::VERSIONS as $package => $rawVersion) {
self::$loadedModules[$package] = PrettyVersions::getVersion($package)->getPrettyVersion();
}
}

Expand Down
8 changes: 0 additions & 8 deletions tests/Fixtures/composer.json

This file was deleted.

18 changes: 0 additions & 18 deletions tests/Fixtures/composer.lock

This file was deleted.

12 changes: 7 additions & 5 deletions tests/Integration/ModulesIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,24 @@

namespace Sentry\Tests\Integration;

use Jean85\PrettyVersions;
use PHPUnit\Framework\TestCase;
use Sentry\Event;
use Sentry\Integration\ModulesIntegration;
use Sentry\Options;

class ModulesIntegrationTest extends TestCase
{
public function testInvoke()
{
$options = new Options(['project_root' => __DIR__ . '/../Fixtures']);
$event = new Event();

$integration = new ModulesIntegration($options);
$integration = new ModulesIntegration();

ModulesIntegration::applyToEvent($integration, $event);

$this->assertEquals(['foo/bar' => '1.2.3.0', 'foo/baz' => '4.5.6.0'], $event->getModules());
$modules = $event->getModules();

$this->assertArrayHasKey('sentry/sentry', $modules, 'Root project missing');
$this->assertArrayHasKey('ocramius/package-versions', $modules, 'Indirect dependency missing');
$this->assertEquals(PrettyVersions::getVersion('sentry/sentry'), $modules['sentry/sentry']);
}
}