Skip to content

Commit 8acddf8

Browse files
committed
Improve code style
1 parent 3c7557c commit 8acddf8

File tree

10 files changed

+247
-129
lines changed

10 files changed

+247
-129
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
"phpstan/extension-installer": "^1.4",
6666
"phpstan/phpstan": "^1.12",
6767
"phpstan/phpstan-strict-rules": "^1.6",
68+
"phpstan/phpstan-symfony": "^1.4",
6869
"phpstan/phpstan-webmozart-assert": "^1.2",
6970
"phpunit/phpunit": "^10.5",
7071
"qossmic/deptrac-shim": "^1.0.2",

composer.lock

Lines changed: 73 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/filesystem/src/FlysystemV1/FlysystemV1.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ public function find(SpecificationInterface $specification): iterable
7878

7979
public function isDirectory(string $path): bool
8080
{
81-
return $this->filesystem->directoryExists($path);
81+
$metadata = $this->filesystem->getMetadata($path);
82+
if ($metadata === false) {
83+
return false;
84+
}
85+
86+
return $metadata['type'] === 'dir';
8287
}
8388
}

packages/guides-cli/src/Command/Serve.php

Lines changed: 67 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -15,34 +15,35 @@
1515

1616
use League\Tactician\CommandBus;
1717
use Monolog\Handler\ErrorLogHandler;
18+
use Monolog\Logger;
1819
use phpDocumentor\FileSystem\FlySystemAdapter;
1920
use phpDocumentor\Guides\Cli\Internal\RunCommand;
2021
use phpDocumentor\Guides\Cli\Internal\ServerFactory;
2122
use phpDocumentor\Guides\Cli\Internal\Watcher\FileModifiedEvent;
22-
use phpDocumentor\Guides\Cli\Internal\Watcher\INotifyWatcher;
2323
use phpDocumentor\Guides\Compiler\CompilerContext;
2424
use phpDocumentor\Guides\Event\PostParseDocument;
2525
use phpDocumentor\Guides\Handlers\CompileDocumentsCommand;
2626
use phpDocumentor\Guides\Handlers\ParseFileCommand;
2727
use phpDocumentor\Guides\Handlers\RenderDocumentCommand;
28+
use phpDocumentor\Guides\Nodes\DocumentNode;
2829
use phpDocumentor\Guides\RenderContext;
2930
use phpDocumentor\Guides\Renderer\DocumentListIterator;
3031
use phpDocumentor\Guides\Renderer\DocumentTreeIterator;
31-
use Psr\EventDispatcher\EventDispatcherInterface;
32-
use Psr\Log\LoggerInterface;
33-
use React\EventLoop\Loop;
3432
use Symfony\Component\Console\Command\Command;
3533
use Symfony\Component\Console\Input\InputInterface;
3634
use Symfony\Component\Console\Input\InputOption;
3735
use Symfony\Component\Console\Output\OutputInterface;
3836

37+
use function assert;
38+
use function is_int;
39+
use function is_string;
3940
use function sprintf;
41+
use function substr;
4042

4143
final class Serve extends Command
4244
{
4345
public function __construct(
44-
private LoggerInterface $logger,
45-
private EventDispatcherInterface $dispatcher,
46+
private readonly Logger $logger,
4647
private SettingsBuilder $settingsBuilder,
4748
private CommandBus $commandBus,
4849
private ServerFactory $serverFactory,
@@ -55,46 +56,53 @@ protected function configure(): void
5556
$this->settingsBuilder->configureCommand($this);
5657
$this->addOption('host', null, InputOption::VALUE_REQUIRED, 'Hostname to serve on', 'localhost');
5758
$this->addOption('port', 'p', InputOption::VALUE_REQUIRED, 'Port to run the server on', 1337);
58-
$this->addOption('listen', 'l', InputOption::VALUE_REQUIRED, 'Address to listen on', '0.0.0.0');
5959
}
6060

6161
protected function execute(InputInterface $input, OutputInterface $output): int
6262
{
6363
$this->logger->pushHandler(new ErrorLogHandler(ErrorLogHandler::OPERATING_SYSTEM));
64-
// Enable tick processing for signal handling
65-
declare(ticks=1);
66-
67-
$loop = Loop::get();
68-
$loop->addSignal(SIGINT, static function () use ($loop, $output): void {
69-
$output->writeln('Shutting down server...');
70-
$loop->stop();
71-
$output->writeln('Server stopped');
72-
exit(0);
73-
});
74-
7564

7665
$dir = $input->getOption('output');
77-
if ($dir === null) {
66+
if (!is_string($dir)) {
7867
$output->writeln('<error>Please specify an output directory using --output option</error>');
7968

8069
return Command::FAILURE;
8170
}
8271

72+
$inputDir = $input->getArgument('input');
73+
if (!is_string($inputDir)) {
74+
$output->writeln('<error>Please specify an input directory using the input argument</error>');
75+
76+
return Command::FAILURE;
77+
}
78+
79+
$host = $input->getOption('host');
80+
if (!is_string($host)) {
81+
$output->writeln('<error>Please specify a valid host using --host option</error>');
82+
83+
return Command::FAILURE;
84+
}
85+
86+
$port = $input->getOption('port');
87+
if (!is_int($port)) {
88+
$output->writeln('<error>Please specify a valid port using --port option</error>');
89+
90+
return Command::FAILURE;
91+
}
92+
8393
$files = FlySystemAdapter::createForPath($dir);
84-
$app = $this->serverFactory->createWebserver(
94+
$app = $this->serverFactory->createDevServer(
95+
$inputDir,
8596
$files,
86-
$loop,
87-
$input->getOption('host'),
88-
$input->getOption('listen'),
89-
(int) $input->getOption('port'),
97+
$host,
98+
'0.0,0.0',
99+
$port,
90100
);
91101

92-
$watcher = new InotifyWatcher($loop, $this->dispatcher, $input->getArgument('input'));
93-
94-
$this->dispatcher->addListener(
102+
$app->addListener(
95103
PostParseDocument::class,
96-
static function (PostParseDocument $event) use ($watcher): void {
97-
$watcher->addPath($event->getOriginalFileName());
104+
static function (PostParseDocument $event) use ($app): void {
105+
$app->watch($event->getOriginalFileName());
98106
},
99107
);
100108

@@ -104,6 +112,7 @@ static function (PostParseDocument $event) use ($watcher): void {
104112
$projectNode = $this->settingsBuilder->createProjectNode();
105113
$sourceFileSystem = FlySystemAdapter::createForPath($settings->getInput());
106114

115+
/** @var array<string, DocumentNode> $documents */
107116
$documents = $this->commandBus->handle(
108117
new RunCommand(
109118
$settings,
@@ -112,9 +121,9 @@ static function (PostParseDocument $event) use ($watcher): void {
112121
),
113122
);
114123

115-
$this->dispatcher->addListener(
124+
$app->addListener(
116125
FileModifiedEvent::class,
117-
function (FileModifiedEvent $event) use ($sourceFileSystem, $projectNode, $settings, $app, $output): void {
126+
function (FileModifiedEvent $event) use ($documents, $sourceFileSystem, $projectNode, $settings, $app, $output): void {
118127
$output->writeln(
119128
sprintf(
120129
'File modified: %s, rerendering...',
@@ -123,7 +132,7 @@ function (FileModifiedEvent $event) use ($sourceFileSystem, $projectNode, $setti
123132
);
124133
$file = substr($event->path, 0, -4);
125134

126-
$documents[$file] = $this->commandBus->handle(
135+
$document = $this->commandBus->handle(
127136
new ParseFileCommand(
128137
$sourceFileSystem,
129138
'',
@@ -134,12 +143,14 @@ function (FileModifiedEvent $event) use ($sourceFileSystem, $projectNode, $setti
134143
true,
135144
),
136145
);
146+
assert($document instanceof DocumentNode);
147+
148+
$documents[$file] = $document;
137149

150+
/** @var array<string, DocumentNode> $documents */
138151
$documents = $this->commandBus->handle(new CompileDocumentsCommand($documents, new CompilerContext($projectNode)));
139152
$destinationFileSystem = FlySystemAdapter::createForPath($settings->getOutput());
140153

141-
$outputFormats = $settings->getOutputFormats();
142-
143154
$documentIterator = new DocumentListIterator(
144155
new DocumentTreeIterator(
145156
[$projectNode->getRootDocumentEntry()],
@@ -148,33 +159,34 @@ function (FileModifiedEvent $event) use ($sourceFileSystem, $projectNode, $setti
148159
$documents,
149160
);
150161

151-
//foreach ($outputFormats as $format) {
152-
153-
$renderContext = RenderContext::forProject(
154-
$projectNode,
155-
$documents,
156-
$sourceFileSystem,
157-
$destinationFileSystem,
158-
'/',
159-
'html',
160-
)->withIterator($documentIterator);
161-
162-
$this->commandBus->handle(
163-
new RenderDocumentCommand(
164-
$documents[$file],
165-
$renderContext->withDocument($documents[$file]),
166-
),
167-
);
168-
//}
162+
$renderContext = RenderContext::forProject(
163+
$projectNode,
164+
$documents,
165+
$sourceFileSystem,
166+
$destinationFileSystem,
167+
'/',
168+
'html',
169+
)->withIterator($documentIterator);
170+
171+
$this->commandBus->handle(
172+
new RenderDocumentCommand(
173+
$documents[$file],
174+
$renderContext->withDocument($documents[$file]),
175+
),
176+
);
169177

170178
$output->writeln('Rerendering completed.');
171179
$app->notifyClients();
172180
},
173181
);
174182

175-
176-
$output->writeln(sprintf('Server running at http://localhost:1337'));
177-
$output->writeln('WebSocket server running at ws://localhost:1337/ws');
183+
$output->writeln(
184+
sprintf(
185+
'Server running at http://%s:%d',
186+
$host,
187+
$port,
188+
),
189+
);
178190
$output->writeln('Press Ctrl+C to stop the server');
179191

180192
$app->run();

packages/guides-cli/src/Internal/HttpHandler.php

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,11 @@
1818
use League\MimeTypeDetection\ExtensionMimeTypeDetector;
1919
use phpDocumentor\FileSystem\FlySystemAdapter;
2020
use Psr\Http\Message\RequestInterface;
21-
use Psr\Log\LoggerInterface;
2221
use Ratchet\ConnectionInterface;
2322
use Ratchet\Http\CloseResponseTrait;
2423
use Ratchet\Http\HttpServerInterface;
2524
use Throwable;
2625

27-
use function sprintf;
2826
use function str_replace;
2927
use function strlen;
3028
use function trim;
@@ -36,7 +34,6 @@ final class HttpHandler implements HttpServerInterface
3634
private ExtensionMimeTypeDetector $detector;
3735

3836
public function __construct(
39-
private LoggerInterface $logger,
4037
private FlySystemAdapter $files,
4138
) {
4239
$this->detector = new ExtensionMimeTypeDetector();
@@ -51,13 +48,6 @@ public function onOpen(ConnectionInterface $conn, RequestInterface|null $request
5148
}
5249

5350
$path = $request->getUri()->getPath();
54-
$this->logger->info(
55-
sprintf(
56-
'Received request for %s from %s',
57-
$path,
58-
$conn->remoteAddress,
59-
),
60-
);
6151

6252
// Remove leading slash and any route parameters
6353
$requestPath = trim($path, '/');
@@ -81,7 +71,7 @@ public function onOpen(ConnectionInterface $conn, RequestInterface|null $request
8171

8272
$headers = [
8373
'Content-Type' => $this->detector->detectMimeTypeFromPath($requestPath) ?? 'text/plain',
84-
'Content-Length' => strlen($content),
74+
'Content-Length' => (string) strlen($content),
8575
];
8676

8777
$conn->send(Message::toString(new Response(200, $headers, $content)));
@@ -128,6 +118,8 @@ public function onError(ConnectionInterface $conn, Throwable $e): void
128118
$this->close($conn, 500);
129119
}
130120

121+
/** @param string $msg */
122+
// phpcs:disable SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
131123
public function onMessage(ConnectionInterface $from, $msg): void
132124
{
133125
// TODO: Implement onMessage() method.

0 commit comments

Comments
 (0)