Skip to content

Commit 325287b

Browse files
authored
feat(chore): Adds download-docs command (#697)
1 parent db00812 commit 325287b

20 files changed

+234
-85
lines changed

.devcontainer/Dockerfile

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ ARG XDEBUG_MODE=coverage
44
RUN apt update -y && apt install -y git composer php-cli php-dom php-curl php-xdebug vim
55
WORKDIR /app
66
RUN git config --global --add safe.directory /app && \
7-
git config --global --add safe.directory /app/generator/doc/doc-en/en && \
8-
git config --global --add safe.directory /app/generator/doc/doc-en/doc-base
9-
CMD cd /app/generator/doc && ./update.sh && \
10-
cd /app && composer install && \
11-
cd /app/generator && composer install && php ./safe.php generate
7+
git config --global --add safe.directory /app/generator/docs
8+
CMD cd /app && composer install && \
9+
cd /app/generator && composer install && \
10+
php ./safe.php download-docs && \
11+
php ./safe.php generate

.github/workflows/auto-regenerate.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ jobs:
2222
- name: "Check out salathe/phpdoc-base"
2323
uses: "actions/checkout@v4"
2424
with:
25-
path: "generator/doc/doc-en/doc-base"
25+
path: "generator/docs/salathe/phpdoc-base"
2626
repository: "salathe/phpdoc-base"
2727
fetch-depth: 0
2828

2929
- name: "Check out php/doc-en"
3030
uses: "actions/checkout@v4"
3131
with:
32-
path: "generator/doc/doc-en/en"
32+
path: "generator/docs"
3333
repository: "php/doc-en"
3434
fetch-depth: 0
3535

.github/workflows/continuous-integration.yml

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,27 +18,31 @@ jobs:
1818
steps:
1919
- name: "Checkout"
2020
uses: "actions/checkout@v4"
21+
2122
- name: "Get current date for the daily cache"
2223
id: "date"
2324
run: echo "date=$(date +'%Y-%m-%d')" >> $GITHUB_OUTPUT
25+
2426
- name: "Cache the php documentation"
25-
id: cache-php-doc
27+
id: cache-php-docs
2628
uses: "actions/cache@v4"
2729
with:
28-
path: "generator/doc/doc-en"
29-
key: php-doc-${{ steps.date.outputs.date }}
30+
path: "generator/docs"
31+
key: php-docs-${{ steps.date.outputs.date }}
32+
3033
- name: "Check out salathe/phpdoc-base"
3134
uses: "actions/checkout@v4"
32-
if: steps.cache-php-doc.outputs.cache-hit != 'true'
35+
if: steps.cache-php-docs.outputs.cache-hit != 'true'
3336
with:
34-
path: "generator/doc/doc-en/doc-base"
37+
path: "generator/docs/salathe/phpdoc-base"
3538
repository: "salathe/phpdoc-base"
3639
fetch-depth: 0
40+
3741
- name: "Check out php/doc-en"
3842
uses: "actions/checkout@v4"
39-
if: steps.cache-php-doc.outputs.cache-hit != 'true'
43+
if: steps.cache-php-docs.outputs.cache-hit != 'true'
4044
with:
41-
path: "generator/doc/doc-en/en"
45+
path: "generator/docs/php/doc-en"
4246
repository: "php/doc-en"
4347
fetch-depth: 0
4448

@@ -61,11 +65,11 @@ jobs:
6165
coverage: "pcov"
6266
php-version: "${{ matrix.php-version }}"
6367
- name: "Fetch cached docs"
64-
id: cache-php-doc
68+
id: cache-docs
6569
uses: "actions/cache@v4"
6670
with:
67-
path: "generator/doc/doc-en"
68-
key: php-doc-${{ needs.fetch_defs.outputs.date }}
71+
path: "generator/docs"
72+
key: php-docs-${{ needs.fetch_defs.outputs.date }}
6973
fail-on-cache-miss: true
7074
- name: "Cache dependencies installed with composer"
7175
uses: "actions/cache@v4"
@@ -140,11 +144,11 @@ jobs:
140144
coverage: "pcov"
141145
php-version: "8.4"
142146
- name: "Fetch cached docs"
143-
id: cache-php-doc
147+
id: cache-php-docs
144148
uses: "actions/cache@v4"
145149
with:
146-
path: "generator/doc/doc-en"
147-
key: php-doc-${{ needs.fetch_defs.outputs.date }}
150+
path: "generator/docs"
151+
key: php-docs-${{ needs.fetch_defs.outputs.date }}
148152
fail-on-cache-miss: true
149153
- name: "Cache dependencies installed with composer"
150154
uses: "actions/cache@v4"

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ vendor/
77
/generator/tests/rector/composer.lock
88
.phpunit.result.cache
99
.phpunit.cache
10+
docs/

CONTRIBUTING.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,10 @@ generator, as well as some handy shortcut shell scripts. You can use
9191
The first step is to download the PHP documentation project locally, using git.
9292

9393
```bash
94-
$ cd generator/doc
95-
$ sh update.sh
94+
$ generator/safe.php download-docs
9695
```
9796

98-
The script can be used to both install the doc and to update it.
99-
All it does is pull from theses mirrors:
97+
This will download the PHP documentation from the official repository.
10098

10199
https://github.com/salathe/phpdoc-base
102100

generator/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
"phpstan/phpstan": "^2",
1515
"symfony/console": "^7",
1616
"symfony/process": "^7",
17-
"symfony/finder": "^7"
17+
"symfony/finder": "^7",
18+
"symfony/filesystem": "^7.2"
1819
},
1920
"require-dev": {
2021
"phpunit/phpunit": "^12",

generator/composer.lock

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

generator/doc/entities/.gitkeep

Whitespace-only changes.

generator/doc/update.sh

Lines changed: 0 additions & 11 deletions
This file was deleted.

generator/safe.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@
77
use Safe\Commands\FunctionInfoCommand;
88
use Safe\Commands\GenerateCommand;
99
use Safe\Commands\ScanObjectsCommand;
10+
use Safe\Commands\DownloadDocsCommand;
1011
use Symfony\Component\Console\Application;
1112

1213
$application = new Application();
1314
$application->addCommands([new GenerateCommand()]);
1415
$application->addCommands([new ScanObjectsCommand()]);
1516
$application->addCommands([new FunctionInfoCommand()]);
17+
$application->addCommands([new DownloadDocsCommand()]);
1618

1719
$application->run();
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Safe\Commands;
6+
7+
use Safe\Filesystem\PathHelper;
8+
use Symfony\Component\Console\Command\Command;
9+
use Symfony\Component\Console\Helper\ProgressIndicator;
10+
use Symfony\Component\Console\Input\InputInterface;
11+
use Symfony\Component\Console\Output\OutputInterface;
12+
use Symfony\Component\Console\Style\SymfonyStyle;
13+
use Symfony\Component\Filesystem\Filesystem;
14+
use Symfony\Component\Process\Process;
15+
16+
final class DownloadDocsCommand extends Command
17+
{
18+
private SymfonyStyle $io;
19+
private Filesystem $filesystem;
20+
21+
protected function configure(): void
22+
{
23+
$this
24+
->setName('download-docs')
25+
->setDescription('Download the recent version of the PHP documentation')
26+
;
27+
}
28+
29+
protected function initialize(InputInterface $input, OutputInterface $output): void
30+
{
31+
$this->io = new SymfonyStyle($input, $output);
32+
$this->filesystem = new Filesystem();
33+
}
34+
35+
protected function execute(InputInterface $input, OutputInterface $output): int
36+
{
37+
$this->io->title($this->getDescription());
38+
39+
// Clean up before cloning.
40+
$this->filesystem->remove(PathHelper::docsDirectory());
41+
$this->filesystem->mkdir(PathHelper::docsDirectory());
42+
43+
$this->clone($output, 'salathe/phpdoc-base');
44+
$this->clone($output, 'php/doc-en');
45+
46+
return self::SUCCESS;
47+
}
48+
49+
private function clone(OutputInterface $output, string $repository): void
50+
{
51+
$progressIndicator = new ProgressIndicator($output);
52+
$progressIndicator->start(\sprintf('Cloning <info>%s</info>', $repository));
53+
54+
$process = new Process(['git', 'clone', \sprintf('https://github.com/%s', $repository), \sprintf('%s/%s', PathHelper::docsDirectory(), $repository)]);
55+
$process->start();
56+
57+
while ($process->isRunning()) {
58+
$progressIndicator->advance();
59+
}
60+
61+
$progressIndicator->finish(\sprintf('Downloaded <info>%s</info>', $repository));
62+
}
63+
}

generator/src/Commands/FunctionInfoCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
3232
$phpStanFunctionMapReader = new PhpStanFunctionMapReader();
3333

3434
$finder = new Finder();
35-
$finder->in(DocPage::findReferenceDir() . "/*/functions/")->name($targetFilename)->sortByName();
35+
$finder->in(DocPage::referenceDir() . "/*/functions/")->name($targetFilename)->sortByName();
3636

3737
foreach ($finder as $file) {
3838
$docPage = new DocPage($file->getPathname());

generator/src/Commands/GenerateCommand.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Safe\Commands;
66

7+
use Safe\Filesystem\PathHelper;
78
use Safe\XmlDocParser\Scanner;
89
use Safe\XmlDocParser\DocPage;
910
use Safe\Generator\FileCreator;
@@ -34,7 +35,7 @@ protected function execute(
3435
$this->rmGenerated();
3536

3637
// Let's build the DTD necessary to load the XML files.
37-
$this->checkout(DocPage::findReferenceDir(), "master");
38+
$this->checkout(DocPage::referenceDir(), "master");
3839
DocPage::buildEntities();
3940

4041
// PHP documentation is a living document, which broadly reflects
@@ -66,8 +67,8 @@ protected function execute(
6667

6768
// Scan the documentation for a given PHP version and find all
6869
// functions that we need to generate safe wrappers for.
69-
$this->checkout(DocPage::findReferenceDir(), $commit);
70-
$scanner = new Scanner(DocPage::findReferenceDir());
70+
$this->checkout(DocPage::referenceDir(), $commit);
71+
$scanner = new Scanner(DocPage::referenceDir());
7172
$res = $scanner->getMethods($scanner->getFunctionsPaths(), $pastFunctionNames, $output);
7273
$output->writeln(
7374
'Functions have been ignored and must be dealt with manually: ' .
@@ -133,8 +134,8 @@ private function rmGenerated(): void
133134
}
134135
}
135136

136-
if (\file_exists(DocPage::findDocDir() . '/entities/generated.ent')) {
137-
\unlink(DocPage::findDocDir() . '/entities/generated.ent');
137+
if (\file_exists(PathHelper::docsDirectory() . '/generated.ent')) {
138+
\unlink(PathHelper::docsDirectory() . '/generated.ent');
138139
}
139140
}
140141

generator/src/Commands/ScanObjectsCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ protected function configure(): void
2222

2323
protected function execute(InputInterface $input, OutputInterface $output): int
2424
{
25-
$scanner = new Scanner(DocPage::findReferenceDir());
25+
$scanner = new Scanner(DocPage::referenceDir());
2626

2727
$paths = $scanner->getMethodsPaths();
2828

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Safe\Filesystem;
6+
7+
final class PathHelper
8+
{
9+
public static function projectRootDir(): string
10+
{
11+
$path = realpath(__DIR__ . '/../../..');
12+
13+
if (false === $path) {
14+
throw new \RuntimeException('Unable to locate root directory');
15+
}
16+
17+
return $path;
18+
}
19+
20+
public static function docsDirectory(): string
21+
{
22+
return \sprintf('%s/generator/docs', self::projectRootDir());
23+
}
24+
25+
private function __construct()
26+
{
27+
}
28+
}

0 commit comments

Comments
 (0)