diff --git a/application/composer.json b/application/composer.json index 5b18540..c2dffb7 100644 --- a/application/composer.json +++ b/application/composer.json @@ -19,6 +19,7 @@ "symfony/orm-pack": "*", "symfony/process": "*", "symfony/security-bundle": "*", + "symfony/serializer": "*", "symfony/serializer-pack": "*", "symfony/swiftmailer-bundle": "^3.1", "symfony/translation": "*", diff --git a/application/composer.lock b/application/composer.lock index e9038bb..bd7957f 100644 --- a/application/composer.lock +++ b/application/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "d49903ff1dc85f65ca277d339dac5d5e", + "content-hash": "ba590f1add8e4ab85f0695854acb1a6d", "packages": [ { "name": "doctrine/annotations", diff --git a/application/src/Controller/SourceController.php b/application/src/Controller/SourceController.php index bef6bcb..0f27252 100644 --- a/application/src/Controller/SourceController.php +++ b/application/src/Controller/SourceController.php @@ -6,8 +6,8 @@ use Symfony\Component\HttpFoundation\Response; use App\Entity\AbstractSource; use Symfony\Component\Routing\Annotation\Route; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; -use App\Source\TemplateGenerator; use Symfony\Component\HttpFoundation\Request; +use App\Source\Generator\StringGenerator; /** * @author kevinfrantz @@ -29,8 +29,8 @@ class SourceController extends AbstractController if (!$source) { 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()); } } diff --git a/application/src/Entity/AbstractEntity.php b/application/src/Entity/AbstractEntity.php index fcfbb08..15090ff 100644 --- a/application/src/Entity/AbstractEntity.php +++ b/application/src/Entity/AbstractEntity.php @@ -23,5 +23,6 @@ class AbstractEntity protected function __construct() { + $this->id = 0; } } diff --git a/application/src/Entity/Attribut/ChildsAttribut.php b/application/src/Entity/Attribut/ChildsAttribut.php index 642450e..770f907 100644 --- a/application/src/Entity/Attribut/ChildsAttribut.php +++ b/application/src/Entity/Attribut/ChildsAttribut.php @@ -2,8 +2,8 @@ namespace App\Entity\Attribut; -use Doctrine\Common\Collections\ArrayCollection; use App\Entity\NodeInterface; +use Doctrine\Common\Collections\Collection; /** * @author kevinfrantz @@ -19,16 +19,16 @@ trait ChildsAttribut * inverseJoinColumns={@ORM\JoinColumn(name="node_id", referencedColumnName="id")} * ) * - * @var ArrayCollection|NodeInterface[] + * @var Collection|NodeInterface[] */ 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; } diff --git a/application/src/Entity/Attribut/ChildsAttributeInterface.php b/application/src/Entity/Attribut/ChildsAttributeInterface.php index 5e90f15..d22984c 100644 --- a/application/src/Entity/Attribut/ChildsAttributeInterface.php +++ b/application/src/Entity/Attribut/ChildsAttributeInterface.php @@ -2,14 +2,14 @@ namespace App\Entity\Attribut; -use Doctrine\Common\Collections\ArrayCollection; +use Doctrine\Common\Collections\Collection; /** * @author kevinfrantz */ interface ChildsAttributeInterface { - public function setChilds(ArrayCollection $childs): void; + public function setChilds(Collection $childs): void; - public function getChilds(): ArrayCollection; + public function getChilds(): Collection; } diff --git a/application/src/Entity/Attribut/ParentsAttribut.php b/application/src/Entity/Attribut/ParentsAttribut.php index 3700ec7..96291b4 100644 --- a/application/src/Entity/Attribut/ParentsAttribut.php +++ b/application/src/Entity/Attribut/ParentsAttribut.php @@ -2,8 +2,8 @@ namespace App\Entity\Attribut; -use Doctrine\Common\Collections\ArrayCollection; use App\Entity\NodeInterface; +use Doctrine\Common\Collections\Collection; /** * @author kevinfrantz @@ -19,16 +19,16 @@ trait ParentsAttribut * inverseJoinColumns={@ORM\JoinColumn(name="node_id", referencedColumnName="id")} * ) * - * @var ArrayCollection|NodeInterface[] + * @var Collection|NodeInterface[] */ protected $parents; - public function getParents(): ArrayCollection + public function getParents(): Collection { return $this->parents; } - public function setParents(ArrayCollection $parents): void + public function setParents(Collection $parents): void { $this->parents = $parents; } diff --git a/application/src/Entity/Attribut/ParentsAttributInterface.php b/application/src/Entity/Attribut/ParentsAttributInterface.php index 7f37f7c..1d3632e 100644 --- a/application/src/Entity/Attribut/ParentsAttributInterface.php +++ b/application/src/Entity/Attribut/ParentsAttributInterface.php @@ -2,14 +2,14 @@ namespace App\Entity\Attribut; -use Doctrine\Common\Collections\ArrayCollection; +use Doctrine\Common\Collections\Collection; /** * @author kevinfrantz */ interface ParentsAttributInterface { - public function setParents(ArrayCollection $parents): void; + public function setParents(Collection $parents): void; - public function getParents(): ArrayCollection; + public function getParents(): Collection; } diff --git a/application/src/Entity/Attribut/PermissionsAttribut.php b/application/src/Entity/Attribut/PermissionsAttribut.php index fbfdd85..75020ce 100644 --- a/application/src/Entity/Attribut/PermissionsAttribut.php +++ b/application/src/Entity/Attribut/PermissionsAttribut.php @@ -2,8 +2,8 @@ namespace App\Entity\Attribut; -use Doctrine\Common\Collections\ArrayCollection; use App\Entity\PermissionInterface; +use Doctrine\Common\Collections\Collection; /** * @author kevinfrantz @@ -11,16 +11,16 @@ use App\Entity\PermissionInterface; trait PermissionsAttribut { /** - * @var ArrayCollection + * @var Collection */ protected $permissions; - public function setPermissions(ArrayCollection $permissions): void + public function setPermissions(Collection $permissions): void { $this->permissions = $permissions; } - public function getPermissions(): ArrayCollection + public function getPermissions(): Collection { return $this->permissions; } diff --git a/application/src/Entity/Attribut/PermissionsAttributInterface.php b/application/src/Entity/Attribut/PermissionsAttributInterface.php index 94e85b6..ead0c03 100644 --- a/application/src/Entity/Attribut/PermissionsAttributInterface.php +++ b/application/src/Entity/Attribut/PermissionsAttributInterface.php @@ -2,17 +2,17 @@ namespace App\Entity\Attribut; -use Doctrine\Common\Collections\ArrayCollection; use App\Entity\PermissionInterface; +use Doctrine\Common\Collections\Collection; /** * @author kevinfrantz */ 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; } diff --git a/application/src/Entity/Attribut/RightAttribut.php b/application/src/Entity/Attribut/RightAttribut.php index 1a41ea0..564695b 100644 --- a/application/src/Entity/Attribut/RightAttribut.php +++ b/application/src/Entity/Attribut/RightAttribut.php @@ -21,6 +21,6 @@ trait RightAttribut public function getRight(): RightInterface { - return $this->getRight(); + return $this->right; } } diff --git a/application/src/Entity/Attribut/RightsAttributInterface.php b/application/src/Entity/Attribut/RightsAttributInterface.php index edc1756..5ec968c 100644 --- a/application/src/Entity/Attribut/RightsAttributInterface.php +++ b/application/src/Entity/Attribut/RightsAttributInterface.php @@ -2,14 +2,14 @@ namespace App\Entity\Attribut; -use Doctrine\Common\Collections\ArrayCollection; +use Doctrine\Common\Collections\Collection; /** * @author kevinfrantz */ interface RightsAttributInterface { - public function setRights(ArrayCollection $rights): void; + public function setRights(Collection $rights): void; - public function getRights(): ArrayCollection; + public function getRights(): Collection; } diff --git a/application/src/Entity/Attribut/RightsAttribute.php b/application/src/Entity/Attribut/RightsAttribute.php index b72dbb7..35d9561 100644 --- a/application/src/Entity/Attribut/RightsAttribute.php +++ b/application/src/Entity/Attribut/RightsAttribute.php @@ -2,7 +2,7 @@ namespace App\Entity\Attribut; -use Doctrine\Common\Collections\ArrayCollection; +use Doctrine\Common\Collections\Collection; /** * @author kevinfrantz @@ -10,16 +10,16 @@ use Doctrine\Common\Collections\ArrayCollection; trait RightsAttribute { /** - * @var ArrayCollection + * @var Collection */ protected $rights; - public function setRights(ArrayCollection $rights): void + public function setRights(Collection $rights): void { $this->rights = $rights; } - public function getRights(): ArrayCollection + public function getRights(): Collection { return $this->rights; } diff --git a/application/src/Entity/Law.php b/application/src/Entity/Law.php index 20b16a1..f8123ff 100644 --- a/application/src/Entity/Law.php +++ b/application/src/Entity/Law.php @@ -18,7 +18,7 @@ class Law extends AbstractEntity implements LawInterface use RightsAttribute, NodeAttribut; /** - * @ORM\OneToMany(targetEntity="Right", mappedBy="id", cascade={"persist", "remove"}) + * @ORM\OneToMany(targetEntity="Right", mappedBy="law", cascade={"persist", "remove"}) * * @var ArrayCollection */ diff --git a/application/src/Entity/Permission.php b/application/src/Entity/Permission.php index d89f764..b88ed0d 100644 --- a/application/src/Entity/Permission.php +++ b/application/src/Entity/Permission.php @@ -43,7 +43,7 @@ class Permission extends AbstractEntity implements PermissionInterface protected $node; /** - * @ORM\ManyToOne(targetEntity="Right") + * @ORM\ManyToOne(targetEntity="Right",inversedBy="permissions") * @ORM\JoinColumn(name="right_id", referencedColumnName="id") * * @var RightInterface diff --git a/application/src/Entity/Right.php b/application/src/Entity/Right.php index 06fe683..a62912a 100644 --- a/application/src/Entity/Right.php +++ b/application/src/Entity/Right.php @@ -9,6 +9,7 @@ use Doctrine\ORM\Mapping as ORM; use Fresh\DoctrineEnumBundle\Validator\Constraints as DoctrineAssert; use App\Entity\Attribut\LawAttribut; use App\Entity\Attribut\PermissionsAttribut; +use Doctrine\Common\Collections\Collection; /** * @author kevinfrantz @@ -20,7 +21,7 @@ class Right extends AbstractEntity implements RightInterface use TypeAttribut,LawAttribut,PermissionsAttribut; /** - * @ORM\ManyToOne(targetEntity="Law") + * @ORM\ManyToOne(targetEntity="Law", inversedBy="rights") * @ORM\JoinColumn(name="law_id", referencedColumnName="id") * * @var LawInterface @@ -36,9 +37,9 @@ class Right extends AbstractEntity implements RightInterface 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; diff --git a/application/src/Source/Generator/AbstractGenerator.php b/application/src/Source/Generator/AbstractGenerator.php new file mode 100644 index 0000000..daf499e --- /dev/null +++ b/application/src/Source/Generator/AbstractGenerator.php @@ -0,0 +1,28 @@ +source = $source; + $this->request = $request; + } +} diff --git a/application/src/Source/Generator/SerializeGenerator.php b/application/src/Source/Generator/SerializeGenerator.php new file mode 100644 index 0000000..cb9a969 --- /dev/null +++ b/application/src/Source/Generator/SerializeGenerator.php @@ -0,0 +1,36 @@ +setCircularReferenceLimit(0); + $normalizer->setCircularReferenceHandler(function ($object) { + return $object->getId(); + }); + $serializer = new Serializer([$normalizer], $encoders); + + return $serializer->serialize($this->source, $this->request->getRequestFormat()); + } +} diff --git a/application/src/Source/Generator/StringGenerator.php b/application/src/Source/Generator/StringGenerator.php new file mode 100644 index 0000000..531b06d --- /dev/null +++ b/application/src/Source/Generator/StringGenerator.php @@ -0,0 +1,35 @@ +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(); + } +} diff --git a/application/src/Source/Generator/TemplateGenerator.php b/application/src/Source/Generator/TemplateGenerator.php new file mode 100644 index 0000000..8c8081d --- /dev/null +++ b/application/src/Source/Generator/TemplateGenerator.php @@ -0,0 +1,44 @@ +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); + } +} diff --git a/application/src/Source/TemplateGenerator.php b/application/src/Source/TemplateGenerator.php deleted file mode 100644 index 93af0b6..0000000 --- a/application/src/Source/TemplateGenerator.php +++ /dev/null @@ -1,50 +0,0 @@ -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); - } -}