Fixes --version and --help

This commit is contained in:
Nuno Maduro
2022-09-17 23:47:47 +01:00
parent 08b62f6633
commit 0e0e2adfbe
26 changed files with 511 additions and 86 deletions

View File

@ -35,16 +35,21 @@ final class Container
/**
* Gets a dependency from the container.
*
* @param class-string $id
* @return mixed
* @template TObject of object
*
* @param class-string<TObject> $id
* @return TObject
*/
public function get(string $id)
public function get(string $id): mixed
{
if (! array_key_exists($id, $this->instances)) {
$this->instances[$id] = $this->build($id);
}
return $this->instances[$id];
/** @var TObject $concrete */
$concrete = $this->instances[$id];
return $concrete;
}
/**
@ -58,9 +63,12 @@ final class Container
/**
* Tries to build the given instance.
*
* @param class-string $id
* @template TObject of object
*
* @param class-string<TObject> $id
* @return TObject
*/
private function build(string $id): object
private function build(string $id): mixed
{
$reflectionClass = new ReflectionClass($id);

71
src/Support/View.php Normal file
View File

@ -0,0 +1,71 @@
<?php
declare(strict_types=1);
namespace Pest\Support;
use Symfony\Component\Console\Output\OutputInterface;
use function Termwind\render;
use function Termwind\renderUsing;
use Termwind\Termwind;
/**
* @internal
*/
final class View
{
/**
* The implementation of the output.
*/
private static OutputInterface $output;
/**
* Renders views using the given Output instance.
*/
public static function renderUsing(OutputInterface $output): void
{
self::$output = $output;
}
/**
* Renders the given view.
*
* @param array<string, mixed> $data
*/
public static function render(string $path, array $data = []): void
{
$contents = self::compile($path, $data);
$existing = Termwind::getRenderer();
renderUsing(self::$output);
try {
render($contents);
} finally {
renderUsing($existing);
}
}
/**
* Compiles the given view.
*
* @param array<string, mixed> $data
*/
private static function compile(string $path, array $data): string
{
extract($data);
ob_start();
$path = str_replace('.', '/', $path);
include sprintf('%s/../../resources/views/%s.php', __DIR__, $path);
$contents = ob_get_contents();
ob_clean();
return (string) $contents;
}
}