Issues (1)

src/AssetManager.php (1 issue)

1
<?php
2
3
namespace Del\Booty;
4
5
class AssetManager
6
{
7
    /** @var string[] $assetFolders */
8
    private $assetFolders = [];
9
10
    /** @var string $destinationFolder */
11
    private $destinationFolder = '';
12
13
    /** @var array $deployInfo */
14
    private $deployInfo = [];
0 ignored issues
show
The private property $deployInfo is not used, and could be removed.
Loading history...
15
16
    /**
17
     * @return string
18
     */
19 2
    public function addAssetsFolder(string $key, string $dir): void
20
    {
21 2
        $dir = realpath($dir);
22
23 2
        if (is_dir($dir)) {
24 1
            $this->assetFolders[$key] = $dir;
25
        }
26
    }
27
28
    /**
29
     * @return string
30
     */
31 2
    public function setDestinationFolder(string $dir): void
32
    {
33 2
        $dir = realpath($dir);
34
35 2
        if (is_dir($dir)) {
36 1
            $this->destinationFolder = $dir;
37
        }
38
    }
39
40
    /**
41
     * @return bool
42
     */
43 2
    public function deployAssets(): bool
44
    {
45 2
        foreach ($this->assetFolders as $key => $dir) {
46 1
            $key = $this->camelCaseToDash($key);
47 1
            $linkFolder = $this->destinationFolder . '/' . $key;
48
49 1
            if (!file_exists($linkFolder)) {
50 1
                symlink($dir, $linkFolder);
51
            }
52
        }
53
54 2
        return true;
55
    }
56
57
    /**
58
     * @param string $key
59
     * @return string
60
     */
61 1
    private function camelCaseToDash(string $key): string
62
    {
63 1
        $newKey = '';
64
65 1
        foreach (str_split($key) as $index => $letter) {
66 1
            if (ctype_upper($letter)) {
67 1
                $letter = strtolower($letter);
68 1
                $letter = $index < 1 ? $letter : '-' . $letter;
69
            }
70 1
            $newKey .= $letter;
71
        }
72
73 1
        return $newKey;
74
    }
75
}