Skip to content

Commit

Permalink
Trigger of autoloader un-tracing did not respect object (#256)
Browse files Browse the repository at this point in the history
* [core] Fix untrace trigger fo symfony

* update changelog
  • Loading branch information
labbati authored Jan 23, 2019
1 parent efe23a6 commit 27b708f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ All notable changes to this project will be documented in this file - [read more
### Fixed
- Composer php compatibility declaration #247
- Add missing files to PECL releases #252
- Trigger of autoloader un-tracing did not respect object #256

## [0.10.0]

Expand Down
18 changes: 16 additions & 2 deletions bridge/dd_wrap_autoloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,36 @@

$dd_autoload_called = false;

$untraceTriggers = [
'Composer\Autoload\ClassLoader',
];

// Instead of tracing autoloaders statically, we should trace them dynamically. This can be done at the moment because
// of https://github.com/DataDog/dd-trace-php/issues/224 and the fact that in some cases, e.g. Symfony's
// `Symfony\Component\Config\Resource\ClassExistenceResource::throwOnRequiredClass` loaders are private.
// As soon as this is fixed we can trace `spl_autoload_register` function and use it as a hook instead of
// statically hooking into a limited number of class loaders.
dd_trace('spl_autoload_register', function () use (&$dd_autoload_called) {
dd_trace('spl_autoload_register', function () use (&$dd_autoload_called, $untraceTriggers) {
$args = func_get_args();
$originalAutoloaderRegistered = call_user_func_array('spl_autoload_register', $args);

$loader = $args[0];

$loaderClass = null;
if (is_array($loader)) {
if (is_string($loader[0])) {
$loaderClass = $loader[0];
} elseif (is_object($loader[0])) {
$loaderClass = get_class($loader[0]);
}
}


// Why unregistering spl_autoload_register?
// In some cases (e.g. Symfony) this 'spl_autoload_register' function is called within a private scope and at the
// moment we are working to have this use case properly handled by the extension. In the meantime we provide
// this workaround.
if (is_array($loader) && $loader[0] === 'Composer\Autoload\ClassLoader' && $loader[1] === 'loadClass') {
if (in_array($loaderClass, $untraceTriggers)) {
dd_untrace('spl_autoload_register');
}

Expand Down

0 comments on commit 27b708f

Please sign in to comment.