Skip to content

Commit

Permalink
View DTO and builder refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
loicsapone committed May 7, 2024
1 parent 2143df4 commit eda2b7b
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 166 deletions.
2 changes: 1 addition & 1 deletion src/Controller/IframeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function __invoke(Request $request, ?Profiler $profiler): Response
throw new NotFoundHttpException();
}

$content = $view->getCurrentVariant()->getHtmlContent();
$content = $view->getHtmlContent();

$pos = strripos((string) $content, '</body>');
if (false !== $pos) {
Expand Down
10 changes: 7 additions & 3 deletions src/Controller/ViewController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace IQ2i\StoriaBundle\Controller;

use IQ2i\StoriaBundle\View\Dto\Variant;
use IQ2i\StoriaBundle\View\ViewBuilder;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
Expand All @@ -33,10 +34,13 @@ public function __invoke(Request $request): Response
{
$view = $this->viewBuilder->createFromRequest($request);

if (null !== $view && null === $view->getCurrentVariant()) {
if (null !== $view && null === $request->query->get('variant')) {
/** @var Variant $variant */
$variant = current($view->getVariants());

return new RedirectResponse($this->router->generate('iq2i_storia_view', [
'view' => $view,
'variant' => $view->getFirstVariant(),
'view' => $view->getPath(),
'variant' => $variant->getPath(),
]));
}

Expand Down
67 changes: 6 additions & 61 deletions src/View/Dto/Variant.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,12 @@

namespace IQ2i\StoriaBundle\View\Dto;

class Variant implements \Stringable
readonly class Variant
{
private ?string $includeContent = null;

private ?string $twigContent = null;

private ?string $htmlContent = null;

private ?string $markdownContent = null;

public function __construct(
private readonly string $path,
private readonly string $name,
private string $path,
private string $name,
private bool $isCurrent = false,
) {
}

Expand All @@ -39,56 +32,8 @@ public function getName(): string
return $this->name;
}

public function getIncludeContent(): ?string
public function isCurrent(): bool
{
return $this->includeContent;
}

public function setIncludeContent(?string $includeContent): static
{
$this->includeContent = $includeContent;

return $this;
}

public function getTwigContent(): ?string
{
return $this->twigContent;
}

public function setTwigContent(?string $twigContent): static
{
$this->twigContent = $twigContent;

return $this;
}

public function getHtmlContent(): ?string
{
return $this->htmlContent;
}

public function setHtmlContent(?string $htmlContent): static
{
$this->htmlContent = $htmlContent;

return $this;
}

public function getMarkdownContent(): ?string
{
return $this->markdownContent;
}

public function setMarkdownContent(?string $markdownContent): static
{
$this->markdownContent = $markdownContent;

return $this;
}

public function __toString(): string
{
return $this->path;
return $this->isCurrent;
}
}
69 changes: 34 additions & 35 deletions src/View/Dto/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,19 @@

namespace IQ2i\StoriaBundle\View\Dto;

class View implements \Stringable
readonly class View
{
private array $variants = [];

public function __construct(
private readonly string $path,
private readonly string $name,
private readonly string $template,
private readonly ?string $currentVariantPath = null,
private string $path,
private string $name,
private string $template,
private bool $isComponent = false,
private bool $isPage = false,
private ?string $twigContent = null,
private ?string $htmlContent = null,
private ?string $includeContent = null,
private ?string $markdownContent = null,
private array $variants = [],
) {
}

Expand All @@ -40,53 +44,48 @@ public function getTemplate(): string
return $this->template;
}

public function getCurrentVariantPath(): ?string
public function isComponent(): bool
{
return $this->currentVariantPath;
return $this->isComponent;
}

public function getCurrentVariant(): ?Variant
public function isPage(): bool
{
$variant = current(array_filter($this->variants, fn (Variant $variant): bool => $this->currentVariantPath === $variant->getPath()));

return $variant ?: null;
return $this->isPage;
}

public function getFirstVariant(): ?Variant
public function getTwigContent(): ?string
{
$variant = current($this->variants);

return $variant ?: null;
return $this->twigContent;
}

public function getVariants(): array
public function getHtmlContent(): ?string
{
return $this->variants;
return $this->htmlContent;
}

public function addVariant(Variant $variant): static
public function getIncludeContent(): ?string
{
if (!\in_array($variant, $this->variants, true)) {
$this->variants[] = $variant;
}

return $this;
return $this->includeContent;
}

public function removeVariant(Variant $variant): static
public function getMarkdownContent(): ?string
{
$key = array_search($variant, $this->variants, true);
if (false === $key) {
return $this;
}

unset($this->variants[$key]);
return $this->markdownContent;
}

return $this;
/**
* @return Variant[]
*/
public function getVariants(): array
{
return $this->variants;
}

public function __toString(): string
public function getCurrentVariant(): ?Variant
{
return $this->path;
$variants = array_filter($this->variants, static fn (Variant $variant) => $variant->isCurrent());

return current($variants);
}
}
105 changes: 55 additions & 50 deletions src/View/ViewBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,73 +34,58 @@ public function __construct(

public function createFromRequest(Request $request): ?View
{
$viewPath = $request->attributes->get('view');
if (null === $viewPath) {
$path = $request->attributes->get('view');
if (null === $path || !file_exists($this->defaultPath.'/'.$path.'.yaml')) {
return null;
}

if (!file_exists($this->defaultPath.'/'.$viewPath.'.yaml')) {
return null;
}

$yaml = Yaml::parse(file_get_contents($this->defaultPath.'/'.$viewPath.'.yaml'));
$viewConfiguration = new ViewConfiguration();
$config = (new Processor())->processConfiguration($viewConfiguration, [$yaml]);
$config = (new Processor())->processConfiguration(
new ViewConfiguration(),
[Yaml::parse(file_get_contents($this->defaultPath.'/'.$path.'.yaml'))]
);

$viewName = $config['name'] ?? null;
if (null === $viewName) {
$viewName = pathinfo(str_replace('.yaml', '', (string) $viewPath), \PATHINFO_FILENAME);
$viewName = ucfirst(strtolower(trim((string) preg_replace(['/([A-Z])/', '/[_\s]+/'], ['_$1', ' '], $viewName))));
$name = $config['name'] ?? null;
if (null === $name) {
$name = pathinfo(str_replace('.yaml', '', (string) $path), \PATHINFO_FILENAME);
$name = ucfirst(strtolower(trim((string) preg_replace(['/([A-Z])/', '/[_\s]+/'], ['_$1', ' '], $name))));
}

$isComponent = false;
$isLocal = false;
$viewTemplate = $config['template'] ?? null;
if (null === $viewTemplate && @file_exists($this->defaultPath.'/'.$viewPath.'.html.twig')) {
$isLocal = true;
$viewTemplate = $this->defaultPath.'/'.$viewPath.'.html.twig';
$isPage = false;
$template = $config['template'] ?? null;
if (null === $template && @file_exists($this->defaultPath.'/'.$path.'.html.twig')) {
$isPage = true;
$template = $this->defaultPath.'/'.$path.'.html.twig';
}

if (null === $viewTemplate && null !== $config['component']) {
$isComponent = false;
if (null === $template && null !== $config['component']) {
$isComponent = true;
$viewTemplate = $config['component'];
$template = $config['component'];
}

if (null === $viewTemplate) {
throw new \LogicException(sprintf('Missing template for component "%s"', $viewPath));
if (null === $template) {
throw new \LogicException(sprintf('Missing template for component "%s"', $path));
}

$view = new View($viewPath, $viewName, $viewTemplate, $request->query->get('variant'));

$markdownPath = $view->getPath().'.md';
$markdownContent = @file_get_contents($this->defaultPath.'/'.$markdownPath);
if (false !== $markdownContent) {
$markdownContent = MarkdownExtra::defaultTransform($markdownContent);
}

foreach ($config['variants'] as $variantPath => $variantConfig) {
$variantName = $variantConfig['name'] ?? null;
if (null === $variantName) {
$variantName = ucfirst(strtolower(trim((string) preg_replace(['/([A-Z])/', '/[_\s]+/'], ['_$1', ' '], (string) $variantPath))));
}

$variant = new Variant($variantPath, $variantName);
$variantPath = $request->query->get('variant');
if (isset($config['variants'][$variantPath])) {
$variantConfig = $config['variants'][$variantPath];

$skeletonPath = $isComponent
? __DIR__.'/../../skeleton/component.tpl.php'
: __DIR__.'/../../skeleton/template.tpl.php';

$variantArgs = [];
foreach ($variantConfig['args'] as $name => $value) {
if (\is_array($value)) {
$variantArgs[':'.$name] = str_replace('"', "'", json_encode($value, \JSON_FORCE_OBJECT | \JSON_NUMERIC_CHECK));
foreach ($variantConfig['args'] as $argName => $argValue) {
if (\is_array($argValue)) {
$variantArgs[':'.$argName] = str_replace('"', "'", json_encode($argValue, \JSON_FORCE_OBJECT | \JSON_NUMERIC_CHECK));
} else {
$variantArgs[$name] = $value;
$variantArgs[$argName] = $argValue;
}
}

$parameters = [
'template' => $view->getTemplate(),
'template' => $template,
'args' => $variantArgs,
'blocks' => $variantConfig['blocks'],
];
Expand All @@ -110,18 +95,38 @@ public function createFromRequest(Request $request): ?View
unset($parameters['blocks']['content']);
}

if (!$isLocal) {
$variant->setIncludeContent($this->generateInclude($skeletonPath, $parameters));
$includeContent = null;
if (!$isPage) {
$includeContent = $this->generateInclude($skeletonPath, $parameters);
}

$variant->setTwigContent($this->getTwigContent($view->getTemplate(), $isComponent));
$variant->setHtmlContent($this->generateHtml($isLocal ? $variant->getTwigContent() : $variant->getIncludeContent(), $parameters['args']));
$variant->setMarkdownContent($markdownContent ?: null);
$twigContent = $this->getTwigContent($template, $isComponent);
$htmlContent = $this->generateHtml($isPage ? $twigContent : $includeContent, $parameters['args']);

$view->addVariant($variant);
$markdownContent = null;
if (file_exists($this->defaultPath.'/'.$path.'.md')) {
$markdownContent = MarkdownExtra::defaultTransform(@file_get_contents($this->defaultPath.'/'.$path.'.md'));
}
}

return $view;
$variants = array_map(static fn (string $name): Variant => new Variant(
$name,
ucfirst(strtolower(trim((string) preg_replace(['/([A-Z])/', '/[_\s]+/'], ['_$1', ' '], $name)))),
$variantPath === $name
), array_keys($config['variants']));

return new View(
$path,
$name,
$template,
$isComponent,
$isPage,
$twigContent ?? null,
$htmlContent ?? null,
$includeContent ?? null,
$markdownContent ?? null,
$variants
);
}

private function generateInclude(string $skeletonPath, array $parameters): string
Expand Down
Loading

0 comments on commit eda2b7b

Please sign in to comment.