Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Version 1.3 #4

Merged
merged 5 commits into from
Oct 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
.gitattributes export-ignore
.github export-ignore
.gitignore export-ignore
docs/ export-ignore
Makefile export-ignore
phpunit.xml.dist export-ignore
tests/ export-ignore
README.md export-ignore
tests export-ignore
121 changes: 49 additions & 72 deletions .github/workflows/acceptance.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,78 +3,33 @@ name: Acceptance
on: [push, pull_request]

jobs:
test-7-4:
runs-on: ubuntu-latest
name: Test PHP 7.4
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Set up PHP 7.4
uses: shivammathur/setup-php@v2
with:
php-version: '7.4'
- name: Composer
run: make deps-install
- name: Test
run: make test

test-8-0:
runs-on: ubuntu-latest
name: Test PHP 8.0
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Set up PHP 8.0
uses: shivammathur/setup-php@v2
with:
php-version: '8.0'
- name: Composer
run: make deps-install
- name: Test
run: make test

test-8-1:
test:
strategy:
matrix:
php-versions: ["7.4", "8.0", "8.1", "8.2", "8.3"]
runs-on: ubuntu-latest
name: Test PHP 8.1
name: Unit test
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Set up PHP 8.1
- name: Set up PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.1'
- name: Composer
run: make deps-install
- name: Test
run: make test

test-8-2:
runs-on: ubuntu-latest
name: Test PHP 8.2
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Set up PHP 8.2
uses: shivammathur/setup-php@v2
with:
php-version: '8.2'
- name: Composer
run: make deps-install
- name: Test
run: make test

test-8-3:
runs-on: ubuntu-latest
name: Test PHP 8.3
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Set up PHP 8.3
uses: shivammathur/setup-php@v2
php-version: ${{ matrix.php-versions }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Get composer cache directory
id: composer-cache
run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT
- name: Cache dependencies
uses: actions/cache@v3
with:
php-version: '8.3'
- name: Composer
run: make deps-install
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }}
restore-keys: ${{ runner.os }}-composer-
- name: Install dependencies
run: composer install --prefer-dist
- name: Test
run: make test

Expand All @@ -84,12 +39,23 @@ jobs:
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Set up PHP 8.2
- name: Set up PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.2'
- name: Composer
run: make deps-install
php-version: "8.2"
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Get composer cache directory
id: composer-cache
run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT
- name: Cache dependencies
uses: actions/cache@v3
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }}
restore-keys: ${{ runner.os }}-composer-
- name: Install dependencies
run: composer install --prefer-dist
- name: Code standard
run: make cs-check

Expand All @@ -99,13 +65,24 @@ jobs:
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Set up PHP 8.2
- name: Set up PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.2'
php-version: "8.2"
extensions: xdebug
- name: Composer
run: make deps-install
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Get composer cache directory
id: composer-cache
run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT
- name: Cache dependencies
uses: actions/cache@v3
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }}
restore-keys: ${{ runner.os }}-composer-
- name: Install dependencies
run: composer install --prefer-dist
- name: Code coverage
env:
COVERALLS_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Expand Down
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ class SocketStream extends Stream {
public function setBlocking(bool $enable): bool; // Change blocking mode
public function setTimeout(int $seconds, int $microseconds = 0): bool; // Set timeout
public function readLine(int $length): ?string // Read a line from stream, up to $length bytes
public function closeRead(): void; // Closes the stream for further reading
public function closeWrite(): void; // Closes the stream for further writing
}
```

Expand Down Expand Up @@ -173,7 +175,7 @@ class StreamFactory implements StreamFactoryInterface {

## StreamException class

The `Phrity\Net\StreamException` is thrown when astream related error occurs.
The `Phrity\Net\StreamException` is thrown when a stream related error occurs.

```php
class StreamException extends RuntimeException {
Expand All @@ -188,6 +190,7 @@ class StreamException extends RuntimeException {

| Version | PHP | |
| --- | --- | --- |
| `1.3` | `^7.4\|^8.0` | Closing read and write separately |
| `1.2` | `^7.4\|^8.0` | Socket client |
| `1.1` | `^7.4\|^8.0` | Stream collection |
| `1.0` | `^7.4\|^8.0` | Initial version |
13 changes: 7 additions & 6 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" colors="true" bootstrap="vendor/autoload.php" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage>
<include>
<directory suffix=".php">./src/</directory>
</include>
</coverage>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" colors="true" bootstrap="vendor/autoload.php" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.2/phpunit.xsd">
<coverage/>
<testsuites>
<testsuite name="Phrity tests">
<directory>./tests/</directory>
</testsuite>
</testsuites>
<source>
<include>
<directory suffix=".php">./src/</directory>
</include>
</source>
</phpunit>
29 changes: 29 additions & 0 deletions src/SocketStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,33 @@ public function readLine(int $length): ?string
return $result === false ? null : $result;
}, new StreamException(StreamException::FAIL_GETS));
}

/**
* Closes the stream for further reading.
* @return void
*/
public function closeRead(): void
{
if ($this->readable && $this->writable) {
stream_socket_shutdown($this->stream, STREAM_SHUT_RD);
$this->evalStream();
} elseif (!$this->writable) {
$this->close();
}
$this->readable = false;
}
/**
* Closes the stream for further writing.
* @return void
*/
public function closeWrite(): void
{
if ($this->readable && $this->writable) {
$x = stream_socket_shutdown($this->stream, STREAM_SHUT_WR);
$this->evalStream();
} elseif (!$this->readable) {
$this->close();
}
$this->writable = false;
}
}
2 changes: 1 addition & 1 deletion src/StreamException.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*/
class StreamException extends RuntimeException
{
// Stream errores
// Stream errors
public const STREAM_DETACHED = 1000;
public const NOT_READABLE = 1010;
public const NOT_WRITABLE = 1011;
Expand Down
34 changes: 34 additions & 0 deletions tests/SocketStreamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,38 @@ public function testWriteOnlyReadLineError(): void
$this->expectExceptionMessage('Stream is not readable.');
$stream->readLine(1024);
}

public function testReadClose(): void
{
$factory = new StreamFactory();
$resource = fopen(__DIR__ . '/fixtures/stream.txt', 'r+');
$stream = $factory->createSocketStreamFromResource($resource);
$this->assertTrue($stream->isReadable());
$this->assertTrue($stream->isWritable());
$stream->closeRead();
$this->assertFalse($stream->isReadable());
$this->assertTrue($stream->isWritable());
$this->assertTrue($stream->isConnected());
$stream->closeWrite();
$this->assertFalse($stream->isReadable());
$this->assertFalse($stream->isWritable());
$this->assertFalse($stream->isConnected());
}

public function testWriteClose(): void
{
$factory = new StreamFactory();
$resource = fopen(__DIR__ . '/fixtures/stream.txt', 'r+');
$stream = $factory->createSocketStreamFromResource($resource);
$this->assertTrue($stream->isReadable());
$this->assertTrue($stream->isWritable());
$stream->closeWrite();
$this->assertTrue($stream->isReadable());
$this->assertFalse($stream->isWritable());
$this->assertTrue($stream->isConnected());
$stream->closeRead();
$this->assertFalse($stream->isReadable());
$this->assertFalse($stream->isWritable());
$this->assertFalse($stream->isConnected());
}
}