opis/closure

Issue with the automatic resolution of __DIR__

kocsismate opened this issue · 1 comments

Hi,

We've just experienced an outage when indirectly using opis/closure via PHP-DI. The issue was caused by three unfortunate coincidences:

  • opis/closure automatically resolves the __DIR__ magic constant
  • our directory structure is different in production and other environments
  • we precompiled our container in a non-production environment

As far as I know, there is no use of automatically resolving __DIR__ by opis/closure, since PHP does it anyway during compilation. Or is there any other motivation for doing so? Is it possible to change this behavior?

We've just experienced an outage when indirectly using opis/closure

@kocsismate That's what usually happens when you copy cache files in production containers without following a strict directory structure. I don't see why opis/closure is to blame here.

The behavior of opis/closure is correct. Let's take the following example

$original = function() {
    return 'foo';
};

$clone = unserialize(serialize(SerializableClosure::from($original)))->getClosure();

Here, $clone must behave exactly as $original and return the same value

// this must be bool(true)
var_dump($original() === $clone());

This should also be true when a closure returns __DIR__.

# File: /foo/bar/closure.php

return function() {
    return __DIR__;
};
# File: /foo/test.php

$original = require __DIR__ . '/bar/closure.php';

$clone = unserialize(serialize(SerializableClosure::from($original)))->getClosure();

// string(8) "/foo/bar"
var_dump($original());

//bool(true)
var_dump($original() === $clone());

I hope this answer you questions.