diff --git a/src/pocketmine/thread/CommonThreadPartsTrait.php b/src/pocketmine/thread/CommonThreadPartsTrait.php new file mode 100644 index 000000000..14fe40d0f --- /dev/null +++ b/src/pocketmine/thread/CommonThreadPartsTrait.php @@ -0,0 +1,82 @@ +classLoader; + } + + public function setClassLoader(?\ClassLoader $loader = null) : void{ + $this->composerAutoloaderPath = \pocketmine\COMPOSER_AUTOLOADER_PATH; + + if($loader === null){ + $loader = Server::getInstance()->getLoader(); + } + $this->classLoader = $loader; + } + + /** + * Registers the class loader for this thread. + * + * WARNING: This method MUST be called from any descendent threads' run() method to make autoloading usable. + * If you do not do this, you will not be able to use new classes that were not loaded when the thread was started + * (unless you are using a custom autoloader). + */ + public function registerClassLoader() : void{ + if($this->composerAutoloaderPath !== null){ + require $this->composerAutoloaderPath; + } + if($this->classLoader !== null){ + $this->classLoader->register(false); + } + } + + final public function run() : void{ + error_reporting(-1); + $this->registerClassLoader(); + //set this after the autoloader is registered + \ErrorUtils::setErrorExceptionHandler(); + $this->onRun(); + } + + /** + * Runs code on the thread. + */ + abstract protected function onRun() : void; + + public function getThreadName() : string{ + return (new \ReflectionClass($this))->getShortName(); + } +} diff --git a/src/pocketmine/thread/Thread.php b/src/pocketmine/thread/Thread.php index c69b01f11..aec708e23 100644 --- a/src/pocketmine/thread/Thread.php +++ b/src/pocketmine/thread/Thread.php @@ -23,51 +23,14 @@ declare(strict_types=1); namespace pocketmine\thread; -use pocketmine\Server; -use function error_reporting; - /** * This class must be extended by all custom threading classes */ abstract class Thread extends \Thread{ - - /** @var \ClassLoader|null */ - protected $classLoader; - /** @var string|null */ - protected $composerAutoloaderPath; - - protected $isKilled = false; - - public function getClassLoader(){ - return $this->classLoader; - } - - public function setClassLoader(?\ClassLoader $loader = null) : void{ - $this->composerAutoloaderPath = \pocketmine\COMPOSER_AUTOLOADER_PATH; - - if($loader === null){ - $loader = Server::getInstance()->getLoader(); - } - $this->classLoader = $loader; - } - - /** - * Registers the class loader for this thread. - * - * WARNING: This method MUST be called from any descendent threads' run() method to make autoloading usable. - * If you do not do this, you will not be able to use new classes that were not loaded when the thread was started - * (unless you are using a custom autoloader). - */ - public function registerClassLoader() : void{ - if($this->composerAutoloaderPath !== null){ - require $this->composerAutoloaderPath; - } - if($this->classLoader !== null){ - $this->classLoader->register(false); - } - } + use CommonThreadPartsTrait; public function start(?int $options = \PTHREADS_INHERIT_ALL) : bool{ + //this is intentionally not traitified ThreadManager::getInstance()->add($this); if($this->getClassLoader() === null){ @@ -76,19 +39,6 @@ abstract class Thread extends \Thread{ return parent::start($options); } - final public function run() : void{ - error_reporting(-1); - $this->registerClassLoader(); - //set this after the autoloader is registered - \ErrorUtils::setErrorExceptionHandler(); - $this->onRun(); - } - - /** - * Runs code on the thread. - */ - abstract protected function onRun() : void; - /** * Stops the thread using the best way possible. Try to stop it yourself before calling this. */ @@ -102,8 +52,4 @@ abstract class Thread extends \Thread{ ThreadManager::getInstance()->remove($this); } - - public function getThreadName() : string{ - return (new \ReflectionClass($this))->getShortName(); - } } diff --git a/src/pocketmine/thread/Worker.php b/src/pocketmine/thread/Worker.php index fc48fb212..75b106a82 100644 --- a/src/pocketmine/thread/Worker.php +++ b/src/pocketmine/thread/Worker.php @@ -23,51 +23,14 @@ declare(strict_types=1); namespace pocketmine\thread; -use pocketmine\Server; -use function error_reporting; - /** * This class must be extended by all custom threading classes */ abstract class Worker extends \Worker{ - - /** @var \ClassLoader|null */ - protected $classLoader; - /** @var string|null */ - protected $composerAutoloaderPath; - - protected $isKilled = false; - - public function getClassLoader(){ - return $this->classLoader; - } - - public function setClassLoader(?\ClassLoader $loader = null) : void{ - $this->composerAutoloaderPath = \pocketmine\COMPOSER_AUTOLOADER_PATH; - - if($loader === null){ - $loader = Server::getInstance()->getLoader(); - } - $this->classLoader = $loader; - } - - /** - * Registers the class loader for this thread. - * - * WARNING: This method MUST be called from any descendent threads' run() method to make autoloading usable. - * If you do not do this, you will not be able to use new classes that were not loaded when the thread was started - * (unless you are using a custom autoloader). - */ - public function registerClassLoader() : void{ - if($this->composerAutoloaderPath !== null){ - require $this->composerAutoloaderPath; - } - if($this->classLoader !== null){ - $this->classLoader->register(false); - } - } + use CommonThreadPartsTrait; public function start(?int $options = \PTHREADS_INHERIT_ALL) : bool{ + //this is intentionally not traitified ThreadManager::getInstance()->add($this); if($this->getClassLoader() === null){ @@ -76,17 +39,6 @@ abstract class Worker extends \Worker{ return parent::start($options); } - final public function run() : void{ - error_reporting(-1); - $this->registerClassLoader(); - $this->onRun(); - } - - /** - * Runs code on the thread. - */ - abstract protected function onRun() : void; - /** * Stops the thread using the best way possible. Try to stop it yourself before calling this. */ @@ -101,8 +53,4 @@ abstract class Worker extends \Worker{ ThreadManager::getInstance()->remove($this); } - - public function getThreadName() : string{ - return (new \ReflectionClass($this))->getShortName(); - } }