Skip to content
Draft
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
36 changes: 36 additions & 0 deletions app/Commands/PresetListCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace App\Commands;

use App\Services\PresetManifest;
use LaravelZero\Framework\Commands\Command;
use Symfony\Component\Console\Attribute\AsCommand;

#[AsCommand('preset:list', 'List all available presets')]
class PresetListCommand extends Command
{
public function handle(PresetManifest $presetManifest): int
{
$presets = $presetManifest->names();

if ($presets === []) {
$this->components->warn('No presets found.');

return self::SUCCESS;
}

$this->newLine();
$this->components->twoColumnDetail(
'<fg=green;options=bold>Preset</>',
'<fg=yellow;options=bold>Path</>',
);

foreach ($presets as $preset) {
$path = $presetManifest->path($preset);
$presets[$preset] = $path;
$this->components->twoColumnDetail($preset, $path);
}

return self::SUCCESS;
}
}
27 changes: 6 additions & 21 deletions app/Factories/ConfigurationResolverFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,14 @@

use App\Project;
use App\Repositories\ConfigurationJsonRepository;
use App\Services\PresetManifest;
use ArrayIterator;
use PhpCsFixer\Config;
use PhpCsFixer\Console\ConfigurationResolver;
use PhpCsFixer\ToolInfo;

class ConfigurationResolverFactory
{
/**
* The list of available presets.
*
* @var array<int, string>
*/
public static $presets = [
'laravel',
'per',
'psr12',
'symfony',
'empty',
];

/**
* Creates a new PHP CS Fixer Configuration Resolver instance
* from the given input and output.
Expand All @@ -37,23 +25,20 @@ public static function fromIO($input, $output)
$path = Project::paths($input);

$localConfiguration = resolve(ConfigurationJsonRepository::class);
$presetManifest = resolve(PresetManifest::class);

$preset = $localConfiguration->preset();

if (! in_array($preset, static::$presets)) {
abort(1, 'Preset not found.');
if (! $presetManifest->has($preset)) {
$availablePresets = implode(', ', $presetManifest->names());
abort(1, "Preset '{$preset}' not found. Available presets: {$availablePresets}");
}

$resolver = new ConfigurationResolver(
new Config('default'),
[
'allow-risky' => 'yes',
'config' => implode(DIRECTORY_SEPARATOR, [
dirname(__DIR__, 2),
'resources',
'presets',
sprintf('%s.php', $preset),
]),
'config' => $presetManifest->path($preset),
'diff' => $output->isVerbose(),
'dry-run' => $input->getOption('test') || $input->getOption('bail'),
'path' => $path,
Expand Down
26 changes: 16 additions & 10 deletions app/Output/SummaryOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

use App\Output\Concerns\InteractsWithSymbols;
use App\Project;
use App\Services\PresetManifest;
use App\ValueObjects\Issue;
use Illuminate\Support\Str;
use PhpCsFixer\Runner\Event\FileProcessed;

use function Termwind\render;
Expand All @@ -15,17 +17,21 @@ class SummaryOutput
use InteractsWithSymbols;

/**
* The list of presets, in a human-readable format.
* Get the list of presets in a human-readable format.
*
* @var array<string, string>
* @return array<string, string>
*/
protected $presets = [
'per' => 'PER',
'psr12' => 'PSR 12',
'laravel' => 'Laravel',
'symfony' => 'Symfony',
'empty' => 'Empty',
];
protected function getPresets(): array
{
$presetManifest = resolve(PresetManifest::class);
$presets = [];

foreach ($presetManifest->names() as $preset) {
$presets[$preset] = Str::headline($preset);
}

return [...$presets, 'per' => 'PER', 'psr12' => 'PSR 12'];
}

/**
* Creates a new Summary Output instance.
Expand Down Expand Up @@ -63,7 +69,7 @@ public function handle($summary, $totalFiles)
'totalFiles' => $totalFiles,
'issues' => $issues,
'testing' => $summary->isDryRun(),
'preset' => $this->presets[$this->config->preset()],
'preset' => $this->getPresets()[$this->config->preset()] ?? $this->config->preset(),
]),
);

Expand Down
9 changes: 9 additions & 0 deletions app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Providers;

use App\Services\PresetManifest;
use Illuminate\Support\ServiceProvider;
use PhpCsFixer\Error\ErrorsManager;
use Symfony\Component\EventDispatcher\EventDispatcher;
Expand Down Expand Up @@ -32,5 +33,13 @@ public function register()
$this->app->singleton(EventDispatcher::class, function () {
return new EventDispatcher;
});

$this->app->singleton(PresetManifest::class, function ($app) {
return new PresetManifest(
$app->make('files'),
$app->basePath(),
$app->basePath('bootstrap/cache/pint_presets.php'),
);
});
}
}
132 changes: 132 additions & 0 deletions app/Services/PresetManifest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
<?php

namespace App\Services;

use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Collection;

class PresetManifest
{
/** @var ?array<string, string> */
protected ?array $manifest = null;

protected string $vendorPath;

public function __construct(
protected Filesystem $files,
protected string $basePath,
protected string $manifestPath,
) {
$this->vendorPath = $this->basePath.'/vendor';
}

/**
* Get all available presets from packages.
*
* @return array<string, string> ['preset-name' => '/absolute/path/to/preset.php']
*/
public function presets(): array
{
return $this->manifest ??= $this->getManifest();
}

/**
* Check if a preset exists.
*/
public function has(string $preset): bool
{
return array_key_exists($preset, $this->presets());
}

/**
* Get the path for a specific preset.
*/
public function path(string $preset): ?string
{
return $this->presets()[$preset] ?? null;
}

/**
* Get all preset names.
*
* @return list<string>
*/
public function names(): array
{
return array_keys($this->presets());
}

/**
* Get the current preset manifest.
*
* @return array<string, string>
*/
protected function getManifest(): array
{
$path = $this->vendorPath.'/composer/installed.json';

if (
! $this->files->exists($this->manifestPath) ||
$this->files->lastModified($path) > $this->files->lastModified($this->manifestPath)
) {
return $this->build();
}

return $this->files->getRequire($this->manifestPath);
}

/**
* Build the manifest and write it to disk.
*
* @return array<string, string>
*/
protected function build(): array
{
$packages = [];
$installedPath = $this->vendorPath.'/composer/installed.json';
$composerPath = $this->basePath.'/composer.json';

if ($this->files->exists($installedPath)) {
$installed = json_decode($this->files->get($installedPath), true);
$packages = $installed['packages'] ?? $installed;
}

$presets = (new Collection($packages))
->keyBy(fn ($package) => $this->vendorPath.'/'.$package['name'])
->when($this->files->exists($composerPath), function ($presets) use ($composerPath) {
$composer = json_decode($this->files->get($composerPath), true);

return $presets->put($this->basePath, $composer);
})
->map(fn ($package) => $package['extra']['laravel-pint']['presets'] ?? [])
->flatMap(function (array $presets, string $basePath): array {
foreach ($presets as $name => $relativePath) {
$absolutePath = $basePath.'/'.$relativePath;

if ($this->files->exists($absolutePath)) {
$presets[$name] = $absolutePath;
} else {
unset($presets[$name]);
}
}

return $presets;
})
->all();

$this->write($presets);

return $presets;
}

/**
* Write the given manifest array to disk.
*
* @param array<string, string> $manifest
*/
protected function write(array $manifest): void
{
$this->files->ensureDirectoryExists(dirname($this->manifestPath), 0755, true);
$this->files->replace($this->manifestPath, '<?php return '.var_export($manifest, true).';');
}
}
2 changes: 2 additions & 0 deletions bootstrap/cache/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*
!.gitignore
11 changes: 11 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,17 @@
"pestphp/pest-plugin": true
}
},
"extra": {
"laravel-pint": {
"presets": {
"laravel": "resources/presets/laravel.php",
"per": "resources/presets/per.php",
"psr12": "resources/presets/psr12.php",
"symfony": "resources/presets/symfony.php",
"empty": "resources/presets/empty.php"
}
}
},
"minimum-stability": "dev",
"prefer-stable": true,
"bin": ["builds/pint"]
Expand Down
23 changes: 23 additions & 0 deletions tests/Feature/PresetDiscoveryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

use App\Services\PresetManifest;

it('can resolve preset paths', function () {
$presetManifest = resolve(PresetManifest::class);

expect($presetManifest->has('laravel'))->toBeTrue();
expect($presetManifest->path('laravel'))->toContain('resources/presets/laravel.php');

expect($presetManifest->has('nonexistent'))->toBeFalse();
expect($presetManifest->path('nonexistent'))->toBeNull();
});

it('can list available presets', function () {
$this->artisan('preset:list')
->expectsOutputToContain('laravel')
->expectsOutputToContain('per')
->expectsOutputToContain('psr12')
->expectsOutputToContain('symfony')
->expectsOutputToContain('empty')
->assertSuccessful();
});
Loading