1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is a part of Sculpin. |
5
|
|
|
* |
6
|
|
|
* (c) Dragonfly Development Inc. |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Symplify\PHP7_Sculpin\PostsBundle\Taxonomy; |
13
|
|
|
|
14
|
|
|
use Symplify\PHP7_Sculpin\DataProvider\DataProviderManager; |
15
|
|
|
use Symplify\PHP7_Sculpin\Generator\GeneratorInterface; |
16
|
|
|
use Symplify\PHP7_Sculpin\Source\SourceInterface; |
17
|
|
|
|
18
|
|
|
final class ProxySourceTaxonomyIndexGenerator implements GeneratorInterface |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var DataProviderManager |
22
|
|
|
*/ |
23
|
|
|
private $dataProviderManager; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
private $dataProviderName; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
private $injectedTaxonKey; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
private $injectedTaxonItemsKey; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var PermalinkStrategyCollection |
42
|
|
|
*/ |
43
|
|
|
private $permalinkStrategyCollection; |
44
|
|
|
|
45
|
|
|
public function __construct( |
46
|
|
|
DataProviderManager $dataProviderManager, |
47
|
|
|
string $dataProviderName, |
48
|
|
|
string $injectedTaxonKey, |
49
|
|
|
string $injectedTaxonItemsKey, |
50
|
|
|
PermalinkStrategyCollection $permalinkStrategyCollection |
51
|
|
|
) { |
52
|
|
|
$this->dataProviderManager = $dataProviderManager; |
53
|
|
|
$this->dataProviderName = $dataProviderName; // post_tags |
54
|
|
|
$this->injectedTaxonKey = $injectedTaxonKey; // tag |
55
|
|
|
$this->injectedTaxonItemsKey = $injectedTaxonItemsKey; // tagged_posts |
56
|
|
|
$this->permalinkStrategyCollection = $permalinkStrategyCollection; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function generate(SourceInterface $source) : array |
60
|
|
|
{ |
61
|
|
|
$dataProvider = $this->dataProviderManager->dataProvider($this->dataProviderName); |
62
|
|
|
$taxons = $dataProvider->provideData(); |
63
|
|
|
|
64
|
|
|
$generatedSources = []; |
65
|
|
|
foreach ($taxons as $taxon => $items) { |
66
|
|
|
$generatedSource = $source->duplicate( |
67
|
|
|
$source->sourceId().':'.$this->injectedTaxonKey.'='.$taxon |
68
|
|
|
); |
69
|
|
|
|
70
|
|
|
$permalink = $source->data()->get('permalink') ?: $source->relativePathname(); |
71
|
|
|
$basename = basename($permalink); |
72
|
|
|
|
73
|
|
|
$permalink = dirname($permalink); |
74
|
|
|
|
75
|
|
|
$indexType = null; |
76
|
|
|
|
77
|
|
|
if (preg_match('/^(.+?)\.(.+)$/', $basename, $matches)) { |
78
|
|
|
$urlTaxon = $this->permalinkStrategyCollection->process($taxon); |
79
|
|
|
$indexType = $matches[2]; |
80
|
|
|
$suffix = in_array($indexType, ['xml', 'rss', 'json']) ? '.'.$indexType : '/'; |
81
|
|
|
$permalink = $permalink.'/'.$urlTaxon.$suffix; |
82
|
|
|
} else { |
|
|
|
|
83
|
|
|
// not sure what case this is? |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
if (0 === strpos($permalink, './')) { |
87
|
|
|
$permalink = substr($permalink, 2); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
if (0 !== strpos($permalink, '/')) { |
91
|
|
|
$permalink = '/'.$permalink; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
if ($permalink) { |
95
|
|
|
// not sure if this is ever going to happen? |
96
|
|
|
$generatedSource->data()->set('permalink', $permalink); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$generatedSource->data()->set($this->injectedTaxonKey, $taxon); |
100
|
|
|
$generatedSource->data()->set($this->injectedTaxonItemsKey, $items); |
101
|
|
|
|
102
|
|
|
if ($indexType) { |
|
|
|
|
103
|
|
|
foreach ($items as $item) { |
104
|
|
|
$key = $this->injectedTaxonKey.'_'.$indexType.'_index_permalinks'; |
105
|
|
|
$taxonIndexPermalinks = $item->data()->get($key) ?: []; |
106
|
|
|
|
107
|
|
|
$taxonIndexPermalinks[$taxon] = $permalink; |
108
|
|
|
|
109
|
|
|
$item->data()->set($key, $taxonIndexPermalinks); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$generatedSources[] = $generatedSource; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return $generatedSources; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
This check looks for the
else
branches ofif
statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.These
else
branches can be removed.could be turned into
This is much more concise to read.