vendor/endroid/qrcode/src/Bundle/Twig/Extension/QrCodeExtension.php line 66

Open in your IDE?
  1. <?php
  2. /*
  3.  * (c) Jeroen van den Enden <info@endroid.nl>
  4.  *
  5.  * This source file is subject to the MIT license that is bundled
  6.  * with this source code in the file LICENSE.
  7.  */
  8. namespace Endroid\QrCode\Bundle\Twig\Extension;
  9. use Endroid\QrCode\Factory\QrCodeFactory;
  10. use Symfony\Component\DependencyInjection\ContainerAwareTrait;
  11. use Symfony\Component\Routing\RouterInterface;
  12. use Twig_Extension;
  13. use Symfony\Component\DependencyInjection\ContainerAwareInterface;
  14. use Twig_SimpleFunction;
  15. class QrCodeExtension extends Twig_Extension implements ContainerAwareInterface
  16. {
  17.     use ContainerAwareTrait;
  18.     /**
  19.      * {@inheritdoc}
  20.      */
  21.     public function getFunctions()
  22.     {
  23.         return [
  24.             new Twig_SimpleFunction('qrcode_url', [$this'qrcodeUrlFunction']),
  25.             new Twig_SimpleFunction('qrcode_data_uri', [$this'qrcodeDataUriFunction']),
  26.         ];
  27.     }
  28.     /**
  29.      * Creates the QR code URL corresponding to the given message.
  30.      *
  31.      * @param string $text
  32.      * @param array  $options
  33.      *
  34.      * @return string
  35.      */
  36.     public function qrcodeUrlFunction($text, array $options = [])
  37.     {
  38.         $params $options;
  39.         $params['text'] = $text;
  40.         // Extension is a mandatory route parameter: if not set retrieve from defaults
  41.         if (!isset($params['extension'])) {
  42.             $defaultOptions $this->getQrCodeFactory()->getDefaultOptions();
  43.             $params['extension'] = $defaultOptions['extension'];
  44.         }
  45.         return $this->getRouter()->generate('endroid_qrcode'$params);
  46.     }
  47.     /**
  48.      * Creates the QR code data corresponding to the given message.
  49.      *
  50.      * @param string $text
  51.      * @param array  $options
  52.      *
  53.      * @return string
  54.      */
  55.     public function qrcodeDataUriFunction($text, array $options = [])
  56.     {
  57.         $qrCode $this->getQrCodeFactory()->createQrCode($options);
  58.         $qrCode->setText($text);
  59.         return $qrCode->getDataUri();
  60.     }
  61.     /**
  62.      * Returns the router.
  63.      *
  64.      * @return RouterInterface
  65.      */
  66.     protected function getRouter()
  67.     {
  68.         return $this->container->get('router');
  69.     }
  70.     /**
  71.      * Returns the QR code factory.
  72.      *
  73.      * @return QrCodeFactory
  74.      */
  75.     protected function getQrCodeFactory()
  76.     {
  77.         return $this->container->get('endroid.qrcode.factory');
  78.     }
  79.     /**
  80.      * {@inheritdoc}
  81.      */
  82.     public function getName()
  83.     {
  84.         return 'endroid_qrcode';
  85.     }
  86. }