Completed
Branch master (446cf6)
by Guillaume
04:16 queued 01:26
created

DocsetPackager::removePreviousDocsetFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace App\Services;
4
5
use App\Contracts\Docset;
6
use Illuminate\Support\Facades\DB;
7
use Illuminate\Support\Facades\File;
8
use Illuminate\Support\Facades\Config;
9
use Illuminate\Support\Facades\Artisan;
10
use Illuminate\Support\Facades\Storage;
11
12
final class DocsetPackager
13
{
14
    protected $docset;
15
16
17
    public function __construct(Docset $docset)
18
    {
19
        $this->docset = $docset;
20
    }
21
22
    public function removePreviousDocsetFile()
23
    {
24
        Storage::deleteDirectory(
25
            $this->docset->file()
26
        );
27
    }
28
29
    public function createDocsetFile()
30
    {
31
        Storage::makeDirectory(
32
            $this->docset->innerDirectory()
33
        );
34
    }
35
36
    public function copyDocFiles()
37
    {
38
        File::copyDirectory(
39
            "storage/{$this->docset->downloadedDirectory()}",
40
            "storage/{$this->docset->innerDirectory()}"
41
        );
42
    }
43
44
    public function createInfoPlist()
45
    {
46
        $infoPlist = <<<EOT
47
            <?xml version="1.0" encoding="UTF-8"?>
48
            <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
49
            <plist version="1.0">
50
            <dict>
51
                <key>CFBundleIdentifier</key>
52
                <string>{$this->docset->code()}</string>
53
                <key>CFBundleName</key>
54
                <string>{$this->docset->name()}</string>
55
                <key>DocSetPlatformFamily</key>
56
                <string>{$this->docset->code()}</string>
57
                <key>dashIndexFilePath</key>
58
                <string>{$this->docset->index()}</string>
59
                <key>DashDocSetFallbackURL</key>
60
                <string>{$this->docset->url()}</string>
61
                <key>DashDocSetPlayURL</key>
62
                <string>{$this->docset->playground()}</string>
63
                <key>isJavaScriptEnabled</key>
64
                <true/>
65
                <key>isDashDocset</key>
66
                <true/>
67
                <key>DashDocSetFamily</key>
68
                <string>dashtoc</string>
69
            </dict>
70
            </plist>
71
            EOT;
72
73
        Storage::put(
74
            $this->docset->infoPlistFile(),
75
            $infoPlist
76
        );
77
    }
78
79
    public function createAndPopulateSQLiteIndex()
80
    {
81
        Config::set(
82
            'database.connections.sqlite.database',
83
            "storage/{$this->docset->databaseFile()}"
84
        );
85
86
        $this->createSQLiteIndex();
87
88
        $this->populateSQLiteIndex();
89
    }
90
91
    protected function createSQLiteIndex()
92
    {
93
        Storage::put(
94
            $this->docset->databaseFile(),
95
            null
96
        );
97
98
        Artisan::call('migrate');
99
    }
100
101
    protected function populateSQLiteIndex()
102
    {
103
        $entries = $this->docsetEntries();
104
105
        $entries->each(static function ($entry) {
106
            DB::table('searchIndex')->insert([
107
                'name' => $entry['name'],
108
                'type' => $entry['type'],
109
                'path' => $entry['path'],
110
            ]);
111
        });
112
    }
113
114
    protected function docsetEntries()
115
    {
116
        $files = $this->docset->htmlFiles();
117
118
        $entries = collect();
119
120
        $files->each(function ($file) use (&$entries) {
121
            $entries = $entries
122
                ->merge($this->docset->entries($file))
123
                ->unique('name');
124
        });
125
126
        return $entries;
127
    }
128
129
    public function formatDocFiles()
130
    {
131
        $files = $this->docset->htmlFiles();
132
133
        $files->each(function ($file) {
134
            $formattedContent = $this->docset->format(Storage::get($file));
135
            Storage::put($file, $formattedContent);
136
        });
137
    }
138
139
    public function copyIcons()
140
    {
141
        if ($this->docset->icon16()) {
142
            Storage::copy(
143
                "{$this->docset->downloadedDirectory()}/{$this->docset->icon16()}",
144
                "{$this->docset->file()}/icon.png"
145
            );
146
        }
147
148
        if ($this->docset->icon32()) {
149
            Storage::copy(
150
                "{$this->docset->downloadedDirectory()}/{$this->docset->icon32()}",
151
                "{$this->docset->file()}/[email protected]"
152
            );
153
        }
154
    }
155
}
156