Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions packages/layout/src/LayoutProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@
use Marko\Layout\Exceptions\CircularSlotException;
use Marko\Layout\Exceptions\LayoutNotFoundException;
use Marko\Layout\Exceptions\SlotNotFoundException;
use Marko\Core\Container\ContainerInterface;
use Psr\Container\ContainerExceptionInterface;
use Marko\Routing\Http\Request;
use Marko\Routing\Http\Response;
use Marko\View\ViewInterface;

readonly class LayoutProcessor implements LayoutProcessorInterface
{
public function __construct(
private ContainerInterface $container,
private LayoutResolver $layoutResolver,
private HandleResolver $handleResolver,
private ComponentCollectorInterface $componentCollector,
Expand All @@ -30,7 +33,7 @@ public function __construct(
* @throws SlotNotFoundException
* @throws CircularSlotException
* @throws LayoutNotFoundException
* @throws AmbiguousSortOrderException
* @throws AmbiguousSortOrderException|ContainerExceptionInterface
*/
public function process(
string $controllerClass,
Expand Down Expand Up @@ -87,7 +90,7 @@ public function process(
* Render all components in a slot and fill any sub-slots they define.
*
* @param array<string, mixed> $routeParameters
* @throws AmbiguousSortOrderException
* @throws AmbiguousSortOrderException|ContainerExceptionInterface
*/
private function renderSlot(
string $slotName,
Expand All @@ -99,7 +102,7 @@ private function renderSlot(
$html = '';

foreach ($slotComponents as $definition) {
$componentInstance = new $definition->className();
$componentInstance = $this->container->get($definition->className);
$data = $this->componentDataResolver->resolve($componentInstance, $routeParameters, $request);
$componentHtml = $this->view->renderToString($definition->template, $data);

Expand Down
36 changes: 36 additions & 0 deletions packages/layout/tests/Unit/Helpers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace Marko\Layout\Tests\Unit;

use Closure;
use Marko\Core\Container\ContainerInterface;

final class Helpers
{
public static function stubContainer(): ContainerInterface
{
return new class () implements ContainerInterface
{
public function get(string $id): object
{
return new $id();
}

public function has(string $id): bool
{
return class_exists($id);
}

public function singleton(string $id): void {}

public function instance(string $id, object $instance): void {}

public function call(Closure $callable): mixed
{
return $callable();
}
};
}
}
2 changes: 2 additions & 0 deletions packages/layout/tests/Unit/LayoutProcessorNestedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
declare(strict_types=1);

use Marko\Layout\Attributes\Component;
use Marko\Layout\Tests\Unit\Helpers;
use Marko\Layout\Attributes\Layout;
use Marko\Layout\ComponentCollection;
use Marko\Layout\ComponentCollectorInterface;
Expand Down Expand Up @@ -129,6 +130,7 @@ public function renderToString(string $template, array $data = []): string
function lpnBuildProcessor(ComponentCollection $collection, ViewInterface $view, string $controllerClass = LpnFixtureController::class): LayoutProcessor
{
return new LayoutProcessor(
container: Helpers::stubContainer(),
layoutResolver: new LayoutResolver(),
handleResolver: new HandleResolver(),
componentCollector: lpnStubCollector($collection),
Expand Down
2 changes: 2 additions & 0 deletions packages/layout/tests/Unit/LayoutProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
declare(strict_types=1);

use Marko\Layout\Attributes\Component;
use Marko\Layout\Tests\Unit\Helpers;
use Marko\Layout\Attributes\Layout;
use Marko\Layout\ComponentCollection;
use Marko\Layout\ComponentCollectorInterface;
Expand Down Expand Up @@ -143,6 +144,7 @@ function defaultCollection(): ComponentCollection
function buildProcessor(ComponentCollection $collection, ViewInterface $view): LayoutProcessor
{
return new LayoutProcessor(
container: Helpers::stubContainer(),
layoutResolver: new LayoutResolver(),
handleResolver: new HandleResolver(),
componentCollector: stubCollector($collection),
Expand Down