Save 37% off PRO during our Black Friday Sale! »

6 composants (méconnus) de Symfony

6 composants (méconnus) de Symfony

Présentation donnée pour la première fois à l'occasion du SfPot à SensioLabs, le 18 novembre 2021.

665dd8a044d0e509e730a20ba9612d37?s=128

Alexandre Daubois

November 19, 2021
Tweet

Transcript

  1. 1

  2. 2

  3. 3

  4. 4

  5. 6

  6. 7

  7. use Symfony\Component\String\Inflector\EnglishInflector; $inflector = new EnglishInflector(); $result = $inflector->singularize('womens'); //

    ['coman'] $result = $inflector->singularize('radii'); // ['radius'] $result = $inflector->singularize('leaves'); // ['leaf', 'leave', 'leaff'] $result = $inflector->pluralize('news'); // ['news'] $result = $inflector->pluralize('datum'); // ['data'] 1 2 3 4 5 6 7 8 9 10 8
  8. // u(...) est équivalent à new UnicodeString(...) u('https://symfony.com')->startsWith('https'); // True

    u('iPhone 13 Pro')->endsWith('Pro'); // True u('https://twitter.com/alexdaubois')->containsAny('daubois'); // True u('https://github.com/alexandre-daubois')->replace('alexandre-daubois', 'sensiolabs'); // https://github.com/sensiolabs u('Symfony: le Framework')->camel(); // 'symfonyLeFramework' u('Symfony: le Framework')->snake(); // 'symfony_le_framework' u('Symfony Pot - Novembre 2021')->truncate(9, '…'); // 'Symfony P…' u('0123456789')->chunk(3); // ['012', '345', '678', '9'] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 9
  9. $slugger = new AsciiSlugger(); $slug = $slugger->slug('Comment utiliser le composant

    String de Symfony ?'); // $slug = 'comment-utiliser-le-composant-string-de-symfony' 1 2 3 10
  10. 12

  11. 13

  12. 14

  13. <head> <!-- 'as' est obligatoire, 'importance' optionnel parmi low, high

    ou auto --> <link rel="preload" href="{{ preload('/app.css', { as: 'style', importance: 'high' }) }}"> </head> 1 2 3 4 15
  14. 17

  15. 18

  16. 19

  17. $lockFactory instanceof LockFactory; // True $taskLocker = $lockFactory->createLock('unique lock name',

    ttl: 15, autoRelease: true); $taskLocker->acquire(true); // Tâche critique plus ou moins longue, avec de la peristence en base de données, etc. // ... $taskLocker->release(); 1 2 3 4 5 6 7 8 9 20
  18. 21

  19. 23

  20. 24

  21. 25

  22. /** * @link https://symfony.com/doc/current/performance.html#profiling-symfony-applications */ use Symfony\Component\Stopwatch\Stopwatch; class VeryLongTaskClass {

    public function __construct( private Stopwatch $stopwatch ) { } public function veryLongTask(): void { $this->stopwatch->start('my-task'); // ... $this->stopwatch->stop('my-task'); echo (string) $this->stopwatch->getEvent(); // e.g. '7.51 MiB - 12 ms' } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 26
  23. {% stopwatch 'render-members' %} {% for member in members %}

    {# ... #} {% endfor %} {% endstopwatch %} 1 2 3 4 5 27
  24. 29

  25. 30

  26. 31

  27. /** * @link https://symfony.com/doc/current/components/property_info.html#reflectionextractor */ use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor; $reflectionExtractor = new

    ReflectionExtractor(); // Lister les propriétés $reflectionExtractor->getProperties($class); // Récupérer le type d'une propriété $reflectionExtractor->getTypes($class, $property); // Info d'accès : existe t-il des accesseurs et/ou mutateurs ? $reflectionExtractor->isReadable($class, $property); $reflectionExtractor->isWritable($class, $property); 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 32
  28. /** * @link https://symfony.com/doc/current/components/property_info.html#reflectionextractor */ use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor; $phpDocExtractor = new

    PhpDocExtractor(); $phpDocExtractor->getShortDescription($class, $property); $phpDocExtractor->getLongDescription($class, $property); 1 2 3 4 5 6 7 8 9 33
  29. 35

  30. 36

  31. // Description php.net : "chiffre les données" openssl_encrypt( string $data,

    string $cipher_algo, string $passphrase, int $options = 0, string $iv = "", string &$tag = null, string $aad = "", int $tag_length = 16 ): string|false 37
  32. interface KeyInterface { public function extractPublicKey(): self; } interface EncryptionInterface

    { public function generateKey(string $secret = null): KeyInterface; public function encrypt(string $message, KeyInterface $myKey): string; public function encryptFor(string $message, KeyInterface $recipientKey): string; public function decrypt(string $message, KeyInterface $key): string; } 38
  33. 39

  34. 40

  35. 41