Skip to content

Commit

Permalink
Locate autoloader file in thread
Browse files Browse the repository at this point in the history
  • Loading branch information
trowski committed Nov 25, 2017
1 parent 29c3aad commit 5af44d5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
34 changes: 24 additions & 10 deletions lib/Thread/Internal/Thread.php
Expand Up @@ -71,19 +71,33 @@ public function __construct($socket, callable $function, array $args = []) {
* @codeCoverageIgnore Only executed in thread.
*/
public function run() {
foreach (\get_declared_classes() as $className) {
if (\strpos($className, "ComposerAutoloaderInit") === 0) {
$autoloaderClassName = $className;
break;
/* First thing we need to do is re-initialize the class autoloader. If
* we don't do this first, any object of a class that was loaded after
* the thread started will just be garbage data and unserializable
* values (like resources) will be lost. This happens even with
* thread-safe objects.
*/

// Protect scope by using an unbound closure (protects static access as well).
(static function () {
$paths = [
\dirname(__DIR__, 3) . \DIRECTORY_SEPARATOR . "vendor" . \DIRECTORY_SEPARATOR . "autoload.php",
\dirname(__DIR__, 5) . \DIRECTORY_SEPARATOR . "autoload.php",
];

foreach ($paths as $path) {
if (\file_exists($path)) {
$autoloadPath = $path;
break;
}
}
}

if (!isset($autoloaderClassName)) {
throw new ContextException("Could not find composer autoloader class name");
}
if (!isset($autoloadPath)) {
throw new ContextException("Could not locate autoload.php");
}

$loader = $autoloaderClassName::getLoader();
$loader->register(true);
require $autoloadPath;
})->bindTo(null, null)();

if ($this->killed) {
return; // Thread immediately killed after start() was called.
Expand Down
2 changes: 1 addition & 1 deletion lib/Thread/Thread.php
Expand Up @@ -145,7 +145,7 @@ public function start() {

$this->thread = new Internal\Thread($this->socket, $this->function, $this->args);

if (!$this->thread->start(\PTHREADS_INHERIT_INI | \PTHREADS_INHERIT_CLASSES)) {
if (!$this->thread->start(\PTHREADS_INHERIT_INI)) {
throw new ContextException('Failed to start the thread.');
}

Expand Down

0 comments on commit 5af44d5

Please sign in to comment.