This commit is contained in:
gnieark 2019-12-16 19:20:43 +01:00
parent d84a7595fc
commit ad4c4e49a2
11 changed files with 367 additions and 28 deletions

View File

@ -18,7 +18,7 @@
"symfony/mailer": "5.0.*", "symfony/mailer": "5.0.*",
"symfony/monolog-bundle": "^3.1", "symfony/monolog-bundle": "^3.1",
"symfony/notifier": "5.0.*", "symfony/notifier": "5.0.*",
"symfony/orm-pack": "*", "symfony/orm-pack": "^1.0",
"symfony/process": "5.0.*", "symfony/process": "5.0.*",
"symfony/security-bundle": "5.0.*", "symfony/security-bundle": "5.0.*",
"symfony/serializer-pack": "*", "symfony/serializer-pack": "*",
@ -32,7 +32,7 @@
}, },
"require-dev": { "require-dev": {
"symfony/debug-pack": "*", "symfony/debug-pack": "*",
"symfony/maker-bundle": "^1.0", "symfony/maker-bundle": "^1.14",
"symfony/profiler-pack": "*", "symfony/profiler-pack": "*",
"symfony/test-pack": "*" "symfony/test-pack": "*"
}, },

2
composer.lock generated
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": "cc6cab883cdc77654d1c749f08f71450", "content-hash": "152e56ea4ef3a500319be7e6561c46cc",
"packages": [ "packages": [
{ {
"name": "doctrine/annotations", "name": "doctrine/annotations",

View File

@ -3,7 +3,7 @@ index:
controller: App\Controller\PadController::showForm controller: App\Controller\PadController::showForm
methods: GET|HEAD methods: GET|HEAD
view: view:
path: '{id}' path: '/{name}'
controller: App\Controller\PadController::view controller: App\Controller\PadController::view
methods: GET|HEAD methods: GET|HEAD

View File

@ -6,6 +6,11 @@ use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\Extension\Core\Type\SubmitType; use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType; use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use App\Entity\Pad;
use Doctrine\ORM\EntityManagerInterface;
Class PadController extends AbstractController Class PadController extends AbstractController
{ {
@ -24,13 +29,49 @@ Class PadController extends AbstractController
'form' => $this->get_padform()->createView() 'form' => $this->get_padform()->createView()
]); ]);
} }
public function view($id) public function view($name)
{ {
return new Response(
'<h1>plop</h1>' $pads = $this->getDoctrine()
); ->getRepository(Pad::class)
->findBy(array('name' => $name));
if(count($pads) == 0 )
{
throw new NotFoundHttpException('This pad does not exist');
} }
$pad = $pads[0];
return $this->render('pad-view.html.twig', [
'head_title' => 'Pad id: ' . $pad->getName(),
'page_title' => 'Pad id: ' . $pad->getName(),
'pad_content' => $pad->getContent()
]);
}
private function get_free_name( $depth = 0, $length=6)
{
if($depth > 3 ){
throw new \UnexpectedValueException("I cant generate an unique key");
}
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$randstring = '';
for ($i = 0; $i < $length; $i++) {
$randstring .= $characters[rand(0, strlen($characters) -1)];
}
$pads = $this->getDoctrine()
->getRepository(Pad::class)
->findBy(array('name' => $randstring));
if(count($pads) > 0){
return $this->get_free_name( $depth + 1);
}
return $randstring;
}
public function post(Request $request) public function post(Request $request)
{ {
@ -38,10 +79,15 @@ Class PadController extends AbstractController
$form->handleRequest($request); $form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) { if ($form->isSubmitted() && $form->isValid()) {
$data = $form->getData(); $data = $form->getData();
return new Response( //save the pad
$data["content"] $entityManager = $this->getDoctrine()->getManager();
); $pad = new PAD();
//return $this->redirectToRoute('task_success'); $pad->setContent($data["content"])
->setName( $this-> get_free_name() );
$entityManager->persist($pad);
// actually executes the queries (i.e. the INSERT query)
$entityManager->flush();
return $this->redirectToRoute('view',["name" => $pad->getName() ]);
} }

View File

@ -1,28 +1,131 @@
<?php <?php
namespace App\Entity; namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass="App\Repository\PadRepository")
*/
class Pad class Pad
{ {
protected $content; /**
protected $id; * @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
public function getContent() /**
* @ORM\Column(type="text", nullable=true)
*/
private $content;
/**
* @ORM\Column(type="datetime")
*/
private $create_date;
/**
* @ORM\Column(type="boolean")
*/
private $is_clear = true;
/**
* @ORM\Column(type="boolean")
*/
private $is_published = true;
/**
* @ORM\Column(type="boolean")
*/
private $expires = true;
/**
* @ORM\Column(type="string", length=255)
*/
private $name;
public function __construct()
{ {
return $this->content; $this->create_date = new \DateTime();
} }
public function setContent( string $content )
{ public function getId(): ?int
$this->content = $content;
return $this;
}
public function getId()
{ {
return $this->id; return $this->id;
} }
public function setId( string $id )
public function getContent(): ?string
{ {
$this->id = $id; return $this->content;
}
public function setContent(?string $content): self
{
$this->content = $content;
return $this; return $this;
} }
public function getCreateDate(): ?\DateTimeInterface
{
return $this->create_date;
}
public function setCreateDate(\DateTimeInterface $create_date): self
{
$this->create_date = $create_date;
return $this;
}
public function getIsClear(): ?bool
{
return $this->is_clear;
}
public function setIsClear(bool $is_clear): self
{
$this->is_clear = $is_clear;
return $this;
}
public function getIsPublished(): ?bool
{
return $this->is_published;
}
public function setIsPublished(bool $is_published): self
{
$this->is_published = $is_published;
return $this;
}
public function getExpires(): ?bool
{
return $this->expires;
}
public function setExpires(bool $expires): self
{
$this->expires = $expires;
return $this;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
} }

View File

@ -0,0 +1,37 @@
<?php
declare(strict_types=1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20191214175405 extends AbstractMigration
{
public function getDescription() : string
{
return '';
}
public function up(Schema $schema) : void
{
// this up() migration is auto-generated, please modify it to your needs
$this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'mysql', 'Migration can only be executed safely on \'mysql\'.');
$this->addSql('CREATE TABLE name (id INT AUTO_INCREMENT NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
$this->addSql('CREATE TABLE pad (id INT AUTO_INCREMENT NOT NULL, content LONGTEXT DEFAULT NULL, create_date DATETIME NOT NULL, is_clear TINYINT(1) NOT NULL, is_published TINYINT(1) NOT NULL, expires TINYINT(1) NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
}
public function down(Schema $schema) : void
{
// this down() migration is auto-generated, please modify it to your needs
$this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'mysql', 'Migration can only be executed safely on \'mysql\'.');
$this->addSql('DROP TABLE name');
$this->addSql('DROP TABLE pad');
}
}

View File

@ -0,0 +1,37 @@
<?php
declare(strict_types=1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20191214194119 extends AbstractMigration
{
public function getDescription() : string
{
return '';
}
public function up(Schema $schema) : void
{
// this up() migration is auto-generated, please modify it to your needs
$this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'mysql', 'Migration can only be executed safely on \'mysql\'.');
$this->addSql('DROP TABLE name');
$this->addSql('ALTER TABLE pad ADD name VARCHAR(255) NOT NULL');
}
public function down(Schema $schema) : void
{
// this down() migration is auto-generated, please modify it to your needs
$this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'mysql', 'Migration can only be executed safely on \'mysql\'.');
$this->addSql('CREATE TABLE name (id INT AUTO_INCREMENT NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE `utf8_unicode_ci` ENGINE = InnoDB COMMENT = \'\' ');
$this->addSql('ALTER TABLE pad DROP name');
}
}

View File

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

View File

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

View File

@ -0,0 +1,16 @@
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<title> {{page_title|e('html')}}</title>
{% block stylesheets %}
<link href="{{ asset('/css/style.css') }}" type="text/css" rel="stylesheet" />
{% endblock %}
</head>
<body>
<h1>{{head_title|e('html')}} </h1>
<pre>
{{ pad_content|e('html') }}
</pre>
<p><a href="/">Créer un nouveau Pad</a></p>
</body>
</html>

View File

@ -2,13 +2,13 @@
<html> <html>
<head> <head>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"> <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<title> {{page_title}}</title> <title>{{ page_title|e('html') }}</title>
{% block stylesheets %} {% block stylesheets %}
<link href="{{ asset('/css/style.css') }}" type="text/css" rel="stylesheet" /> <link href="{{ asset('/css/style.css') }}" type="text/css" rel="stylesheet" />
{% endblock %} {% endblock %}
</head> </head>
<body> <body>
<h1>{{head_title}} </h1> <h1>{{head_title|e('html')}} </h1>
{{ form_start(form) }} {{ form_start(form) }}
{{ form_widget(form.content, {}) }} {{ form_widget(form.content, {}) }}
{{ form_end(form) }} {{ form_end(form) }}