Skip to content

Commit 46c77f3

Browse files
Command helper options, flag, arguments. helper Perfommance
1 parent e92bed2 commit 46c77f3

File tree

4 files changed

+118
-36
lines changed

4 files changed

+118
-36
lines changed

Capsule/Artisan.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ public static function call(string $input): int
7272
$argv = array_merge(['tame'], [$command], $args);
7373

7474
$artisan = new self();
75+
7576
return $artisan->run($argv);
7677
}
7778

@@ -146,7 +147,7 @@ public function run(array $argv): int
146147
// Resolve primary once and track unresolved flags across providers
147148
$primaryMethod = $sub ?: 'handle';
148149
$unresolvedFlags = array_keys($options);
149-
150+
150151
foreach ($entries as $entry) {
151152
// If registered with a class instance, support subcommands and flag-to-method routing
152153
if (isset($entry['instance']) && \is_object($entry['instance'])) {
@@ -198,13 +199,6 @@ public function run(array $argv): int
198199
return max($exitCode, 1);
199200
}
200201

201-
// Any flags not resolved by any provider are invalid
202-
$unresolvedFlags = array_values($unresolvedFlags);
203-
if (!empty($unresolvedFlags)) {
204-
Logger::error("Invalid option/method: --" . implode(', --', $unresolvedFlags) . "\n");
205-
$exitCode = max($exitCode, 1);
206-
}
207-
208202
return $exitCode;
209203
}
210204

Capsule/CommandHelper.php

Lines changed: 103 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,30 @@ protected function checkConnection($conn): void
4040
exit();
4141
}
4242
}
43+
44+
/**
45+
* Determine if the current environment is production.
46+
*/
47+
protected function isProduction(): bool
48+
{
49+
$env = Env::env('APP_ENV');
50+
$productionAliases = ['prod', 'production', 'live'];
51+
52+
return in_array(Str::lower($env), $productionAliases, true);
53+
}
4354

4455
/**
4556
* Check if the command should be forced when running in production.
4657
*/
47-
protected function forceChecker($options = []): void
58+
protected function forceChecker(): void
4859
{
49-
$force = isset($options['force']) || isset($options['f']);
60+
// backtrace
61+
$trace = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT, 4);
62+
63+
// get backtrace information about the caller's context
64+
$args = $this->debugTraceArgumentHandler($trace);
65+
66+
$force = (isset($args['force']) || isset($args['f']));
5067

5168
if ($this->isProduction()) {
5269
if (!$force) {
@@ -57,28 +74,72 @@ protected function forceChecker($options = []): void
5774
}
5875

5976
/**
60-
* Extracts the flag types from option keys like "drop-types" or "drop-views".
77+
* Extracts all arguments available from command
78+
*
79+
* @param int|null $position
80+
* @return array
6181
*/
62-
protected function flag($options = []): array
82+
protected function arguments($position = null)
6383
{
64-
$types = [];
65-
foreach ($options as $key => $value) {
66-
if (strpos($key, 'drop-') === 0 && $value) {
67-
$types[] = substr($key, strlen('drop-')); // get the part after "drop-"
68-
}
69-
}
70-
return $types;
84+
// backtrace
85+
$trace = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT, 4);
86+
87+
// get backtrace information about the caller's context
88+
$args = $this->debugTraceArgumentHandler($trace, 'arguments');
89+
90+
return $args[$position] ?? $args;
91+
}
92+
93+
/**
94+
* Extracts all flags available from command
95+
*
96+
* @param string $key
97+
* @return array
98+
*/
99+
protected function flags()
100+
{
101+
// backtrace
102+
$trace = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT, 4);
103+
104+
// get backtrace information about the caller's context
105+
$args = $this->debugTraceArgumentHandler($trace);
106+
107+
return $args;
71108
}
109+
110+
/**
111+
* Get a specific flag value from options array.
112+
* Example: option('path')
113+
*
114+
* @param string $key
115+
* @return mixed
116+
*/
117+
protected function flag(string $key)
118+
{
119+
// backtrace
120+
$trace = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT, 4);
72121

122+
// get backtrace information about the caller's context
123+
$args = $this->debugTraceArgumentHandler($trace);
124+
125+
return $args[$key] ?? null;
126+
}
127+
73128
/**
74-
* Determine if the current environment is production.
129+
* Check if (flag) exists and is truthy.
130+
*
131+
* @param string $key
132+
* @return bool
75133
*/
76-
protected function isProduction(): bool
134+
protected function hasFlag($key)
77135
{
78-
$env = Env::env('APP_ENV');
79-
$productionAliases = ['prod', 'production', 'live'];
136+
// backtrace
137+
$trace = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT, 4);
80138

81-
return in_array(Str::lower($env), $productionAliases, true);
139+
// get backtrace information about the caller's context
140+
$args = $this->debugTraceArgumentHandler($trace);
141+
142+
return in_array($key, array_keys($args));
82143
}
83144

84145
/**
@@ -93,28 +154,49 @@ protected function isProduction(): bool
93154
protected function option(string $key, $default = null)
94155
{
95156
// backtrace
96-
$trace = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT, 2);
157+
$trace = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT, 4);
97158

98159
// get backtrace information about the caller's context
99-
$args = $trace[1]['args'][1] ?? [];
160+
$args = $this->debugTraceArgumentHandler($trace);
100161

101162
return $args[$key] ?? $default;
102163
}
103164

104165
/**
105-
* Check if an option/flag exists and is truthy.
166+
* Check if (option) exists and is truthy.
106167
*/
107168
protected function hasOption(string $key): bool
108169
{
109170
// backtrace
110-
$trace = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT, 2);
171+
$trace = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT, 4);
111172

112173
// get backtrace information about the caller's context
113-
$args = $trace[1]['args'][1] ?? [];
174+
$args = $this->debugTraceArgumentHandler($trace);
114175

115176
return !empty($args[$key]);
116177
}
117178

179+
/**
180+
* Get backtrace information about the caller's context
181+
*
182+
* @param mixed $trace
183+
* @param string $key
184+
* @return array
185+
*/
186+
protected function debugTraceArgumentHandler($trace, $key = 'options')
187+
{
188+
$trace = $trace[3];
189+
$data = ['arguments' => [], 'options' => []];
190+
191+
if(isset($trace['function']) && $trace['function'] == 'invokeCommandMethod'){
192+
$args = $trace['args'];
193+
194+
$data = ['arguments' => $args[2], 'options' => $args[3]];
195+
}
196+
197+
return $data[$key] ?? $data;
198+
}
199+
118200
/**
119201
* Extract table name from a migration string.
120202
* Supports common patterns like:

Capsule/Traits/ArtisanTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ private function invokeCommandMethod(object $instance, string $method, array $ar
231231
return $ref->invoke($instance);
232232
} catch (\Throwable $e) {
233233
$flagInfo = $invokedByFlag ? " (from --{$invokedByFlag})" : '';
234-
fwrite(STDERR, "Error running {$method}{$flagInfo}: {$e->getMessage()}\n");
234+
$this->error("Error running {$method}{$flagInfo}: {$e->getMessage()}");
235235
return 1;
236236
}
237237
}

helpers.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ function Tame_isAppFramework()
3737
}
3838
}
3939

40+
/**
41+
* Helps without calling the method multiple times
42+
*/
43+
$Support_Tame_isAppFramework = Tame_isAppFramework();
44+
45+
4046
if (! function_exists('Tame')) {
4147
/**
4248
* Tame Object
@@ -208,7 +214,7 @@ function TameZip()
208214
}
209215
}
210216

211-
if (! Tame_isAppFramework() && ! function_exists('bcrypt')) {
217+
if (! $Support_Tame_isAppFramework && ! function_exists('bcrypt')) {
212218
/**
213219
* Password Encrypter.
214220
* This function encrypts a password using bcrypt with a generated salt.
@@ -303,7 +309,7 @@ function TameSession()
303309
}
304310
}
305311

306-
if (! Tame_isAppFramework() && ! function_exists('config')) {
312+
if (! $Support_Tame_isAppFramework && ! function_exists('config')) {
307313
/**
308314
* Get the value of a configuration option.
309315
*
@@ -322,7 +328,7 @@ function config($key, $default = null)
322328
}
323329
}
324330

325-
if (! Tame_isAppFramework() && ! function_exists('env')) {
331+
if (! $Support_Tame_isAppFramework && ! function_exists('env')) {
326332
/**
327333
* Get ENV (Enviroment) Data
328334
* - If .env was not used,
@@ -437,7 +443,7 @@ function config_time(?array $options = [])
437443
}
438444
}
439445

440-
if (! Tame_isAppFramework() && ! function_exists('__')) {
446+
if (! $Support_Tame_isAppFramework && ! function_exists('__')) {
441447
/**
442448
* Translate the given message.
443449
*
@@ -617,7 +623,7 @@ function to_json($value)
617623
}
618624
}
619625

620-
if (! Tame_isAppFramework() && ! function_exists('dump')) {
626+
if (! $Support_Tame_isAppFramework && ! function_exists('dump')) {
621627
/**
622628
* Dump Data
623629
* @param mixed $data
@@ -630,7 +636,7 @@ function dump(...$data)
630636
}
631637
}
632638

633-
if (! Tame_isAppFramework() && ! function_exists('dd')) {
639+
if (! $Support_Tame_isAppFramework && ! function_exists('dd')) {
634640
/**
635641
* Dump and Data
636642
* @param mixed $data

0 commit comments

Comments
 (0)