<commit_before><?php namespace TightenCo\Jigsaw;

class CollectionDataLoader
{
    private $settings;
    private $filesystem;
    private $outputPathResolver;
    private $handlers;

    public function __construct($settings, $filesystem, $outputPathResolver, $handlers = [])
    {
        $this->settings = collect($settings);
        $this->filesystem = $filesystem;
        $this->outputPathResolver = $outputPathResolver;
        $this->handlers = collect($handlers);
    }

    public function load($source)
    {
        return $this->settings->map(function ($collectionSettings, $name) use ($source) {
            // Merge in any default collection settings
            $collectionSettings = array_merge([
                'helpers' => []
            ], $collectionSettings);

            return $this->loadSingleCollectionData($source, $name, $collectionSettings);
        })->all();
    }

    private function loadSingleCollectionData($source, $collectionName, $settings)
    {
        return collect($this->filesystem->allFiles("{$source}/_{$collectionName}"))->map(function ($file) use ($settings) {
            return $this->buildCollectionItem($file, $settings);
        })->all();
    }

    private function buildCollectionItem($file, $settings)
    {
        $handler = $this->handlers->first(function ($_, $handler) use ($file) {
            return $handler->shouldHandle($file);
        }, function () { throw new Exception('No matching collection item handler'); });

        $data = $handler->getData($file);
        $link = $this->getCollectionItemLink($data, $settings);

        return new CollectionItem(array_merge($data, ['link' => $link]), $settings['helpers']);
    }

    private function getCollectionItemLink($data, $settings)
    {
        $permalink = $settings['permalink']->__invoke($data);

        return $this->outputPathResolver->link(dirname($permalink), basename($permalink), 'html');
    }
}<commit_msg>Automatically slugify permalinks<commit_after><?php namespace TightenCo\Jigsaw;

class CollectionDataLoader
{
    private $settings;
    private $filesystem;
    private $outputPathResolver;
    private $handlers;

    public function __construct($settings, $filesystem, $outputPathResolver, $handlers = [])
    {
        $this->settings = collect($settings);
        $this->filesystem = $filesystem;
        $this->outputPathResolver = $outputPathResolver;
        $this->handlers = collect($handlers);
    }

    public function load($source)
    {
        return $this->settings->map(function ($collectionSettings, $name) use ($source) {
            // Merge in any default collection settings
            $collectionSettings = array_merge([
                'helpers' => []
            ], $collectionSettings);

            return $this->loadSingleCollectionData($source, $name, $collectionSettings);
        })->all();
    }

    private function loadSingleCollectionData($source, $collectionName, $settings)
    {
        return collect($this->filesystem->allFiles("{$source}/_{$collectionName}"))->map(function ($file) use ($settings) {
            return $this->buildCollectionItem($file, $settings);
        })->all();
    }

    private function buildCollectionItem($file, $settings)
    {
        $handler = $this->handlers->first(function ($_, $handler) use ($file) {
            return $handler->shouldHandle($file);
        }, function () { throw new Exception('No matching collection item handler'); });

        $data = $handler->getData($file);
        $link = $this->getCollectionItemLink($data, $settings);

        return new CollectionItem(array_merge($data, ['link' => $link]), $settings['helpers']);
    }

    private function getCollectionItemLink($data, $settings)
    {
        $permalink = slugify($settings['permalink']->__invoke($data));

        return $this->outputPathResolver->link(dirname($permalink), basename($permalink), 'html');
    }
}
{'fraction_non_alphanumeric': 0.14795918367346939, 'fraction_numerical': 0.0, 'mean_word_length': 3.257328990228013, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5873130', 'n_tokens_mistral': 1045, 'n_tokens_neox': 1029, 'n_words': 212}