Skip to content

Instantly share code, notes, and snippets.

View romaricdrigon's full-sized avatar

Romaric Drigon romaricdrigon

  • Lausanne, Switzerland
View GitHub Profile
@romaricdrigon
romaricdrigon / LoginAttempt.php
Created April 18, 2019 11:59
Code de l'article "Limiter le nombre de tentatives de connexions sous Symfony"
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass="App\Repository\LoginAttemptRepository")
* @ORM\Table()
*/
@romaricdrigon
romaricdrigon / MyController.php
Created April 12, 2019 14:55
But de l'article "Récupérer plus simplement l'utilisateur dans un contrôleur Symfony"
<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\User\UserInterface;
class MyController extends AbstractController
{
@romaricdrigon
romaricdrigon / ExampleController.php
Created April 12, 2019 14:44
Ajout d'un ParamConverter pour "User"
<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Security\Core\User\UserInterface;
class ExampleController extends AbstractController
{
public function pageAvecUnUtilisateur(UserInterface $user)
@romaricdrigon
romaricdrigon / exemple.php
Created April 5, 2019 16:17
Exemple basique des composants Mailer et Mime de Symfony
<?php
$email = (new Email())
->from('[email protected]')
->to('[email protected]')
->subject('Bien le bonjour!')
->text('Ceci sera le contenu txt')
->html('Et ceci la version <code>HTML</code>')
;
$transport = Transport::*fromDsn*(getenv('MAILER_URL'));
@romaricdrigon
romaricdrigon / exemple.email.twig
Created April 5, 2019 14:56
Preview des composants Mime et Mailer de Symfony
{% block subject %}Ce week-end{% endblock %}
{% block html %}
Bonjour,<br />
<p>
Chers collègues de <a href="http://netinfluence.ch">netinfluence</a>, fait-on une <em>fondue</em> ce week-end?
</p>
Romaric
@romaricdrigon
romaricdrigon / DateTimeUtcType.php
Created March 21, 2019 15:17
Exemple de type Doctrine pour stocker des dates avec timezone sous MySQL
<?php
namespace App\Type;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\ConversionException;
use Doctrine\DBAL\Types\Type;
class DateTimeImmutableUtcType extends Type
{
@romaricdrigon
romaricdrigon / Event.php
Last active March 21, 2019 15:12
Example d'entité stockant une date avec timezone
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity()
*/
class Event
@romaricdrigon
romaricdrigon / DateTimeUtcType.php
Last active March 19, 2019 15:13
Type Doctrine custom
<?php
namespace App\Type;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\ConversionException;
use Doctrine\DBAL\Types\Type;
class DateTimeUtcType extends Type
{
@romaricdrigon
romaricdrigon / exemple.php
Created March 15, 2019 15:23
Exemple d'utilisation du Stopwatch
<?php
// L'astuce est bien de récupérer l'instance StopWatch du container,
// éventuellement via auto-injection (sous Symfony 4),
// mais pas d'en créer une nouvelle (ie, new Stopwatch()...).
$stopwatch = $this->container->get('debug.stopwatch');
$stopwatch->start('mon_temps', 'netinfluence');
// ...
usleep(100);
@romaricdrigon
romaricdrigon / MySecurityDataCollector.php
Created March 15, 2019 15:08
Example of a Symfony (3) custom DataCollector
<?php
namespace AppBundle\DataCollector;
use Doctrine\Common\Annotations\AnnotationReader;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\DataCollector\DataCollector;