<commit_before><?php

/**
 * This File is part of the Thapp\JitImage package
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For full copyright and license information, please refer to the LICENSE file
 * that was distributed with this package.
 */

namespace Thapp\JitImage;

use Illuminate\Support\ServiceProvider;

/**
 * Class: JitImageServiceProvider
 *
 * @uses ServiceProvider
 *
 * @package Thapp\JitImage
 * @version $Id$
 * @author  <NAME> <<EMAIL>>
 * @license MIT
 */
class JitImageServiceProvider extends ServiceProvider
{
    /**
     * register
     *
     * @access public
     * @return void
     */
    public function register()
    {
        $this->package('thapp/jitimage');

        $this->registerDriver();
        $this->registerResolver();
        $this->registerResponse();
        $this->regsiterCommands();
    }

    /**
     * registerDriver
     *
     * @access protected
     * @return void
     */
    protected function registerDriver()
    {
        $config = $this->app['config'];

        $driver = sprintf('\Thapp\JitImage\Driver\%sDriver', $driverName = ucfirst($config->get('jitimage::driver', 'gd')));
        $this->app->bind('Thapp\JitImage\Cache\CacheInterface', function ()
            {
                $path = storage_path() . '/jit';
                $cache = new \Thapp\JitImage\Cache\ImageCache(
                    $this->app['Thapp\JitImage\ImageInterface'],
                    $this->app['files'],
                    $path
                );
                return $cache;
            }
        );


        $this->app->bind('Thapp\JitImage\Driver\BinLocatorInterface', 'Thapp\JitImage\Driver\ImBinLocator');
        $this->app->bind('Thapp\JitImage\Driver\SourceLoaderInterface', 'Thapp\JitImage\Driver\ImageSourceLoader');

        $this->app->extend('Thapp\JitImage\Driver\BinLocatorInterface', function ($locator) use ($config)
            {
                extract($config->get('jitimage::imagemagick', ['path' => '/usr/local/bin', 'bin' => 'convert']));

                $locator->setConverterPath(sprintf('%s%s%s', rtrim($path, DIRECTORY_SEPARATOR), DIRECTORY_SEPARATOR, $bin));

                return $locator;
            }
        );

        $this->app->bind('Thapp\JitImage\ImageInterface', 'Thapp\JitImage\Image');
        $this->app->bind('Thapp\JitImage\Driver\DriverInterface', $driver);

        $this->app->extend('Thapp\JitImage\ImageInterface', function ($image)
            {
                $image->setQuality($this->app['config']->get('jitimage::quality', 80));
                return $image;
            }
        );


        $this->app['jitimage'] = $this->app->share(function () {
            return $this->app->make('Thapp\JitImage\JitImage');
        });

        $this->app['jitimage.cache'] = $this->app->share(function () {
            return $this->app->make('Thapp\JitImage\Cache\CacheInterface');
        });

        $this->registerFilter($driverName, $this->getFilters());
    }

    /**
     * registerResolver
     *
     * @access protected
     * @return void
     */
    protected function registerResolver()
    {
        $config = $this->app['config'];

        $this->app->singleton('Thapp\JitImage\ResolverInterface', 'Thapp\JitImage\JitImageResolver');

        $this->app->bind('Thapp\JitImage\ResolverConfigInterface', function () use ($config) {

                $conf = [
                    'trusted_sites' => $this->extractDomains($config->get('jitimage::trusted-sites', [])),
                    'cache_prefix'  => $config->get('jitimage::cacheprefix', 'jit_'),
                    'cache_route'   => $config->get('jitimage::cacheroute', 'jit/storage'),
                    'base'          => $config->get('jitimage::base', public_path()),
                    'cache'         => in_array($config->getEnvironment(), $config->get('jitimage::cache', []))
                ];
                return new \Thapp\JitImage\JitResolveConfiguration($conf);
        });
    }

    /**
     * registerResponse
     *
     * @access protected
     * @return void
     */
    protected function registerResponse()
    {
        $type = $this->app['config']->get('jitimage::response-type', 'generic');

        $this->app->bind('Thapp\JitImage\Response\FileResponseInterface',
            sprintf('Thapp\JitImage\Response\%sFileResponse', ucfirst($type))
        );
    }

    /**
     * registerController
     *
     * @access protected
     * @return void;
     */
    protected function registerController()
    {
        $config = $this->app['config'];

        $recepies   = $config->get('jitimage::recepies', []);
        $route      = $config->get('jitimage::route', 'image');
        $cacheroute = $config->get('jitimage::cacheroute', 'jit/storage');

        $this->app['router']
            ->get($cacheroute . '/{id}', 'Thapp\JitImage\Controller\JitController@getCached')
            ->where('id', '(.*\/){1}.*');

        if (!empty($recepies)) {
            return $this->registerRecepies($recepies, $route);
        }


        $this->app['router']
            ->get($route . '/{params}/{source}/{filter?}', 'Thapp\JitImage\Controller\JitController@getImage')
            ->where('params', '(\d+\/?){1,4}([0-9A-Fa-f]{3}|[0-9A-Fa-f]{6})?')
            ->where('source', '(([^0-9A-Fa-f]{3}|[^0-9A-Fa-f]{6}).*?(?=\/filter:.*)?)')
            ->where('filter', '(filter:.*)');
    }<commit_msg>fix regex to match only valid params depending on the given mode<commit_after><?php

/**
 * This File is part of the Thapp\JitImage package
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For full copyright and license information, please refer to the LICENSE file
 * that was distributed with this package.
 */

namespace Thapp\JitImage;

use Illuminate\Support\ServiceProvider;

/**
 * Class: JitImageServiceProvider
 *
 * @uses ServiceProvider
 *
 * @package Thapp\JitImage
 * @version $Id$
 * @author  <NAME> <<EMAIL>>
 * @license MIT
 */
class JitImageServiceProvider extends ServiceProvider
{
    /**
     * register
     *
     * @access public
     * @return void
     */
    public function register()
    {
        $this->package('thapp/jitimage');

        $this->registerDriver();
        $this->registerResolver();
        $this->registerResponse();
        $this->regsiterCommands();
    }

    /**
     * registerDriver
     *
     * @access protected
     * @return void
     */
    protected function registerDriver()
    {
        $config = $this->app['config'];

        $driver = sprintf('\Thapp\JitImage\Driver\%sDriver', $driverName = ucfirst($config->get('jitimage::driver', 'gd')));
        $this->app->bind('Thapp\JitImage\Cache\CacheInterface', function ()
            {
                $path = storage_path() . '/jit';
                $cache = new \Thapp\JitImage\Cache\ImageCache(
                    $this->app['Thapp\JitImage\ImageInterface'],
                    $this->app['files'],
                    $path
                );
                return $cache;
            }
        );


        $this->app->bind('Thapp\JitImage\Driver\BinLocatorInterface', 'Thapp\JitImage\Driver\ImBinLocator');
        $this->app->bind('Thapp\JitImage\Driver\SourceLoaderInterface', 'Thapp\JitImage\Driver\ImageSourceLoader');

        $this->app->extend('Thapp\JitImage\Driver\BinLocatorInterface', function ($locator) use ($config)
            {
                extract($config->get('jitimage::imagemagick', ['path' => '/usr/local/bin', 'bin' => 'convert']));

                $locator->setConverterPath(sprintf('%s%s%s', rtrim($path, DIRECTORY_SEPARATOR), DIRECTORY_SEPARATOR, $bin));

                return $locator;
            }
        );

        $this->app->bind('Thapp\JitImage\ImageInterface', 'Thapp\JitImage\Image');
        $this->app->bind('Thapp\JitImage\Driver\DriverInterface', $driver);

        $this->app->extend('Thapp\JitImage\ImageInterface', function ($image)
            {
                $image->setQuality($this->app['config']->get('jitimage::quality', 80));
                return $image;
            }
        );


        $this->app['jitimage'] = $this->app->share(function () {
            return $this->app->make('Thapp\JitImage\JitImage');
        });

        $this->app['jitimage.cache'] = $this->app->share(function () {
            return $this->app->make('Thapp\JitImage\Cache\CacheInterface');
        });

        $this->registerFilter($driverName, $this->getFilters());
    }

    /**
     * registerResolver
     *
     * @access protected
     * @return void
     */
    protected function registerResolver()
    {
        $config = $this->app['config'];

        $this->app->singleton('Thapp\JitImage\ResolverInterface', 'Thapp\JitImage\JitImageResolver');

        $this->app->bind('Thapp\JitImage\ResolverConfigInterface', function () use ($config) {

                $conf = [
                    'trusted_sites' => $this->extractDomains($config->get('jitimage::trusted-sites', [])),
                    'cache_prefix'  => $config->get('jitimage::cacheprefix', 'jit_'),
                    'cache_route'   => $config->get('jitimage::cacheroute', 'jit/storage'),
                    'base'          => $config->get('jitimage::base', public_path()),
                    'cache'         => in_array($config->getEnvironment(), $config->get('jitimage::cache', []))
                ];
                return new \Thapp\JitImage\JitResolveConfiguration($conf);
        });
    }

    /**
     * registerResponse
     *
     * @access protected
     * @return void
     */
    protected function registerResponse()
    {
        $type = $this->app['config']->get('jitimage::response-type', 'generic');

        $this->app->bind('Thapp\JitImage\Response\FileResponseInterface',
            sprintf('Thapp\JitImage\Response\%sFileResponse', ucfirst($type))
        );
    }

    /**
     * registerController
     *
     * @access protected
     * @return void;
     */
    protected function registerController()
    {
        $config = $this->app['config'];

        $recepies   = $config->get('jitimage::recepies', []);
        $route      = $config->get('jitimage::route', 'image');
        $cacheroute = $config->get('jitimage::cacheroute', 'jit/storage');

        $this->app['router']
            ->get($cacheroute . '/{id}', 'Thapp\JitImage\Controller\JitController@getCached')
            ->where('id', '(.*\/){1}.*');

        if (!empty($recepies)) {
            return $this->registerRecepies($recepies, $route);
        }

        $this->app['router']
            ->get($route . '/{params}/{source}/{filter?}', 'Thapp\JitImage\Controller\JitController@getImage')
            ->where('params', '([0]|[1|4](\/\d+){2}|[2](\/\d+){3}|[3](\/\d+){3}\/?([0-9A-Fa-f]{3}|[0-9A-Fa-f]{6})?)')
            ->where('source', '(([^0-9A-Fa-f]{3}|[^0-9A-Fa-f]{6}).*?(?=\/filter:.*)?)')
            ->where('filter', '(filter:.*)');
    }
{'fraction_non_alphanumeric': 0.1841208436953118, 'fraction_numerical': 0.00368425900340794, 'mean_word_length': 2.2853252647503783, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 167, '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': '5135719', 'n_tokens_mistral': 3381, 'n_tokens_neox': 3275, 'n_words': 453}