File tree Expand file tree Collapse file tree 4 files changed +60
-7
lines changed Expand file tree Collapse file tree 4 files changed +60
-7
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
1
+ *
Original file line number Diff line number Diff line change @@ -32,7 +32,6 @@ public function findImplementations(string $interface): array
32
32
{
33
33
return $ this ->classes
34
34
->filter (static fn (ReflectionClass $ ref ) => $ ref ->implementsInterface ($ interface ))
35
- ->map (static fn (ReflectionClass $ ref ) => $ ref ->newInstance ())
36
35
->all ();
37
36
}
38
37
Original file line number Diff line number Diff line change 5
5
namespace App \Aoc ;
6
6
7
7
use App \Aoc \Discovery \ImplementationsDiscovery ;
8
+ use loophp \collection \Collection ;
8
9
use RuntimeException ;
9
10
10
11
final readonly class SolutionFactory
@@ -15,12 +16,11 @@ public function __construct(private ImplementationsDiscovery $implementations)
15
16
16
17
public function make (Challenge $ challenge ): Solution
17
18
{
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 ));
21
21
22
22
return $ this ->getSolutions ()
23
- ->first ( static fn (Solution $ solution ) => $ supports ($ solution ))
23
+ ->find (callbacks: static fn (Solution $ solution ) => $ supports ($ solution ))
24
24
?? throw new RuntimeException (sprintf ('No solution for %s ' , $ challenge ));
25
25
}
26
26
@@ -35,8 +35,8 @@ public function mostRecentChallenge(): Challenge
35
35
?? throw new RuntimeException ('No implementations yet ' );
36
36
}
37
37
38
- public function getSolutions (): \ Illuminate \ Support \ Collection
38
+ public function getSolutions (): Collection
39
39
{
40
- return collect ($ this ->implementations ->findImplementations (Solution::class));
40
+ return Collection:: fromIterable ($ this ->implementations ->findImplementations (Solution::class));
41
41
}
42
42
}
You can’t perform that action at this time.
0 commit comments