<commit_before><?php

namespace blink\session;

use blink\core\InvalidConfigException;
use blink\core\Object;
use blink\session\Contract as SessionContract;

/**
 * Class FileStorage
 *
 * @package blink\session
 */
class FileStorage extends Object implements StorageContract
{
    public $path;
    public $divisor = 1000;

    protected $timeout;

    public function init()
    {
        if (!$this->path || !file_exists($this->path) || !is_writable($this->path)) {
            throw new InvalidConfigException("The param: '{$this->path}' is invalid or not writable");
        }

        if (rand(0, $this->divisor) <= 0) {
            $this->gc();
        }
    }

    /**
     * @inheritDoc
     */
    public function read($id)
    {
        if (file_exists($this->path . '/' . $id)) {
            return unserialize(file_get_contents($this->path . '/' . $id));
        }
    }

    /**
     * @inheritDoc
     */
    public function write($id, array $data)
    {
        return file_put_contents($this->path . '/' . $id, serialize($data)) !== false;
    }

    /**
     * @inheritDoc
     */
    public function destroy($id)
    {
        if (file_exists($this->path . '/' . $id)) {
            return unlink($this->path . '/' . $id);
        } else {
            return false;
        }
    }

    /**
     * @inheritDoc
     */
    public function timeout($timeout)
    {
        $this->timeout = $timeout;
    }

    protected function gc()
    {
        $iterator = new \DirectoryIterator($this->path);
        $now = time();

        foreach ($iterator as $file) {
            if ($file->isDot()) {
                continue;
            }
            if ($file->getMTime() < $now - $this->timeout) {
                unlink($file->getRealPath());
            }
        }
    }
}<commit_msg>Added automatically session directory creation support, close #4<commit_after><?php

namespace blink\session;

use blink\core\InvalidConfigException;
use blink\core\Object;
use blink\session\Contract as SessionContract;

/**
 * Class FileStorage
 *
 * @package blink\session
 */
class FileStorage extends Object implements StorageContract
{
    public $path;
    public $divisor = 1000;

    protected $timeout;

    public function init()
    {
        if (!$this->path) {
            throw new InvalidConfigException("The parameter 'blink\\session\\FileStorage::path' must be configured");
        }

        if (!file_exists($this->path)) {
            @mkdir($this->path, 0777, true);
        }

        if (!file_exists($this->path) || !is_writable($this->path)) {
            throw new InvalidConfigException("The parameter 'blink\\session\\FileStorage::path': '{$this->path}' is invalid or not writable");
        }

        if (rand(0, $this->divisor) <= 0) {
            $this->gc();
        }
    }

    /**
     * @inheritDoc
     */
    public function read($id)
    {
        if (file_exists($this->path . '/' . $id)) {
            return unserialize(file_get_contents($this->path . '/' . $id));
        }
    }

    /**
     * @inheritDoc
     */
    public function write($id, array $data)
    {
        return file_put_contents($this->path . '/' . $id, serialize($data)) !== false;
    }

    /**
     * @inheritDoc
     */
    public function destroy($id)
    {
        if (file_exists($this->path . '/' . $id)) {
            return unlink($this->path . '/' . $id);
        } else {
            return false;
        }
    }

    /**
     * @inheritDoc
     */
    public function timeout($timeout)
    {
        $this->timeout = $timeout;
    }

    protected function gc()
    {
        $iterator = new \DirectoryIterator($this->path);
        $now = time();

        foreach ($iterator as $file) {
            if ($file->isDot()) {
                continue;
            }
            if ($file->getMTime() < $now - $this->timeout) {
                @unlink($file->getRealPath());
            }
        }
    }
}
{'fraction_non_alphanumeric': 0.1564089521871821, 'fraction_numerical': 0.0043234994913530014, 'mean_word_length': 1.8254310344827587, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 39, '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': '6690401', 'n_tokens_mistral': 1185, 'n_tokens_neox': 1145, 'n_words': 242}