|
1
|
|
|
<?php |
|
2
|
|
|
namespace Bookdown\Bookdown\Config; |
|
3
|
|
|
|
|
4
|
|
|
use Bookdown\Bookdown\Exception; |
|
5
|
|
|
|
|
6
|
|
|
class IndexConfig |
|
7
|
|
|
{ |
|
8
|
|
|
protected $file; |
|
9
|
|
|
protected $dir; |
|
10
|
|
|
protected $isRemote = false; |
|
11
|
|
|
protected $json; |
|
12
|
|
|
protected $title; |
|
13
|
|
|
protected $content; |
|
14
|
|
|
protected $indexOrigin = ''; |
|
15
|
|
|
protected $tocDepth; |
|
16
|
|
|
|
|
17
|
41 |
|
public function __construct($file, $data) |
|
18
|
|
|
{ |
|
19
|
41 |
|
$this->initFile($file); |
|
20
|
41 |
|
$this->initJson($data); |
|
21
|
40 |
|
$this->init(); |
|
22
|
32 |
|
} |
|
23
|
|
|
|
|
24
|
40 |
|
protected function init() |
|
25
|
|
|
{ |
|
26
|
40 |
|
$this->initDir(); |
|
27
|
40 |
|
$this->initTitle(); |
|
28
|
39 |
|
$this->initContent(); |
|
29
|
33 |
|
$this->initTocDepth(); |
|
30
|
33 |
|
} |
|
31
|
|
|
|
|
32
|
41 |
|
protected function initFile($file) |
|
33
|
|
|
{ |
|
34
|
41 |
|
$this->file = $file; |
|
35
|
41 |
|
$this->isRemote = strpos($file, '://') !== false; |
|
36
|
41 |
|
} |
|
37
|
|
|
|
|
38
|
40 |
|
protected function initDir() |
|
39
|
|
|
{ |
|
40
|
40 |
|
$this->dir = dirname($this->file) . DIRECTORY_SEPARATOR; |
|
41
|
40 |
|
} |
|
42
|
|
|
|
|
43
|
41 |
|
protected function initJson($data) |
|
44
|
|
|
{ |
|
45
|
41 |
|
$this->json = json_decode($data); |
|
46
|
41 |
|
if (! $this->json) { |
|
47
|
1 |
|
throw new Exception("Malformed JSON in '{$this->file}'."); |
|
48
|
|
|
} |
|
49
|
40 |
|
} |
|
50
|
|
|
|
|
51
|
40 |
|
protected function initTitle() |
|
52
|
|
|
{ |
|
53
|
40 |
|
if (empty($this->json->title)) { |
|
54
|
1 |
|
throw new Exception("No title set in '{$this->file}'."); |
|
55
|
|
|
} |
|
56
|
39 |
|
$this->title = $this->json->title; |
|
57
|
39 |
|
} |
|
58
|
|
|
|
|
59
|
39 |
|
protected function initContent() |
|
60
|
|
|
{ |
|
61
|
39 |
|
$content = empty($this->json->content) |
|
62
|
39 |
|
? array() |
|
63
|
39 |
|
: $this->json->content; |
|
64
|
|
|
|
|
65
|
39 |
|
if (! $content) { |
|
66
|
1 |
|
throw new Exception("No content listed in '{$this->file}'."); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
38 |
|
if (! is_array($content)) { |
|
70
|
1 |
|
throw new Exception("Content must be an array in '{$this->file}'."); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
37 |
|
foreach ($content as $key => $val) { |
|
74
|
37 |
|
$this->initContentItem($val); |
|
75
|
35 |
|
} |
|
76
|
33 |
|
} |
|
77
|
|
|
|
|
78
|
37 |
|
protected function initContentItem($origin) |
|
79
|
|
|
{ |
|
80
|
37 |
|
if (is_object($origin)) { |
|
81
|
34 |
|
$spec = (array) $origin; |
|
82
|
34 |
|
$name = key($spec); |
|
83
|
34 |
|
$origin = current($spec); |
|
84
|
34 |
|
return $this->addContent($name, $origin); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
3 |
|
if (! is_string($origin)) { |
|
88
|
1 |
|
throw new Exception("Content origin must be object or string in '{$this->file}'."); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
2 |
|
if (substr($origin, -13) == 'bookdown.json') { |
|
92
|
2 |
|
$name = basename(dirname($origin)); |
|
93
|
2 |
|
return $this->addContent($name, $origin); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
2 |
|
$name = basename($origin); |
|
97
|
2 |
|
$pos = strrpos($name, '.'); |
|
98
|
2 |
|
if ($pos !== false) { |
|
99
|
2 |
|
$name = substr($name, 0, $pos); |
|
100
|
2 |
|
} |
|
101
|
2 |
|
return $this->addContent($name, $origin); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
26 |
|
protected function initTocDepth() |
|
105
|
|
|
{ |
|
106
|
26 |
|
$this->tocDepth = empty($this->json->tocDepth) |
|
107
|
26 |
|
? 0 |
|
108
|
26 |
|
: (int) $this->json->tocDepth; |
|
109
|
26 |
|
} |
|
110
|
|
|
|
|
111
|
36 |
|
protected function addContent($name, $origin) |
|
112
|
|
|
{ |
|
113
|
36 |
|
if ($name == 'index') { |
|
114
|
1 |
|
throw new Exception("Disallowed 'index' content name in '{$this->file}'."); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
35 |
|
if (isset($this->content[$name])) { |
|
118
|
1 |
|
throw new Exception("Content name '{$name}' already set in '{$this->file}'."); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
35 |
|
$this->content[$name] = $this->fixPath($origin); |
|
122
|
35 |
|
} |
|
123
|
|
|
|
|
124
|
35 |
|
protected function fixPath($path) |
|
125
|
|
|
{ |
|
126
|
35 |
|
if (strpos($path, '://') !== false) { |
|
127
|
9 |
|
return $path; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
35 |
|
if ($this->isRemote() && $path{0} === DIRECTORY_SEPARATOR) { |
|
131
|
1 |
|
throw new Exception( |
|
132
|
1 |
|
"Cannot handle absolute content path '{$path}' in remote '{$this->file}'." |
|
133
|
1 |
|
); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
35 |
|
if ($path{0} === DIRECTORY_SEPARATOR) { |
|
137
|
29 |
|
return $path; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
35 |
|
return $this->getDir() . ltrim($path, DIRECTORY_SEPARATOR); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
35 |
|
public function isRemote() |
|
144
|
|
|
{ |
|
145
|
35 |
|
return $this->isRemote; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
5 |
|
public function getFile() |
|
149
|
|
|
{ |
|
150
|
5 |
|
return $this->file; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
35 |
|
public function getDir() |
|
154
|
|
|
{ |
|
155
|
35 |
|
return $this->dir; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
25 |
|
public function getTitle() |
|
159
|
|
|
{ |
|
160
|
25 |
|
return $this->title; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
8 |
|
public function getContent() |
|
164
|
|
|
{ |
|
165
|
8 |
|
return $this->content; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
6 |
|
public function getTocDepth() |
|
169
|
|
|
{ |
|
170
|
6 |
|
return $this->tocDepth; |
|
171
|
|
|
} |
|
172
|
|
|
} |
|
173
|
|
|
|