<commit_before><?php

namespace Athorrent\Service;

use Asm89\Twig\CacheExtension\CacheStrategy\GenerationalCacheStrategy;
use Asm89\Twig\CacheExtension\Extension as CacheExtension;
use Athorrent\Utils\Cache\CacheProvider;
use Athorrent\Utils\Cache\KeyGenerator;
use Silex\Application;
use Silex\ServiceProviderInterface;
use SPE\FilesizeExtensionBundle\Twig\FilesizeExtension;
use Twig_Environment;
use Twig_SimpleFunction;

class TwigServiceProvider implements ServiceProviderInterface
{
    private $manifest;

    public function register(Application $app)
    {
        $app->register(new \Silex\Provider\TwigServiceProvider(), [
            'twig.path' => APP_DIR . DIRECTORY_SEPARATOR . 'views',
            'twig.options' => ['cache' => CACHE_DIR . DIRECTORY_SEPARATOR . 'twig']
        ]);

        $app['twig'] = $app->share($app->extend('twig', function (Twig_Environment $twig) use ($app) {
            return $this->extendTwig($twig, $app);
        }));
    }

    public function extendTwig(Twig_Environment $twig, Application $app)
    {
        $this->initializeCache($twig, $app);

        $twig->addExtension(new FilesizeExtension());

        $twig->addFunction(new Twig_SimpleFunction('torrentStateToClass', [$this, 'torrentStateToClass']));

        $twig->addFunction(new Twig_SimpleFunction('asset_path', [$this, 'getAssetPath']));

        $twig->addFunction(new Twig_SimpleFunction('asset_url', [$this, 'getAssetUrl']));

        $twig->addFunction(new Twig_SimpleFunction('css', [$this, 'includeCss']));

        $twig->addFunction(new Twig_SimpleFunction('js', [$this, 'includeJs']));

        $twig->addFunction(new Twig_SimpleFunction('format_age', [$this, 'formatAge']));

        $twig->addFunction(new Twig_SimpleFunction('path', function ($action, $parameters = [], $prefixAction = null) use ($app) {
            return $app['alias_resolver']->generatePath($action, $parameters, $prefixAction);
        }));

        $twig->addFunction(new Twig_SimpleFunction('uri', function ($action, $parameters = [], $prefixAction = null) use ($app) {
            return $app['alias_resolver']->generateUrl($action, $parameters, $prefixAction);
        }));

        return $twig;
    }

    public function boot(Application $app)
    {
        $this->manifest = json_decode(file_get_contents(WEB_DIR . DIRECTORY_SEPARATOR . 'manifest.json'), true);
    }

    protected function initializeCache(Twig_Environment $twig, Application $app)
    {
        $cacheProvider = new CacheProvider($app['cache']);
        $keyGenerator = new KeyGenerator($app['locale']);

        $cacheStrategy = new GenerationalCacheStrategy($cacheProvider, $keyGenerator, 0);
        $cacheExtension = new CacheExtension($cacheStrategy);

        $twig->addExtension($cacheExtension);
    }

    public function torrentStateToClass($torrent)
    {
        $state = $torrent['state'];

        if ($state === 'paused') {
            $class = 'warning';
        } elseif ($state === 'seeding' || $state === 'downloading') {
            $class = 'success';
        } elseif ($state === 'disabled') {
            $class = 'disabled';
        } else {
            $class = 'info';
        }

        return $class;
    }

    public function getAssetPath($assetId)
    {
        return $this->manifest[$assetId];
    }

    public function getAssetUrl($assetId)
    {
        return '//' . STATIC_HOST . $this->getAssetPath($assetId);
    }

    protected function includeResource($assetId, $inline)<commit_msg>getAssetPath: return assetId if entry doesn't exists<commit_after><?php

namespace Athorrent\Service;

use Asm89\Twig\CacheExtension\CacheStrategy\GenerationalCacheStrategy;
use Asm89\Twig\CacheExtension\Extension as CacheExtension;
use Athorrent\Utils\Cache\CacheProvider;
use Athorrent\Utils\Cache\KeyGenerator;
use Silex\Application;
use Silex\ServiceProviderInterface;
use SPE\FilesizeExtensionBundle\Twig\FilesizeExtension;
use Twig_Environment;
use Twig_SimpleFunction;

class TwigServiceProvider implements ServiceProviderInterface
{
    private $manifest;

    public function register(Application $app)
    {
        $app->register(new \Silex\Provider\TwigServiceProvider(), [
            'twig.path' => APP_DIR . DIRECTORY_SEPARATOR . 'views',
            'twig.options' => ['cache' => CACHE_DIR . DIRECTORY_SEPARATOR . 'twig']
        ]);

        $app['twig'] = $app->share($app->extend('twig', function (Twig_Environment $twig) use ($app) {
            return $this->extendTwig($twig, $app);
        }));
    }

    public function extendTwig(Twig_Environment $twig, Application $app)
    {
        $this->initializeCache($twig, $app);

        $twig->addExtension(new FilesizeExtension());

        $twig->addFunction(new Twig_SimpleFunction('torrentStateToClass', [$this, 'torrentStateToClass']));

        $twig->addFunction(new Twig_SimpleFunction('asset_path', [$this, 'getAssetPath']));

        $twig->addFunction(new Twig_SimpleFunction('asset_url', [$this, 'getAssetUrl']));

        $twig->addFunction(new Twig_SimpleFunction('css', [$this, 'includeCss']));

        $twig->addFunction(new Twig_SimpleFunction('js', [$this, 'includeJs']));

        $twig->addFunction(new Twig_SimpleFunction('format_age', [$this, 'formatAge']));

        $twig->addFunction(new Twig_SimpleFunction('path', function ($action, $parameters = [], $prefixAction = null) use ($app) {
            return $app['alias_resolver']->generatePath($action, $parameters, $prefixAction);
        }));

        $twig->addFunction(new Twig_SimpleFunction('uri', function ($action, $parameters = [], $prefixAction = null) use ($app) {
            return $app['alias_resolver']->generateUrl($action, $parameters, $prefixAction);
        }));

        return $twig;
    }

    public function boot(Application $app)
    {
        $this->manifest = json_decode(file_get_contents(WEB_DIR . DIRECTORY_SEPARATOR . 'manifest.json'), true);
    }

    protected function initializeCache(Twig_Environment $twig, Application $app)
    {
        $cacheProvider = new CacheProvider($app['cache']);
        $keyGenerator = new KeyGenerator($app['locale']);

        $cacheStrategy = new GenerationalCacheStrategy($cacheProvider, $keyGenerator, 0);
        $cacheExtension = new CacheExtension($cacheStrategy);

        $twig->addExtension($cacheExtension);
    }

    public function torrentStateToClass($torrent)
    {
        $state = $torrent['state'];

        if ($state === 'paused') {
            $class = 'warning';
        } elseif ($state === 'seeding' || $state === 'downloading') {
            $class = 'success';
        } elseif ($state === 'disabled') {
            $class = 'disabled';
        } else {
            $class = 'info';
        }

        return $class;
    }

    public function getAssetPath($assetId)
    {
        if (isset($this->manifest[$assetId])) {
            return $this->manifest[$assetId];
        }

        return '/' . $assetId;
    }

    public function getAssetUrl($assetId)
    {
        return '//' . STATIC_HOST . $this->getAssetPath($assetId);
    }

    protected function includeResource($assetId, $inline)
{'fraction_non_alphanumeric': 0.14543926019335857, 'fraction_numerical': 0.0014011489421325487, 'mean_word_length': 3.3926153846153846, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 50, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7539705', 'n_tokens_mistral': 2140, 'n_tokens_neox': 2057, 'n_words': 385}