Passed
Push — master ( fd4a88...7449e0 )
by Caen
04:01 queued 12s
created

DataCollections::yaml()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hyde\Support;
6
7
use Hyde\Facades\Filesystem;
8
use Hyde\Framework\Actions\MarkdownFileParser;
9
use Hyde\Framework\Concerns\InteractsWithDirectories;
10
use Illuminate\Support\Collection;
11
use Illuminate\Support\Str;
12
use function json_decode;
13
use function unslash;
14
15
/**
16
 * Automatically generates Laravel Collections from static data files,
17
 * such as Markdown components and YAML files using Hyde Autodiscovery.
18
 *
19
 * This class acts both as a base collection class, a factory for
20
 * creating collections, and static facade shorthand helper methods.
21
 *
22
 * The static "facade" methods are what makes this class special,
23
 * they allow you to quickly access the data collections.
24
 *
25
 * To use them retrieve a collection, call a facade method with the
26
 * name of the data collection subdirectory directory.
27
 *
28
 * All collections are indexed by their filename, which is relative
29
 * to the configured data collection source directory.
30
 */
31
class DataCollections extends Collection
32
{
33
    use InteractsWithDirectories;
34
35
    /**
36
     * The base directory for all data collections. Can be modified using a service provider.
37
     */
38
    public static string $sourceDirectory = 'resources/collections';
39
40
    /**
41
     * Get a collection of Markdown documents in the resources/collections/<$key> directory.
42
     *
43
     * Each Markdown file will be parsed into a MarkdownDocument with front matter.
44
     *
45
     * @return DataCollections<string, \Hyde\Markdown\Models\MarkdownDocument>
46
     */
47
    public static function markdown(string $name): static
48
    {
49
        static::needsDirectory(static::$sourceDirectory);
50
51
        return new static(DataCollections::findFiles($name, 'md')->mapWithKeys(function (string $file): array {
52
            return [static::makeIdentifier($file) => (new MarkdownFileParser($file))->get()];
53
        }));
54
    }
55
56
    /**
57
     * Get a collection of YAML documents in the resources/collections/<$key> directory.
58
     *
59
     * Each YAML file will be parsed into a FrontMatter object.
60
     *
61
     * @return DataCollections<string, \Hyde\Markdown\Models\FrontMatter>
62
     */
63
    public static function yaml(string $name): static
64
    {
65
        static::needsDirectory(static::$sourceDirectory);
66
67
        return new static(DataCollections::findFiles($name, ['yaml', 'yml'])->mapWithKeys(function (string $file): array {
68
            return [static::makeIdentifier($file) => (new MarkdownFileParser($file))->get()->matter()];
69
        }));
70
    }
71
72
    /**
73
     * Get a collection of JSON documents in the resources/collections/<$key> directory.
74
     *
75
     * Each JSON file will be parsed into a stdClass object, or an associative array, depending on the second parameter.
76
     *
77
     * @return DataCollections<string, \stdClass|array>
78
     */
79
    public static function json(string $name, bool $asArray = false): static
80
    {
81
        static::needsDirectory(static::$sourceDirectory);
82
83
        return new static(DataCollections::findFiles($name, 'json')->mapWithKeys(function (string $file) use ($asArray): array {
84
            return [static::makeIdentifier($file) => json_decode(Filesystem::get($file), $asArray)];
85
        }));
86
    }
87
88
    protected static function findFiles(string $name, array|string $extensions): Collection
89
    {
90
        return Filesystem::smartGlob(sprintf('%s/%s/*.{%s}',
91
            static::$sourceDirectory, $name, implode(',', (array) $extensions)
92
        ), GLOB_BRACE);
93
    }
94
95
    protected static function makeIdentifier(string $path): string
96
    {
97
        return unslash(Str::after($path, static::$sourceDirectory));
98
    }
99
}
100