Skip to content
This repository was archived by the owner on Aug 2, 2025. It is now read-only.

Commit 10f1d94

Browse files
committed
Add command to generate static files
1 parent 7e16f80 commit 10f1d94

File tree

7 files changed

+204
-19
lines changed

7 files changed

+204
-19
lines changed

.env

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
# https://symfony.com/doc/current/best_practices.html#use-environment-variables-for-infrastructure-configuration
1515

1616
DATABASE_DSN=sqlite:/src/var/db.sqlite
17+
FILESYSTEM_ROOT=var/output
1718

1819
###> symfony/framework-bundle ###
1920
APP_ENV=dev

config/services.yaml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,30 @@ services:
22
_defaults:
33
autowire: true
44
autoconfigure: true
5+
bind:
6+
string $outputFolder: '%env(FILESYSTEM_ROOT)%'
57

68
App\:
79
resource: '../src/'
810
exclude:
11+
- '../src/Command/SynchronizeCommand.php'
912
- '../src/DependencyInjection/'
1013
- '../src/Entity/'
1114
- '../src/Kernel.php'
1215
- '../src/Tests/'
1316

17+
App\Command\SynchronizeCommand.db:
18+
class: App\Command\SynchronizeCommand
19+
arguments:
20+
$storage: '@App\Storage\PDOStorage'
21+
$name: 'synchronize:database'
22+
23+
App\Command\SynchronizeCommand.fs:
24+
class: App\Command\SynchronizeCommand
25+
arguments:
26+
$storage: '@App\Storage\FilesystemStorage'
27+
$name: 'synchronize:files'
28+
1429
App\Controller\:
1530
resource: '../src/Controller/'
1631
tags: ['controller.service_arguments']

src/Command/SynchronizeCommand.php

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@
77
use App\{
88
Clock,
99
PhpVersionFetcher,
10-
Repository\PDO\LastUpdateRepository,
11-
Repository\PDO\PdoPhpReleaseRepository,
12-
Repository\PDO\PdoPhpVersionRepository,
10+
Storage,
1311
};
1412
use Symfony\Component\Console\{
1513
Command\Command,
@@ -21,33 +19,39 @@ final class SynchronizeCommand extends Command
2119
{
2220
public function __construct(
2321
private Clock $clock,
24-
private LastUpdateRepository $lastUpdateRepository,
25-
private PdoPhpReleaseRepository $releaseRepository,
26-
private PdoPhpVersionRepository $versionRepository,
2722
private PhpVersionFetcher $fetcher,
23+
private Storage $storage,
24+
string $name
2825
) {
29-
parent::__construct('synchronize');
26+
parent::__construct($name);
3027
}
3128

3229
protected function execute(InputInterface $input, OutputInterface $output): int
3330
{
34-
$currentTime = $this->clock->now()->format('Y-m-d H:i:s');
35-
$output->writeln("<info>[$currentTime] Process current versions</info>");
36-
$currents = $this->fetcher->currents();
37-
$this->versionRepository->save(...$currents);
31+
$versions = [];
3832

39-
$currentTime = $this->clock->now()->format('Y-m-d H:i:s');
40-
$output->writeln("<info>[$currentTime] Process unmaintened versions</info>");
41-
$eol = $this->fetcher->eol();
42-
$this->versionRepository->save(...$eol);
33+
$this->log($output, 'Fetch current versions');
34+
foreach ($this->fetcher->currents() as $version) {
35+
$versions[] = $version;
36+
}
4337

44-
$currentTime = $this->clock->now()->format('Y-m-d H:i:s');
45-
$output->writeln("<info>[$currentTime] Process releases versions</info>");
38+
$this->log($output, 'Fetch unmaintened versions');
39+
foreach ($this->fetcher->eol() as $version) {
40+
$versions[] = $version;
41+
}
42+
43+
$this->log($output, 'Fetch releases versions');
4644
$releases = $this->fetcher->releases();
47-
$this->releaseRepository->save(...$releases);
4845

49-
$this->lastUpdateRepository->save();
46+
$this->log($output, 'Write data');
47+
$this->storage->write($versions, $releases);
5048

5149
return 0;
5250
}
51+
52+
private function log(OutputInterface $output, string $text): void
53+
{
54+
$currentTime = $this->clock->now()->format('Y-m-d H:i:s');
55+
$output->writeln("<info>[$currentTime] $text</info>");
56+
}
5357
}

src/Model/Collection.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Model;
6+
7+
use JDecool\Collection\Collection as BaseCollection;
8+
9+
/**
10+
* @template TKey
11+
* @template TValue
12+
*/
13+
class Collection
14+
{
15+
public static function createEmpty(): self
16+
{
17+
return new self();
18+
}
19+
20+
public static function fromIterable(iterable $data): self
21+
{
22+
$arr = [];
23+
foreach ($data as $key => $value) {
24+
$arr[$key] = $value;
25+
}
26+
27+
return new self($arr);
28+
}
29+
30+
private function __construct(
31+
private array $data = [],
32+
) {
33+
}
34+
35+
/**
36+
* @param TKey $key
37+
* @param TValue $value
38+
*/
39+
public function add($key, $value): void
40+
{
41+
$this->data[$key] = $value;
42+
}
43+
}

src/Storage.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App;
6+
7+
interface Storage
8+
{
9+
public function write(iterable $versions, iterable $releases): void;
10+
}

src/Storage/FilesystemStorage.php

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Storage;
6+
7+
use App\Clock;
8+
use App\Model\PhpVersion;
9+
use App\Storage;
10+
use JDecool\Collection\Collection;
11+
use RuntimeException;
12+
use Symfony\Component\Serializer\SerializerInterface;
13+
14+
final class FilesystemStorage implements Storage
15+
{
16+
public function __construct(
17+
private Clock $clock,
18+
private SerializerInterface $serializer,
19+
private string $outputFolder = '',
20+
) {
21+
if (trim($this->outputFolder) === '') {
22+
$this->outputFolder = getcwd();
23+
}
24+
}
25+
26+
public function write(iterable $versions, iterable $releases): void
27+
{
28+
$this->writeAllVersionsFile($versions);
29+
$this->writeMaintenedVersions($versions);
30+
$this->writeUnmaintenedVersions($versions);
31+
$this->writeReleaseVersions($releases);
32+
}
33+
34+
private function writeAllVersionsFile(iterable $versions): void
35+
{
36+
$this->writeFile('all.json', $versions);
37+
}
38+
39+
/**
40+
* @param iterable<PhpVersion> $versions
41+
*/
42+
private function writeMaintenedVersions(iterable $versions): void
43+
{
44+
$data = (new Collection($versions))->filter(
45+
fn (PhpVersion $version): bool => $version->getEndOfLife() > $this->clock->now(),
46+
);
47+
48+
$this->writeFile('maintened.json', $data);
49+
}
50+
51+
private function writeUnmaintenedVersions(iterable $versions): void
52+
{
53+
$data = (new Collection($versions))->filter(
54+
fn (PhpVersion $version): bool => $version->getEndOfLife() <= $this->clock->now(),
55+
);
56+
57+
$this->writeFile('unmaintened.json', $data);
58+
}
59+
60+
private function writeReleaseVersions(iterable $releases): void
61+
{
62+
$this->writeFile('releases.json', $releases);
63+
}
64+
65+
private function writeFile(string $path, iterable $data): void
66+
{
67+
$fullpath = rtrim($this->outputFolder, DIRECTORY_SEPARATOR).'/'.ltrim($path, DIRECTORY_SEPARATOR);
68+
69+
$basedir = dirname($fullpath);
70+
if (!file_exists($basedir)) {
71+
@mkdir($basedir, 0777, true);
72+
}
73+
74+
if (false === file_put_contents($fullpath, ['items' => $this->serializer->serialize($data, 'json')])) {
75+
throw new RuntimeException("An error occured while writing file.");
76+
}
77+
}
78+
}

src/Storage/PDOStorage.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Storage;
6+
7+
use App\Model\PhpRelease;
8+
use App\Model\PhpVersion;
9+
use App\Repository\PDO\LastUpdateRepository;
10+
use App\Repository\PDO\PdoPhpReleaseRepository;
11+
use App\Repository\PDO\PdoPhpVersionRepository;
12+
use App\Storage;
13+
14+
final class PDOStorage implements Storage
15+
{
16+
public function __construct(
17+
private LastUpdateRepository $lastUpdateRepository,
18+
private PdoPhpReleaseRepository $releaseRepository,
19+
private PdoPhpVersionRepository $versionRepository
20+
) {
21+
}
22+
23+
/**
24+
* @param iterable<PhpVersion> $versions
25+
* @param iterable<PhpRelease> $releases
26+
*/
27+
public function write(iterable $versions, iterable $releases): void
28+
{
29+
$this->versionRepository->save(...$versions);
30+
$this->releaseRepository->save(...$releases);
31+
32+
$this->lastUpdateRepository->save();
33+
}
34+
}

0 commit comments

Comments
 (0)