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\Core; |
13
|
|
|
|
14
|
|
|
use Symplify\PHP7_Sculpin\Core\Converter\ConverterManager; |
15
|
|
|
use Symplify\PHP7_Sculpin\Core\Event\SculpinEvents; |
16
|
|
|
use Symplify\PHP7_Sculpin\Core\Event\SourceSetEvent; |
17
|
|
|
use Symplify\PHP7_Sculpin\Core\Formatter\FormatterManager; |
18
|
|
|
use Symplify\PHP7_Sculpin\Core\Generator\GeneratorManager; |
19
|
|
|
use Symplify\PHP7_Sculpin\Core\Io\ConsoleIo; |
20
|
|
|
use Symplify\PHP7_Sculpin\Core\Output\SourceOutput; |
21
|
|
|
use Symplify\PHP7_Sculpin\Core\Output\FilesystemWriter; |
22
|
|
|
use Symplify\PHP7_Sculpin\Core\Permalink\SourcePermalinkFactory; |
23
|
|
|
use Symplify\PHP7_Sculpin\Core\Source\DataSourceInterface; |
24
|
|
|
use Symplify\PHP7_Sculpin\Core\Source\SourceSet; |
25
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
26
|
|
|
|
27
|
|
|
final class Sculpin |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var EventDispatcherInterface |
31
|
|
|
*/ |
32
|
|
|
private $eventDispatcher; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var SourcePermalinkFactory |
36
|
|
|
*/ |
37
|
|
|
private $permalinkFactory; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var FilesystemWriter |
41
|
|
|
*/ |
42
|
|
|
private $writer; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var GeneratorManager |
46
|
|
|
*/ |
47
|
|
|
private $generatorManager; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var FormatterManager |
51
|
|
|
*/ |
52
|
|
|
private $formatterManager; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var ConverterManager |
56
|
|
|
*/ |
57
|
|
|
private $converterManager; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @var ConsoleIo |
61
|
|
|
*/ |
62
|
|
|
private $consoleIo; |
63
|
|
|
|
64
|
|
|
public function __construct( |
65
|
|
|
EventDispatcherInterface $eventDispatcher, |
66
|
|
|
SourcePermalinkFactory $permalinkFactory, |
67
|
|
|
FilesystemWriter $writer, |
68
|
|
|
GeneratorManager $generatorManager, |
69
|
|
|
FormatterManager $formatterManager, |
70
|
|
|
ConverterManager $converterManager, |
71
|
|
|
ConsoleIo $consoleIo |
72
|
|
|
) { |
73
|
|
|
$this->eventDispatcher = $eventDispatcher; |
74
|
|
|
$this->permalinkFactory = $permalinkFactory; |
75
|
|
|
$this->writer = $writer; |
76
|
|
|
$this->generatorManager = $generatorManager; |
77
|
|
|
$this->formatterManager = $formatterManager; |
78
|
|
|
$this->converterManager = $converterManager; |
79
|
|
|
$this->consoleIo = $consoleIo; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function run(DataSourceInterface $dataSource, SourceSet $sourceSet) |
83
|
|
|
{ |
84
|
|
|
$found = false; |
85
|
|
|
|
86
|
|
|
$dataSource->refresh($sourceSet); |
87
|
|
|
|
88
|
|
|
$this->eventDispatcher->dispatch(SculpinEvents::EVENT_BEFORE_RUN, new SourceSetEvent($sourceSet)); |
89
|
|
|
|
90
|
|
|
if ($updatedSources = array_filter($sourceSet->updatedSources(), function ($source) { |
91
|
|
|
return !$source->isGenerated(); |
92
|
|
|
})) { |
93
|
|
|
if (!$found) { |
94
|
|
|
$this->consoleIo->write('Detected new or updated files'); |
95
|
|
|
$found = true; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
foreach ($updatedSources as $source) { |
99
|
|
|
$this->generatorManager->generate($source, $sourceSet); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
foreach ($sourceSet->updatedSources() as $source) { |
104
|
|
|
$permalink = $this->permalinkFactory->create($source); |
105
|
|
|
$source->setPermalink($permalink); |
106
|
|
|
$source->data()->set('url', $permalink->relativeUrlPath()); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
View Code Duplication |
if ($updatedSources = $sourceSet->updatedSources()) { |
|
|
|
|
110
|
|
|
if (!$found) { |
111
|
|
|
$this->consoleIo->write('Detected new or updated files'); |
112
|
|
|
$found = true; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
foreach ($updatedSources as $source) { |
116
|
|
|
$this->converterManager->convertSource($source); |
117
|
|
|
|
118
|
|
|
if ($source->canBeFormatted()) { |
119
|
|
|
$source->data()->set('blocks', $this->formatterManager->formatSourceBlocks($source)); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
|
View Code Duplication |
if ($updatedSources = $sourceSet->updatedSources()) { |
|
|
|
|
125
|
|
|
if (!$found) { |
126
|
|
|
$this->consoleIo->write('Detected new or updated files'); |
127
|
|
|
$found = true; |
|
|
|
|
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
foreach ($updatedSources as $source) { |
131
|
|
|
if ($source->canBeFormatted()) { |
132
|
|
|
$source->setFormattedContent($this->formatterManager->formatSourcePage($source)); |
133
|
|
|
} else { |
134
|
|
|
$source->setFormattedContent($source->content()); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
foreach ($sourceSet->updatedSources() as $source) { |
140
|
|
|
if ($source->isGenerator() || $source->shouldBeSkipped()) { |
141
|
|
|
continue; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
$this->writer->write(new SourceOutput($source)); |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.