1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* Copyright (c) Arnaud Ligny <[email protected]> |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Cecil\Command; |
10
|
|
|
|
11
|
|
|
use Cecil\Util\Plateform; |
12
|
|
|
use Symfony\Component\Console\Input\ArrayInput; |
13
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
14
|
|
|
use Symfony\Component\Console\Input\InputDefinition; |
15
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
16
|
|
|
use Symfony\Component\Console\Input\InputOption; |
17
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
18
|
|
|
use Symfony\Component\Filesystem\Exception\IOExceptionInterface; |
19
|
|
|
use Symfony\Component\Finder\Finder; |
20
|
|
|
use Symfony\Component\Process\Exception\ProcessFailedException; |
21
|
|
|
use Symfony\Component\Process\Process; |
22
|
|
|
use Yosymfony\ResourceWatcher\Crc32ContentHash; |
23
|
|
|
use Yosymfony\ResourceWatcher\ResourceCacheMemory; |
24
|
|
|
use Yosymfony\ResourceWatcher\ResourceWatcher; |
25
|
|
|
|
26
|
|
|
class CommandServe extends Command |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
public static $tmpDir = '.cecil'; |
32
|
|
|
/** |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
protected $drafts; |
36
|
|
|
/** |
37
|
|
|
* @var bool |
38
|
|
|
*/ |
39
|
|
|
protected $open; |
40
|
|
|
/** |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
protected $host; |
44
|
|
|
/** |
45
|
|
|
* @var string |
46
|
|
|
*/ |
47
|
|
|
protected $port; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* {@inheritdoc} |
51
|
|
|
*/ |
52
|
|
View Code Duplication |
protected function configure() |
53
|
|
|
{ |
54
|
|
|
$this |
55
|
|
|
->setName('serve') |
56
|
|
|
->setAliases(['s']) |
57
|
|
|
->setDescription('Start the built-in server') |
58
|
|
|
->setDefinition( |
59
|
|
|
new InputDefinition([ |
60
|
|
|
new InputArgument('path', InputArgument::OPTIONAL, 'If specified, use the given path as working directory'), |
61
|
|
|
new InputOption('drafts', 'd', InputOption::VALUE_NONE, 'Include drafts'), |
62
|
|
|
new InputOption('open', 'o', InputOption::VALUE_NONE, 'Open browser automatically'), |
63
|
|
|
new InputOption('host', null, InputOption::VALUE_OPTIONAL, 'Server host'), |
64
|
|
|
new InputOption('port', null, InputOption::VALUE_OPTIONAL, 'Server port'), |
65
|
|
|
]) |
66
|
|
|
) |
67
|
|
|
->setHelp('Start the live-reloading-built-in web server.'); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* {@inheritdoc} |
72
|
|
|
*/ |
73
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
74
|
|
|
{ |
75
|
|
|
$this->drafts = $input->getOption('drafts'); |
|
|
|
|
76
|
|
|
$this->open = $input->getOption('open'); |
|
|
|
|
77
|
|
|
$this->host = $input->getOption('host') ?? 'localhost'; |
|
|
|
|
78
|
|
|
$this->port = $input->getOption('port') ?? '8000'; |
|
|
|
|
79
|
|
|
|
80
|
|
|
$this->setUpServer($output); |
81
|
|
|
$command = sprintf( |
82
|
|
|
'php -S %s:%d -t %s %s', |
83
|
|
|
$this->host, |
84
|
|
|
$this->port, |
85
|
|
|
$this->getPath() . '/' . $this->getBuilder($output)->getConfig()->get('output.dir'), |
86
|
|
|
sprintf('%s/%s/router.php', $this->getPath(), self::$tmpDir) |
87
|
|
|
); |
88
|
|
|
$process = new Process($command); |
|
|
|
|
89
|
|
|
|
90
|
|
|
// (re)build before serve |
91
|
|
|
$buildCommand = $this->getApplication()->find('build'); |
92
|
|
|
$buildInput = new ArrayInput([ |
93
|
|
|
'command' => 'build', |
94
|
|
|
'path' => $this->getPath(), |
95
|
|
|
'--drafts' => $this->drafts, |
96
|
|
|
]); |
97
|
|
|
$returnCode = $buildCommand->run($buildInput, $output); |
|
|
|
|
98
|
|
|
|
99
|
|
|
// handle process |
100
|
|
|
if (!$process->isStarted()) { |
101
|
|
|
// write changes cache |
102
|
|
|
$finder = new Finder(); |
103
|
|
|
$finder->files() |
104
|
|
|
->name('*.md') |
105
|
|
|
->name('*.twig') |
106
|
|
|
->name('*.yml') |
107
|
|
|
->name('*.css') |
108
|
|
|
->name('*.scss') |
109
|
|
|
->name('*.js') |
110
|
|
|
->in($this->getPath()) |
111
|
|
|
->exclude($this->getBuilder($output)->getConfig()->get('output.dir')); |
112
|
|
View Code Duplication |
if (!$this->nowatcher) { |
|
|
|
|
113
|
|
|
$hashContent = new Crc32ContentHash(); |
114
|
|
|
$resourceCache = new ResourceCacheMemory(); |
115
|
|
|
$resourceWatcher = new ResourceWatcher($resourceCache, $finder, $hashContent); |
116
|
|
|
$resourceWatcher->initialize(); |
117
|
|
|
} |
118
|
|
|
// start server |
119
|
|
|
try { |
120
|
|
|
$output->writeln(sprintf('<info>Starting server (http://%s:%d)...</info>', $this->host, $this->port)); |
121
|
|
|
$process->start(); |
122
|
|
|
if ($this->open) { |
123
|
|
|
Plateform::openBrowser(sprintf('http://%s:%s', $this->host, $this->port)); |
124
|
|
|
} |
125
|
|
|
while ($process->isRunning()) { |
126
|
|
|
if (!$this->nowatcher) { |
127
|
|
|
$result = $resourceWatcher->findChanges(); |
|
|
|
|
128
|
|
|
if ($result->hasChanges()) { |
129
|
|
|
// re-build |
130
|
|
|
$output->writeln('<comment>Changes detected.</comment>'); |
131
|
|
|
$returnCode = $buildCommand->run($buildInput, $output); |
|
|
|
|
132
|
|
|
} |
133
|
|
|
usleep(1000000); // wait 1s |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
} catch (ProcessFailedException $e) { |
137
|
|
|
$this->tearDownServer(); |
138
|
|
|
|
139
|
|
|
throw new \Exception(sprintf($e->getMessage())); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
return 0; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
View Code Duplication |
private function setUpServer($output) |
147
|
|
|
{ |
148
|
|
|
try { |
149
|
|
|
$root = __DIR__ . '/../../'; |
150
|
|
|
if (Plateform::isPhar()) { |
151
|
|
|
$root = Plateform::getPharPath() . '/'; |
152
|
|
|
} |
153
|
|
|
// copy router |
154
|
|
|
$this->fs->copy( |
155
|
|
|
$root . 'res/server/router.php', |
156
|
|
|
$this->getPath() . '/' . self::$tmpDir . '/router.php', |
157
|
|
|
true |
158
|
|
|
); |
159
|
|
|
// copy livereload JS |
160
|
|
|
if (!$this->nowatcher) { |
161
|
|
|
$this->fs->copy( |
162
|
|
|
$root . 'res/server/livereload.js', |
163
|
|
|
$this->getPath() . '/' . self::$tmpDir . '/livereload.js', |
164
|
|
|
true |
165
|
|
|
); |
166
|
|
|
} |
167
|
|
|
// copy baseurl text file |
168
|
|
|
$this->fs->dumpFile( |
169
|
|
|
$this->getPath() . '/' . self::$tmpDir . '/baseurl', |
170
|
|
|
sprintf( |
171
|
|
|
'%s;%s', |
172
|
|
|
$this->getBuilder($output)->getConfig()->get('baseurl'), |
173
|
|
|
sprintf('http://%s:%s/', $this->host, $this->port) |
174
|
|
|
) |
175
|
|
|
); |
176
|
|
|
} catch (IOExceptionInterface $e) { |
177
|
|
|
throw new \Exception(sprintf('An error occurred while copying file at "%s"', $e->getPath())); |
178
|
|
|
} |
179
|
|
|
if (!is_file(sprintf('%s/%s/router.php', $this->getPath(), self::$tmpDir))) { |
180
|
|
|
throw new \Exception(sprintf('Router not found: "./%s/router.php"', self::$tmpDir)); |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|
184
|
|
View Code Duplication |
public function tearDownServer() |
185
|
|
|
{ |
186
|
|
|
try { |
187
|
|
|
$this->fs->remove($this->getPath() . '/' . self::$tmpDir); |
188
|
|
|
} catch (IOExceptionInterface $e) { |
189
|
|
|
throw new \Exception(sprintf($e->getMessage())); |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.