<commit_before><?php namespace CupOfTea\TwoStream\Foundation\Ws;

use Log;
use Auth;
use Session;
use Exception;

use Illuminate\Auth\AuthManager;
use Illuminate\Pipeline\Pipeline;
use Illuminate\Support\Facades\Facade;
use CupOfTea\TwoStream\Routing\WsRouter;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\Routing\TerminableMiddleware;
use CupOfTea\TwoStream\Session\ReadOnlySessionManager;
use CupOfTea\TwoStream\Contracts\Ws\Kernel as KernelContract;

class Kernel implements KernelContract
{
    
    /**
     * The application implementation.
     *
     * @var \Illuminate\Contracts\Foundation\Application
     */
    protected $app;
    
    /**
     * The router instance.
     *
     * @var \CupOfTea\TwoStream\Routing\Router
     */
    protected $router;
    
    /**
     * The application's middleware stack.
     *
     * @var array
     */
    protected $middleware = [];
    
    /**
     * The application's route middleware.
     *
     * @var array
     */
    protected $routeMiddleware = [];
    
    /**
     * Create a new HTTP kernel instance.
     *
     * @param  \Illuminate\Contracts\Foundation\Application  $app
     * @param  \CupOfTea\TwoStream\Routing\WsRouter  $router
     * @return void
     */
    public function __construct(Application $app, WsRouter $router)
    {
        $this->app = $app;
        $this->app['config']['session.driver'] = 'array';
        $this->router = $router;
        foreach ($this->routeMiddleware as $key => $middleware) {
            $router->middleware($key, $middleware);
        }
    }
    
    /**
     * Handle an incoming HTTP request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function handle($request)
    {
        $this->loadGuard();
        
        try {
            $response = $this->sendRequestThroughRouter($request);
        } catch (Exception $e) {
            $this->reportException($e);
            throw($e);
        }
        $this->app['events']->fire('wskernel.handled', [$request, $response]);
        return $response;
    }
    
    protected function loadGuard()
    {
        $guard = new AuthManager($this->app);
        Auth::swap($guard);
    }
    
    /**<commit_msg>Handle errors better<commit_after><?php namespace CupOfTea\TwoStream\Foundation\Ws;

use Log;
use Auth;
use Session;
use Exception;

use Illuminate\Auth\AuthManager;
use Illuminate\Pipeline\Pipeline;
use Illuminate\Support\Facades\Facade;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\Routing\TerminableMiddleware;

use CupOfTea\TwoStream\Routing\WsRouter;
use CupOfTea\TwoStream\Session\ReadOnlySessionManager;
use CupOfTea\TwoStream\Contracts\Ws\Kernel as KernelContract;

class Kernel implements KernelContract
{
    
    /**
     * The application implementation.
     *
     * @var \Illuminate\Contracts\Foundation\Application
     */
    protected $app;
    
    /**
     * The router instance.
     *
     * @var \CupOfTea\TwoStream\Routing\Router
     */
    protected $router;
    
    /**
     * The application's middleware stack.
     *
     * @var array
     */
    protected $middleware = [];
    
    /**
     * The application's route middleware.
     *
     * @var array
     */
    protected $routeMiddleware = [];
    
    /**
     * Create a new HTTP kernel instance.
     *
     * @param  \Illuminate\Contracts\Foundation\Application  $app
     * @param  \CupOfTea\TwoStream\Routing\WsRouter  $router
     * @return void
     */
    public function __construct(Application $app, WsRouter $router)
    {
        $this->app = $app;
        $this->app['config']['session.driver'] = 'array';
        $this->router = $router;
        foreach ($this->routeMiddleware as $key => $middleware) {
            $router->middleware($key, $middleware);
        }
    }
    
    /**
     * Handle an incoming HTTP request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function handle($request)
    {
        $this->loadGuard();
        
        try {
            $response = $this->sendRequestThroughRouter($request);
        } catch (Exception $e) {
            $this->reportException($e);
            $response = [
                'error' => [
                    'msg' => $e->getMessage(),
                    'domain' => 'php.' . snake_case(get_class($e)),
                    'full_error' => $e
                ]
            ];
        }
        $this->app['events']->fire('wskernel.handled', [$request, $response]);
        return $response;
    }
    
    protected function loadGuard()
    {
        $guard = new AuthManager($this->app);
        Auth::swap($guard);
    }
    
    /**
{'fraction_non_alphanumeric': 0.12874816523380164, 'fraction_numerical': 0.0, 'mean_word_length': 2.156849768365321, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 32, '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': '6932572', 'n_tokens_mistral': 1397, 'n_tokens_neox': 1431, 'n_words': 270}