<commit_before><?php

/**
 * @package     Mautic
 * @copyright   2016 Mautic Contributors. All rights reserved.
 * @author      Mautic
 * @link        http://mautic.org
 * @license     GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
 */

namespace Mautic\Middleware;

use Stack\StackedHttpKernel;
use Symfony\Component\HttpKernel\HttpKernelInterface;

class MiddlewareBuilder
{
    protected $specs;
    
    public function __construct($env = 'prod')
    {
        $this->specs = new \SplPriorityQueue();

        $middlewares = glob(__DIR__ . '/*Middleware.php');
        
        $this->addMiddlewares($middlewares);
        
        if ($envMiddlewares = glob(__DIR__ . '/' . ucfirst($env) . '/*Middleware.php')) {
            $this->addMiddlewares($envMiddlewares, $env);
        }
    }
    
    public function addMiddlewares(array $middlewares, $env = null)
    {
        $prefix = 'Mautic\\Middleware\\';
        
        if ($env) {
            $prefix .= ucfirst($env) . '\\';
        }
        
        foreach ($middlewares as $middleware) {
            $this->push($prefix . basename(substr($middleware, 0, -4)));
        }
    }
    
    public function push($kernelClass)
    {
        $reflection = new \ReflectionClass($kernelClass);
        $priority = $reflection->getConstant('PRIORITY');

        $this->specs->insert($reflection, $priority);
    }

    public function resolve(HttpKernelInterface $app)
    {
        $middlewares = array($app);
        
        /** @var \ReflectionClass $spec */
        foreach ($this->specs as $spec) {
            $app = $spec->newInstanceArgs([$app]);
            
            array_unshift($middlewares, $app);
        }
        
        return new StackedHttpKernel($app, $middlewares);
    }
}
<commit_msg>Update MiddlewareBuilder logic<commit_after><?php

/**
 * @package     Mautic
 * @copyright   2016 Mautic Contributors. All rights reserved.
 * @author      Mautic
 * @link        http://mautic.org
 * @license     GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
 */

namespace Mautic\Middleware;

use Stack\StackedHttpKernel;
use Symfony\Component\HttpKernel\HttpKernelInterface;

class MiddlewareBuilder
{
    protected $specs;
    
    public function __construct($env = null)
    {
        $this->specs = new \SplPriorityQueue();

        $middlewares = glob(__DIR__ . '/*Middleware.php');
        
        $this->addMiddlewares($middlewares);
        
        if (isset($env)) {
            $envMiddlewares = glob(__DIR__ . '/' . ucfirst($env) . '/*Middleware.php');

            if (! empty($envMiddlewares)) {
                $this->addMiddlewares($envMiddlewares, $env);
            }
        }
    }
    
    public function addMiddlewares(array $middlewares, $env = null)
    {
        $prefix = 'Mautic\\Middleware\\';
        
        if ($env) {
            $prefix .= ucfirst($env) . '\\';
        }
        
        foreach ($middlewares as $middleware) {
            $this->push($prefix . basename(substr($middleware, 0, -4)));
        }
    }
    
    public function push($kernelClass)
    {
        $reflection = new \ReflectionClass($kernelClass);
        $priority = $reflection->getConstant('PRIORITY');

        $this->specs->insert($reflection, $priority);
    }

    public function resolve(HttpKernelInterface $app)
    {
        $middlewares = array($app);
        
        /** @var \ReflectionClass $spec */
        foreach ($this->specs as $spec) {
            $app = $spec->newInstanceArgs([$app]);
            
            array_unshift($middlewares, $app);
        }
        
        return new StackedHttpKernel($app, $middlewares);
    }
}

{'fraction_non_alphanumeric': 0.14140860509728692, 'fraction_numerical': 0.004932858317347218, 'mean_word_length': 2.0672268907563027, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '4316166', 'n_tokens_mistral': 1129, 'n_tokens_neox': 1105, 'n_words': 197}