Skip to content

Commit db4a6d3

Browse files
update
1 parent 1794bc5 commit db4a6d3

File tree

3 files changed

+63
-1
lines changed

3 files changed

+63
-1
lines changed

Capsule/Artisan.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,33 @@ public function __construct()
4848
$this->discoverExternal();
4949
}
5050

51+
/**
52+
* Programmatically execute a CLI command string.
53+
*
54+
* Example:
55+
* Artisan::call("make:command UserCommand --path=users");
56+
*
57+
* Notes:
58+
* - Quoted args are respected: "--path=\"my path\""
59+
* - Placeholder tokens like [name] are ignored if passed literally.
60+
*/
61+
public static function call(string $input): int
62+
{
63+
// Tokenize input and drop placeholder tokens like [name]
64+
$tokens = self::tokenize($input);
65+
$tokens = array_values(array_filter($tokens, static function ($t) {
66+
return !preg_match('/^\[[^\]]+\]$/', (string)$t);
67+
}));
68+
69+
// Build argv in the same format as PHP CLI provides
70+
$command = $tokens[0] ?? 'list';
71+
$args = array_slice($tokens, 1);
72+
$argv = array_merge(['tame'], [$command], $args);
73+
74+
$artisan = new self();
75+
return $artisan->run($argv);
76+
}
77+
5178
/**
5279
* Register a command by name with description
5380
*

Capsule/Traits/ArtisanDiscovery.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,34 @@ trait ArtisanDiscovery
2727
*/
2828
private static array $registeredProviders = [];
2929

30+
31+
/**
32+
* Split a command string into tokens while respecting quotes.
33+
*/
34+
private static function tokenize(string $input): array
35+
{
36+
$input = trim($input);
37+
if ($input === '') {
38+
return [];
39+
}
40+
41+
$tokens = [];
42+
$pattern = '/"([^"\\]*(?:\\.[^"\\]*)*)"|\'([^\'\\]*(?:\\.[^\'\\]*)*)\'|(\S+)/';
43+
if (preg_match_all($pattern, $input, $matches, PREG_SET_ORDER)) {
44+
foreach ($matches as $m) {
45+
if (($m[1] ?? '') !== '') {
46+
$tokens[] = stripcslashes($m[1]);
47+
} elseif (($m[2] ?? '') !== '') {
48+
$tokens[] = stripcslashes($m[2]);
49+
} else {
50+
$tokens[] = $m[3];
51+
}
52+
}
53+
}
54+
55+
return $tokens;
56+
}
57+
3058
/**
3159
* Discover providers by scanning vendor composer.json
3260
* No reliance on composer/composer installed.json or installed.php.

Commands/MakeCommand.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace Tamedevelopers\Support\Commands;
66

7-
7+
use Tamedevelopers\Support\Capsule\Artisan;
88
use Tamedevelopers\Support\Capsule\Logger;
99
use Tamedevelopers\Support\Capsule\CommandHelper;
1010

@@ -18,6 +18,13 @@ class MakeCommand extends CommandHelper
1818
*/
1919
public function handle()
2020
{
21+
22+
dd(
23+
'ss',
24+
Artisan::call('make:command')
25+
);
26+
27+
exit();
2128
Logger::helpHeader('<yellow>Usage:</yellow>');
2229
Logger::writeln(' php tame make:command [name] --path=users');
2330
Logger::writeln('');

0 commit comments

Comments
 (0)