<commit_before><?php

namespace Okeanrst\FastCGIZF2Adapter;

use PHPFastCGI\FastCGIDaemon\KernelInterface;
use PHPFastCGI\FastCGIDaemon\Http\RequestInterface;
use Zend\Mvc\Service\ServiceManagerConfig;
use Okeanrst\FastCGIZF2Adapter\ServiceManager;
use Zend\ModuleManager\ModuleEvent;
use Zend\Psr7Bridge\Psr7ServerRequest;
use Zend\Psr7Bridge\Psr7Response;

/**
 * Wraps a Zend Framework 2 application object as an implementation of the kernel
 * interface.
 */
class AppWrapper implements KernelInterface
{
    protected $serviceManager;
    
    protected $moduleManager;	
	
    protected $config;    
     
    protected $startServicesInstances = [];
    
    protected $startAbstractFactorys = [];   
     
    public function __construct($configuration = [])
    {
        @define('PHP_SAPI', ' cgi-fcgi');
        $smConfig = isset($configuration['service_manager']) ? $configuration['service_manager'] : [];
        $smConfig = new ServiceManagerConfig($smConfig);
        
        $this->serviceManager = new ServiceManager();
        $smConfig->configureServiceManager($this->serviceManager);
        $this->serviceManager->setService('ApplicationConfig', $configuration);

        $this->moduleManager = $this->serviceManager->get('ModuleManager');
        $events = $this->moduleManager->getEventManager();
        $event  = $this->moduleManager->getEvent();
        $event->setName(ModuleEvent::EVENT_LOAD_MODULES);
        $events->triggerEvent($event);
        $this->serviceManager->setAllowOverride(true);
        
        $startInstServ = $this->serviceManager->getRegisteredServices()['instances'];
        foreach($startInstServ as $v) {
            array_push($this->startServicesInstances, $v);
        }
        $this->startServicesInstances = array_flip($this->startServicesInstances);
        
        $this->startAbstractFactorys = $this->serviceManager->getAbstractFactorys();
    }
    
    public function handleRequest(RequestInterface $request)
    {
        ini_set('session.use_cookies', false);        
        
        $sesId = '';
        if ($request->getCookies() && ($sesId = $request->getCookies()[session_name()]) && $sesId !== '') {
            session_id($sesId);
        } 

        $serviceManager = $this->serviceManager;
        
        $moduleManager = $this->moduleManager;  
        $events = $moduleManager->getEventManager();
        $event  = $moduleManager->getEvent();
        $event->setName(ModuleEvent::EVENT_LOAD_MODULES_POST);
        $events->triggerEvent($event);
        
        $listenersFromAppConfig = isset($configuration['listeners']) ? $configuration['listeners'] : [];
        $config = $serviceManager->get('config');
        $listenersFromConfigService = isset($config['listeners']) ? $config['listeners'] : [];
        $listeners = array_unique(array_merge($listenersFromConfigService, $listenersFromAppConfig));
        
       
        $serverRequest = $request->getServerRequest();
        $request = Psr7ServerRequest::toZend($serverRequest);
		
		$serviceManager->setService('Request', $request);
        
        $application = $serviceManager->get('Application');
		$application->bootstrap($listeners);       
        
		ob_start();
		$application->run();
		$out = ob_get_contents();
		ob_end_clean();       
		$response = $application->getResponse();        
        
        $sid = defined('SID') ? constant('SID') : false;
        if (session_status() == PHP_SESSION_ACTIVE || ($sid !== false && session_id())) {
            if ($sesId !== session_id()) {
                $stringCookieSession = session_name().'='.session_id().'; path=/';
                $response->getHeaders()->addHeaders(['Set-Cookie' => $stringCookieSession]);
            }            
            session_write_close();
            @define('SID', false);           
            session_id(strtr(base64_encode(random_bytes(20)), ['=' => '', '+' => '-', '/' => '-' ]));
        }
        
        if (isset($_SESSION)) {
            foreach ($_SESSION as $key => $val) {
                unset($_SESSION[$key]);
            }
        }
               
        $finishRegisteredServices = $serviceManager->getRegisteredServices();
        
        foreach ($finishRegisteredServices['instances'] as $num => $name) {
            if (!in_array($name, $services, true)) { 
       
                if (!array_key_exists($name, $this->startServicesInstances) ) {
                    $serviceManager->unregisterService($name);                    
                }
            }
        }
        
        $serviceManager->unsetAbstractFactorys();
        foreach ($this->startAbstractFactorys as $key => $fab) {
            $serviceManager->addAbstractFactory($fab);
        }        
        
		$diactorosResponse = Psr7Response::fromZend($response);
        //var_dump($out);
        return $diactorosResponse;
    }
}<commit_msg>Update AppWrapper.php<commit_after><?php

namespace Okeanrst\FastCGIZF2Adapter;

use PHPFastCGI\FastCGIDaemon\KernelInterface;
use PHPFastCGI\FastCGIDaemon\Http\RequestInterface;
use Zend\Mvc\Service\ServiceManagerConfig;
use Okeanrst\FastCGIZF2Adapter\ServiceManager;
use Zend\ModuleManager\ModuleEvent;
use Zend\Psr7Bridge\Psr7ServerRequest;
use Zend\Psr7Bridge\Psr7Response;

/**
 * Wraps a Zend Framework 2 application object as an implementation of the kernel
 * interface.
 */
class AppWrapper implements KernelInterface
{
    protected $serviceManager;
    
    protected $moduleManager;	
	
    protected $config;    
     
    protected $startServicesInstances = [];
    
    protected $startAbstractFactorys = [];   
     
    public function __construct($configuration = [])
    {
        @define('PHP_SAPI', ' cgi-fcgi');
        $smConfig = isset($configuration['service_manager']) ? $configuration['service_manager'] : [];
        $smConfig = new ServiceManagerConfig($smConfig);
        
        $this->serviceManager = new ServiceManager();
        $smConfig->configureServiceManager($this->serviceManager);
        $this->serviceManager->setService('ApplicationConfig', $configuration);

        $this->moduleManager = $this->serviceManager->get('ModuleManager');
        $events = $this->moduleManager->getEventManager();
        $event  = $this->moduleManager->getEvent();
        $event->setName(ModuleEvent::EVENT_LOAD_MODULES);
        $events->triggerEvent($event);
        $this->serviceManager->setAllowOverride(true);
        
        $startInstServ = $this->serviceManager->getRegisteredServices()['instances'];
        foreach($startInstServ as $v) {
            array_push($this->startServicesInstances, $v);
        }
        $this->startServicesInstances = array_flip($this->startServicesInstances);
        
        $this->startAbstractFactorys = $this->serviceManager->getAbstractFactorys();
    }
    
    public function handleRequest(RequestInterface $request)
    {
        ini_set('session.use_cookies', false);        
        
        $sesId = '';
        if ($request->getCookies() && ($sesId = $request->getCookies()[session_name()]) && $sesId !== '') {
            session_id($sesId);
        } 

        $serviceManager = $this->serviceManager;
        
        $moduleManager = $this->moduleManager;  
        $events = $moduleManager->getEventManager();
        $event  = $moduleManager->getEvent();
        $event->setName(ModuleEvent::EVENT_LOAD_MODULES_POST);
        $events->triggerEvent($event);
        
        $listenersFromAppConfig = isset($configuration['listeners']) ? $configuration['listeners'] : [];
        $config = $serviceManager->get('config');
        $listenersFromConfigService = isset($config['listeners']) ? $config['listeners'] : [];
        $listeners = array_unique(array_merge($listenersFromConfigService, $listenersFromAppConfig));
        
       
        $serverRequest = $request->getServerRequest();
        $request = Psr7ServerRequest::toZend($serverRequest);
		
		$serviceManager->setService('Request', $request);
        
        $application = $serviceManager->get('Application');
		$application->bootstrap($listeners);       
        
		ob_start();
		$application->run();
		$out = ob_get_contents();
		ob_end_clean();       
		$response = $application->getResponse();        
        
        $sid = defined('SID') ? constant('SID') : false;
        if (session_status() == PHP_SESSION_ACTIVE || ($sid !== false && session_id())) {
            if ($sesId !== session_id()) {
                $stringCookieSession = session_name().'='.session_id().'; path=/';
                $response->getHeaders()->addHeaders(['Set-Cookie' => $stringCookieSession]);
            }            
            session_write_close();
            @define('SID', false);           
            session_id(strtr(base64_encode(random_bytes(20)), ['=' => '', '+' => '-', '/' => '-' ]));
        }
        
        if (isset($_SESSION)) {
            foreach ($_SESSION as $key => $val) {
                unset($_SESSION[$key]);
            }
        }
               
        $finishRegisteredServices = $serviceManager->getRegisteredServices();
        
        foreach ($finishRegisteredServices['instances'] as $num => $name) {
            if (!array_key_exists($name, $this->startServicesInstances) ) {
                $serviceManager->unregisterService($name);                    
            }
        }
        
        $serviceManager->unsetAbstractFactorys();
        foreach ($this->startAbstractFactorys as $key => $fab) {
            $serviceManager->addAbstractFactory($fab);
        }        
        
		$diactorosResponse = Psr7Response::fromZend($response);
        //var_dump($out);
        return $diactorosResponse;
    }
}
{'fraction_non_alphanumeric': 0.14273910349101426, 'fraction_numerical': 0.0026853955794257384, 'mean_word_length': 2.676157934700076, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 111, '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': '2085146', 'n_tokens_mistral': 2677, 'n_tokens_neox': 2602, 'n_words': 375}