Skip to content

Commit

Permalink
try
Browse files Browse the repository at this point in the history
  • Loading branch information
rodber committed Dec 2, 2023
1 parent cb6a9b8 commit c614cbd
Show file tree
Hide file tree
Showing 18 changed files with 614 additions and 26 deletions.
16 changes: 8 additions & 8 deletions .vscode/templates.code-snippets
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"body": [
"<?php",
"",
"namespace Chevere\\%namespace%;",
"namespace Chevere\\Writer;",
"",
"/**",
" * Describes the component in charge of ${1:doing}.",
Expand All @@ -22,7 +22,7 @@
"body": [
"<?php",
"",
"namespace Chevere\\%namespace%;",
"namespace Chevere\\Writer;",
"",
"final class $TM_FILENAME_BASE",
"{",
Expand All @@ -36,7 +36,7 @@
"body": [
"<?php",
"",
"namespace Chevere\\%namespace%;",
"namespace Chevere\\Writer;",
"",
"use Chevere\\Exceptions\\Core\\Exception;",
"",
Expand All @@ -54,7 +54,7 @@
"body": [
"<?php",
"",
"namespace Chevere\\%namespace%;",
"namespace Chevere\\Writer;",
"",
"trait $TM_FILENAME_BASE",
"{",
Expand All @@ -68,7 +68,7 @@
"body": [
"<?php",
"",
"namespace Chevere\\%namespace%\\\\${TM_DIRECTORY/.*src\\/(([^\\/]*)(\\/)?)|(\\/)([^\\/]*)/$2${3:+.}${5:+.}$5/g};",
"namespace Chevere\\Writer\\\\${TM_DIRECTORY/.*src\\/(([^\\/]*)(\\/)?)|(\\/)([^\\/]*)/$2${3:+.}${5:+.}$5/g};",
"",
"/**",
" * Describes the component in charge of ${1:doing}.",
Expand All @@ -85,7 +85,7 @@
"body": [
"<?php",
"",
"namespace Chevere\\%namespace%\\\\${TM_DIRECTORY/.*src\\/(([^\\/]*)(\\/)?)|(\\/)([^\\/]*)/$2${3:+.}${5:+.}$5/g};",
"namespace Chevere\\Writer\\\\${TM_DIRECTORY/.*src\\/(([^\\/]*)(\\/)?)|(\\/)([^\\/]*)/$2${3:+.}${5:+.}$5/g};",
"",
"final class $TM_FILENAME_BASE",
"{",
Expand All @@ -99,7 +99,7 @@
"body": [
"<?php",
"",
"namespace Chevere\\%namespace%\\\\${TM_DIRECTORY/.*src\\/(([^\\/]*)(\\/)?)|(\\/)([^\\/]*)/$2${3:+.}${5:+.}$5/g};",
"namespace Chevere\\Writer\\\\${TM_DIRECTORY/.*src\\/(([^\\/]*)(\\/)?)|(\\/)([^\\/]*)/$2${3:+.}${5:+.}$5/g};",
"",
"use Chevere\\Exceptions\\Core\\Exception;",
"",
Expand All @@ -117,7 +117,7 @@
"body": [
"<?php",
"",
"namespace Chevere\\%namespace%\\\\${TM_DIRECTORY/.*src\\/(([^\\/]*)(\\/)?)|(\\/)([^\\/]*)/$2${3:+.}${5:+.}$5/g};",
"namespace Chevere\\Writer\\\\${TM_DIRECTORY/.*src\\/(([^\\/]*)(\\/)?)|(\\/)([^\\/]*)/$2${3:+.}${5:+.}$5/g};",
"",
"trait $TM_FILENAME_BASE",
"{",
Expand Down
26 changes: 13 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
# %namespace%
# Writer

> 🔔 Subscribe to the [newsletter](https://chv.to/chevere-newsletter) to don't miss any update regarding Chevere.
![Chevere](chevere.svg)

[![Build](https://img.shields.io/github/actions/workflow/status/chevere/reponame/test.yml?branch=%branch%&style=flat-square)](https://github.com/chevere/reponame/actions)
![Code size](https://img.shields.io/github/languages/code-size/chevere/reponame?style=flat-square)
[![Apache-2.0](https://img.shields.io/github/license/chevere/reponame?style=flat-square)](LICENSE)
[![Build](https://img.shields.io/github/actions/workflow/status/chevere/writer/test.yml?branch=1.0&style=flat-square)](https://github.com/chevere/writer/actions)
![Code size](https://img.shields.io/github/languages/code-size/chevere/writer?style=flat-square)
[![Apache-2.0](https://img.shields.io/github/license/chevere/writer?style=flat-square)](LICENSE)
[![PHPStan](https://img.shields.io/badge/PHPStan-level%209-blueviolet?style=flat-square)](https://phpstan.org/)
[![Mutation testing badge](https://img.shields.io/endpoint?style=flat-square&url=https%3A%2F%2Fbadge-api.stryker-mutator.io%2Fgithub.com%2Fchevere%2Freponame%2F%branch%)](https://dashboard.stryker-mutator.io/reports/github.com/chevere/reponame/%branch%)

[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=chevere_reponame&metric=alert_status)](https://sonarcloud.io/dashboard?id=chevere_reponame)
[![Maintainability Rating](https://sonarcloud.io/api/project_badges/measure?project=chevere_reponame&metric=sqale_rating)](https://sonarcloud.io/dashboard?id=chevere_reponame)
[![Reliability Rating](https://sonarcloud.io/api/project_badges/measure?project=chevere_reponame&metric=reliability_rating)](https://sonarcloud.io/dashboard?id=chevere_reponame)
[![Security Rating](https://sonarcloud.io/api/project_badges/measure?project=chevere_reponame&metric=security_rating)](https://sonarcloud.io/dashboard?id=chevere_reponame)
[![Coverage](https://sonarcloud.io/api/project_badges/measure?project=chevere_reponame&metric=coverage)](https://sonarcloud.io/dashboard?id=chevere_reponame)
[![Technical Debt](https://sonarcloud.io/api/project_badges/measure?project=chevere_reponame&metric=sqale_index)](https://sonarcloud.io/dashboard?id=chevere_reponame)
[![CodeFactor](https://www.codefactor.io/repository/github/chevere/reponame/badge)](https://www.codefactor.io/repository/github/chevere/reponame)
[![Mutation testing badge](https://img.shields.io/endpoint?style=flat-square&url=https%3A%2F%2Fbadge-api.stryker-mutator.io%2Fgithub.com%2Fchevere%2Fwriter%2F1.0)](https://dashboard.stryker-mutator.io/reports/github.com/chevere/writer/1.0)

[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=chevere_writer&metric=alert_status)](https://sonarcloud.io/dashboard?id=chevere_writer)
[![Maintainability Rating](https://sonarcloud.io/api/project_badges/measure?project=chevere_writer&metric=sqale_rating)](https://sonarcloud.io/dashboard?id=chevere_writer)
[![Reliability Rating](https://sonarcloud.io/api/project_badges/measure?project=chevere_writer&metric=reliability_rating)](https://sonarcloud.io/dashboard?id=chevere_writer)
[![Security Rating](https://sonarcloud.io/api/project_badges/measure?project=chevere_writer&metric=security_rating)](https://sonarcloud.io/dashboard?id=chevere_writer)
[![Coverage](https://sonarcloud.io/api/project_badges/measure?project=chevere_writer&metric=coverage)](https://sonarcloud.io/dashboard?id=chevere_writer)
[![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)

## Documentation

Expand Down
11 changes: 7 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "chevere/reponame",
"description": "A chevere reponame package",
"name": "chevere/writer",
"description": "A chevere writer package",
"homepage": "https://chevere.org",
"type": "library",
"license": "Apache-2.0",
Expand All @@ -12,7 +12,10 @@
}
],
"require": {
"chevere/chevere": "^3.0"
"php": "^8.1",
"chevere/message": "^0.1.0",
"nyholm/psr7": "^1.5",
"thecodingmachine/safe": "^2.5"
},
"require-dev": {
"phpstan/phpstan": "^1.9",
Expand All @@ -24,7 +27,7 @@
"src/functions.php"
],
"psr-4": {
"Chevere\\%namespace%\\": "src/"
"Chevere\\Writer\\": "src/"
}
},
"autoload-dev": {
Expand Down
2 changes: 1 addition & 1 deletion sonar-project.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
sonar.projectKey=chevere_reponame
sonar.projectKey=chevere_writer
sonar.organization=chevere
sonar.host.url=https://sonarcloud.io
sonar.sourceEncoding=UTF-8
Expand Down
Empty file removed src/.git-keep
Empty file.
29 changes: 29 additions & 0 deletions src/Interfaces/WriterInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?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\Writer\Interfaces;

use Stringable;

interface WriterInterface extends Stringable
{
/**
* Returns the contents written. Must not alter the file cursor.
*/
public function __toString(): string;

/**
* Writes the given string.
*/
public function write(string $string): void;
}
65 changes: 65 additions & 0 deletions src/Interfaces/WritersInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?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\Writer\Interfaces;

interface WritersInterface
{
/**
* Return an instance with the specified $writer for all writers.
*
* This method MUST retain the state of the current instance, and return
* an instance that contains the specified $writer for all writers.
*/
public function with(WriterInterface $writer): self;

/**
* Return an instance with the specified output WriterInterface.
*
* This method MUST retain the state of the current instance, and return
* an instance that contains the specified output WriterInterface.
*/
public function withOutput(WriterInterface $writer): self;

public function output(): WriterInterface;

/**
* Return an instance with the specified error WriterInterface.
*
* This method MUST retain the state of the current instance, and return
* an instance that contains the specified error WriterInterface.
*/
public function withError(WriterInterface $writer): self;

public function error(): WriterInterface;

/**
* Return an instance with the specified debug WriterInterface.
*
* This method MUST retain the state of the current instance, and return
* an instance that contains the specified debug WriterInterface.
*/
public function withDebug(WriterInterface $writer): self;

public function debug(): WriterInterface;

/**
* Return an instance with the specified log WriterInterface.
*
* This method MUST retain the state of the current instance, and return
* an instance that contains the specified log WriterInterface.
*/
public function withLog(WriterInterface $writer): self;

public function log(): WriterInterface;
}
34 changes: 34 additions & 0 deletions src/NullWriter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?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\Writer;

use Chevere\Writer\Interfaces\WriterInterface;

final class NullWriter implements WriterInterface
{
public function __construct()
{
// null
}

public function __toString(): string
{
return '';
}

public function write(string $string): void
{
// null
}
}
55 changes: 55 additions & 0 deletions src/StreamWriter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?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\Writer;

use Chevere\Writer\Interfaces\WriterInterface;
use InvalidArgumentException;
use Psr\Http\Message\StreamInterface;
use RuntimeException;
use Throwable;
use function Chevere\Message\message;

/**
* @codeCoverageIgnore
* @infection-ignore-all
*/
final class StreamWriter implements WriterInterface
{
public function __construct(
private StreamInterface $stream
) {
if (! $this->stream->isWritable()) {
throw new InvalidArgumentException(
(string) message('Stream provided is not writable')
);
}
}

public function __toString(): string
{
return $this->stream->__toString();
}

public function write(string $string): void
{
try {
$this->stream->write($string);
} catch (Throwable $e) {
throw new RuntimeException(
previous: $e,
message: (string) message('Unable to write provided string')
);
}
}
}
38 changes: 38 additions & 0 deletions src/Traits/WriterTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?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\Writer\Traits;

use Chevere\Writer\Interfaces\WriterInterface;

/**
* @codeCoverageIgnore
* @infection-ignore-all
*/
trait WriterTrait
{
private WriterInterface $writer;

public function withWriter(WriterInterface $writer): static
{
$new = clone $this;
$new->writer = $writer;

return $new;
}

public function writer(): WriterInterface
{
return $this->writer;
}
}
Loading

0 comments on commit c614cbd

Please sign in to comment.