-
Notifications
You must be signed in to change notification settings - Fork 1
/
ProxySourceTaxonomyIndexGenerator.php
115 lines (93 loc) · 3.86 KB
/
ProxySourceTaxonomyIndexGenerator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
/*
* This file is a part of Sculpin.
*
* (c) Dragonfly Development Inc.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sculpin\Contrib\Taxonomy;
use Sculpin\Core\DataProvider\DataProviderManager;
use Sculpin\Core\Generator\GeneratorInterface;
use Sculpin\Core\Source\SourceInterface;
class ProxySourceTaxonomyIndexGenerator implements GeneratorInterface
{
private $dataProviderManager;
private $dataProviderName;
private $injectedTaxonKey;
private $injectedTaxonItemsKey;
private $permalinkStrategyCollection;
public function __construct(
DataProviderManager $dataProviderManager,
$dataProviderName,
$injectedTaxonKey,
$injectedTaxonItemsKey,
PermalinkStrategyCollection $permalinkStrategyCollection
) {
$this->dataProviderManager = $dataProviderManager;
$this->dataProviderName = $dataProviderName; // post_tags
$this->injectedTaxonKey = $injectedTaxonKey; // tag
$this->injectedTaxonItemsKey = $injectedTaxonItemsKey; // tagged_posts
$this->permalinkStrategyCollection = $permalinkStrategyCollection;
}
public function generate(SourceInterface $source)
{
$dataProvider = $this->dataProviderManager->dataProvider($this->dataProviderName);
$taxons = $dataProvider->provideData();
$generatedSources = array();
foreach ($taxons as $taxon => $items) {
$generatedSource = $source->duplicate(
$source->sourceId().':'.$this->injectedTaxonKey.'='.$taxon
);
$permalink = $source->data()->get('permalink') ?: $source->relativePathname();
$basename = basename($permalink);
$permalink = dirname($permalink);
$indexType = null;
if (preg_match('/^(.+?)\.(.+)$/', $basename, $matches)) {
$urlTaxon = $this->permalinkStrategyCollection->process($taxon);
$indexType = $matches[2];
$suffix = in_array($indexType, array('xml', 'rss', 'json')) ? '.'.$indexType : '/';
$permalink = $permalink.'/'.$urlTaxon.$suffix;
} else {
// not sure what case this is?
}
if (0 === strpos($permalink, './')) {
$permalink = substr($permalink, 2);
}
if (0 !== strpos($permalink, '/')) {
$permalink = '/'.$permalink;
}
if ($permalink) {
// not sure if this is ever going to happen?
$generatedSource->data()->set('permalink', $permalink);
}
$generatedSource->data()->set($this->injectedTaxonKey, $taxon);
$generatedSource->data()->set($this->injectedTaxonItemsKey, $items);
if ($indexType) {
foreach ($items as $item) {
$key = $this->injectedTaxonKey.'_'.$indexType.'_index_permalinks';
$taxonIndexPermalinks = $item->data()->get($key) ?: array();
$taxonIndexPermalinks[$taxon] = $permalink;
$item->data()->set($key, $taxonIndexPermalinks);
}
}
//
// TODO: REMOVE THIS
//
// This is needed for BC purposes. This should be removed
// eventually and existing markup should be updated.
//
switch ($this->injectedTaxonItemsKey) {
case 'tag_posts':
$generatedSource->data()->set('tagged_posts', $items);
break;
case 'category_posts':
$generatedSource->data()->set('categoried_posts', $items);
break;
}
$generatedSources[] = $generatedSource;
}
return $generatedSources;
}
}