Skip to content

Commit

Permalink
typos
Browse files Browse the repository at this point in the history
  • Loading branch information
rodber committed Jan 8, 2024
1 parent 76ba5da commit a6105b9
Show file tree
Hide file tree
Showing 9 changed files with 205 additions and 34 deletions.
39 changes: 39 additions & 0 deletions .github/banner/writer-logo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added .github/banner/writer-social.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
91 changes: 89 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,100 @@
[![Technical Debt](https://sonarcloud.io/api/project_badges/measure?project=chevere_writer&metric=sqale_index)](https://sonarcloud.io/dashboard?id=chevere_writer)
[![CodeFactor](https://www.codefactor.io/repository/github/chevere/writer/badge)](https://www.codefactor.io/repository/github/chevere/writer)

![Writer](.github/banner/writer-logo.svg)

## Summary

Writer provides tooling for writing to streams.

## Installing

Writer is available through [Packagist](https://packagist.org/packages/chevere/writer) and the repository source is at [chevere/writer](https://github.com/chevere/writer).

```sh
composer require chevere/writer
```

## StreamWriter

Use `StreamWriter` to write strings to a stream.

```php
use Chevere\Writer\StreamWriter;
use function Chevere\Writer\streamFor;

$stream = streamFor('php://output', 'r');
$writer = new StreamWriter($stream);
$writer->write('Hello, world!');
```

## NullWriter

Use `NullWriter` when requiring `null` write override.

## Writers

Use `Writers` to interact with pre-defined streams for output, error, debug and log. By default only output and error streams are defined.

| Stream | Default |
| ------ | ------------ |
| output | StreamWriter |
| error | StreamWriter |
| debug | NullWriter |
| log | NullWriter |

```php
use Chevere\Writer\Writers;

$writers = new Writers();
$writers->error();
$writers->debug();
$writers->log();
```

### Output stream

Use `output` to interact with the output stream. Use `withOutput` to set a custom output stream.

```php
$with = $writers->withOutput($stream);
$with->output(); // $stream
```

### Error stream

Use `error` to interact with the error stream. Use `withError` to set a custom error stream.

```php
$with = $writers->withError($stream);
$with->error(); // $stream
```

### Debug stream

Use `debug` to interact with the debug stream. Use `withDebug` to set a custom debug stream.

```php
$with = $writers->withDebug($stream);
$with->debug(); // $stream
```

### Log stream

Use `log` to interact with the log stream. Use `withLog` to set a custom log stream.

```php
$with = $writers->withLog($stream);
$with->log(); // $stream
```

## Documentation

Documentation is available at [chevere.org](https://chevere.org/).
Documentation is available at [chevere.org](https://chevere.org/packages/writer).

## License

Copyright 2023 [Rodolfo Berrios A.](https://rodolfoberrios.com/)
Copyright [Rodolfo Berrios A.](https://rodolfoberrios.com/)

Chevere is licensed under the Apache License, Version 2.0. See [LICENSE](LICENSE) for the full license text.

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "chevere/writer",
"description": "A chevere writer package",
"description": "Tooling for writing to streams",
"homepage": "https://chevere.org",
"type": "library",
"license": "Apache-2.0",
Expand Down
5 changes: 2 additions & 3 deletions src/StreamWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
use Psr\Http\Message\StreamInterface;
use RuntimeException;
use Throwable;
use function Chevere\Message\message;

/**
* @codeCoverageIgnore
Expand All @@ -31,7 +30,7 @@ public function __construct(
) {
if (! $this->stream->isWritable()) {
throw new InvalidArgumentException(
(string) message('Stream provided is not writable')
'Stream provided is not writable'
);
}
}
Expand All @@ -48,7 +47,7 @@ public function write(string $string): void
} catch (Throwable $e) {
throw new RuntimeException(
previous: $e,
message: (string) message('Unable to write provided string')
message: 'Unable to write provided string'
);
}
}
Expand Down
8 changes: 6 additions & 2 deletions src/WritersInstance.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

use Chevere\Writer\Interfaces\WritersInterface;
use LogicException;
use function Chevere\Message\message;

final class WritersInstance
{
Expand All @@ -26,11 +25,16 @@ public function __construct(WritersInterface $writers)
self::$instance = $writers;
}

public function __destruct()
{
self::$instance = null;
}

public static function get(): WritersInterface
{
if (! isset(self::$instance)) {
throw new LogicException(
(string) message('No writers instance present')
'No writers instance present'
);
}

Expand Down
40 changes: 14 additions & 26 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,52 +14,40 @@
namespace Chevere\Writer;

use Chevere\Writer\Interfaces\WritersInterface;
use ErrorException;
use InvalidArgumentException;
use Nyholm\Psr7\Stream;
use Psr\Http\Message\StreamInterface;
use Throwable;
use function Chevere\Message\message;
use function Safe\fopen;

/**
* @codeCoverageIgnore
*/
function writers(): WritersInterface
{
return WritersInstance::get();
}

/**
* @codeCoverageIgnore
*
* @throws InvalidArgumentException
*/
function streamFor(string $uri, string $mode): StreamInterface
{
try {
return Stream::create(fopen($uri, $mode));
} catch (Throwable $e) {
throw new InvalidArgumentException(
previous: $e,
message: (string) message(
'Unable to create stream for `%uri%`',
uri: $uri
)
error_clear_last();
$fopen = @fopen($uri, $mode);
if ($fopen === false) {
$error = error_get_last();

throw new ErrorException(
message: $error['message'] ?? 'An error occured',
code: 0,
severity: $error['type'] ?? 1
);
}

return Stream::create($fopen);
}

/**
* @codeCoverageIgnore
* @throws InvalidArgumentException
*/
function streamTemp(string $content = ''): StreamInterface
{
try {
return Stream::create($content);
} catch (Throwable $e) {
throw new InvalidArgumentException(
previous: $e,
message: (string) message('Unable to create temp stream')
);
}
return Stream::create($content);
}
53 changes: 53 additions & 0 deletions tests/FunctionsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

/*
* This file is part of Chevere.
*
* (c) Rodolfo Berrios <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Chevere\Tests;

use Chevere\Writer\Writers;
use Chevere\Writer\WritersInstance;
use ErrorException;
use PHPUnit\Framework\TestCase;
use function Chevere\Writer\streamFor;
use function Chevere\Writer\writers;

final class FunctionsTest extends TestCase
{
public function testWritersInstance(): void
{
$writers = new Writers();
$instance = new WritersInstance($writers);
$this->assertSame($instance->get(), writers());
unset($instance);
}

public function testStreamFor(): void
{
$this->expectNotToPerformAssertions();
$uri = 'php://output';
$mode = 'r+';
streamFor($uri, $mode);
}

public function testStreamForError(): void
{
$uri = '404';
$mode = 'r+';
$this->expectException(ErrorException::class);
$this->expectExceptionMessage(
<<<PLAIN
fopen(404): Failed to open stream: No such file or directory
PLAIN
);
streamFor($uri, $mode);
}
}
1 change: 1 addition & 0 deletions tests/WritersInstanceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,6 @@ public function testConstruct(): void
$writers = new Writers();
$instance = new WritersInstance($writers);
$this->assertSame($writers, $instance::get());
unset($instance);
}
}

0 comments on commit a6105b9

Please sign in to comment.