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 Sculpin\Bundle\SculpinBundle\Command; |
13
|
|
|
|
14
|
|
|
use Dflydev\DotAccessConfiguration\Configuration; |
15
|
|
|
use Sculpin\Bundle\SculpinBundle\HttpServer\HttpServer; |
16
|
|
|
use Sculpin\Bundle\SculpinBundle\HttpServer\HttpServerFactory; |
17
|
|
|
use Sculpin\Core\Sculpin; |
18
|
|
|
use Sculpin\Core\Source\CompositeDataSource; |
19
|
|
|
use Sculpin\Core\Source\SourceSet; |
20
|
|
|
use Symfony\Component\Console\Command\Command; |
21
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
22
|
|
|
use Symfony\Component\Console\Input\InputOption; |
23
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
24
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
25
|
|
|
|
26
|
|
|
final class GenerateCommand extends Command |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
private $outputDirectory; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var Filesystem |
35
|
|
|
*/ |
36
|
|
|
private $filesystem; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var Sculpin |
40
|
|
|
*/ |
41
|
|
|
private $sculpin; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var CompositeDataSource |
45
|
|
|
*/ |
46
|
|
|
private $compositeDataSource; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var Configuration |
50
|
|
|
*/ |
51
|
|
|
private $configuration; |
52
|
|
|
/** |
53
|
|
|
* @var HttpServerFactory |
54
|
|
|
*/ |
55
|
|
|
private $httpServerFactory; |
56
|
|
|
|
57
|
|
|
public function __construct( |
58
|
|
|
string $outputDirectory, |
59
|
|
|
Filesystem $filesystem, |
60
|
|
|
Sculpin $sculpin, |
61
|
|
|
CompositeDataSource $compositeDataSource, |
62
|
|
|
Configuration $configuration, |
63
|
|
|
HttpServerFactory $httpServerFactory |
64
|
|
|
) { |
65
|
|
|
$this->filesystem = $filesystem; |
66
|
|
|
$this->sculpin = $sculpin; |
67
|
|
|
$this->compositeDataSource = $compositeDataSource; |
68
|
|
|
$this->configuration = $configuration; |
69
|
|
|
$this->outputDirectory = $outputDirectory; |
70
|
|
|
|
71
|
|
|
parent::__construct(); |
72
|
|
|
$this->httpServerFactory = $httpServerFactory; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* {@inheritdoc} |
77
|
|
|
*/ |
78
|
|
|
protected function configure() |
79
|
|
|
{ |
80
|
|
|
$this->setName('generate'); |
81
|
|
|
$this->setDescription('Generate a site from source.'); |
82
|
|
|
$this->addOption('clean', null, InputOption::VALUE_NONE, 'Cleans the output directory prior to generation.'); |
83
|
|
|
$this->addOption('watch', null, InputOption::VALUE_NONE, 'Watch source and regenerate site as changes are made.'); |
84
|
|
|
$this->addOption('server', null, InputOption::VALUE_NONE, 'Start an HTTP server to host your generated site'); |
85
|
|
|
$this->addOption('url', null, InputOption::VALUE_REQUIRED, 'Override URL.'); |
86
|
|
|
$this->addOption('port', null, InputOption::VALUE_REQUIRED, 'Port'); |
87
|
|
|
$this->addOption('project-dir', null, InputOption::VALUE_REQUIRED, 'The project directory.', '.'); |
88
|
|
|
|
89
|
|
|
$this->setHelp('The <info>generate</info> command generates a site.'); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* {@inheritdoc} |
94
|
|
|
*/ |
95
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
96
|
|
|
{ |
97
|
|
|
if ($input->getOption('clean')) { |
98
|
|
|
$this->clean($output, $this->outputDirectory); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
$watch = $input->getOption('watch') ?: false; |
102
|
|
|
$sourceSet = new SourceSet(); |
103
|
|
|
|
104
|
|
|
if ($url = $input->getOption('url')) { |
105
|
|
|
$this->configuration->set('url', $url); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
if ($input->getOption('server')) { |
109
|
|
|
$this->sculpin->run($this->compositeDataSource, $sourceSet); |
110
|
|
|
|
111
|
|
|
$httpServer = $this->httpServerFactory->create(); |
112
|
|
|
// $httpServer = new HttpServer( |
|
|
|
|
113
|
|
|
// $output, |
114
|
|
|
// $this->outputDirectory, |
115
|
|
|
// $input->getOption('port') |
116
|
|
|
// ); |
117
|
|
|
|
118
|
|
View Code Duplication |
if ($watch) { |
|
|
|
|
119
|
|
|
$httpServer->addPeriodicTimer(1, function () use ($sourceSet) { |
120
|
|
|
clearstatcache(); |
121
|
|
|
$sourceSet->reset(); |
122
|
|
|
|
123
|
|
|
$this->sculpin->run($this->compositeDataSource, $sourceSet); |
124
|
|
|
}); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
$httpServer->run(); |
128
|
|
View Code Duplication |
} else { |
|
|
|
|
129
|
|
|
do { |
130
|
|
|
$this->sculpin->run($this->compositeDataSource, $sourceSet); |
131
|
|
|
|
132
|
|
|
if ($watch) { |
133
|
|
|
sleep(2); |
134
|
|
|
clearstatcache(); |
135
|
|
|
$sourceSet->reset(); |
136
|
|
|
} |
137
|
|
|
} while ($watch); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
private function clean(OutputInterface $output, string $dir) |
142
|
|
|
{ |
143
|
|
|
if ($this->filesystem->exists($dir)) { |
144
|
|
|
$output->writeln(sprintf('Deleting %s', $dir)); |
145
|
|
|
$this->filesystem->remove($dir); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.