88use Tamedevelopers \Support \Str ;
99use Tamedevelopers \Support \Constant ;
1010use Tamedevelopers \Support \Capsule \Logger ;
11+ use Tamedevelopers \Support \Process \HttpRequest ;
1112
1213
1314class CommandHelper
@@ -18,17 +19,34 @@ class CommandHelper
1819 */
1920 protected $ conn ;
2021
22+ /**
23+ * The database connector instance.
24+ * @var bool|null
25+ */
26+ protected $ isConsole ;
27+
2128 /**
2229 * Constructor
2330 * @param \Tamedevelopers\Database\Connectors\Connector|null $conn
2431 */
2532 public function __construct ($ conn = null )
2633 {
27- if (class_exists ('Tamedevelopers\Database\DB ' ) && is_null ($ conn )){
28- $ conn = \Tamedevelopers \Database \DB ::connection ();
34+ $ instance = "Tamedevelopers\Database\DB " ;
35+ if (class_exists ($ instance ) && is_null ($ conn )){
36+ $ conn = $ instance ::connection ();
2937 }
3038
3139 $ this ->conn = $ conn ;
40+ $ this ->isConsole = $ this ->isConsole ();
41+ }
42+
43+ /**
44+ * Checking for incoming CLI type
45+ * @param bool
46+ */
47+ protected function isConsole (): bool
48+ {
49+ return HttpRequest::runningInConsole ();
3250 }
3351
3452 /**
@@ -40,8 +58,11 @@ protected function checkConnection($conn): void
4058 $ checkConnection = $ conn ->dbConnection ();
4159
4260 if ($ checkConnection ['status ' ] != Constant::STATUS_200 ){
43- $ this ->error ($ checkConnection ['message ' ]);
44- exit ();
61+ if ($ this ->isConsole ()){
62+ $ this ->error ($ checkConnection ['message ' ]);
63+ exit (1 );
64+ }
65+ return ;
4566 }
4667 }
4768
@@ -72,7 +93,10 @@ protected function forceChecker(): void
7293 if ($ this ->isProduction ()) {
7394 if (!$ force ) {
7495 $ this ->error ("You are in production! Use [--force|-f] flag, to run this command. " );
75- exit (1 );
96+ if ($ this ->isConsole ()){
97+ exit (1 );
98+ }
99+ return ;
76100 }
77101 }
78102 }
@@ -271,8 +295,8 @@ protected function extractTableName($migration = null)
271295 */
272296 protected function confirm (string $ question , bool $ default = false ): bool
273297 {
274- $ yesNo = $ default ? 'Y /n ' : 'y /N ' ;
275- $ answer = readline ("{$ question } [ {$ yesNo }] : " );
298+ $ yesNo = $ default ? 'y /n ' : 'Y /N ' ;
299+ $ answer = readline ("{$ question } ( {$ yesNo }) : " );
276300
277301 if (empty ($ answer )) {
278302 return $ default ;
0 commit comments