2016-12-29 19:16:04 -06:00
|
|
|
<?php
|
2015-12-04 23:50:32 -06:00
|
|
|
|
2016-08-23 16:47:40 -05:00
|
|
|
namespace Amp\Parallel\Worker;
|
2016-08-18 11:04:48 -05:00
|
|
|
|
2017-11-29 17:58:00 -06:00
|
|
|
use Amp\Parallel\Context\Thread;
|
2015-12-05 01:09:42 -06:00
|
|
|
|
2015-12-04 23:50:32 -06:00
|
|
|
/**
|
|
|
|
* The built-in worker factory type.
|
|
|
|
*/
|
2018-10-07 09:50:45 -05:00
|
|
|
class DefaultWorkerFactory implements WorkerFactory
|
|
|
|
{
|
2017-12-13 22:18:12 -06:00
|
|
|
/** @var string */
|
|
|
|
private $className;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $envClassName Name of class implementing \Amp\Parallel\Worker\Environment to instigate in each
|
|
|
|
* worker. Defaults to \Amp\Parallel\Worker\BasicEnvironment.
|
|
|
|
*
|
|
|
|
* @throws \Error If the given class name does not exist or does not implement \Amp\Parallel\Worker\Environment.
|
|
|
|
*/
|
2018-10-07 09:50:45 -05:00
|
|
|
public function __construct(string $envClassName = BasicEnvironment::class)
|
|
|
|
{
|
2017-12-13 22:18:12 -06:00
|
|
|
if (!\class_exists($envClassName)) {
|
|
|
|
throw new \Error(\sprintf("Invalid environment class name '%s'", $envClassName));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!\is_subclass_of($envClassName, Environment::class)) {
|
|
|
|
throw new \Error(\sprintf(
|
|
|
|
"The class '%s' does not implement '%s'",
|
|
|
|
$envClassName,
|
|
|
|
Environment::class
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->className = $envClassName;
|
|
|
|
}
|
|
|
|
|
2015-12-04 23:50:32 -06:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*
|
|
|
|
* The type of worker created depends on the extensions available. If multi-threading is enabled, a WorkerThread
|
2017-02-17 17:00:24 -06:00
|
|
|
* will be created. If threads are not available a WorkerProcess will be created.
|
2015-12-04 23:50:32 -06:00
|
|
|
*/
|
2018-10-07 09:50:45 -05:00
|
|
|
public function create(): Worker
|
|
|
|
{
|
2018-10-21 10:34:32 -05:00
|
|
|
if (Thread::isSupported()) {
|
2017-12-13 22:18:12 -06:00
|
|
|
return new WorkerThread($this->className);
|
2015-12-04 23:50:32 -06:00
|
|
|
}
|
|
|
|
|
2017-12-05 18:21:39 -06:00
|
|
|
return new WorkerProcess(
|
2017-12-13 22:18:12 -06:00
|
|
|
$this->className,
|
2017-12-12 21:06:11 -06:00
|
|
|
[],
|
2017-12-08 11:45:13 -06:00
|
|
|
\getenv("AMP_PHP_BINARY") ?: (\defined("AMP_PHP_BINARY") ? \AMP_PHP_BINARY : null)
|
2017-12-05 18:21:39 -06:00
|
|
|
);
|
2015-12-04 23:50:32 -06:00
|
|
|
}
|
|
|
|
}
|