Support yield return polyfill

This commit is contained in:
Daniil Gentili 2018-12-27 02:47:21 +00:00
parent fadf967e6a
commit 69a2131206
4 changed files with 69 additions and 8 deletions

View File

@ -56,7 +56,8 @@
"src/Worker.php",
"src/Pool.php",
"src/HttpProxy.php",
"src/SocksProxy.php"
"src/SocksProxy.php",
"src/YieldReturnValue.php"
]
}
}

30
src/YieldReturnValue.php Normal file
View File

@ -0,0 +1,30 @@
<?php
/**
* Yield return value PHP5 polyfill.
*
* This file is part of MadelineProto.
* MadelineProto is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
* MadelineProto is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Affero General Public License for more details.
* You should have received a copy of the GNU General Public License along with MadelineProto.
* If not, see <http://www.gnu.org/licenses/>.
*
* @author Daniil Gentili <daniil@daniil.it>
* @copyright 2016-2018 Daniil Gentili <daniil@daniil.it>
* @license https://opensource.org/licenses/AGPL-3.0 AGPLv3
*
* @link https://docs.madelineproto.xyz MadelineProto documentation
*/
class YieldReturnValue
{
private $value;
public function __construct($value)
{
$this->value = $value;
}
public function getReturn()
{
return $this->value;
}
}

View File

@ -34,6 +34,8 @@ use Amp\Internal;
use Amp\Promise;
use Amp\Success;
use React\Promise\PromiseInterface as ReactPromise;
/**
* Creates a promise from a generator function yielding promises.
*
@ -65,8 +67,18 @@ final class Coroutine implements Promise
try {
$yielded = $this->generator->current();
if (!$yielded instanceof Promise) {
if ($yielded instanceof \YieldReturnValue) {
$this->resolve($yielded->getReturn());
return;
}
if (!$this->generator->valid()) {
$this->resolve($this->generator->getReturn());
if (method_exists($this->generator, 'getReturn')) {
$this->resolve($this->generator->getReturn());
} else {
$this->resolve(null);
}
return;
}
@ -100,8 +112,19 @@ final class Coroutine implements Promise
$yielded = $this->generator->send($this->value);
}
if (!$yielded instanceof Promise) {
if ($yielded instanceof \YieldReturnValue) {
$this->resolve($yielded->getReturn());
$this->onResolve = null;
return;
}
if (!$this->generator->valid()) {
$this->resolve($this->generator->getReturn());
if (method_exists($this->generator, 'getReturn')) {
$this->resolve($this->generator->getReturn());
} else {
$this->resolve(null);
}
$this->onResolve = null;
return;
@ -146,6 +169,10 @@ final class Coroutine implements Promise
if ($yielded instanceof \Generator) {
return new self($yielded);
}
if ($yielded instanceof ReactPromise) {
return Promise\adapt($yielded);
}
// No match, continue to returning Failure below.
} catch (\Throwable $exception) {
// Conversion to promise failed, fall-through to returning Failure below.

View File

@ -1,6 +1,6 @@
#!/bin/bash -e
#composer global require spatie/7to5 dev-master#7b3e0f4254aadd81cf1a7ef2ddad68d5fcdadcc1
composer global require spatie/7to5 dev-master#171ed9b96940f12cb348ffd11f9be4f596bb0146
[ -f $HOME/.composer/vendor/bin/php7to5 ] && php7to5=$HOME/.composer/vendor/bin/php7to5
[ -f $HOME/.config/composer/vendor/bin/php7to5 ] && php7to5=$HOME/.config/composer/vendor/bin/php7to5
@ -35,11 +35,14 @@ composer update
cp -a ../src vendor/danog/madelineproto
cd ..
cp -a phar7 phar5
#$php7to5 convert --copy-all phar7 phar5 >/dev/null
#cp -a phar7 phar5
$php7to5 convert --copy-all phar7 phar5 >/dev/null
find phar5 -type f -exec sed 's/\w* \.\.\./.../' -i {} +
#sed 's/^Loop::set.*;//g' -i phar5/vendor/amphp/amp/lib/Loop.php
#echo 'Loop::set((new DriverFactory())->create());' >> phar5/vendor/amphp/amp/lib/Loop.php
sed 's/^Loop::set.*;//g' -i phar5/vendor/amphp/amp/lib/Loop.php
echo 'Loop::set((new DriverFactory())->create());' >> phar5/vendor/amphp/amp/lib/Loop.php
cp phar5/vendor/danog/madelineproto/src/danog/MadelineProto/Coroutine.php phar5/vendor/amphp/amp/lib/
sed -i 's/namespace danog\\MadelineProto;//g' phar5/vendor/amphp/amp/lib/Coroutine.php
php makephar.php phar5 madeline.phar $(cat .git/refs/heads/master)