Skip to content

Commit f7209c4

Browse files
XammieJeroen-G
authored andcommitted
Add trait WithCachedAutowire to cache the autowire config during testing
1 parent b2dee57 commit f7209c4

5 files changed

Lines changed: 106 additions & 103 deletions

File tree

src/AutowireServiceProvider.php

Lines changed: 12 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,13 @@
44

55
namespace JeroenG\Autowire;
66

7-
use App\Console\Commands\Bot\SendEndOfWeekUpdate;
8-
use App\Domain\Bot\AlarmBellInterface;
97
use Illuminate\Support\Facades\App;
108
use Illuminate\Support\Facades\Event;
119
use Illuminate\Support\Facades\File;
1210
use Illuminate\Support\ServiceProvider;
13-
use JeroenG\Autowire\Attribute\Tag as TagAttribute;
14-
use JeroenG\Autowire\Attribute\Autowire as AutowireAttribute;
15-
use JeroenG\Autowire\Attribute\Configure as ConfigureAttribute;
16-
use JeroenG\Autowire\Attribute\Listen as ListenAttribute;
1711
use JeroenG\Autowire\Console\AutowireCacheCommand;
1812
use JeroenG\Autowire\Console\AutowireClearCommand;
19-
use JeroenG\Autowire\TaggedInterface;
13+
use JeroenG\Autowire\Testing\CachedState;
2014
use JsonException;
2115
use Illuminate\Contracts\Filesystem\FileNotFoundException;
2216

@@ -33,11 +27,18 @@ public function register(): void
3327
{
3428
$this->mergeConfigFrom(__DIR__ . '/../config/autowire.php', 'autowire');
3529

30+
if (CachedState::$cachedAutowire !== null) {
31+
$this->loadFromCache(CachedState::$cachedAutowire);
32+
33+
return;
34+
}
35+
3636
try {
3737
$cache = File::getRequire(App::bootstrapPath('cache/autowire.php'));
3838
$this->loadFromCache($cache);
3939
} catch (FileNotFoundException|JsonException) {
40-
$this->crawlAndLoad();
40+
$cache = CacheBuilder::factory()->build();
41+
$this->loadFromCache($cache);
4142
}
4243
}
4344

@@ -78,49 +79,9 @@ private function loadFromCache(array $cache): void
7879
$this->define($implementation, $definition);
7980
}
8081

81-
array_walk($tagCache, function (TaggedInterface $taggedInterface): void {$this->app->tag($taggedInterface->implementations, $taggedInterface->tag);});
82-
}
83-
84-
private function crawlAndLoad(): void
85-
{
86-
$crawler = Crawler::in(config('autowire.directories'));
87-
$autowireAttribute = config('autowire.autowire_attribute', AutowireAttribute::class);
88-
$configureAttribute = config('autowire.configure_attribute', ConfigureAttribute::class);
89-
$listenAttribute = config('autowire.listen_attribute', ListenAttribute::class);
90-
$tagAttribute = config('autowire.tag_attribute', TagAttribute::class);
91-
$electrician = new Electrician($crawler, $autowireAttribute, $configureAttribute, $listenAttribute, $tagAttribute,);
92-
93-
$wires = $crawler->filter(fn (string $name) => $electrician->canAutowire($name))->classNames();
94-
$listeners = $crawler->filter(fn (string $name) => $electrician->canListen($name))->classNames();
95-
$configures = $crawler->filter(fn (string $name) => $electrician->canConfigure($name))->classNames();
96-
$taggables = $crawler->filter(fn (string $name) => $electrician->canTag($name))->classNames();
97-
98-
foreach ($wires as $interface) {
99-
$wire = $electrician->connect($interface);
100-
$this->app->bindIf($wire->interface, $wire->implementation);
101-
}
102-
103-
foreach ($listeners as $listener) {
104-
$events = $electrician->events($listener);
105-
106-
Event::listen($events, $listener);
107-
}
108-
109-
foreach ($configures as $implementation) {
110-
$configuration = $electrician->configure($implementation);
111-
112-
foreach ($configuration->definitions as $definition) {
113-
$this->define($implementation, $definition);
114-
}
115-
}
116-
117-
array_walk(
118-
$taggables,
119-
function (string $interface) use ($electrician): void {
120-
$taggedInterface = $electrician->tag($interface);
121-
$this->app->tag($taggedInterface->implementations, $taggedInterface->tag);
122-
},
123-
);
82+
array_walk($tagCache, function (TaggedInterface $taggedInterface): void {
83+
$this->app->tag($taggedInterface->implementations, $taggedInterface->tag);
84+
});
12485
}
12586

12687
private function define(string $implementation, ConfigurationValue $definition): void

src/CacheBuilder.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace JeroenG\Autowire;
4+
5+
use JeroenG\Autowire\Attribute\Autowire as AutowireAttribute;
6+
use JeroenG\Autowire\Attribute\Configure as ConfigureAttribute;
7+
use JeroenG\Autowire\Attribute\Listen as ListenAttribute;
8+
use JeroenG\Autowire\Attribute\Tag as TagAttribute;
9+
10+
class CacheBuilder
11+
{
12+
public static function factory(): self
13+
{
14+
return new self();
15+
}
16+
17+
public function build(): array {
18+
$crawler = Crawler::in(config('autowire.directories'));
19+
$autowireAttribute = config('autowire.autowire_attribute', AutowireAttribute::class);
20+
$configureAttribute = config('autowire.configure_attribute', ConfigureAttribute::class);
21+
$listenAttribute = config('autowire.listen_attribute', ListenAttribute::class);
22+
$tagAttribute = config('autowire.tag_attribute', TagAttribute::class);
23+
$electrician = new Electrician($crawler, $autowireAttribute, $configureAttribute, $listenAttribute, $tagAttribute);
24+
25+
$autowires = $crawler->filter(fn(string $name) => $electrician->canAutowire($name))->classNames();
26+
$listeners = $crawler->filter(fn(string $name) => $electrician->canListen($name))->classNames();
27+
$configures = $crawler->filter(fn(string $name) => $electrician->canConfigure($name))->classNames();
28+
$taggables = $crawler->filter(fn (string $name) => $electrician->canTag($name))->classNames();
29+
30+
$autowireCache = [];
31+
$listenCache = [];
32+
$configureCache = [];
33+
34+
foreach ($autowires as $interface) {
35+
$wire = $electrician->connect($interface);
36+
$autowireCache[$wire->interface] = $wire->implementation;
37+
}
38+
39+
foreach ($listeners as $listener) {
40+
$listenCache[$listener] = $electrician->events($listener);
41+
}
42+
43+
foreach ($configures as $implementation) {
44+
$configuration = $electrician->configure($implementation);
45+
46+
foreach ($configuration->definitions as $definition) {
47+
$configureCache[$implementation] = [
48+
'type' => $definition->type,
49+
'need' => $definition->need,
50+
'give' => $definition->give,
51+
];
52+
}
53+
}
54+
55+
$tagCache = array_values(array_map(fn (string $interface): TaggedInterface => $electrician->tag($interface), $taggables));
56+
57+
return [
58+
'autowire' => $autowireCache,
59+
'listen' => $listenCache,
60+
'configure' => $configureCache,
61+
'tag' => $tagCache,
62+
];
63+
}
64+
}

src/Console/AutowireCacheCommand.php

Lines changed: 2 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,7 @@
55
use Illuminate\Console\Command;
66
use Illuminate\Support\Facades\App;
77
use Illuminate\Support\Facades\File;
8-
use JeroenG\Autowire\Attribute\Autowire as AutowireAttribute;
9-
use JeroenG\Autowire\Attribute\Tag as TagAttribute;
10-
use JeroenG\Autowire\Attribute\Configure as ConfigureAttribute;
11-
use JeroenG\Autowire\Attribute\Listen as ListenAttribute;
12-
use JeroenG\Autowire\Crawler;
13-
use JeroenG\Autowire\Electrician;
14-
use JeroenG\Autowire\TaggedInterface;
8+
use JeroenG\Autowire\CacheBuilder;
159

1610
class AutowireCacheCommand extends Command
1711
{
@@ -21,51 +15,7 @@ class AutowireCacheCommand extends Command
2115

2216
public function handle(): int
2317
{
24-
$crawler = Crawler::in(config('autowire.directories'));
25-
$autowireAttribute = config('autowire.autowire_attribute', AutowireAttribute::class);
26-
$configureAttribute = config('autowire.configure_attribute', ConfigureAttribute::class);
27-
$listenAttribute = config('autowire.listen_attribute', ListenAttribute::class);
28-
$tagAttribute = config('autowire.tag_attribute', TagAttribute::class);
29-
$electrician = new Electrician($crawler, $autowireAttribute, $configureAttribute, $listenAttribute, $tagAttribute);
30-
31-
$autowires = $crawler->filter(fn(string $name) => $electrician->canAutowire($name))->classNames();
32-
$listeners = $crawler->filter(fn(string $name) => $electrician->canListen($name))->classNames();
33-
$configures = $crawler->filter(fn(string $name) => $electrician->canConfigure($name))->classNames();
34-
$taggables = $crawler->filter(fn (string $name) => $electrician->canTag($name))->classNames();
35-
36-
$autowireCache = [];
37-
$listenCache = [];
38-
$configureCache = [];
39-
40-
foreach ($autowires as $interface) {
41-
$wire = $electrician->connect($interface);
42-
$autowireCache[$wire->interface] = $wire->implementation;
43-
}
44-
45-
foreach ($listeners as $listener) {
46-
$listenCache[$listener] = $electrician->events($listener);
47-
}
48-
49-
foreach ($configures as $implementation) {
50-
$configuration = $electrician->configure($implementation);
51-
52-
foreach ($configuration->definitions as $definition) {
53-
$configureCache[$implementation] = [
54-
'type' => $definition->type,
55-
'need' => $definition->need,
56-
'give' => $definition->give,
57-
];
58-
}
59-
}
60-
61-
$tagCache = array_values(array_map(fn (string $interface): TaggedInterface => $electrician->tag($interface), $taggables));
62-
63-
$cache = [
64-
'autowire' => $autowireCache,
65-
'listen' => $listenCache,
66-
'configure' => $configureCache,
67-
'tag' => $tagCache,
68-
];
18+
$cache = CacheBuilder::factory()->build();
6919

7020
File::put(
7121
App::bootstrapPath('cache/autowire.php'),

src/Testing/CachedState.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace JeroenG\Autowire\Testing;
4+
5+
class CachedState
6+
{
7+
/**
8+
* This is used to store the cached autowire configuration when running tests using the `WithCachedAutowire` trait
9+
*/
10+
public static ?array $cachedAutowire = null;
11+
}

src/Testing/WithCachedAutowire.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace JeroenG\Autowire\Testing;
4+
5+
use JeroenG\Autowire\CacheBuilder;
6+
7+
trait WithCachedAutowire
8+
{
9+
public function setUpWithCachedAutowire(): void
10+
{
11+
if (CachedState::$cachedAutowire === null) {
12+
$cache = CacheBuilder::factory()->build();
13+
14+
CachedState::$cachedAutowire = $cache;
15+
}
16+
}
17+
}

0 commit comments

Comments
 (0)