-
Notifications
You must be signed in to change notification settings - Fork 1
/
Sculpin.php
242 lines (207 loc) · 7.46 KB
/
Sculpin.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
<?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\Core;
use Dflydev\DotAccessConfiguration\Configuration;
use Sculpin\Core\Converter\ConverterManager;
use Sculpin\Core\Event\SourceSetEvent;
use Sculpin\Core\Formatter\FormatterManager;
use Sculpin\Core\Generator\GeneratorManager;
use Sculpin\Core\Io\IoInterface;
use Sculpin\Core\Io\NullIo;
use Sculpin\Core\Output\SourceOutput;
use Sculpin\Core\Output\WriterInterface;
use Sculpin\Core\Permalink\SourcePermalinkFactoryInterface;
use Sculpin\Core\Source\DataSourceInterface;
use Sculpin\Core\Source\SourceSet;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
/**
* Sculpin.
*
* @author Beau Simensen <beau@dflydev.com>
*/
class Sculpin
{
const GIT_VERSION = '@git_version@';
const EVENT_BEFORE_RUN = 'sculpin.core.before_run';
const EVENT_AFTER_RUN = 'sculpin.core.after_run';
const EVENT_BEFORE_CONVERT = 'sculpin.core.before_convert';
const EVENT_AFTER_CONVERT = 'sculpin.core.after_convert';
const EVENT_BEFORE_FORMAT = 'sculpin.core.before_format';
const EVENT_AFTER_FORMAT = 'sculpin.core.after_format';
/**
* Site Configuration
*
* @var Configuration
*/
protected $siteConfiguration;
/**
* Event Dispatcher
*
* @var EventDispatcherInterface
*/
protected $eventDispatcher;
/**
* Permalink factory
*
* @var SourcePermalinkFactoryInterface
*/
protected $permalinkFactory;
/**
* Writer
*
* @var WriterInterface
*/
protected $writer;
/**
* Generator Manager
*
* @var GeneratorManager
*/
protected $generatorManager;
/**
* Formatter Manager
*
* @var FormatterManager
*/
protected $formatterManager;
/**
* Converter Manager
*
* @var ConverterManager
*/
protected $converterManager;
/**
* IO
*
* @var IoInterface
*/
protected $io;
/**
* Constructor.
*
* @param Configuration $siteConfiguration Site Configuration
* @param EventDispatcherInterface $eventDispatcher Event dispatcher
* @param SourcePermalinkFactoryInterface $permalinkFactory Permalink factory
* @param WriterInterface $writer Writer
* @param GeneratorManager $generatorManager Generator Manager
* @param FormatterManager $formatterManager Formatter Manager
* @param ConverterManager $converterManager Converter Manager
*/
public function __construct(
Configuration $siteConfiguration,
EventDispatcherInterface $eventDispatcher,
SourcePermalinkFactoryInterface $permalinkFactory,
WriterInterface $writer,
GeneratorManager $generatorManager,
FormatterManager $formatterManager,
ConverterManager $converterManager
) {
$this->siteConfiguration = $siteConfiguration;
$this->eventDispatcher = $eventDispatcher;
$this->permalinkFactory = $permalinkFactory;
$this->writer = $writer;
$this->generatorManager = $generatorManager;
$this->formatterManager = $formatterManager;
$this->converterManager = $converterManager;
}
/**
* Run.
*
* @param DataSourceInterface $dataSource Data source
* @param SourceSet $sourceSet Source set
* @param IoInterface $io IO Interface
*/
public function run(DataSourceInterface $dataSource, SourceSet $sourceSet, IoInterface $io = null)
{
if (null === $io) {
$io = new NullIo();
}
$found = false;
$startTime = microtime(true);
$dataSource->refresh($sourceSet);
$this->eventDispatcher->dispatch(self::EVENT_BEFORE_RUN, new SourceSetEvent($sourceSet));
if ($updatedSources = array_filter($sourceSet->updatedSources(), function ($source) {
return !$source->isGenerated();
})) {
if (!$found) {
$io->write('Detected new or updated files');
$found = true;
}
$total = count($updatedSources);
$io->write('Generating: ', false);
$io->write('', false);
$counter = 0;
$timer = microtime(true);
foreach ($updatedSources as $source) {
$this->generatorManager->generate($source, $sourceSet);
$io->overwrite(sprintf("%3d%%", 100*((++$counter)/$total)), false);
}
$io->write(sprintf(" (%d sources / %4.2f seconds)", $total, microtime(true) - $timer));
}
foreach ($sourceSet->updatedSources() as $source) {
$permalink = $this->permalinkFactory->create($source);
$source->setPermalink($permalink);
$source->data()->set('url', $permalink->relativeUrlPath());
}
if ($updatedSources = $sourceSet->updatedSources()) {
if (!$found) {
$io->write('Detected new or updated files');
$found = true;
}
$total = count($updatedSources);
$io->write('Converting: ', false);
$io->write('', false);
$counter = 0;
$timer = microtime(true);
foreach ($updatedSources as $source) {
$this->converterManager->convertSource($source);
if ($source->canBeFormatted()) {
$source->data()->set('blocks', $this->formatterManager->formatSourceBlocks($source));
}
$io->overwrite(sprintf("%3d%%", 100*((++$counter)/$total)), false);
}
$io->write(sprintf(" (%d sources / %4.2f seconds)", $total, microtime(true) - $timer));
}
if ($updatedSources = $sourceSet->updatedSources()) {
if (!$found) {
$io->write('Detected new or updated files');
$found = true;
}
$total = count($updatedSources);
$io->write('Formatting: ', false);
$io->write('', false);
$counter = 0;
$timer = microtime(true);
foreach ($updatedSources as $source) {
if ($source->canBeFormatted()) {
$source->setFormattedContent($this->formatterManager->formatSourcePage($source));
} else {
$source->setFormattedContent($source->content());
}
$io->overwrite(sprintf("%3d%%", 100*((++$counter)/$total)), false);
}
$this->eventDispatcher->dispatch(self::EVENT_AFTER_FORMAT, new SourceSetEvent($sourceSet));
$io->write(sprintf(" (%d sources / %4.2f seconds)", $total, microtime(true) - $timer));
}
foreach ($sourceSet->updatedSources() as $source) {
if ($source->isGenerator() || $source->shouldBeSkipped()) {
continue;
}
$this->writer->write(new SourceOutput($source));
if ($io->isVerbose()) {
$io->write(' + ' . $source->sourceId());
}
}
$this->eventDispatcher->dispatch(self::EVENT_AFTER_RUN, new SourceSetEvent($sourceSet));
if ($found) {
$io->write(sprintf("Processing completed in %4.2f seconds", microtime(true) - $startTime));
}
}
}