Skip to content

Commit

Permalink
feat(core): New package helper Viewer.
Browse files Browse the repository at this point in the history
  • Loading branch information
LastDragon-ru committed Aug 28, 2023
1 parent a5d98e4 commit 9502ebe
Show file tree
Hide file tree
Showing 9 changed files with 166 additions and 27 deletions.
45 changes: 45 additions & 0 deletions packages/core/src/Helpers/Viewer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php declare(strict_types = 1);

namespace LastDragon_ru\LaraASP\Core\Helpers;

use Illuminate\Contracts\View\Factory as ViewFactoryContract;
use Illuminate\Contracts\View\View as ViewContract;

/**
* Special wrapper around {@see ViewFactoryContract} to help render package's views.
*/
abstract class Viewer {
public function __construct(
protected readonly Translator $translator,
protected readonly ViewFactoryContract $factory,
protected readonly string $package,
) {
// empty
}

/**
* @param array<string, mixed> $data
*/
public function get(string $view, array $data = []): ViewContract {
return $this->factory->make(
"{$this->package}::{$view}",
$this->getDefaultData() + $data,
);
}

/**
* @param array<string, mixed> $data
*/
public function render(string $view, array $data = []): string {
return $this->get($view, $data)->render();
}

/**
* @return array<string, mixed>
*/
protected function getDefaultData(): array {
return [
'translator' => $this->translator,
];
}
}
60 changes: 60 additions & 0 deletions packages/core/src/Helpers/ViewerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php declare(strict_types = 1);

namespace LastDragon_ru\LaraASP\Core\Helpers;

use Illuminate\Contracts\View\Factory as ViewFactory;
use Illuminate\Contracts\View\View as ViewContract;
use LastDragon_ru\LaraASP\Core\Testing\Package\TestCase;
use Mockery;
use PHPUnit\Framework\Attributes\CoversClass;

/**
* @internal
*/
#[CoversClass(Viewer::class)]
class ViewerTest extends TestCase {
public function testGet(): void {
$view = 'view';
$data = ['a' => 123];
$package = 'package';
$translator = Mockery::mock(Translator::class);
$factory = Mockery::mock(ViewFactory::class);
$factory
->shouldReceive('make')
->with("{$package}::{$view}", ['translator' => $translator] + $data)
->once()
->andReturn(
Mockery::mock(ViewContract::class),
);

$viewer = new class($translator, $factory, $package) extends Viewer {
// empty
};

$viewer->get($view, $data);
}

public function testRender(): void {
$view = 'view';
$data = ['a' => 123];
$package = 'package';
$content = 'content';
$translator = Mockery::mock(Translator::class);
$factory = Mockery::mock(ViewFactory::class);
$template = Mockery::mock(ViewContract::class);
$template
->shouldReceive('render')
->once()
->andReturn($content);

$viewer = Mockery::mock(Viewer::class, [$translator, $factory, $package]);
$viewer->makePartial();
$viewer
->shouldReceive('get')
->with($view, $data)
->once()
->andReturn($template);

self::assertEquals($content, $viewer->render($view, $data));
}
}
3 changes: 3 additions & 0 deletions packages/core/src/Provider/WithViews.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
namespace LastDragon_ru\LaraASP\Core\Provider;

use Illuminate\Support\ServiceProvider;
use LastDragon_ru\LaraASP\Core\Helpers\Viewer;

/**
* @see Viewer
*
* @mixin ServiceProvider
*/
trait WithViews {
Expand Down
23 changes: 15 additions & 8 deletions packages/documentator/src/Commands/Commands.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Support\Str;
use LastDragon_ru\LaraASP\Core\Utils\Cast;
use LastDragon_ru\LaraASP\Documentator\Package;
use LastDragon_ru\LaraASP\Documentator\PackageViewer;
use LastDragon_ru\LaraASP\Documentator\Utils\ArtisanSerializer;
use LastDragon_ru\LaraASP\Documentator\Utils\Path;
use Symfony\Component\Console\Application;
Expand All @@ -14,7 +15,6 @@
use Symfony\Component\Finder\Finder;

use function is_dir;
use function view;

// @phpcs:disable Generic.Files.LineLength.TooLong

Expand All @@ -35,12 +35,12 @@ class Commands extends Command {
{--defaults : Include application default arguments/options like `--help`, etc.}
SIGNATURE;

public function __invoke(Filesystem $filesystem, ArtisanSerializer $serializer): void {
public function __invoke(PackageViewer $viewer, Filesystem $filesystem, ArtisanSerializer $serializer): void {
// Options
$application = Cast::to(Application::class, $this->getApplication());
$namespace = $application->findNamespace(Cast::toString($this->argument('namespace')));
$target = Cast::toString($this->argument('target'));
$default = Cast::toBool($this->option('defaults'));
$defaults = Cast::toBool($this->option('defaults'));
$commands = $application->all($namespace);

// Cleanup
Expand All @@ -65,9 +65,17 @@ static function () use ($filesystem, $target): void {

$this->components->task(
"Command: {$command->getName()}",
static function () use ($filesystem, $serializer, $namespace, $target, $default, $command): void {
static function () use (
$filesystem,
$serializer,
$viewer,
$namespace,
$target,
$defaults,
$command,
): void {
// Default options?
if ($default) {
if ($defaults) {
$command->mergeApplicationDefinition();
} else {
$command->setDefinition(
Expand All @@ -78,11 +86,10 @@ static function () use ($filesystem, $serializer, $namespace, $target, $default,
// Render
$name = Str::after((string) $command->getName(), "{$namespace}:");
$path = Path::getPath($target, "{$name}.md");
$package = Package::Name;
$content = view("{$package}::commands.markdown", [
$content = $viewer->render('commands.markdown', [
'serializer' => $serializer,
'command' => $command,
])->render();
]);

$filesystem->dumpFile($path, $content);
},
Expand Down
13 changes: 6 additions & 7 deletions packages/documentator/src/Commands/Requirements.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use LastDragon_ru\LaraASP\Documentator\Metadata\Metadata;
use LastDragon_ru\LaraASP\Documentator\Metadata\Storage;
use LastDragon_ru\LaraASP\Documentator\Package;
use LastDragon_ru\LaraASP\Documentator\PackageViewer;
use LastDragon_ru\LaraASP\Documentator\Utils\Git;
use LastDragon_ru\LaraASP\Documentator\Utils\Path;
use LastDragon_ru\LaraASP\Documentator\Utils\Version;
Expand Down Expand Up @@ -36,7 +37,6 @@
use function str_starts_with;
use function trim;
use function uksort;
use function view;

use const JSON_THROW_ON_ERROR;

Expand Down Expand Up @@ -73,7 +73,7 @@ class Requirements extends Command {
```
HELP;

public function __invoke(Git $git, Serializer $serializer): void {
public function __invoke(PackageViewer $viewer, Git $git, Serializer $serializer): void {
// Get Versions
$cwd = Cast::toString($this->argument('cwd') ?? getcwd());
$tags = $this->getPackageVersions($git, $cwd, ['HEAD']);
Expand Down Expand Up @@ -134,12 +134,11 @@ public function __invoke(Git $git, Serializer $serializer): void {
$requirements = $this->getRequirements($packages, $metadata);

// Render
$package = Package::Name;
$output = view("{$package}::requirements.markdown", [
$output = $viewer->render('requirements.markdown', [
'packages' => $packages,
'requirements' => $requirements,
])->render();
$output = trim($output);
]);
$output = trim($output);

$this->output->writeln($output);
}
Expand All @@ -154,7 +153,7 @@ protected function getPackageVersions(Git $git, string $cwd, array $tags = []):
$tags = array_unique($tags);
$tags = array_combine($tags, $tags);

uksort($tags, static fn($a, $b) => -Version::compare($a, $b));
uksort($tags, static fn ($a, $b) => -Version::compare($a, $b));

return $tags;
}
Expand Down
13 changes: 13 additions & 0 deletions packages/documentator/src/PackageTranslator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php declare(strict_types = 1);

namespace LastDragon_ru\LaraASP\Documentator;

use Illuminate\Contracts\Translation\Translator as TranslatorContract;
use LastDragon_ru\LaraASP\Core\Helpers\Translator;
use LastDragon_ru\LaraASP\Formatter\Package;

class PackageTranslator extends Translator {
public function __construct(TranslatorContract $translator) {
parent::__construct($translator, Package::Name);
}
}
12 changes: 12 additions & 0 deletions packages/documentator/src/PackageViewer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php declare(strict_types = 1);

namespace LastDragon_ru\LaraASP\Documentator;

use Illuminate\Contracts\View\Factory as ViewFactory;
use LastDragon_ru\LaraASP\Core\Helpers\Viewer;

class PackageViewer extends Viewer {
public function __construct(PackageTranslator $translator, ViewFactory $factory) {
parent::__construct($translator, $factory, Package::Name);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace LastDragon_ru\LaraASP\Documentator\Preprocessor\Instructions;

use LastDragon_ru\LaraASP\Documentator\Package;
use LastDragon_ru\LaraASP\Documentator\PackageViewer;
use LastDragon_ru\LaraASP\Documentator\Preprocessor\Exceptions\DocumentTitleIsMissing;
use LastDragon_ru\LaraASP\Documentator\Preprocessor\Exceptions\TargetIsNotDirectory;
use LastDragon_ru\LaraASP\Documentator\Preprocessor\Instruction;
Expand All @@ -16,10 +16,11 @@
use function is_dir;
use function strcmp;
use function usort;
use function view;

class IncludeDocumentList implements Instruction {
public function __construct() {
public function __construct(
protected readonly PackageViewer $viewer,
) {
// empty
}

Expand Down Expand Up @@ -94,10 +95,9 @@ public function process(string $path, string $target): string {
});

// Render
$package = Package::Name;
$list = view("{$package}::document-list.markdown", [
$list = $this->viewer->render('document-list.markdown', [
'documents' => $documents,
])->render();
]);

// Return
return $list;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace LastDragon_ru\LaraASP\Documentator\Preprocessor\Instructions;

use Exception;
use LastDragon_ru\LaraASP\Documentator\Package;
use LastDragon_ru\LaraASP\Documentator\PackageViewer;
use LastDragon_ru\LaraASP\Documentator\Preprocessor\Exceptions\DocumentTitleIsMissing;
use LastDragon_ru\LaraASP\Documentator\Preprocessor\Exceptions\PackageComposerJsonIsMissing;
use LastDragon_ru\LaraASP\Documentator\Preprocessor\Exceptions\PackageReadmeIsMissing;
Expand All @@ -23,12 +23,13 @@
use function json_decode;
use function strcmp;
use function usort;
use function view;

use const JSON_THROW_ON_ERROR;

class IncludePackageList implements Instruction {
public function __construct() {
public function __construct(
protected readonly PackageViewer $viewer,
) {
// empty
}

Expand Down Expand Up @@ -110,10 +111,9 @@ public function process(string $path, string $target): string {
});

// Render
$packageInfo = Package::Name;
$list = view("{$packageInfo}::package-list.markdown", [
$list = $this->viewer->render('package-list.markdown', [
'packages' => $packages,
])->render();
]);

// Return
return $list;
Expand Down

0 comments on commit 9502ebe

Please sign in to comment.