@@ -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:
0 commit comments