Skip to content

Commit f6816b8

Browse files
committed
Add support for custom Artisan executable path
- Update git-hooks.php to include a new configuration option 'artisan_path', allowing users to set the path for the Artisan executable within Laravel or Laravel Zero applications. This improves compatibility with different project structures. - Modify hook template and related source code to use the configured 'artisan_path' instead of the default base_path('artisan'). - Add a test case to validate proper installation of hook files with a custom Artisan path. - Update Pest.php and TestCase.php to respect the configured 'artisan_path'.
1 parent 1f5da0f commit f6816b8

File tree

6 files changed

+38
-4
lines changed

6 files changed

+38
-4
lines changed

config/git-hooks.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,4 +204,21 @@
204204
],
205205
],
206206

207+
/*
208+
|--------------------------------------------------------------------------
209+
| Artisan Executable Path
210+
|--------------------------------------------------------------------------
211+
|
212+
| This configuration option allows you to set the path of the Artisan
213+
| executable within your Laravel or Laravel Zero application.
214+
|
215+
| By default, the path is set to the base_path('artisan') which corresponds
216+
| to the main Artisan executable in a standard Laravel installation.
217+
|
218+
| In case you are using a Laravel Zero CLI tool, you may need to change
219+
| this value to match the path of your main executable file instead
220+
| of artisan.php.
221+
|
222+
*/
223+
'artisan_path' => base_path('artisan'),
207224
];

src/Console/Commands/stubs/hook

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ if sh -c ": >/dev/tty" >/dev/null 2>/dev/null; then
44
exec < /dev/tty
55
fi
66

7-
php {path}/artisan {command} $@ >&2
7+
php {artisanPath} {command} $@ >&2

src/GitHooks.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ public function install(string $hookName)
5656

5757
$hookPath = $this->getGitHooksDir().'/'.$hookName;
5858
$hookScript = str_replace(
59-
['{command}', '{path}'],
60-
[$command, base_path()],
59+
['{command}', '{artisanPath}'],
60+
[$command, config('git-hooks.artisan_path')],
6161
$this->getHookStub()
6262
);
6363

tests/Features/Commands/RegisterHooksTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,19 @@
3737
->toBeFile()
3838
->toContainHookArtisanCommand($hookName);
3939
})->with('registrableHookTypes');
40+
41+
test('Installs hook file in .git/hooks folder with custom artisan path', function ($hookClass, $hookName) {
42+
$this->config->set('git-hooks.'.$hookName, [
43+
$hookClass,
44+
]);
45+
46+
$this->config->set('git-hooks.artisan_path', base_path('custom-artisan-path'));
47+
48+
$this->artisan('git-hooks:register')->assertSuccessful();
49+
50+
$hookFile = base_path('.git/hooks/'.$hookName);
51+
52+
expect($hookFile)
53+
->toBeFile()
54+
->toContainHookArtisanCommand($hookName);
55+
})->with('registrableHookTypes');

tests/Pest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434

3535
expect()->extend('toContainHookArtisanCommand', function ($hookName) {
3636
$this->value = file_get_contents($this->value);
37-
$artisanCommand = sprintf('php %s git-hooks:%s $@ >&2', base_path('artisan'), $hookName);
37+
$artisanCommand = sprintf('php %s git-hooks:%s $@ >&2', config('git-hooks.artisan_path'), $hookName);
3838

3939
return $this->toContain($artisanCommand);
4040
});

tests/TestCase.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ public function defineEnvironment($app)
5151
'post-merge' => [],
5252
'pre-push' => [],
5353
'code_analyzers' => [],
54+
'artisan_path' => base_path('artisan'),
5455
]);
5556

5657
$this->config = $app['config'];

0 commit comments

Comments
 (0)