-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from Jeroen-G/caching
Add ability to cache autowiring and configurations
- Loading branch information
Showing
7 changed files
with
166 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace JeroenG\Autowire\Console; | ||
|
||
use Illuminate\Console\Command; | ||
use Illuminate\Support\Facades\App; | ||
use Illuminate\Support\Facades\File; | ||
use JeroenG\Autowire\Crawler; | ||
use JeroenG\Autowire\Electrician; | ||
|
||
class AutowireCacheCommand extends Command | ||
{ | ||
protected $signature = 'autowire:cache'; | ||
|
||
protected $description = 'Cache the autowiring and configurations.'; | ||
|
||
public function handle(): int | ||
{ | ||
$crawler = Crawler::in(config('autowire.directories')); | ||
$electrician = new Electrician($crawler); | ||
|
||
$autowires = $crawler->filter(fn(string $name) => $electrician->canAutowire($name))->classNames(); | ||
$configures = $crawler->filter(fn(string $name) => $electrician->canConfigure($name))->classNames(); | ||
$autowireCache = []; | ||
$configureCache = []; | ||
|
||
foreach ($autowires as $interface) { | ||
$wire = $electrician->connect($interface); | ||
$autowireCache[$wire->interface] = $wire->implementation; | ||
} | ||
|
||
foreach ($configures as $implementation) { | ||
$configuration = $electrician->configure($implementation); | ||
|
||
foreach ($configuration->definitions as $definition) { | ||
$configureCache[$implementation] = [ | ||
'type' => $definition->type, | ||
'need' => $definition->need, | ||
'give' => $definition->give, | ||
]; | ||
} | ||
} | ||
|
||
$cache = [ | ||
'autowire' => $autowireCache, | ||
'configure' => $configureCache, | ||
]; | ||
|
||
File::put(App::bootstrapPath('cache/autowire.json'), json_encode($cache, JSON_THROW_ON_ERROR)); | ||
|
||
$this->info('Autowire cache created!'); | ||
return 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace JeroenG\Autowire\Console; | ||
|
||
use Illuminate\Console\Command; | ||
use Illuminate\Support\Facades\App; | ||
use Illuminate\Support\Facades\File; | ||
use JeroenG\Autowire\Crawler; | ||
use JeroenG\Autowire\Electrician; | ||
|
||
class AutowireClearCommand extends Command | ||
{ | ||
protected $signature = 'autowire:clear'; | ||
|
||
protected $description = 'Clear the cache.'; | ||
|
||
public function handle(): int | ||
{ | ||
$deleted = File::delete(App::bootstrapPath('cache/autowire.json')); | ||
|
||
if (!$deleted) { | ||
$this->error('Could not clear cache.'); | ||
return 1; | ||
} | ||
|
||
$this->info('Autowire cache cleared!'); | ||
return 0; | ||
} | ||
} |