<commit_before><?php
declare(strict_types = 1);

namespace Innmind\Filesystem\Adapter;

use Innmind\Filesystem\{
    AdapterInterface,
    FileInterface,
    File,
    Directory,
    DirectoryInterface,
    Stream\Stream,
    Exception\FileNotFoundException
};
use Innmind\Immutable\Map;
use Symfony\Component\Filesystem\Filesystem;

class FilesystemAdapter implements AdapterInterface
{
    private $path;
    private $filesystem;
    private $files;

    public function __construct(string $path)
    {
        $this->path = $path;
        $this->filesystem = new Filesystem;
        $this->files = new Map('string', FileInterface::class);
    }

    /**
     * {@inheritdoc}
     */
    public function add(FileInterface $file): AdapterInterface
    {
        $this->createFileAt($this->path, $file);

        return $this;
    }

    /**
     * {@inheritdoc}
     */
    public function get(string $file): FileInterface
    {
        if (!$this->has($file)) {
            throw new FileNotFoundException;
        }

        return $this->open($this->path, $file);
    }

    /**
     * {@inheritdoc}
     */
    public function has(string $file): bool
    {
        return $this->filesystem->exists($this->path . '/' . $file);
    }

    /**
     * {@inheritdoc}
     */
    public function remove(string $file): AdapterInterface
    {
        if (!$this->has($file)) {
            throw new FileNotFoundException;
        }

        $this->filesystem->remove($this->path . '/' . $file);

        return $this;
    }

    /**
     * Create the wished file at the given absolute path
     *
     * @param string $path
     * @param FileInterface $file
     *
     * @return void
     */
    private function createFileAt(string $path, FileInterface $file)
    {
        if ($file instanceof DirectoryInterface) {
            $folder = $path . '/' . (string) $file->name();

            if (
                $this->files->contains($folder) &&
                $this->files->get($folder) === $file
            ) {
                return;
            }

            $this->filesystem->mkdir($folder);

            foreach ($file as $subFile) {
                $this->createFileAt($folder, $subFile);
            }

            $this->files = $this->files->put($folder, $file);

            return;
        }

        $path .= '/' . (string) $file->name();

        if (
            $this->files->contains($path) &&
            $this->files->get($path) === $file
        ) {
            return;
        }

        $this->filesystem->dumpFile(
            $path,
            (string) $file->content()
        );
        $this->files = $this->files->put($path, $file);
    }

    /**
     * Open the file in the given folder
     *
     * @param string $folder
     * @param string $file
     *
     * @return FileInterface
     */<commit_msg>write file content chunk by chunk (to avoid loading whole stream into memory)<commit_after><?php
declare(strict_types = 1);

namespace Innmind\Filesystem\Adapter;

use Innmind\Filesystem\{
    AdapterInterface,
    FileInterface,
    File,
    Directory,
    DirectoryInterface,
    Stream\Stream,
    Exception\FileNotFoundException
};
use Innmind\Immutable\Map;
use Symfony\Component\Filesystem\Filesystem;

class FilesystemAdapter implements AdapterInterface
{
    private $path;
    private $filesystem;
    private $files;

    public function __construct(string $path)
    {
        $this->path = $path;
        $this->filesystem = new Filesystem;
        $this->files = new Map('string', FileInterface::class);
    }

    /**
     * {@inheritdoc}
     */
    public function add(FileInterface $file): AdapterInterface
    {
        $this->createFileAt($this->path, $file);

        return $this;
    }

    /**
     * {@inheritdoc}
     */
    public function get(string $file): FileInterface
    {
        if (!$this->has($file)) {
            throw new FileNotFoundException;
        }

        return $this->open($this->path, $file);
    }

    /**
     * {@inheritdoc}
     */
    public function has(string $file): bool
    {
        return $this->filesystem->exists($this->path . '/' . $file);
    }

    /**
     * {@inheritdoc}
     */
    public function remove(string $file): AdapterInterface
    {
        if (!$this->has($file)) {
            throw new FileNotFoundException;
        }

        $this->filesystem->remove($this->path . '/' . $file);

        return $this;
    }

    /**
     * Create the wished file at the given absolute path
     *
     * @param string $path
     * @param FileInterface $file
     *
     * @return void
     */
    private function createFileAt(string $path, FileInterface $file)
    {
        if ($file instanceof DirectoryInterface) {
            $folder = $path . '/' . (string) $file->name();

            if (
                $this->files->contains($folder) &&
                $this->files->get($folder) === $file
            ) {
                return;
            }

            $this->filesystem->mkdir($folder);

            foreach ($file as $subFile) {
                $this->createFileAt($folder, $subFile);
            }

            $this->files = $this->files->put($folder, $file);

            return;
        }

        $path .= '/' . (string) $file->name();

        if (
            $this->files->contains($path) &&
            $this->files->get($path) === $file
        ) {
            return;
        }

        $stream = $file->content();
        $stream->rewind();
        $handle = fopen($path, 'w');

        while (!$stream->isEof()) {
            fwrite($handle, $stream->read(8192));
        }

        $this->files = $this->files->put($path, $file);
    }

    /**
     * Open the file in the given folder
     *
     * @param string $folder
     * @param string $file
     *
     * @return FileInterface
     */
{'fraction_non_alphanumeric': 0.14601542416452443, 'fraction_numerical': 0.0010282776349614395, 'mean_word_length': 1.8125301204819277, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 78, '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': '1979386', 'n_tokens_mistral': 1704, 'n_tokens_neox': 1651, 'n_words': 350}