hey
This commit is contained in:
parent
d84a7595fc
commit
ad4c4e49a2
|
@ -18,7 +18,7 @@
|
|||
"symfony/mailer": "5.0.*",
|
||||
"symfony/monolog-bundle": "^3.1",
|
||||
"symfony/notifier": "5.0.*",
|
||||
"symfony/orm-pack": "*",
|
||||
"symfony/orm-pack": "^1.0",
|
||||
"symfony/process": "5.0.*",
|
||||
"symfony/security-bundle": "5.0.*",
|
||||
"symfony/serializer-pack": "*",
|
||||
|
@ -32,7 +32,7 @@
|
|||
},
|
||||
"require-dev": {
|
||||
"symfony/debug-pack": "*",
|
||||
"symfony/maker-bundle": "^1.0",
|
||||
"symfony/maker-bundle": "^1.14",
|
||||
"symfony/profiler-pack": "*",
|
||||
"symfony/test-pack": "*"
|
||||
},
|
||||
|
|
2
composer.lock
generated
2
composer.lock
generated
|
@ -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": "cc6cab883cdc77654d1c749f08f71450",
|
||||
"content-hash": "152e56ea4ef3a500319be7e6561c46cc",
|
||||
"packages": [
|
||||
{
|
||||
"name": "doctrine/annotations",
|
||||
|
|
|
@ -3,7 +3,7 @@ index:
|
|||
controller: App\Controller\PadController::showForm
|
||||
methods: GET|HEAD
|
||||
view:
|
||||
path: '{id}'
|
||||
path: '/{name}'
|
||||
controller: App\Controller\PadController::view
|
||||
methods: GET|HEAD
|
||||
|
||||
|
|
|
@ -6,6 +6,11 @@ use Symfony\Component\HttpFoundation\Request;
|
|||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
|
||||
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
|
||||
{
|
||||
|
@ -24,13 +29,49 @@ Class PadController extends AbstractController
|
|||
'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)
|
||||
{
|
||||
|
||||
|
@ -38,10 +79,15 @@ Class PadController extends AbstractController
|
|||
$form->handleRequest($request);
|
||||
if ($form->isSubmitted() && $form->isValid()) {
|
||||
$data = $form->getData();
|
||||
return new Response(
|
||||
$data["content"]
|
||||
);
|
||||
//return $this->redirectToRoute('task_success');
|
||||
//save the pad
|
||||
$entityManager = $this->getDoctrine()->getManager();
|
||||
$pad = new PAD();
|
||||
$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() ]);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -1,28 +1,131 @@
|
|||
<?php
|
||||
|
||||
namespace App\Entity;
|
||||
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
|
||||
/**
|
||||
* @ORM\Entity(repositoryClass="App\Repository\PadRepository")
|
||||
*/
|
||||
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 )
|
||||
{
|
||||
$this->content = $content;
|
||||
return $this;
|
||||
}
|
||||
public function getId()
|
||||
|
||||
public function getId(): ?int
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
|
37
src/Migrations/Version20191214175405.php
Normal file
37
src/Migrations/Version20191214175405.php
Normal 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');
|
||||
}
|
||||
}
|
37
src/Migrations/Version20191214194119.php
Normal file
37
src/Migrations/Version20191214194119.php
Normal 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');
|
||||
}
|
||||
}
|
50
src/Repository/NameRepository.php
Normal file
50
src/Repository/NameRepository.php
Normal 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()
|
||||
;
|
||||
}
|
||||
*/
|
||||
}
|
50
src/Repository/PadRepository.php
Normal file
50
src/Repository/PadRepository.php
Normal 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()
|
||||
;
|
||||
}
|
||||
*/
|
||||
}
|
16
templates/pad-view.html.twig
Normal file
16
templates/pad-view.html.twig
Normal 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>
|
|
@ -2,13 +2,13 @@
|
|||
<html>
|
||||
<head>
|
||||
<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 %}
|
||||
<link href="{{ asset('/css/style.css') }}" type="text/css" rel="stylesheet" />
|
||||
{% endblock %}
|
||||
</head>
|
||||
<body>
|
||||
<h1>{{head_title}} </h1>
|
||||
<h1>{{head_title|e('html')}} </h1>
|
||||
{{ form_start(form) }}
|
||||
{{ form_widget(form.content, {}) }}
|
||||
{{ form_end(form) }}
|
||||
|
|
Loading…
Reference in New Issue
Block a user