44
55namespace JeroenG \Autowire ;
66
7- use App \Console \Commands \Bot \SendEndOfWeekUpdate ;
8- use App \Domain \Bot \AlarmBellInterface ;
97use Illuminate \Support \Facades \App ;
108use Illuminate \Support \Facades \Event ;
119use Illuminate \Support \Facades \File ;
1210use 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 ;
1711use JeroenG \Autowire \Console \AutowireCacheCommand ;
1812use JeroenG \Autowire \Console \AutowireClearCommand ;
19- use JeroenG \Autowire \TaggedInterface ;
13+ use JeroenG \Autowire \Testing \ CachedState ;
2014use JsonException ;
2115use 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
0 commit comments