Skip to content

refactor: remove unnecessary interfaces to simplify package usge #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
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
34 changes: 16 additions & 18 deletions src/Application/ContentGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,27 @@
namespace ContentGenerator\Application;

use ContentGenerator\Domain\Context\ContextDataProvider;
use ContentGenerator\Domain\Context\ContextRepositoryInterface;
use ContentGenerator\Domain\Template\TemplateRepositoryInterface;
use ContentGenerator\Domain\Template\TemplateParser;
use ContentGenerator\Domain\Context\Context;
use ContentGenerator\Domain\Template\Template;

class ContentGenerator
{
private ContextRepositoryInterface $contextRepository;
private TemplateRepositoryInterface $templateRepository;
private ContextManager $contextManager;
private TemplateManager $templateManager;

public function __construct(
ContextRepositoryInterface $contextRepository,
TemplateRepositoryInterface $templateRepository
ContextManager $contextManager,
TemplateManager $templateManager
) {
$this->contextRepository = $contextRepository;
$this->templateRepository = $templateRepository;
$this->contextManager = $contextManager;
$this->templateManager = $templateManager;
}

public function registerContext(string $contextName, ContextDataProvider $provider): void
{
$this->contextRepository->addContext(new Context($contextName, $provider));
$this->contextRepository->removeMissingContext($contextName);
$this->contextManager->addContext(new Context($contextName, $provider));
$this->contextManager->removeMissingContext($contextName);
}

/**
Expand All @@ -34,7 +32,7 @@ public function registerContext(string $contextName, ContextDataProvider $provid
public function registerTemplate(string $templateName, string $templateContent, array $parameters = []): void
{
$template = new Template($templateName, $templateContent);
$this->templateRepository->addTemplate($template);
$this->templateManager->addTemplate($template);

$this->checkAndRegisterNestedContexts(templateContent: $templateContent, parameters: $parameters);
}
Expand All @@ -55,15 +53,15 @@ public function checkAndRegisterNestedContexts(
throw new \RuntimeException("Detected recursive context: $var");
}

if (is_null($this->contextRepository->getContext($var))) {
$this->contextRepository->addMissingContext($var);
if (is_null($this->contextManager->getContext($var))) {
$this->contextManager->addMissingContext($var);
continue;
}

$visited[] = $var;

// Check nested templates
$nestedTemplate = $this->contextRepository->getContext($var)->render($parameters);
$nestedTemplate = $this->contextManager->getContext($var)->render($parameters);
if (is_string($nestedTemplate) && $this->containsTemplateVariables($nestedTemplate)) {
$this->checkAndRegisterNestedContexts($nestedTemplate, $visited, $parameters);
}
Expand All @@ -82,12 +80,12 @@ private function containsTemplateVariables(string $template): bool
*/
public function generateContent(string $templateName, array $parameters = []): string
{
$template = $this->templateRepository->getTemplate($templateName);
$template = $this->templateManager->getTemplate($templateName);
if (!$template) {
throw new \RuntimeException("Template '$templateName' not found.");
}

$contexts = $this->contextRepository->getAllContexts();
$contexts = $this->contextManager->getAllContexts();
$renderedContent = $template->render($contexts, $parameters);

return $renderedContent;
Expand All @@ -98,11 +96,11 @@ public function generateContent(string $templateName, array $parameters = []): s
*/
public function getMissingContexts(): array
{
return $this->contextRepository->getMissingContexts();
return $this->contextManager->getMissingContexts();
}

public function removeContext(string $contextName): void
{
$this->contextRepository->removeContext($contextName);
$this->contextManager->removeContext($contextName);
}
}
3 changes: 1 addition & 2 deletions src/Application/ContextManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
namespace ContentGenerator\Application;

use ContentGenerator\Domain\Context\Context;
use ContentGenerator\Domain\Context\ContextRepositoryInterface;

class ContextManager implements ContextRepositoryInterface
class ContextManager
{
/**
* @var array<string, Context> $contexts
Expand Down
3 changes: 1 addition & 2 deletions src/Application/TemplateManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
namespace ContentGenerator\Application;

use ContentGenerator\Domain\Template\Template;
use ContentGenerator\Domain\Template\TemplateRepositoryInterface;

class TemplateManager implements TemplateRepositoryInterface
class TemplateManager
{
/**
* @var array<string, Template> $templates
Expand Down
21 changes: 0 additions & 21 deletions src/Domain/Context/ContextRepositoryInterface.php

This file was deleted.

9 changes: 0 additions & 9 deletions src/Domain/Template/TemplateRepositoryInterface.php

This file was deleted.

Loading