Skip to content

Commit bfef968

Browse files
Disable SDK from autoloading during composer execution. (#1727)
* Disable SDK from autoloading during composer execution. Fixes #1673. * Moved Composer run check into ComposerHandler utility class. * Include check for COMPOSER_BINARY as entrypoint to script execution. * Switch to class_exists check when trying to detect that we are running under Composer.
1 parent 4b42c97 commit bfef968

4 files changed

Lines changed: 61 additions & 1 deletion

File tree

composer.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,5 +209,10 @@
209209
"OpenTelemetry\\Tests\\Integration\\SDK\\Common\\Configuration\\TestEnvSourceProvider"
210210
]
211211
}
212+
},
213+
"scripts": {
214+
"test-psr3": [
215+
"OpenTelemetry\\Tests\\Integration\\Composer\\Psr3Compatability::run"
216+
]
212217
}
213218
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace OpenTelemetry\SDK\Common\Util;
6+
7+
use function class_exists;
8+
use function getenv;
9+
10+
final class ComposerHandler
11+
{
12+
public static function isRunning(): bool
13+
{
14+
/**
15+
* This is set when composer is running a script; eg:
16+
* composer run-script test-psr3
17+
*
18+
* However, it is not set in the following case:
19+
* composer test-psr3
20+
*/
21+
if (getenv('COMPOSER_DEV_MODE')) {
22+
return true;
23+
}
24+
25+
return class_exists(\Composer\Console\Application::class, false);
26+
}
27+
}

src/SDK/_autoload.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,17 @@
22

33
declare(strict_types=1);
44

5-
\OpenTelemetry\SDK\SdkAutoloader::autoload();
5+
/**
6+
* If OTEL_PHP_AUTOLOAD_ENABLED=true, there may be compatability issues
7+
* if the version of PSR-3 installed in ./vendor conflicts with that of the
8+
* packaged composer PSR-3 library.
9+
*
10+
* If COMPOSER_DEV_MODE is present, then we can assume that a composer script
11+
* is running, and we can prevent the PSR-3 compatability issues by disabling
12+
* the SDK from activating.
13+
*
14+
* @see https://github.com/open-telemetry/opentelemetry-php/issues/1673
15+
*/
16+
if (\OpenTelemetry\SDK\Common\Util\ComposerHandler::isRunning() === false) {
17+
\OpenTelemetry\SDK\SdkAutoloader::autoload();
18+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace OpenTelemetry\Tests\Integration\Composer;
6+
7+
use Composer\Script\Event;
8+
9+
final class Psr3Compatability
10+
{
11+
public static function run(Event $event): void
12+
{
13+
require_once $event->getComposer()->getConfig()->get('vendor-dir') . '/autoload.php';
14+
}
15+
}

0 commit comments

Comments
 (0)