<commit_before><?php

namespace NyroDev\UtilityBundle\Services;

use Doctrine\Persistence\ObjectRepository;
use Html2Text\Html2Text;
use NyroDev\UtilityBundle\Utility\Pager;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

class NyrodevService extends AbstractService
{
    /**
     * @var KernelInterface
     */
    protected $kernel;

    public function __construct($container, KernelInterface $kernel)
    {
        parent::__construct($container);
        $this->kernel = $kernel;
    }

    /**
     * @return KernelInterface
     */
    public function getKernel()
    {
        return $this->kernel;
    }

    /**
     * Kernel request listener to setLocale if configured.
     *
     * @param GetResponseEvent $event Kernel request event
     */
    public function onKernelRequest(GetResponseEvent $event)
    {
        if ($event->isMasterRequest() && $this->getParameter('nyroDev_utility.setLocale')) {
            $locale = $event->getRequest()->getLocale();

            if (0 === strpos($locale, 'change_')) {
                $tmp = explode('change_', $locale);
                $locale = $tmp[1];
                // Update already instanciated objects
                $event->getRequest()->setLocale($locale);
                $this->get('translator')->setLocale($locale);
            }

            if (!defined('NYRO_LOCALE')) {
                define('NYRO_LOCALE', $locale);
            }

            $locales = [
                $locale,
                $locale.'@euro',
                $locale.'.utf8',
            ];
            if (2 == strlen($locale)) {
                $locUp = strtoupper($locale);
                $locale .= '_'.('ZH' == $locUp ? 'CN' : $locUp);
                $locales[] = $locale;
                $locales[] = $locale.'@euro';
                $locales[] = $locale.'.utf8';
            }
            foreach (array_reverse($locales) as $loc) {
                $tmp = setlocale(LC_ALL, $loc);
                if (mb_strtolower($tmp) == mb_strtolower($loc)) {
                    break;
                }
            }
        }
    }

    /**
     * Kernel response listener to add content-language if configured.
     *
     * @param FilterResponseEvent $event Kernel response event
     */
    public function onKernelResponse(FilterResponseEvent $event)
    {
        if ($this->getParameter('nyroDev_utility.setContentLanguageResponse') && $event->getResponse() && $event->getResponse()->headers && $event->getRequest()->getLocale()) {
            $event->getResponse()->headers->set('Content-Language', $event->getRequest()->getLocale());
        }
    }

    /**
     * Generates a URL from the given parameters, allowing extra parameters (like comma).
     *
     * @param string $name       The name of the route
     * @param mixed  $parameters An array of parameters
     * @param bool   $absolute   Whether to generate an absolute URL
     *
     * @return string The generated URL
     */
    public function generateUrl($name, $parameters = [], $absolute = false)
    {
        if ('#' == $name) {
            return '#';
        }

        return str_replace('%2C', ',', $this->get('router')->generate($name, $parameters, $absolute ? UrlGeneratorInterface::ABSOLUTE_URL : UrlGeneratorInterface::ABSOLUTE_PATH));
    }

    /**
     * Absolutize an URL.
     *
     * @param string $path
     *
     * @return string
     */
    public function getFullUrl($path)
    {
        if (0 === strpos($path, 'http') || 0 === strpos($path, 'mailto:') || 0 === strpos($path, '#')) {
            return $path;
        }
        $router = $this->get('router');
        $baseUrl = null;

        if ('/' != $path[0]) {
            $path = '/'.$path;
            $baseUrl = $router->getContext()->getBaseUrl();
        }

        if ($baseUrl && '/' != $baseUrl[0]) {
            $baseUrl = '/'.$baseUrl;
        }

        $host = $router->getContext()->getHost();
        $port = null;
        if ($router->getContext()->isSecure()) {
            $port = $router->getContext()->getHttpsPort();
            if ('443' == $port) {
                $port = null;
            }
        } else {
            $port = $router->getContext()->getHttpPort();
            if ('80' == $port) {
                $port = null;
            }
        }
        if ($port) {
            $host .= ':'.$port;
        }

        return $router->getContext()->getScheme().'://'.$host.$baseUrl.$path;
    }

    /**
     * Set a templating slot.
     *<commit_msg>Fix getFullUrl for https<commit_after><?php

namespace NyroDev\UtilityBundle\Services;

use Doctrine\Persistence\ObjectRepository;
use Html2Text\Html2Text;
use NyroDev\UtilityBundle\Utility\Pager;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

class NyrodevService extends AbstractService
{
    /**
     * @var KernelInterface
     */
    protected $kernel;

    public function __construct($container, KernelInterface $kernel)
    {
        parent::__construct($container);
        $this->kernel = $kernel;
    }

    /**
     * @return KernelInterface
     */
    public function getKernel()
    {
        return $this->kernel;
    }

    /**
     * Kernel request listener to setLocale if configured.
     *
     * @param GetResponseEvent $event Kernel request event
     */
    public function onKernelRequest(GetResponseEvent $event)
    {
        if ($event->isMasterRequest() && $this->getParameter('nyroDev_utility.setLocale')) {
            $locale = $event->getRequest()->getLocale();

            if (0 === strpos($locale, 'change_')) {
                $tmp = explode('change_', $locale);
                $locale = $tmp[1];
                // Update already instanciated objects
                $event->getRequest()->setLocale($locale);
                $this->get('translator')->setLocale($locale);
            }

            if (!defined('NYRO_LOCALE')) {
                define('NYRO_LOCALE', $locale);
            }

            $locales = [
                $locale,
                $locale.'@euro',
                $locale.'.utf8',
            ];
            if (2 == strlen($locale)) {
                $locUp = strtoupper($locale);
                $locale .= '_'.('ZH' == $locUp ? 'CN' : $locUp);
                $locales[] = $locale;
                $locales[] = $locale.'@euro';
                $locales[] = $locale.'.utf8';
            }
            foreach (array_reverse($locales) as $loc) {
                $tmp = setlocale(LC_ALL, $loc);
                if (mb_strtolower($tmp) == mb_strtolower($loc)) {
                    break;
                }
            }
        }
    }

    /**
     * Kernel response listener to add content-language if configured.
     *
     * @param FilterResponseEvent $event Kernel response event
     */
    public function onKernelResponse(FilterResponseEvent $event)
    {
        if ($this->getParameter('nyroDev_utility.setContentLanguageResponse') && $event->getResponse() && $event->getResponse()->headers && $event->getRequest()->getLocale()) {
            $event->getResponse()->headers->set('Content-Language', $event->getRequest()->getLocale());
        }
    }

    /**
     * Generates a URL from the given parameters, allowing extra parameters (like comma).
     *
     * @param string $name       The name of the route
     * @param mixed  $parameters An array of parameters
     * @param bool   $absolute   Whether to generate an absolute URL
     *
     * @return string The generated URL
     */
    public function generateUrl($name, $parameters = [], $absolute = false)
    {
        if ('#' == $name) {
            return '#';
        }

        return str_replace('%2C', ',', $this->get('router')->generate($name, $parameters, $absolute ? UrlGeneratorInterface::ABSOLUTE_URL : UrlGeneratorInterface::ABSOLUTE_PATH));
    }

    /**
     * Absolutize an URL.
     *
     * @param string $path
     *
     * @return string
     */
    public function getFullUrl($path)
    {
        if (0 === strpos($path, 'http') || 0 === strpos($path, 'mailto:') || 0 === strpos($path, '#')) {
            return $path;
        }
        $router = $this->get('router');
        $baseUrl = null;

        if ('/' != $path[0]) {
            $path = '/'.$path;
            $baseUrl = $router->getContext()->getBaseUrl();
        }

        if ($baseUrl && '/' != $baseUrl[0]) {
            $baseUrl = '/'.$baseUrl;
        }

        $host = $router->getContext()->getHost();
        $port = null;
        if ($router->getContext()->getScheme() === 'https') {
            $port = $router->getContext()->getHttpsPort();
            if ('443' == $port) {
                $port = null;
            }
        } else {
            $port = $router->getContext()->getHttpPort();
            if ('80' == $port) {
                $port = null;
            }
        }
        if ($port) {
            $host .= ':'.$port;
        }

        return $router->getContext()->getScheme().'://'.$host.$baseUrl.$path;
    }

    /**
     * Set a templating slot.
     *
{'fraction_non_alphanumeric': 0.15210048553937092, 'fraction_numerical': 0.0037998733375554147, 'mean_word_length': 1.9517133956386292, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 75, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '939011', 'n_tokens_mistral': 2695, 'n_tokens_neox': 2636, 'n_words': 532}