master
Destroyeur6315 1 year ago
parent b11bcb096f
commit dedd507ccd

@ -0,0 +1,40 @@
<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use App\Service\TwokManager;
use Psr\Log\LoggerInterface;
use App\Entity\Twok;
class TweetTokController extends AbstractController
{
#[Route('/home', name: 'app_tweet_tok')]
public function home(TwokManager $twok_manager): Response
{
$twok = $twok_manager->getTwokByID(37497);
return $this->render('tweet_tok/index.html.twig', [
'controller_name' => 'TweetTokController',
'author'=> $twok->getAuteur(),
'content' => $twok->getMessage()
]);
}
#[Route('/home/twok', name: 'add_twok')]
public function addTwok(TwokManager $twok_manager): Response
{
$twok = new Twok(10, 'romain', 'L\'instant présent est le plus important. Ne perdons pas notre temps
avec le passé et le futur', '09/02/2024 10:30:33');
$twok = $twok_manager->addTwok($twok);
return $this->render('tweet_tok/index.html.twig', [
'controller_name' => 'TweetTokController',
'author'=> 'test',
'content' => 'test'
]);
}
}

@ -0,0 +1,85 @@
<?php
namespace App\Entity;
use App\Repository\TwokRepository;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Symfony\UX\Turbo\Attribute\Broadcast;
use Symfony\Component\Validator\Constraints\DateTime;
#[ORM\Entity(repositoryClass: TwokRepository::class)]
#[Broadcast]
class Twok
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\Column(length: 255, nullable: true)]
private ?string $auteur = null;
#[ORM\Column(length: 255, nullable: true)]
private ?string $message = null;
#[ORM\Column(type: Types::DATETIME_MUTABLE, nullable: true)]
private ?string $date = null;
public function __construct(
int $id, string $auteur, string $message, string $date
) {
$this->id = $id;
$this->auteur = $auteur;
$this->message = $message;
$this->date = $date;
}
public function getId(): ?int
{
return $this->id;
}
public function setId(int $id): static
{
$this->id = $id;
return $this;
}
public function getAuteur(): ?string
{
return $this->auteur;
}
public function setAuteur(?string $auteur): static
{
$this->auteur = $auteur;
return $this;
}
public function getMessage(): ?string
{
return $this->message;
}
public function setMessage(?string $message): static
{
$this->message = $message;
return $this;
}
public function getDate(): ?string
{
return $this->date;
}
public function setDate(?string $date): static
{
$this->date = $date;
return $this;
}
}

@ -0,0 +1,48 @@
<?php
namespace App\Repository;
use App\Entity\Twok;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
/**
* @extends ServiceEntityRepository<Twok>
*
* @method Twok|null find($id, $lockMode = null, $lockVersion = null)
* @method Twok|null findOneBy(array $criteria, array $orderBy = null)
* @method Twok[] findAll()
* @method Twok[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class TwokRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Twok::class);
}
// /**
// * @return Twok[] Returns an array of Twok objects
// */
// public function findByExampleField($value): array
// {
// return $this->createQueryBuilder('t')
// ->andWhere('t.exampleField = :val')
// ->setParameter('val', $value)
// ->orderBy('t.id', 'ASC')
// ->setMaxResults(10)
// ->getQuery()
// ->getResult()
// ;
// }
// public function findOneBySomeField($value): ?Twok
// {
// return $this->createQueryBuilder('t')
// ->andWhere('t.exampleField = :val')
// ->setParameter('val', $value)
// ->getQuery()
// ->getOneOrNullResult()
// ;
// }
}

@ -0,0 +1,47 @@
<?php
namespace App\Service;
use App\Entity\Twok;
use Symfony\Component\Validator\Constraints\DateTime;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Encoder\XmlEncoder;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Serializer;
class TwokManager{
public function getTwokByID(int $id): Twok{
$data = $this->getAllTwork();
foreach ($data as $ligne) {
if ($ligne['id'] == $id) {
$twok = new Twok($ligne['id'], $ligne['author'], $ligne['content'], $ligne['created_at']);
return $twok;
break;
}
}
return new Twok(0, '', '', '');
}
public function addTwok(Twok $my_twok) {
$encoders = [new XmlEncoder(), new JsonEncoder()];
$normalizers = [new ObjectNormalizer()];
$serializer = new Serializer($normalizers, $encoders);
$path = '../var/test.json';
var_dump($my_twok);
$jsonContent = $serializer->serialize($my_twok, 'json');
// $jsonString = json_encode($my_twok);
file_put_contents($path, $jsonContent);
}
public function getAllTwork(): array{
$path = '../var/twoks_db.json';
$jsonString = file_get_contents($path);
$jsonData = json_decode($jsonString, true);
return $jsonData;
}
}

@ -0,0 +1,22 @@
{# Learn how to use Turbo Streams: https://github.com/symfony/ux-turbo#broadcast-doctrine-entities-update #}
{% block create %}
<turbo-stream action="append" target="twoks">
<template>
<div id="{{ 'twok_' ~ id }}">
#{{ id }} created
</div>
</template>
</turbo-stream>
{% endblock %}
{% block update %}
<turbo-stream action="update" target="twok_{{ id }}">
<template>
#{{ id }} updated
</template>
</turbo-stream>
{% endblock %}
{% block remove %}
<turbo-stream action="remove" target="twok_{{ id }}"></turbo-stream>
{% endblock %}

@ -0,0 +1,24 @@
{% extends 'base.html.twig' %}
{% block title %}Hello TweetTokController!{% endblock %}
{% block body %}
<style>
.example-wrapper { margin: 1em auto; max-width: 800px; width: 95%; font: 18px/1.5 sans-serif; }
.example-wrapper code { background: #F5F5F5; padding: 2px 6px; }
</style>
<div class="example-wrapper">
<h1>Hello {{ controller_name }}! ✅</h1>
This friendly message is coming from:
<ul>
<li>Your controller at <code>/Users/romain/Documents/Cours/BUT-3/symfony/TweetTok/src/Controller/TweetTokController.php</code></li>
<li>Your template at <code>/Users/romain/Documents/Cours/BUT-3/symfony/TweetTok/templates/tweet_tok/index.html.twig</code></li>
</ul>
<h5> {{ author }} </h5>
<h5> {{ content }} </h5>
</div>
{% endblock %}
Loading…
Cancel
Save