Skip to content

Commit

Permalink
Support return type hint mixed for Swoole 4.9
Browse files Browse the repository at this point in the history
  • Loading branch information
limingxinleo committed Nov 26, 2021
1 parent af6bc28 commit 136be3c
Show file tree
Hide file tree
Showing 6 changed files with 237 additions and 130 deletions.
1 change: 0 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,4 @@ jobs:
run: docker run --entrypoint php -p 9501:9501 -d swoole:latest examples/http_server.php
- name: Run Test Cases
run: |
composer analyse
composer test
158 changes: 113 additions & 45 deletions src/Channel.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,61 +14,129 @@
use Hyperf\Engine\Contract\ChannelInterface;
use Hyperf\Engine\Exception\RuntimeException;

class Channel extends \Swoole\Coroutine\Channel implements ChannelInterface
{
/**
* @var bool
*/
protected $closed = false;

public function getCapacity()
if (PHP_VERSION_ID > 80000 && SWOOLE_VERSION_ID >= 40900) {
class Channel extends \Swoole\Coroutine\Channel implements ChannelInterface
{
return $this->capacity;
}
protected bool $closed = false;

public function getLength()
{
return $this->length();
}
public function push(mixed $data, float $timeout = -1): bool
{
return parent::push($data, $timeout);
}

public function isAvailable()
{
return ! $this->isClosing();
}
public function pop(float $timeout = -1): mixed
{
return parent::pop($timeout);
}

public function close()
{
$this->closed = true;
parent::close();
}
public function getCapacity(): int
{
return $this->capacity;
}

public function hasProducers()
{
throw new RuntimeException('Not supported.');
}
public function getLength(): int
{
return $this->length();
}

public function hasConsumers()
{
throw new RuntimeException('Not supported.');
}
public function isAvailable(): bool
{
return ! $this->isClosing();
}

public function isReadable()
{
throw new RuntimeException('Not supported.');
}
public function close(): bool
{
$this->closed = true;
return parent::close();
}

public function isWritable()
{
throw new RuntimeException('Not supported.');
}
public function hasProducers(): bool
{
throw new RuntimeException('Not supported.');
}

public function isClosing()
{
return $this->closed || $this->errCode === SWOOLE_CHANNEL_CLOSED;
}
public function hasConsumers(): bool
{
throw new RuntimeException('Not supported.');
}

public function isReadable(): bool
{
throw new RuntimeException('Not supported.');
}

public function isWritable(): bool
{
throw new RuntimeException('Not supported.');
}

public function isClosing(): bool
{
return $this->closed || $this->errCode === SWOOLE_CHANNEL_CLOSED;
}

public function isTimeout()
public function isTimeout(): bool
{
return ! $this->closed && $this->errCode === SWOOLE_CHANNEL_TIMEOUT;
}
}
} else {
class Channel extends \Swoole\Coroutine\Channel implements ChannelInterface
{
return ! $this->closed && $this->errCode === SWOOLE_CHANNEL_TIMEOUT;
/**
* @var bool
*/
protected $closed = false;

public function getCapacity(): int
{
return $this->capacity;
}

public function getLength(): int
{
return $this->length();
}

public function isAvailable(): bool
{
return ! $this->isClosing();
}

public function close(): bool
{
$this->closed = true;
return parent::close();
}

public function hasProducers(): bool
{
throw new RuntimeException('Not supported.');
}

public function hasConsumers(): bool
{
throw new RuntimeException('Not supported.');
}

public function isReadable(): bool
{
throw new RuntimeException('Not supported.');
}

public function isWritable(): bool
{
throw new RuntimeException('Not supported.');
}

public function isClosing(): bool
{
return $this->closed || $this->errCode === SWOOLE_CHANNEL_CLOSED;
}

public function isTimeout(): bool
{
return ! $this->closed && $this->errCode === SWOOLE_CHANNEL_TIMEOUT;
}
}
}
196 changes: 120 additions & 76 deletions src/Contract/ChannelInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,80 +11,124 @@
*/
namespace Hyperf\Engine\Contract;

interface ChannelInterface
{
/**
* @param mixed $data [required]
* @param float|int $timeout [optional] = -1
* @return bool
*/
public function push($data, $timeout = -1);

/**
* @param float $timeout seconds [optional] = -1
* @return mixed when pop failed, return false
*/
public function pop($timeout = -1);

/**
* Swow: When the channel is closed, all the data in it will be destroyed.
* Swoole: When the channel is closed, the data in it can still be popped out, but push behavior will no longer succeed.
* @return mixed
*/
public function close();

/**
* @return int
*/
public function getCapacity();

/**
* @return int
*/
public function getLength();

/**
* @return bool
*/
public function isAvailable();

/**
* @return bool
*/
public function hasProducers();

/**
* @return bool
*/
public function hasConsumers();

/**
* @return bool
*/
public function isEmpty();

/**
* @return bool
*/
public function isFull();

/**
* @return bool
*/
public function isReadable();

/**
* @return bool
*/
public function isWritable();

/**
* @return bool
*/
public function isClosing();

/**
* @return bool
*/
public function isTimeout();
if (PHP_VERSION_ID > 80000 && SWOOLE_VERSION_ID >= 40900) {
interface ChannelInterface
{
/**
* @param float|int $timeout [optional] = -1
*/
public function push(mixed $data, float $timeout = -1): bool;

/**
* @param float $timeout seconds [optional] = -1
* @return mixed when pop failed, return false
*/
public function pop(float $timeout = -1): mixed;

/**
* Swow: When the channel is closed, all the data in it will be destroyed.
* Swoole: When the channel is closed, the data in it can still be popped out, but push behavior will no longer succeed.
*/
public function close(): bool;

public function getCapacity(): int;

public function getLength(): int;

public function isAvailable(): bool;

public function hasProducers(): bool;

public function hasConsumers(): bool;

public function isEmpty(): bool;

public function isFull(): bool;

public function isReadable(): bool;

public function isWritable(): bool;

public function isClosing(): bool;

public function isTimeout(): bool;
}
} else {
interface ChannelInterface
{
/**
* @param mixed $data [required]
* @param float|int $timeout [optional] = -1
* @return bool
*/
public function push($data, $timeout = -1);

/**
* @param float $timeout seconds [optional] = -1
* @return mixed when pop failed, return false
*/
public function pop($timeout = -1);

/**
* Swow: When the channel is closed, all the data in it will be destroyed.
* Swoole: When the channel is closed, the data in it can still be popped out, but push behavior will no longer succeed.
* @return mixed
*/
public function close(): bool;

/**
* @return int
*/
public function getCapacity();

/**
* @return int
*/
public function getLength();

/**
* @return bool
*/
public function isAvailable();

/**
* @return bool
*/
public function hasProducers();

/**
* @return bool
*/
public function hasConsumers();

/**
* @return bool
*/
public function isEmpty();

/**
* @return bool
*/
public function isFull();

/**
* @return bool
*/
public function isReadable();

/**
* @return bool
*/
public function isWritable();

/**
* @return bool
*/
public function isClosing();

/**
* @return bool
*/
public function isTimeout();
}
}
5 changes: 1 addition & 4 deletions src/Contract/Http/ClientInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@

interface ClientInterface
{
/**
* @return $this
*/
public function set(array $settings);
public function set(array $settings): bool;

/**
* @param string[][] $headers
Expand Down
Loading

0 comments on commit 136be3c

Please sign in to comment.