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 Sculpin\Bundle\SculpinBundle\HttpServer\HttpServer; |
15
|
|
|
use Sculpin\Core\Sculpin; |
16
|
|
|
use Sculpin\Core\Source\SourceSet; |
17
|
|
|
use Symfony\Component\Console\Command\Command; |
18
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
19
|
|
|
use Symfony\Component\Console\Input\InputOption; |
20
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
21
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
22
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
23
|
|
|
|
24
|
|
|
final class GenerateCommand extends Command |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var ContainerInterface |
28
|
|
|
*/ |
29
|
|
|
private $container; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var Filesystem |
33
|
|
|
*/ |
34
|
|
|
private $filesystem; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var Sculpin |
38
|
|
|
*/ |
39
|
|
|
private $sculpin; |
40
|
|
|
|
41
|
|
|
public function __construct( |
42
|
|
|
ContainerInterface $container, |
43
|
|
|
Filesystem $filesystem, |
44
|
|
|
Sculpin $sculpin |
45
|
|
|
) { |
46
|
|
|
$this->container = $container; |
47
|
|
|
$this->filesystem = $filesystem; |
48
|
|
|
$this->sculpin = $sculpin; |
49
|
|
|
|
50
|
|
|
parent::__construct(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* {@inheritdoc} |
55
|
|
|
*/ |
56
|
|
|
protected function configure() |
57
|
|
|
{ |
58
|
|
|
$this->setName('generate'); |
59
|
|
|
$this->setDescription('Generate a site from source.'); |
60
|
|
|
$this->addOption('clean', null, InputOption::VALUE_NONE, 'Cleans the output directory prior to generation.'); |
61
|
|
|
$this->addOption('watch', null, InputOption::VALUE_NONE, 'Watch source and regenerate site as changes are made.'); |
62
|
|
|
$this->addOption('server', null, InputOption::VALUE_NONE, 'Start an HTTP server to host your generated site'); |
63
|
|
|
$this->addOption('url', null, InputOption::VALUE_REQUIRED, 'Override URL.'); |
64
|
|
|
$this->addOption('port', null, InputOption::VALUE_REQUIRED, 'Port'); |
65
|
|
|
$this->addOption('project-dir', null, InputOption::VALUE_REQUIRED, 'The project directory.', '.'); |
66
|
|
|
|
67
|
|
|
$this->setHelp('The <info>generate</info> command generates a site.'); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* {@inheritdoc} |
72
|
|
|
*/ |
73
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
74
|
|
|
{ |
75
|
|
|
$docroot = $this->container->getParameter('sculpin.output_dir'); |
76
|
|
|
if ($input->getOption('clean')) { |
77
|
|
|
$this->clean($output, $docroot); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$watch = $input->getOption('watch') ?: false; |
81
|
|
|
$dataSource = $this->container->get('sculpin.data_source'); |
82
|
|
|
$sourceSet = new SourceSet(); |
83
|
|
|
|
84
|
|
|
$config = $this->container->get('sculpin.site_configuration'); |
85
|
|
|
if ($url = $input->getOption('url')) { |
86
|
|
|
$config->set('url', $url); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
if ($input->getOption('server')) { |
90
|
|
|
$this->sculpin->run($dataSource, $sourceSet); |
91
|
|
|
|
92
|
|
|
$kernel = $this->container->get('kernel'); |
93
|
|
|
|
94
|
|
|
$httpServer = new HttpServer( |
95
|
|
|
$output, |
96
|
|
|
$docroot, |
97
|
|
|
$kernel->getEnvironment(), |
98
|
|
|
$kernel->isDebug(), |
99
|
|
|
$input->getOption('port') |
100
|
|
|
); |
101
|
|
|
|
102
|
|
|
if ($watch) { |
103
|
|
|
$httpServer->addPeriodicTimer(1, function () use ($dataSource, $sourceSet) { |
104
|
|
|
clearstatcache(); |
105
|
|
|
$sourceSet->reset(); |
106
|
|
|
|
107
|
|
|
$this->sculpin->run($dataSource, $sourceSet); |
108
|
|
|
}); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
$httpServer->run(); |
112
|
|
|
} else { |
113
|
|
|
do { |
114
|
|
|
$this->sculpin->run($dataSource, $sourceSet); |
115
|
|
|
|
116
|
|
|
if ($watch) { |
117
|
|
|
sleep(2); |
118
|
|
|
clearstatcache(); |
119
|
|
|
$sourceSet->reset(); |
120
|
|
|
} |
121
|
|
|
} while ($watch); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
private function clean(OutputInterface $output, string $dir) |
126
|
|
|
{ |
127
|
|
|
if ($this->filesystem->exists($dir)) { |
128
|
|
|
$output->writeln(sprintf('Deleting %s', $dir)); |
129
|
|
|
$this->filesystem->remove($dir); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|