Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
InteractsWithDirectories
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
3 / 3
5
100.00% covered (success)
100.00%
1 / 1
 needsDirectory
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 needsDirectories
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 needsParentDirectory
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5namespace Hyde\Framework\Concerns;
6
7use Hyde\Facades\Filesystem;
8
9use function dirname;
10
11trait InteractsWithDirectories
12{
13    /**
14     * Ensure the supplied directory exist by creating it if it does not.
15     *
16     * @param  string  $directory  relative file path to the directory
17     */
18    protected static function needsDirectory(string $directory): void
19    {
20        if (! Filesystem::exists($directory)) {
21            Filesystem::makeDirectory($directory, recursive: true);
22        }
23    }
24
25    /**
26     * Ensure the supplied directories exist by creating them if they don't.
27     *
28     * @param  array<string>  $directories  array with relative file paths to the directories
29     */
30    protected static function needsDirectories(array $directories): void
31    {
32        foreach ($directories as $directory) {
33            static::needsDirectory($directory);
34        }
35    }
36
37    /**
38     * Ensure the supplied file's parent directory exists by creating it if it does not.
39     */
40    protected static function needsParentDirectory(string $file, int $levels = 1): void
41    {
42        static::needsDirectory(dirname($file, $levels));
43    }
44}