1
|
|
|
<?php |
2
|
|
|
namespace library\components { |
3
|
|
|
|
4
|
|
|
use library\storage\Storage; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Class DocumentComponent |
8
|
|
|
* |
9
|
|
|
* Has optional parameter `folder` to prefix the relative url with a folder |
10
|
|
|
* Has optional parameter `document` to select a given document |
11
|
|
|
* Has optional parameter `documentParameterName` to select the parametername to be used |
12
|
|
|
* to set the found document to. |
13
|
|
|
* |
14
|
|
|
* @package library\components |
15
|
|
|
*/ |
16
|
|
|
class DocumentComponent extends BaseComponent |
17
|
|
|
{ |
18
|
|
|
protected $documentParameterName = 'document'; |
19
|
|
|
|
20
|
|
|
public function run(Storage $storage) |
21
|
|
|
{ |
22
|
|
|
parent::run($storage); |
23
|
|
|
|
24
|
|
|
if (isset($this->parameters['documentParameterName'])) { |
25
|
|
|
$this->documentParameterName = $this->parameters['documentParameterName']; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
if ($this->matchedSitemapItem->regex == false) { |
29
|
|
|
if (isset($this->parameters['document'])) { |
30
|
|
|
$this->parameters[$this->documentParameterName] = $storage->getDocumentBySlug($this->parameters['document']); |
31
|
|
|
} else { |
32
|
|
|
throw new \Exception('When not using a regex, you need to set the parameter `document` with the path to the document in this sitemap item: ' . $this->matchedSitemapItem->title); |
33
|
|
|
} |
34
|
|
|
} else { |
35
|
|
|
if (isset($this->parameters['document'])) { |
36
|
|
|
$this->parameters[$this->documentParameterName] = $storage->getDocumentBySlug($this->parameters['document']); |
37
|
|
|
} else { |
38
|
|
|
$relativeDocumentUri = current($this->matchedSitemapItem->matches[1]); |
39
|
|
|
if (isset($this->parameters['folder'])) { |
40
|
|
|
if (substr($this->parameters['folder'], -1) !== '/') { |
41
|
|
|
$this->parameters['folder'] = $this->parameters['folder'] . '/'; |
42
|
|
|
} |
43
|
|
|
$relativeDocumentUri = $this->parameters['folder'] . $relativeDocumentUri; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$document = $storage->getDocumentBySlug($relativeDocumentUri); |
47
|
|
|
|
48
|
|
|
if ($document->type == 'folder') { |
49
|
|
|
throw new \Exception('The found document is a folder.'); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
if ($document->state != 'published') { |
53
|
|
|
throw new \Exception('Found document is unpublished.'); |
54
|
|
|
} |
55
|
|
|
$this->parameters[$this->documentParameterName] = $document; |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
} |