<commit_before><?php
/**
 * spiral
 *
 * @author    Wolfy-J
 */

namespace Spiral\Session;

use Spiral\Core\Component;
use Spiral\Core\Container\SingletonInterface;
use Spiral\Core\FactoryInterface;
use Spiral\Session\Configs\SessionConfig;
use Spiral\Session\Exceptions\MultipleSessionException;
use Spiral\Session\Exceptions\SessionException;

/**
 * Initiates session instance and configures session handlers.
 */
class SessionFactory extends Component implements SingletonInterface
{
    /**
     * @var \Spiral\Session\Configs\SessionConfig
     */
    private $config;

    /**
     * @var \Spiral\Core\FactoryInterface
     */
    private $factory;

    /**
     * @param \Spiral\Session\Configs\SessionConfig $config
     * @param \Spiral\Core\FactoryInterface         $factory
     */
    public function __construct(SessionConfig $config, FactoryInterface $factory)
    {
        $this->config = $config;
        $this->factory = $factory;
    }

    /**
     * @param string $clientSignature User specific token, does not provide full security but
     *                                hardens session transfer.
     * @param string $id              When null - expect php to create session automatically.
     *
     * @return \Spiral\Session\SessionInterface
     *
     * @throws \Spiral\Session\Exceptions\MultipleSessionException
     */
    public function initSession(string $clientSignature, string $id = null): SessionInterface
    {
        if (session_status() == PHP_SESSION_ACTIVE) {
            throw new MultipleSessionException("Unable to initiate session, session already started");
        }

        //No automatic cookies
        ini_set('session.use_cookies', false);

        //Initiating proper session handler
        if ($this->config->sessionHandler() !== null) {
            try {
                $handler = $this->initHandler($this->config->sessionHandler());
            } catch (\Throwable $e) {
                throw new SessionException(
                    "Unable to init session handler {$this->config->sessionHandler()}",
                    $e->getCode(),
                    $e
                );
            }

            session_set_save_handler($handler, true);
        }

        return $this->factory->make(
            SessionInterface::class,
            [
                'clientSignature' => $clientSignature,
                'lifetime'        => $this->config->sessionLifetime(),
                'id'              => $id
            ]
        );
    }

    /**
     * @param string $handler
     *
     * @return mixed|null|object
     */
    protected function initHandler(string $handler)
    {
        //Init handler
        return $this->factory->make(
            $this->config->handlerClass($handler),
            $this->config->handlerOptions($handler)
        );
    }
}<commit_msg>session lifetime bug<commit_after><?php
/**
 * spiral
 *
 * @author    Wolfy-J
 */

namespace Spiral\Session;

use Spiral\Core\Component;
use Spiral\Core\Container\SingletonInterface;
use Spiral\Core\FactoryInterface;
use Spiral\Session\Configs\SessionConfig;
use Spiral\Session\Exceptions\MultipleSessionException;
use Spiral\Session\Exceptions\SessionException;

/**
 * Initiates session instance and configures session handlers.
 */
class SessionFactory extends Component implements SingletonInterface
{
    /**
     * @var \Spiral\Session\Configs\SessionConfig
     */
    private $config;

    /**
     * @var \Spiral\Core\FactoryInterface
     */
    private $factory;

    /**
     * @param \Spiral\Session\Configs\SessionConfig $config
     * @param \Spiral\Core\FactoryInterface         $factory
     */
    public function __construct(SessionConfig $config, FactoryInterface $factory)
    {
        $this->config = $config;
        $this->factory = $factory;
    }

    /**
     * @param string $clientSignature User specific token, does not provide full security but
     *                                hardens session transfer.
     * @param string $id              When null - expect php to create session automatically.
     *
     * @return \Spiral\Session\SessionInterface
     *
     * @throws \Spiral\Session\Exceptions\MultipleSessionException
     */
    public function initSession(string $clientSignature, string $id = null): SessionInterface
    {
        if (session_status() == PHP_SESSION_ACTIVE) {
            throw new MultipleSessionException("Unable to initiate session, session already started");
        }

        //No automatic cookies
        ini_set('session.use_cookies', false);

        //Initiating proper session handler
        if (!empty($this->config->sessionHandler())) {
            try {
                $handler = $this->initHandler($this->config->sessionHandler());
            } catch (\Throwable $e) {
                throw new SessionException(
                    "Unable to init session handler {$this->config->sessionHandler()}",
                    $e->getCode(),
                    $e
                );
            }

            session_set_save_handler($handler, true);
        }

        return $this->factory->make(
            SessionInterface::class,
            [
                'clientSignature' => $clientSignature,
                'lifetime'        => $this->config->sessionLifetime(),
                'id'              => $id
            ]
        );
    }

    /**
     * @param string $handler
     *
     * @return mixed|null|object
     */
    protected function initHandler(string $handler)
    {
        //Init handler
        return $this->factory->make(
            $this->config->handlerClass($handler),
            $this->config->handlerOptions($handler)
        );
    }
}
{'fraction_non_alphanumeric': 0.11146161934805468, 'fraction_numerical': 0.0, 'mean_word_length': 2.0147913365029053, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2353284', 'n_tokens_mistral': 1478, 'n_tokens_neox': 1440, 'n_words': 321}