generated from chevere/package-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
614 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.