mirror of
https://github.com/pestphp/pest.git
synced 2026-03-10 01:37:21 +01:00
Fixes --version and --help
This commit is contained in:
@ -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
71
src/Support/View.php
Normal 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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user