Skip to content

Commit b9e3bfa

Browse files
committed
**WIP**
1 parent 6ad1840 commit b9e3bfa

File tree

4 files changed

+60
-7
lines changed

4 files changed

+60
-7
lines changed

build-container.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#!/usr/bin/env php
2+
<?php
3+
4+
declare(strict_types=1);
5+
6+
use App\Aoc\Discovery\ImplementationsDiscovery;
7+
use App\Aoc\Runner\InputParser;
8+
use App\Aoc\Solution;
9+
use loophp\collection\Collection;
10+
11+
require_once './vendor/autoload.php';
12+
13+
/** @var ImplementationsDiscovery $implementationsDiscovery */
14+
$implementationsDiscovery = require __DIR__ . '/config/implementationsDiscovery.php';
15+
16+
$map = [
17+
InputParser::class => __DIR__ . '/config/cache/inputParsers.php',
18+
Solution::class => __DIR__ . '/config/cache/solutions.php',
19+
];
20+
21+
foreach ($map as $type => $target) {
22+
is_dir($dir = dirname($target)) || mkdir($dir, recursive: true);
23+
$implementations = Collection::fromIterable($implementationsDiscovery->findImplementations($type))
24+
->sort(
25+
callback: static fn (ReflectionClass $alpha, ReflectionClass $bravo) => $alpha->getName() <=> $bravo->getName(),
26+
);
27+
28+
$uses = $implementations
29+
->map(static fn (ReflectionClass $class) => sprintf('use %s;', $class->getName()))
30+
->implode("\n");
31+
32+
$contents = $implementations
33+
->map(static fn (ReflectionClass $class) => sprintf(' %s::class,', $class->getShortName()))
34+
->implode("\n");
35+
file_put_contents(
36+
$target,
37+
sprintf(
38+
<<<EOF
39+
<?php
40+
declare(strict_types=1);
41+
42+
%s
43+
44+
return [
45+
%s
46+
];
47+
48+
EOF,
49+
$uses,
50+
$contents,
51+
),
52+
);
53+
}

config/cache/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*

src/Aoc/Discovery/ImplementationsDiscovery.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ public function findImplementations(string $interface): array
3232
{
3333
return $this->classes
3434
->filter(static fn (ReflectionClass $ref) => $ref->implementsInterface($interface))
35-
->map(static fn (ReflectionClass $ref) => $ref->newInstance())
3635
->all();
3736
}
3837

src/Aoc/SolutionFactory.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace App\Aoc;
66

77
use App\Aoc\Discovery\ImplementationsDiscovery;
8+
use loophp\collection\Collection;
89
use RuntimeException;
910

1011
final readonly class SolutionFactory
@@ -15,12 +16,11 @@ public function __construct(private ImplementationsDiscovery $implementations)
1516

1617
public function make(Challenge $challenge): Solution
1718
{
18-
$supports = static fn (Solution $solution) => collect($solution->challenges())->contains(
19-
static fn (Challenge $supports) => $supports->equals($challenge),
20-
);
19+
$supports = static fn (Solution $solution) => null !== Collection::fromIterable($solution->challenges())
20+
->find(callbacks: static fn (Challenge $supports) => $supports->equals($challenge));
2121

2222
return $this->getSolutions()
23-
->first(static fn (Solution $solution) => $supports($solution))
23+
->find(callbacks: static fn (Solution $solution) => $supports($solution))
2424
?? throw new RuntimeException(sprintf('No solution for %s', $challenge));
2525
}
2626

@@ -35,8 +35,8 @@ public function mostRecentChallenge(): Challenge
3535
?? throw new RuntimeException('No implementations yet');
3636
}
3737

38-
public function getSolutions(): \Illuminate\Support\Collection
38+
public function getSolutions(): Collection
3939
{
40-
return collect($this->implementations->findImplementations(Solution::class));
40+
return Collection::fromIterable($this->implementations->findImplementations(Solution::class));
4141
}
4242
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy