Skip to content

Commit 0995e71

Browse files
committed
wip BladeFormatterPreCommitHook implemented
1 parent 57de3d5 commit 0995e71

File tree

3 files changed

+62
-4
lines changed

3 files changed

+62
-4
lines changed

config/git-hooks.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,10 @@
178178
'phpcbf_path' => env('PHPCBF_PATH', 'vendor/bin/phpcbf'),
179179
'standard' => env('PHPCS_STANDARD', 'phpcs.xml'),
180180
],
181+
'blade_formatter' => [
182+
'path' => env('BLADE_FORMATTER_PATH', 'node_modules/.bin/blade-formatter'),
183+
// Create your own .bladeformatterrc.json file in the root of your project
184+
],
181185
],
182186

183187
];

src/Console/Commands/Hooks/BaseCodeAnalyzerPreCommitHook.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Closure;
66
use Igorsgm\GitHooks\Exceptions\HookFailException;
77
use Igorsgm\GitHooks\Facades\GitHooks;
8+
use Igorsgm\GitHooks\Git\ChangedFile;
89
use Igorsgm\GitHooks\Git\ChangedFiles;
910
use Igorsgm\GitHooks\Traits\ProcessHelper;
1011
use Illuminate\Console\Command;
@@ -22,8 +23,9 @@ abstract class BaseCodeAnalyzerPreCommitHook
2223
public $command;
2324

2425
/*
25-
* List of files extensions that will be analyzed by the hook
26-
* @var array
26+
* List of files extensions that will be analyzed by the hook.
27+
* Can also be a regular expression.
28+
* @var array|string
2729
*/
2830
public $fileExtensions = [];
2931

@@ -89,8 +91,12 @@ public function handleCommittedFiles(ChangedFiles $files, Closure $next)
8991
*/
9092
protected function analizeCommittedFiles($commitFiles)
9193
{
94+
/** @var ChangedFile $file */
9295
foreach ($commitFiles as $file) {
93-
if (! in_array($file->extension(), $this->fileExtensions)) {
96+
if (
97+
(is_array($this->fileExtensions) && ! in_array($file->extension(), $this->fileExtensions)) ||
98+
(is_string($this->fileExtensions) && ! preg_match($this->fileExtensions, $file->getFilePath()))
99+
) {
94100
continue;
95101
}
96102

@@ -183,7 +189,7 @@ protected function suggestAutoFixOrExit()
183189
*/
184190
public function setFileExtensions($fileExtensions)
185191
{
186-
$this->fileExtensions = (array) $fileExtensions;
192+
$this->fileExtensions = $fileExtensions;
187193

188194
return $this;
189195
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace Igorsgm\GitHooks\Console\Commands\Hooks;
4+
5+
use Closure;
6+
use Igorsgm\GitHooks\Contracts\CodeAnalyzerPreCommitHook;
7+
use Igorsgm\GitHooks\Git\ChangedFiles;
8+
9+
class BladeFormatterPreCommitHook extends BaseCodeAnalyzerPreCommitHook implements CodeAnalyzerPreCommitHook
10+
{
11+
/**
12+
* Get the name of the hook.
13+
*/
14+
public function getName(): ?string
15+
{
16+
return 'Blade Formatter';
17+
}
18+
19+
/**
20+
* Analyze and fix committed blade.php files using blade-formatter npm package
21+
*
22+
* @param ChangedFiles $files The files that have been changed in the current commit.
23+
* @param Closure $next A closure that represents the next middleware in the pipeline.
24+
* @return mixed|null
25+
*/
26+
public function handle(ChangedFiles $files, Closure $next)
27+
{
28+
return $this->setFileExtensions('/\.blade\.php$/')
29+
->setAnalyzerExecutable(config('git-hooks.code_analyzers.blade_formatter.path'), true)
30+
->handleCommittedFiles($files, $next);
31+
}
32+
33+
/**
34+
* Returns the command to run Blade Formatter tester
35+
*/
36+
public function analyzerCommand(): string
37+
{
38+
return trim(sprintf('%s -c', $this->getAnalyzerExecutable()));
39+
}
40+
41+
/**
42+
* Returns the command to run Blade Formatter fixer
43+
*/
44+
public function fixerCommand(): string
45+
{
46+
return trim(sprintf('%s --write', $this->getFixerExecutable()));
47+
}
48+
}

0 commit comments

Comments
 (0)