Optimized implementation of format types

This commit is contained in:
Kevin Frantz 2018-09-14 18:26:09 +02:00
parent f8138fb361
commit 66008bc309
21 changed files with 185 additions and 89 deletions

View File

@ -19,6 +19,7 @@
"symfony/orm-pack": "*", "symfony/orm-pack": "*",
"symfony/process": "*", "symfony/process": "*",
"symfony/security-bundle": "*", "symfony/security-bundle": "*",
"symfony/serializer": "*",
"symfony/serializer-pack": "*", "symfony/serializer-pack": "*",
"symfony/swiftmailer-bundle": "^3.1", "symfony/swiftmailer-bundle": "^3.1",
"symfony/translation": "*", "symfony/translation": "*",

View File

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"content-hash": "d49903ff1dc85f65ca277d339dac5d5e", "content-hash": "ba590f1add8e4ab85f0695854acb1a6d",
"packages": [ "packages": [
{ {
"name": "doctrine/annotations", "name": "doctrine/annotations",

View File

@ -6,8 +6,8 @@ use Symfony\Component\HttpFoundation\Response;
use App\Entity\AbstractSource; use App\Entity\AbstractSource;
use Symfony\Component\Routing\Annotation\Route; use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use App\Source\TemplateGenerator;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use App\Source\Generator\StringGenerator;
/** /**
* @author kevinfrantz * @author kevinfrantz
@ -29,8 +29,8 @@ class SourceController extends AbstractController
if (!$source) { if (!$source) {
throw $this->createNotFoundException('No source found for id '.$id); throw $this->createNotFoundException('No source found for id '.$id);
} }
$templateGenerator = new TemplateGenerator($source, $request); $templateGenerator = new StringGenerator($request, $source, $this->container->get('twig'));
return $this->render($templateGenerator->getTemplatePath(), ['source' => $source]); return new Response($templateGenerator->render());
} }
} }

View File

@ -23,5 +23,6 @@ class AbstractEntity
protected function __construct() protected function __construct()
{ {
$this->id = 0;
} }
} }

View File

@ -2,8 +2,8 @@
namespace App\Entity\Attribut; namespace App\Entity\Attribut;
use Doctrine\Common\Collections\ArrayCollection;
use App\Entity\NodeInterface; use App\Entity\NodeInterface;
use Doctrine\Common\Collections\Collection;
/** /**
* @author kevinfrantz * @author kevinfrantz
@ -19,16 +19,16 @@ trait ChildsAttribut
* inverseJoinColumns={@ORM\JoinColumn(name="node_id", referencedColumnName="id")} * inverseJoinColumns={@ORM\JoinColumn(name="node_id", referencedColumnName="id")}
* ) * )
* *
* @var ArrayCollection|NodeInterface[] * @var Collection|NodeInterface[]
*/ */
protected $childs; protected $childs;
public function getChilds(): ArrayCollection public function getChilds(): Collection
{ {
return $this->getChilds(); return $this->childs;
} }
public function setChilds(ArrayCollection $childs): void public function setChilds(Collection $childs): void
{ {
$this->childs = $childs; $this->childs = $childs;
} }

View File

@ -2,14 +2,14 @@
namespace App\Entity\Attribut; namespace App\Entity\Attribut;
use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection;
/** /**
* @author kevinfrantz * @author kevinfrantz
*/ */
interface ChildsAttributeInterface interface ChildsAttributeInterface
{ {
public function setChilds(ArrayCollection $childs): void; public function setChilds(Collection $childs): void;
public function getChilds(): ArrayCollection; public function getChilds(): Collection;
} }

View File

@ -2,8 +2,8 @@
namespace App\Entity\Attribut; namespace App\Entity\Attribut;
use Doctrine\Common\Collections\ArrayCollection;
use App\Entity\NodeInterface; use App\Entity\NodeInterface;
use Doctrine\Common\Collections\Collection;
/** /**
* @author kevinfrantz * @author kevinfrantz
@ -19,16 +19,16 @@ trait ParentsAttribut
* inverseJoinColumns={@ORM\JoinColumn(name="node_id", referencedColumnName="id")} * inverseJoinColumns={@ORM\JoinColumn(name="node_id", referencedColumnName="id")}
* ) * )
* *
* @var ArrayCollection|NodeInterface[] * @var Collection|NodeInterface[]
*/ */
protected $parents; protected $parents;
public function getParents(): ArrayCollection public function getParents(): Collection
{ {
return $this->parents; return $this->parents;
} }
public function setParents(ArrayCollection $parents): void public function setParents(Collection $parents): void
{ {
$this->parents = $parents; $this->parents = $parents;
} }

View File

@ -2,14 +2,14 @@
namespace App\Entity\Attribut; namespace App\Entity\Attribut;
use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection;
/** /**
* @author kevinfrantz * @author kevinfrantz
*/ */
interface ParentsAttributInterface interface ParentsAttributInterface
{ {
public function setParents(ArrayCollection $parents): void; public function setParents(Collection $parents): void;
public function getParents(): ArrayCollection; public function getParents(): Collection;
} }

View File

@ -2,8 +2,8 @@
namespace App\Entity\Attribut; namespace App\Entity\Attribut;
use Doctrine\Common\Collections\ArrayCollection;
use App\Entity\PermissionInterface; use App\Entity\PermissionInterface;
use Doctrine\Common\Collections\Collection;
/** /**
* @author kevinfrantz * @author kevinfrantz
@ -11,16 +11,16 @@ use App\Entity\PermissionInterface;
trait PermissionsAttribut trait PermissionsAttribut
{ {
/** /**
* @var ArrayCollection * @var Collection
*/ */
protected $permissions; protected $permissions;
public function setPermissions(ArrayCollection $permissions): void public function setPermissions(Collection $permissions): void
{ {
$this->permissions = $permissions; $this->permissions = $permissions;
} }
public function getPermissions(): ArrayCollection public function getPermissions(): Collection
{ {
return $this->permissions; return $this->permissions;
} }

View File

@ -2,17 +2,17 @@
namespace App\Entity\Attribut; namespace App\Entity\Attribut;
use Doctrine\Common\Collections\ArrayCollection;
use App\Entity\PermissionInterface; use App\Entity\PermissionInterface;
use Doctrine\Common\Collections\Collection;
/** /**
* @author kevinfrantz * @author kevinfrantz
*/ */
interface PermissionsAttributInterface interface PermissionsAttributInterface
{ {
public function setPermissions(ArrayCollection $permissions): void; public function setPermissions(Collection $permissions): void;
public function getPermissions(): ArrayCollection; public function getPermissions(): Collection;
public function addPermission(PermissionInterface $permission): void; public function addPermission(PermissionInterface $permission): void;
} }

View File

@ -21,6 +21,6 @@ trait RightAttribut
public function getRight(): RightInterface public function getRight(): RightInterface
{ {
return $this->getRight(); return $this->right;
} }
} }

View File

@ -2,14 +2,14 @@
namespace App\Entity\Attribut; namespace App\Entity\Attribut;
use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection;
/** /**
* @author kevinfrantz * @author kevinfrantz
*/ */
interface RightsAttributInterface interface RightsAttributInterface
{ {
public function setRights(ArrayCollection $rights): void; public function setRights(Collection $rights): void;
public function getRights(): ArrayCollection; public function getRights(): Collection;
} }

View File

@ -2,7 +2,7 @@
namespace App\Entity\Attribut; namespace App\Entity\Attribut;
use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection;
/** /**
* @author kevinfrantz * @author kevinfrantz
@ -10,16 +10,16 @@ use Doctrine\Common\Collections\ArrayCollection;
trait RightsAttribute trait RightsAttribute
{ {
/** /**
* @var ArrayCollection * @var Collection
*/ */
protected $rights; protected $rights;
public function setRights(ArrayCollection $rights): void public function setRights(Collection $rights): void
{ {
$this->rights = $rights; $this->rights = $rights;
} }
public function getRights(): ArrayCollection public function getRights(): Collection
{ {
return $this->rights; return $this->rights;
} }

View File

@ -18,7 +18,7 @@ class Law extends AbstractEntity implements LawInterface
use RightsAttribute, NodeAttribut; use RightsAttribute, NodeAttribut;
/** /**
* @ORM\OneToMany(targetEntity="Right", mappedBy="id", cascade={"persist", "remove"}) * @ORM\OneToMany(targetEntity="Right", mappedBy="law", cascade={"persist", "remove"})
* *
* @var ArrayCollection * @var ArrayCollection
*/ */

View File

@ -43,7 +43,7 @@ class Permission extends AbstractEntity implements PermissionInterface
protected $node; protected $node;
/** /**
* @ORM\ManyToOne(targetEntity="Right") * @ORM\ManyToOne(targetEntity="Right",inversedBy="permissions")
* @ORM\JoinColumn(name="right_id", referencedColumnName="id") * @ORM\JoinColumn(name="right_id", referencedColumnName="id")
* *
* @var RightInterface * @var RightInterface

View File

@ -9,6 +9,7 @@ use Doctrine\ORM\Mapping as ORM;
use Fresh\DoctrineEnumBundle\Validator\Constraints as DoctrineAssert; use Fresh\DoctrineEnumBundle\Validator\Constraints as DoctrineAssert;
use App\Entity\Attribut\LawAttribut; use App\Entity\Attribut\LawAttribut;
use App\Entity\Attribut\PermissionsAttribut; use App\Entity\Attribut\PermissionsAttribut;
use Doctrine\Common\Collections\Collection;
/** /**
* @author kevinfrantz * @author kevinfrantz
@ -20,7 +21,7 @@ class Right extends AbstractEntity implements RightInterface
use TypeAttribut,LawAttribut,PermissionsAttribut; use TypeAttribut,LawAttribut,PermissionsAttribut;
/** /**
* @ORM\ManyToOne(targetEntity="Law") * @ORM\ManyToOne(targetEntity="Law", inversedBy="rights")
* @ORM\JoinColumn(name="law_id", referencedColumnName="id") * @ORM\JoinColumn(name="law_id", referencedColumnName="id")
* *
* @var LawInterface * @var LawInterface
@ -36,9 +37,9 @@ class Right extends AbstractEntity implements RightInterface
protected $type; protected $type;
/** /**
* @ORM\OneToMany(targetEntity="Permission", mappedBy="id", cascade={"persist", "remove"}) * @ORM\OneToMany(targetEntity="Permission", mappedBy="right", cascade={"persist", "remove"})
* *
* @var ArrayCollection * @var Collection
*/ */
protected $permissions; protected $permissions;

View File

@ -0,0 +1,28 @@
<?php
namespace App\Source\Generator;
use App\Entity\SourceInterface;
use Symfony\Component\HttpFoundation\Request;
/**
* @author kevinfrantz
*/
abstract class AbstractGenerator
{
/**
* @var SourceInterface
*/
protected $source;
/**
* @var Request
*/
protected $request;
public function __construct(Request $request, SourceInterface $source)
{
$this->source = $source;
$this->request = $request;
}
}

View File

@ -0,0 +1,36 @@
<?php
namespace App\Source\Generator;
use Symfony\Component\Serializer\Encoder\XmlEncoder;
use Symfony\Component\Serializer\Encoder\YamlEncoder;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Encoder\CsvEncoder;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Serializer;
/**
* @author kevinfrantz
*/
final class SerializeGenerator extends AbstractGenerator
{
const SERIALIZABLE_FORMATS = [
'xml',
'json',
'csv',
'yaml',
];
public function serialize(): string
{
$encoders = [new XmlEncoder(), new JsonEncoder(), new YamlEncoder(), new CsvEncoder()];
$normalizer = new ObjectNormalizer();
$normalizer->setCircularReferenceLimit(0);
$normalizer->setCircularReferenceHandler(function ($object) {
return $object->getId();
});
$serializer = new Serializer([$normalizer], $encoders);
return $serializer->serialize($this->source, $this->request->getRequestFormat());
}
}

View File

@ -0,0 +1,35 @@
<?php
namespace App\Source\Generator;
use Symfony\Component\HttpFoundation\Request;
use App\Entity\SourceInterface;
/**
* @author kevinfrantz
*/
final class StringGenerator extends AbstractGenerator
{
/**
* @var \Twig_Environment
*/
protected $twig;
public function __construct(Request $request, SourceInterface $source, \Twig_Environment $twig)
{
parent::__construct($request, $source);
$this->twig = $twig;
}
public function render(): string
{
if (in_array($this->request->getRequestFormat(), SerializeGenerator::SERIALIZABLE_FORMATS)) {
$serializeGenerator = new SerializeGenerator($this->request, $this->source);
return $serializeGenerator->serialize();
}
$templateGenerator = new TemplateGenerator($this->request, $this->source, $this->twig);
return $templateGenerator->render();
}
}

View File

@ -0,0 +1,44 @@
<?php
namespace App\Source\Generator;
use App\Entity\SourceInterface;
use Symfony\Component\HttpFoundation\Request;
/**
* @author kevinfrantz
*/
final class TemplateGenerator extends AbstractGenerator
{
const SOURCE_TEMPLATE_ROOT = 'source';
/**
* @var \Twig_Environment
*/
protected $twig;
public function __construct(Request $request, SourceInterface $source, \Twig_Environment $twig)
{
parent::__construct($request, $source);
$this->twig = $twig;
}
public function render(): string
{
return $this->twig->render($this->getTemplatePath(), ['source' => $this->source]);
}
private function getTemplatePath(): string
{
return self::SOURCE_TEMPLATE_ROOT.'/'.$this->generateName().'.'.$this->request->getRequestFormat().'.twig';
}
private function generateName(): string
{
$reflection = new \ReflectionClass($this->source);
$shortName = $reflection->getShortName();
$lowerName = strtolower($shortName);
return str_replace('source', '', $lowerName);
}
}

View File

@ -1,50 +0,0 @@
<?php
namespace App\Source;
use App\Entity\SourceInterface;
use Symfony\Component\HttpFoundation\Request;
/**
* @author kevinfrantz
*/
class TemplateGenerator
{
const SOURCE_TEMPLATE_ROOT = 'source';
/**
* @var SourceInterface
*/
protected $source;
/**
* @var Request
*/
protected $request;
/**
* @var string
*/
protected $format;
public function __construct(SourceInterface $source, Request $request)
{
$this->source = $source;
$this->request = $request;
$this->format = $this->request->getRequestFormat();
}
public function getTemplatePath(): string
{
return self::SOURCE_TEMPLATE_ROOT.'/'.$this->generateName().'.'.$this->format.'.twig';
}
private function generateName(): string
{
$reflection = new \ReflectionClass($this->source);
$shortName = $reflection->getShortName();
$lowerName = strtolower($shortName);
return str_replace('source', '', $lowerName);
}
}