Skip to content

Commit

Permalink
🎨 Improve the cache manifest implementation (#222)
Browse files Browse the repository at this point in the history
  • Loading branch information
Log1x committed Mar 1, 2024
1 parent bf075f9 commit f70d3b1
Show file tree
Hide file tree
Showing 8 changed files with 151 additions and 94 deletions.
89 changes: 8 additions & 81 deletions src/AcfComposer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Log1x\AcfComposer;

use Illuminate\Support\Collection;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
use ReflectionClass;
Expand Down Expand Up @@ -39,11 +38,9 @@ class AcfComposer
protected array $plugins = [];

/**
* The cached manifest.
*
* @var \Illuminate\Support\Collection
* The cache manifest.
*/
protected $manifest;
protected Manifest $manifest;

/**
* The composer classes.
Expand All @@ -61,6 +58,7 @@ class AcfComposer
public function __construct(Application $app)
{
$this->app = $app;
$this->manifest = Manifest::make($this);
}

/**
Expand Down Expand Up @@ -194,103 +192,32 @@ public function registerPlugin(string $path, string $namespace): void
/**
* Retrieve the registered composers.
*/
public function getComposers(): array
public function composers(): array
{
return $this->composers;
}

/**
* Retrieve the registered paths.
*/
public function getPaths(): array
public function paths(): array
{
return array_unique($this->paths);
}

/**
* Retrieve the registered plugins.
*/
public function getPlugins(): array
public function plugins(): array
{
return $this->plugins;
}

/**
* Retrieve the cache manifest.
*/
protected function manifest(): Collection
{
if ($this->manifest) {
return $this->manifest;
}

if (! $this->manifestExists()) {
return $this->manifest = collect();
}

return $this->manifest = Collection::make(require $this->manifestPath());
}

/**
* Retrieve the cache manifest path.
*/
protected function manifestPath(): string
{
$path = config('acf-composer.manifest', storage_path('framework/cache'));

if (Str::endsWith($path, '.php')) {
return $path;
}

return Str::finish($path, '/').'acf-composer.php';
}

/**
* Determine if the cache manifest exists.
*/
public function manifestExists(): bool
{
return file_exists($this->manifestPath());
}

/**
* Cache the composer.
*/
public function cache(Composer $composer): bool
{
$manifest = $this->manifest()
->put($composer::class, $composer->getFields(cache: false))
->all();

return file_put_contents(
$this->manifestPath(),
'<?php return '.var_export($manifest, true).';'
) !== false;
}

/**
* Retrieve the cached composer.
*/
public function getCache(Composer $composer): array
{
return $this->manifest()->get($composer::class);
}

/**
* Determine if the composer is cached.
*/
public function hasCache(Composer $class): bool
{
return $this->manifest()->has($class::class);
}

/**
* Clear the cache.
*/
public function clearCache(): bool
public function manifest(): Manifest
{
return $this->manifestExists()
? unlink($this->manifestPath())
: false;
return $this->manifest;
}
}
4 changes: 2 additions & 2 deletions src/Composer.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ public function getFields(bool $cache = true): array
return $this->fields;
}

if ($cache && $this->composer->hasCache($this)) {
return $this->composer->getCache($this);
if ($cache && $this->composer->manifest()->has($this)) {
return $this->composer->manifest()->get($this);
}

$fields = is_a($fields = $this->fields(), FieldsBuilder::class)
Expand Down
21 changes: 15 additions & 6 deletions src/Console/CacheCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ class CacheCommand extends Command
*/
protected $signature = 'acf:cache
{--clear : Clear the cached field groups}
{--status : Show the current cache status}';
{--status : Show the current cache status}
{--force : Force cache the field groups}';

/**
* The console command description.
Expand Down Expand Up @@ -45,25 +46,33 @@ public function handle()
}

if ($this->option('status')) {
return $this->composer->manifestExists()
return $this->composer->manifest()->exists()
? $this->components->info('The <fg=blue>ACF Composer</> field groups are currently <fg=green;options=bold>cached</>.')
: $this->components->info('The <fg=blue>ACF Composer</> field groups are currently <fg=red;options=bold>not cached</>.');
}

$composers = collect(
$this->composer->getComposers()
$this->composer->composers()
)->flatten();

$composers->each(function ($composer) {
if (! $this->composer->cache($composer)) {
$this->components->error('<fg=red>'.$composer::class.'</fg> failed to cache.');
if (! $this->composer->manifest()->add($composer)) {
$name = $composer::class;

return;
return $this->components->error("Failed to add <fg=red>{$name}</> to the manifest.");
}

$this->count++;
});

if ($this->count !== $composers->count() && ! $this->option('force')) {
return $this->components->error('Failed to cache the <fg=red>ACF Composer</> field groups.');
}

if (! $this->composer->manifest()->write()) {
return $this->components->error('Failed to write the <fg=red>ACF Composer</> manifest.');
}

$this->components->info("<fg=blue>{$this->count}</> field group(s) cached successfully.");
}
}
2 changes: 1 addition & 1 deletion src/Console/ClearCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function handle()
{
$this->composer = $this->laravel['AcfComposer'];

return $this->composer->clearCache()
return $this->composer->manifest()->delete()
? $this->components->info('Successfully cleared the <fg=blue>ACF Composer</> cache manifest.')
: $this->components->info('The <fg=blue>ACF Composer</> cache manifest is already cleared.');
}
Expand Down
2 changes: 1 addition & 1 deletion src/Console/UpgradeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function handle()

$this->components->info('Checking for outdated <fg=blue>ACF Composer</> classes...');

$classes = collect($this->composer->getPaths())->flatMap(fn ($classes, $path) => collect($classes)
$classes = collect($this->composer->paths())->flatMap(fn ($classes, $path) => collect($classes)
->map(fn ($class) => Str::of($class)->replace('\\', '/')->after('/')->start($path.'/')->finish('.php')->toString())
->all()
)
Expand Down
4 changes: 2 additions & 2 deletions src/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ public function getFields(bool $cache = true): array
return $this->fields;
}

if ($cache && $this->composer->hasCache($this)) {
return $this->composer->getCache($this);
if ($cache && $this->composer->manifest()->has($this)) {
return $this->composer->manifest()->get($this);
}

$fields = $this->fields();
Expand Down
121 changes: 121 additions & 0 deletions src/Manifest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<?php

namespace Log1x\AcfComposer;

use Exception;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;

class Manifest
{
/**
* The ACF Composer instance.
*/
protected AcfComposer $composer;

/**
* The manifest collection.
*/
protected Collection $manifest;

/**
* Create a new Manifest instance.
*/
public function __construct(AcfComposer $composer)
{
$this->composer = $composer;

$this->manifest = $this->exists()
? Collection::make(require $this->path())
: Collection::make();
}

/**
* Make a new instance of Manifest.
*/
public static function make(AcfComposer $composer): self
{
return new static($composer);
}

/**
* Retrieve the manifest path.
*/
protected function path(): string
{
$path = config('acf-composer.manifest', storage_path('framework/cache'));

if (Str::endsWith($path, '.php')) {
return $path;
}

return Str::finish($path, '/').'acf-composer.php';
}

/**
* Determine if the manifest exists.
*/
public function exists(): bool
{
return file_exists($this->path());
}

/**
* Add the Composer to the manifest.
*/
public function add(Composer $composer): bool
{
try {
$this->manifest->put($composer::class, $composer->getFields(cache: false));

return true;
} catch (Exception) {
return false;
}
}

/**
* Retrieve the cached Composer from the manifest.
*/
public function get(Composer $composer): array
{
return $this->manifest->get($composer::class);
}

/**
* Determine if the Composer is cached.
*/
public function has(Composer $class): bool
{
return $this->manifest->has($class::class);
}

/**
* Write the the manifest to disk.
*/
public function write(): bool
{
return file_put_contents(
$this->path(),
'<?php return '.var_export($this->toArray(), true).';'
) !== false;
}

/**
* Delete the manifest from disk.
*/
public function delete(): bool
{
return $this->exists()
? unlink($this->path())
: false;
}

/**
* Retrieve the manifest as an array.
*/
public function toArray(): array
{
return $this->manifest->all();
}
}
2 changes: 1 addition & 1 deletion src/Providers/AcfComposerServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function boot()

if (class_exists(AboutCommand::class) && class_exists(InstalledVersions::class)) {
AboutCommand::add('ACF Composer', [
'Status' => $composer->manifestExists() ? '<fg=green;options=bold>CACHED</>' : '<fg=yellow;options=bold>NOT CACHED</>',
'Status' => $composer->manifest()->exists() ? '<fg=green;options=bold>CACHED</>' : '<fg=yellow;options=bold>NOT CACHED</>',
'Version' => InstalledVersions::getPrettyVersion('log1x/acf-composer'),
]);
}
Expand Down

0 comments on commit f70d3b1

Please sign in to comment.