@@ -30,7 +30,7 @@ public function __construct($walletPath, $walletPass, $user, $pass, $port, $host
3030 * $rpcpass: Password RPC;
3131 * $rpcport: Port Used By RPC;
3232 */
33- public function start (){
33+ public function start ($ repeat = true ){
3434
3535 if (!$ this ->isRunning ()){
3636
@@ -47,7 +47,13 @@ public function start(){
4747
4848 $ output = implode (" " , $ output );
4949
50- if (str_contains ($ output , 'starting daemon ' ) || str_contains ($ output , 'already running ' )){
50+ if (str_contains ($ output , 'already running ' ) && $ repeat ){
51+ $ this ->stop ();
52+ sleep (1 );
53+ return $ this ->start (false );
54+ }
55+
56+ if (str_contains ($ output , 'starting daemon ' )){
5157 return true ;
5258 }else {
5359 throw new \Exception ('Could not start Electrum daemon. ' );
@@ -61,19 +67,28 @@ public function start(){
6167
6268 public function stop (){
6369
64- if ($ this ->isRunning ()){
65-
66- $ params = [];
67- $ response = $ this ->call ("stop " , $ params );
68-
69- if (!is_array ($ response ) && (str_contains ($ response , 'Daemon stopped ' ) || str_contains ($ response , 'Connection refused ' ))){
70- return true ;
71- }else {
72- throw new \Exception ('Could not stop Electrum daemon. ' );
73- }
70+ $ output = NULL ;
71+ $ code = null ;
72+
73+ exec ("{$ this ->binary } stop 2>&1 " , $ output , $ code );
74+
75+ if (is_null ($ output )){
76+ return false ;
77+ }
78+
79+ $ output = implode (" " , $ output );
80+
81+ if (str_contains ($ output , 'Daemon stopped ' )){
82+ return true ;
83+ }
7484
75- }else {
85+ $ params = [];
86+ $ response = $ this ->call ("stop " , $ params );
87+
88+ if (!is_array ($ response ) && (str_contains ($ response , 'Daemon stopped ' ))){
7689 return true ;
90+ }else {
91+ throw new \Exception ('Could not stop Electrum daemon. ' );
7792 }
7893
7994 }
@@ -433,6 +448,8 @@ public function isRunning(){
433448 return (isset ($ response ['connected ' ]) && $ response ['connected ' ]);
434449 }
435450
451+ }catch (\Exception $ e ){
452+ return false ;
436453 }catch (Throwable $ e ){
437454 return false ;
438455 }
@@ -577,7 +594,20 @@ private function calculateFee($type_input_counts, $num_outputs, $feerate){
577594
578595 private function getBinary (){
579596
580- $ output = null ;
597+ $ output = NULL ;
598+ $ code = null ;
599+
600+ exec ("electrum --help 2>&1 " , $ output , $ code );
601+
602+ if (!is_null ($ output )){
603+
604+ $ output = implode (" " , $ output );
605+
606+ if (str_contains ($ output , 'usage: electrum ' )){
607+ return "electrum " ;
608+ }
609+
610+ }
581611
582612 exec ("which electrum " , $ output );
583613
@@ -612,7 +642,7 @@ private function call($method, $params = []){
612642 $ response = curl_exec ($ ch );
613643
614644 if (curl_errno ($ ch )){
615- return false ;
645+ throw new \ Exception ( ' Curl error: ' . curl_error ( $ ch )) ;
616646 }
617647
618648 curl_close ($ ch );
0 commit comments