|
- <?php
-
- namespace Lc\SovBundle\Twig;
-
- use App\Repository\ReminderRepository;
- use Doctrine\ORM\EntityManagerInterface;
- use Lc\SovBundle\Component\MetaComponent;
- use Lc\SovBundle\Component\StringComponent;
- use Lc\SovBundle\Form\Newsletter\NewsletterType;
- use Lc\SovBundle\Repository\Reminder\ReminderStore;
- use Lc\SovBundle\Translation\TranslatorAdmin;
- use Liip\ImagineBundle\Imagine\Cache\CacheManager;
- use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
- use Symfony\Component\Form\FormFactoryInterface;
- use Symfony\Component\HttpFoundation\RequestStack;
- use Symfony\Component\HttpKernel\KernelInterface;
- use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
- use Symfony\Component\Security\Core\Security;
- use Symfony\Contracts\Translation\TranslatorInterface;
- use Twig\Extension\AbstractExtension;
- use Twig\TwigFilter;
- use Twig\TwigFunction;
-
- class TwigExtension extends AbstractExtension
- {
- protected $em;
- protected $kernel;
- protected $parameterBag;
- protected $cacheManager;
- protected $requestStack;
- protected $router;
- protected $translator;
- protected $translatorAdmin;
- protected $reminderStore;
- protected $security;
- protected FormFactoryInterface $formFactory;
- protected StringComponent $stringComponent;
-
- public function __construct(
- KernelInterface $kernel,
- ParameterBagInterface $parameterBag,
- CacheManager $cacheManager,
- EntityManagerInterface $entityManager,
- RequestStack $requestStack,
- UrlGeneratorInterface $router,
- TranslatorInterface $translator,
- TranslatorAdmin $translatorAdmin,
- ReminderStore $reminderStore,
- Security $security,
- FormFactoryInterface $formFactory,
- StringComponent $stringComponent,
- MetaComponent $metaComponent
- ) {
- $this->kernel = $kernel;
- $this->parameterBag = $parameterBag;
- $this->cacheManager = $cacheManager;
- $this->em = $entityManager;
- $this->requestStack = $requestStack;
- $this->router = $router;
- $this->translator = $translator;
- $this->translatorAdmin = $translatorAdmin;
- $this->reminderStore = $reminderStore;
- $this->security = $security;
- $this->formFactory = $formFactory;
- $this->stringComponent = $stringComponent;
- $this->metaComponent = $metaComponent;
- }
-
- public function getFunctions()
- {
- return [
- new TwigFunction('sov_liip', [$this, 'liip']),
- new TwigFunction('sov_get_by_devalias', [$this, 'getByDevAlias']),
- new TwigFunction('sov_parameter', [$this, 'getParameter']),
- new TwigFunction('sov_homepage_route', [$this, 'getHomepageRoute']),
- new TwigFunction('lc_format_price', [$this, 'formatPrice']),
- new TwigFunction('die', [$this, 'die']),
- new TwigFunction('get_form_newsletter', [$this, 'getFormNewsletter']),
- new TwigFunction('sov_limit_text', [$this, 'limitText']),
- ];
- }
-
- public function die()
- {
- die();
- }
-
- public function getFilters()
- {
- return [
- new TwigFilter('uc_first', [$this, 'ucFirst']),
- new TwigFilter('format_price', [$this, 'formatPrice']),
- new TwigFilter('sov_cache', [$this, 'sovCache']),
- ];
- }
-
- public function sovCache($file)
- {
- $cacheTime = filemtime($this->kernel->getProjectDir() . '/public' . $file);
- if ($cacheTime) {
- return $file . '?c=' . $cacheTime;
- } else {
- return $file . "?c=0";
- }
- }
-
- public function formatPrice($price, $unbreakableSpace = true)
- {
- $price = number_format($price, 2, ',', ' ');
- $price .= $unbreakableSpace ? ' ' : ' ';
- $price .= '€';
-
- return $price;
- }
-
- public function ucFirst($string)
- {
- return ucfirst($string);
- }
-
- public function liip($path, $thumb = 'tile', $default = 'default.jpg')
- {
- if (substr($path, 0, 1) === '/') {
- $path = substr($path, 1);
- }
-
- if ($path) {
- $fileManagerFolder = substr($this->getFileManagerFolder(), 1);
-
- if (strpos($path, $fileManagerFolder) === false) {
- $path = $fileManagerFolder.'/'.$path;
- }
-
- if (file_exists($path)) {
- return $this->cacheManager->getBrowserPath($path, $thumb);
- }
- }
-
- return $this->cacheManager->getBrowserPath($this->getFileManagerFolder().'/'.$default, $thumb);
- }
-
- public function getFileManagerFolder()
- {
- return $this->parameterBag->get('app.path_uploads');
- }
-
- public function getHomepageRoute()
- {
- return $this->parameterBag->get('lc_sov.homepage_route');
- }
-
- public function getParameter($name)
- {
- return $this->parameterBag->get($name);
- }
-
- public function getFormNewsletter()
- {
- $form = $this->formFactory->create(NewsletterType::class);
- return $form->createView();
- }
-
- public function limitText(string $text, int $limit)
- {
- return $this->stringComponent->limitText($text, $limit);
- }
-
- }
|