Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Add an acf:ide-helpers command to generate IDE helpers for configured custom field types #201

Merged
merged 2 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 107 additions & 0 deletions src/Console/IdeHelpersCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php

namespace Log1x\AcfComposer\Console;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
use Log1x\AcfComposer\AcfComposer;

class IdeHelpersCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'acf:ide-helpers
{--path= : The path to generate the IDE helpers}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Generate IDE helpers for configured custom field types';

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

/**
* Execute the console command.
*/
public function handle()
{
$types = config('acf.types', []);

if (! $types) {
return $this->components->info('You <fg=blue>do not</> have any configured custom field types.');
}

$methods = [];

foreach ($types as $key => $value) {
$key = Str::studly($key);

$methods[] = "* @method \Log1x\AcfComposer\Builder\FieldBuilder add{$key}(string \$name, array \$args = [])";
}

$methods = implode("\n\t ", $methods);

$path = $this->option('path') ?
base_path($this->option('path')) :
__DIR__.'/../../_ide-helpers.php';

$builder = <<<EOT
namespace Log1x\AcfComposer {
/**
{$methods}
*
* @codeCoverageIgnore
*/
class Builder
{
}
}
EOT;

$fieldBuilder = <<<EOT
namespace Log1x\AcfComposer\Builder {
/**
{$methods}
*
* @codeCoverageIgnore
*/
class FieldBuilder
{
}

/**
{$methods}
*
* @codeCoverageIgnore
*/
class GroupBuilder
{
}

/**
{$methods}
*
* @codeCoverageIgnore
*/
class RepeaterBuilder
{
}
}
EOT;

File::ensureDirectoryExists(dirname($path));

File::put($path, "<?php\n\n{$builder}\n\n{$fieldBuilder}");

$this->components->info('The <fg=blue>IDE helpers</> have been successfully generated.');
}
}
1 change: 1 addition & 0 deletions src/Providers/AcfComposerServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public function boot()
Console\CacheCommand::class,
Console\ClearCommand::class,
Console\FieldMakeCommand::class,
Console\IdeHelpersCommand::class,
Console\OptionsMakeCommand::class,
Console\PartialMakeCommand::class,
Console\StubPublishCommand::class,
Expand Down