Optimized Template Management and implemented TemplateNameService

This commit is contained in:
Kevin Frantz 2019-02-15 16:55:49 +01:00
parent b0d57e41f3
commit 3e1590ffbf
13 changed files with 301 additions and 28 deletions

View File

@ -8,9 +8,6 @@ The application MUST use [Symfony 4. coding standards](https://symfony.com/doc/c
### PHP
PHP code MUST follow the [PSR-4](https://www.php-fig.org/psr/psr-4/) standard.
### Twig
Twig templates MUST follow the [Symfony Template best practices](https://symfony.com/doc/current/best_practices/templates.html).
### Naming
#### Interfaces

View File

@ -12,6 +12,8 @@ use App\Domain\PathManagement\NamespacePathMap;
/**
* @author kevinfrantz
*
* @deprecated
*/
class EntityMetaInformation implements EntityMetaInformationInterface
{

View File

@ -11,6 +11,8 @@ use App\Domain\PathManagement\NamespacePathMapInterface;
/**
* Offers some meta information about an entity.
*
* @deprecated
*
* @author kevinfrantz
*/
interface EntityMetaInformationInterface

View File

@ -0,0 +1,29 @@
<?php
namespace App\Domain\MVCManagement;
use FOS\RestBundle\View\View;
/**
* @author kevinfrantz
*/
final class MVCRoutineService implements MVCRoutineServiceInterface
{
/**
* {@inheritdoc}
*
* @see \App\Domain\MVCManagement\MVCRoutineServiceInterface::process()
*/
public function process(): void
{
}
/**
* {@inheritdoc}
*
* @see \App\Domain\MVCManagement\MVCRoutineServiceInterface::getView()
*/
public function getView(): View
{
}
}

View File

@ -0,0 +1,23 @@
<?php
namespace App\Domain\MVCManagement;
use FOS\RestBundle\View\View;
/**
* This interface offers the options to process an MVC routine.
*
* @author kevinfrantz
*/
interface MVCRoutineServiceInterface
{
/**
* Process the injected services.
*/
public function process(): void;
/**
* @return View
*/
public function getView(): View;
}

View File

@ -0,0 +1,107 @@
<?php
namespace App\Domain\TemplateManagement;
use App\Domain\RequestManagement\Action\RequestedActionServiceInterface;
/**
* @author kevinfrantz
*/
final class TemplateNameService implements TemplateNameServiceInterface
{
/**
* @var string The namespace which should be ignored
*/
const BASE_NAMESPACE = 'App\\Entity';
/**
* @var string the basic entry point for templates
*/
const BASE_ENTITY_TEMPLATE_FOLDER = 'entity';
/**
* @var string
*/
const MOLECULE_PRAEFFIX = '';
/**
* @var string
*/
const ATOM_PRAEFFIX = '_';
/**
* @var string
*/
const TWIG_SUFFIX = '.html.twig';
/**
* @var RequestedActionServiceInterface
*/
private $requestedActionService;
/**
* @param RequestedActionServiceInterface $requestedActionService
*/
public function __construct(RequestedActionServiceInterface $requestedActionService)
{
$this->requestedActionService = $requestedActionService;
}
/**
* @return string
*/
private function getBasePath(): string
{
$origineClass = $this->requestedActionService->getRequestedEntity()->getClass();
$baseReplaced = str_replace(self::BASE_NAMESPACE, self::BASE_ENTITY_TEMPLATE_FOLDER, $origineClass);
$elements = explode('\\', $baseReplaced);
array_pop($elements); //Removes class name
$templatePath = implode('/', $elements);
$lowerCasePath = strtolower($templatePath);
return $lowerCasePath.'/';
}
/**
* @return string the short class name in lower cases
*/
private function getShortName(): string
{
$origineClass = $this->requestedActionService->getRequestedEntity()->getClass();
$elements = explode('\\', $origineClass);
$class = $elements[count($elements) - 1];
$lcFirst = lcfirst($class);
$bigLettersSubstituted = preg_replace('/([A-Z])/', '_$1', $lcFirst);
$lowerCase = strtolower($bigLettersSubstituted);
return $lowerCase;
}
/**
* @return string
*/
private function getActionSuffix(): string
{
return '_'.strtolower($this->requestedActionService->getActionType());
}
/**
* @param string $type
*
* @return string
*/
private function getTemplatePath(?string $type): string
{
return $this->getBasePath().$type.$this->getShortName().$this->getActionSuffix().self::TWIG_SUFFIX;
}
public function getAtomTemplateName(): string
{
return $this->getTemplatePath(self::ATOM_PRAEFFIX);
}
public function getMoleculeTemplateName(): string
{
return $this->getTemplatePath(self::MOLECULE_PRAEFFIX);
}
}

View File

@ -0,0 +1,19 @@
<?php
namespace App\Domain\TemplateManagement;
/**
* @author kevinfrantz
*/
interface TemplateNameServiceInterface
{
/**
* @return string A template inclusiv frame. (Standalone)
*/
public function getMoleculeTemplateName(): string;
/**
* @return string a template without a frame
*/
public function getAtomTemplateName(): string;
}

View File

@ -6,6 +6,9 @@ use App\DBAL\Types\RESTResponseType;
/**
* @author kevinfrantz
*
* @deprecated
* @see TemplatePathService
*/
final class TemplatePathInformation implements TemplatePathInformationInterface
{

View File

@ -8,6 +8,9 @@ use App\DBAL\Types\RESTResponseType;
* Manages all informations which are needed to process templates.
*
* @author kevinfrantz
*
* @deprecated
* @see TemplatePathServiceInterface
*/
interface TemplatePathInformationInterface extends ReloadTypeInterface
{

View File

@ -3,10 +3,10 @@
namespace App\Domain\ViewManagement;
use FOS\RestBundle\View\View;
use App\Domain\SecureCRUDManagement\Factory\SecureCRUDFactoryService;
use App\Domain\SecureCRUDManagement\Factory\SecureCRUDFactoryServiceInterface;
use App\Domain\RequestManagement\User\RequestedUserInterface;
use App\Domain\RequestManagement\Entity\RequestedEntityInterface;
use App\Domain\RequestManagement\Action\RequestedActionInterface;
use App\Domain\ActionManagement\ActionServiceInterface;
use App\Domain\ActionManagement\ActionFactoryServiceInterface;
use App\Domain\ActionManagement\ActionFactoryService;
/**
* @author kevinfrantz
@ -19,36 +19,23 @@ class ViewBuilder implements ViewBuilderInterface
protected $view;
/**
* @var SecureCRUDFactoryServiceInterface
* @var RequestedActionInterface
*/
protected $secureCrudFactoryService;
protected $actionService;
/**
* @var RequestedEntityInterface
* @var ActionFactoryServiceInterface
*/
protected $requestedEntity;
protected $actionFactoryService;
/**
* @var RequestedUserInterface
* @param ActionServiceInterface $actionService
* @param ActionFactoryServiceInterface $actionFactoryService
*/
protected $requestedUser;
/**
* @param RequestedUserInterface $requestedUserRight
* @param SecureCRUDFactoryService $secureCrudFactoryService
*/
public function __construct(RequestedUserInterface $requestedUserRight, SecureCRUDFactoryService $secureCrudFactoryService, RequestedEntityInterface $requestedEntity)
public function __construct(ActionServiceInterface $actionService, ActionFactoryServiceInterface $actionFactoryService)
{
$this->view = new View();
$this->requestedUser = $requestedUserRight;
$this->secureCrudFactoryService = $secureCrudFactoryService;
$this->requestedEntity = $requestedEntity;
}
private function process()
{
$secureCrudService = $this->secureCrudFactoryService->create($this->requestedUser);
$entity = $secureCrudService->process($this->requestedEntity);
$this->actionService = $actionService;
}
/**
@ -57,4 +44,24 @@ class ViewBuilder implements ViewBuilderInterface
public function getView(): View
{
}
/**
* {@inheritdoc}
*
* @see \App\Domain\ViewManagement\ViewBuilderInterface::getActionService()
*/
public function getActionService(): ActionServiceInterface
{
return $this->actionService;
}
/**
* {@inheritdoc}
*
* @see \App\Domain\ViewManagement\ViewBuilderInterface::build()
*/
public function build(): void
{
$this->view->create();
}
}

View File

@ -3,6 +3,7 @@
namespace App\Domain\ViewManagement;
use FOS\RestBundle\View\View;
use App\Domain\ActionManagement\ActionServiceInterface;
/**
* @author kevinfrantz
@ -13,4 +14,14 @@ interface ViewBuilderInterface
* @return View
*/
public function getView(): View;
/**
* @return ActionServiceInterface
*/
public function getActionService(): ActionServiceInterface;
/**
* Builds the view.
*/
public function build(): void;
}

View File

@ -0,0 +1,5 @@
# Twig
More informations about twig you will find on the [official website](https://twig.symfony.com/).
## Convention
Twig templates MUST follow the [Symfony Template best practices](https://symfony.com/doc/current/best_practices/templates.html).

View File

@ -0,0 +1,65 @@
<?php
namespace tests\Unit\Domain\TemplateManagement;
use PHPUnit\Framework\TestCase;
use App\Domain\TemplateManagement\TemplateNameServiceInterface;
use App\Domain\TemplateManagement\TemplateNameService;
use App\Domain\RequestManagement\Entity\RequestedEntityServiceInterface;
use App\Domain\RequestManagement\Action\RequestedActionServiceInterface;
/**
* @author kevinfrantz
*/
class TemplateNameServiceTest extends TestCase
{
/**
* @var TemplateNameServiceInterface
*/
private $templateNameService;
/**
* @var string
*/
const CLASS_NAME = 'App\\Entity\\Source\\PureSource';
/**
* @var string
*/
const ACTION_TYPE = 'CREATE';
/**
* @var string
*/
const EXPECTED_MOLECULE_TEMPLATE_NAME = 'entity/source/pure_source_create.html.twig';
/**
* @var string
*/
const EXPECTED_ATOM_TEMPLATE_NAME = 'entity/source/_pure_source_create.html.twig';
/**
* {@inheritdoc}
*
* @see \PHPUnit\Framework\TestCase::setUp()
*/
public function setUp(): void
{
$requestedEntityService = $this->createMock(RequestedEntityServiceInterface::class);
$requestedEntityService->method('getClass')->willReturn(self::CLASS_NAME);
$requestedActionService = $this->createMock(RequestedActionServiceInterface::class);
$requestedActionService->method('getRequestedEntity')->willReturn($requestedEntityService);
$requestedActionService->method('getActionType')->willReturn(self::ACTION_TYPE);
$this->templateNameService = new TemplateNameService($requestedActionService);
}
public function testGetMoleculeName(): void
{
$this->assertEquals(self::EXPECTED_MOLECULE_TEMPLATE_NAME, $this->templateNameService->getMoleculeTemplateName());
}
public function testGetAtomName(): void
{
$this->assertEquals(self::EXPECTED_ATOM_TEMPLATE_NAME, $this->templateNameService->getAtomTemplateName());
}
}