swoole-src
swoole-src copied to clipboard
Feature request: getContext() to return custom class inheriting Coroutine\Context
Please answer these questions before submitting your issue. Thanks!
- What did you do? If possible, provide a simple script for reproducing the error.
Currently, there is no way to extend the Coroutine\Context class. A workaround would be to override the Swoole\Coroutine::getContext() method to return an instance of a custom class that wraps around the Swoole\Coroutine\Context class. The suggestion is to getContext() to support an argument like: getContext(string $class_name = 'Swoole\Coroutine\Context') So a custom provided class name (which inherits Coroutine\Context) can be provided instead. The same way it works for: https://www.php.net/manual/en/function.simplexml-load-file.php
-
What did you expect to see?
-
What did you see instead?
-
What version of Swoole are you using (show your
php --ri swoole
)?
Swoole => enabled Author => Swoole Team [email protected] Version => 4.3.5 Built => Jul 3 2019 10:42:48 coroutine => enabled epoll => enabled eventfd => enabled signalfd => enabled cpu_affinity => enabled spinlock => enabled rwlock => enabled http2 => enabled zlib => enabled mutex_timedlock => enabled pthread_barrier => enabled futex => enabled async_redis => enabled
Directive => Local Value => Master Value swoole.enable_coroutine => On => On swoole.display_errors => On => On swoole.use_shortname => On => On swoole.unixsock_buffer_size => 8388608 => 8388608
- What is your machine environment used (including version of kernel & php & gcc) ?
Perhaps actually before this is considered the API for getContext() should be reviewed. Perhaps it will be better getContext() to accept as first argument the $coroutine_id for which we want to retrieve the context and only then to support an optional class name. So a better method signature would be: getContext(int $cid, string $class_name = 'Swoole\Coroutine\Context')
I meant: getContext(int $cid = 0, string $class_name = 'Swoole\Coroutine\Context') Or getContext(?int $cid = NULL, string $class_name = 'Swoole\Coroutine\Context')
P.S. I just noticed that in 4.3.5 it actually has $cid, the docs I was reading are older.
why not Co::getContext()['foo'] = $anything_you_want
We needed to have custom methods on this class. Of course, we could use closures and assign them as properties which will act as a method but this is monkey patching and not a good practice. $context = Co::getContext(); $context->do_something = function(){}; $context->do_something(); And it becomes more complex if static methods are needed. So we currently do a wrapper class around Swoole\Coroutine\Context
After some more thought actually having another argument for the class is not a good idea (as it has to be provided on every getContext call and also an error should be added if a different class is provided). Instead, the best would be to have it as an option to Coroutine like:
Coroutine::set( ['context_class' => Some\CustomClass::class]);