<commit_before><?php

namespace SebastiaanLuca\Router;

use Illuminate\Container\Container;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Foundation\Bootstrap\BootProviders;
use Illuminate\Routing\Router;
use SebastiaanLuca\Router\EventHandlers\RegisterRouteMiddleware;

/**
 * Class ExtendedRouter
 *
 * An extension to the application's router to provide extra functionality.
 *
 * @package SebastiaanLuca\Routing
 */
class ExtendedRouter extends Router
{
    
    /**
     * The registered middleware for named routes.
     *
     * @var array
     */
    protected $routeMiddleware = [];
    
    /**
     * Indicates if all the service providers have been loaded.
     *
     * @var bool
     */
    protected $isBootstrapped = false;
    
    /**
     * Create a new Router instance.
     *
     * @param \Illuminate\Contracts\Events\Dispatcher $events
     * @param \Illuminate\Container\Container $container
     */
    public function __construct(Dispatcher $events, Container $container = null)
    {
        parent::__construct($events, $container);
        
        // Add all registered named route middlewares after all service providers have been loaded
        app('events')->listen('bootstrapped: ' . BootProviders::class, RegisterRouteMiddleware::class);
    }
    
    
    
    /**
     * Add middleware to a named route.
     *
     * Optionally handles wildcard named routes.
     *
     * @param string $name
     * @param array $middleware
     */
    protected function addMiddlewareToRoute($name, array $middleware)
    {
        // Get all registered app routes
        $routes = $this->getRoutes()->getIterator();
        
        // Check if the named route is a wildcard route
        $wildcard = false;
        
        if (ends_with($name, '*')) {
            $wildcard = true;
            $name = substr($name, 0, -1);
        }
        
        /** @var \Illuminate\Routing\Route $route */
        foreach ($routes as $route) {
            // Only apply middleware if route name matches 1-to-1 and it's not a wildcard
            // or if it is a wildcard and the first part before the asterisk is the beginning
            // of the route's name.
            if ((! $wildcard && $route->getName() !== $name) || ($wildcard && ! starts_with($route->getName(), $name))) {
                continue;
            }
            
            // If we have any middleware registered to the route, apply it
            // TODO: get current route middleware, merge with new, remove duplicates using array_unique, re-set middleware on route
            $route->middleware($middleware);
        }
    }<commit_msg>Remove comment (middleware already gets merged)<commit_after><?php

namespace SebastiaanLuca\Router;

use Illuminate\Container\Container;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Foundation\Bootstrap\BootProviders;
use Illuminate\Routing\Router;
use SebastiaanLuca\Router\EventHandlers\RegisterRouteMiddleware;

/**
 * Class ExtendedRouter
 *
 * An extension to the application's router to provide extra functionality.
 *
 * @package SebastiaanLuca\Routing
 */
class ExtendedRouter extends Router
{
    
    /**
     * The registered middleware for named routes.
     *
     * @var array
     */
    protected $routeMiddleware = [];
    
    /**
     * Indicates if all the service providers have been loaded.
     *
     * @var bool
     */
    protected $isBootstrapped = false;
    
    /**
     * Create a new Router instance.
     *
     * @param \Illuminate\Contracts\Events\Dispatcher $events
     * @param \Illuminate\Container\Container $container
     */
    public function __construct(Dispatcher $events, Container $container = null)
    {
        parent::__construct($events, $container);
        
        // Add all registered named route middlewares after all service providers have been loaded
        app('events')->listen('bootstrapped: ' . BootProviders::class, RegisterRouteMiddleware::class);
    }
    
    
    
    /**
     * Add middleware to a named route.
     *
     * Optionally handles wildcard named routes.
     *
     * @param string $name
     * @param array $middleware
     */
    protected function addMiddlewareToRoute($name, array $middleware)
    {
        // Get all registered app routes
        $routes = $this->getRoutes()->getIterator();
        
        // Check if the named route is a wildcard route
        $wildcard = false;
        
        if (ends_with($name, '*')) {
            $wildcard = true;
            $name = substr($name, 0, -1);
        }
        
        /** @var \Illuminate\Routing\Route $route */
        foreach ($routes as $route) {
            // Only apply middleware if route name matches 1-to-1 and it's not a wildcard
            // or if it is a wildcard and the first part before the asterisk is the beginning
            // of the route's name.
            if ((! $wildcard && $route->getName() !== $name) || ($wildcard && ! starts_with($route->getName(), $name))) {
                continue;
            }
            
            // If we have any middleware registered to the route, apply it
            $route->middleware($middleware);
        }
    }
{'fraction_non_alphanumeric': 0.10365384615384615, 'fraction_numerical': 0.0015384615384615385, 'mean_word_length': 2.214462299134734, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 15, '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': '5863842', 'n_tokens_mistral': 1382, 'n_tokens_neox': 1393, 'n_words': 419}