22
33namespace TweakPHP \Client ;
44
5+ use PhpParser \ParserFactory ;
6+ use PhpParser \PrettyPrinter \Standard ;
57use Psy \Configuration ;
68use Psy \ExecutionLoopClosure ;
79use Psy \Shell ;
@@ -16,6 +18,10 @@ class Tinker
1618
1719 protected OutputModifier $ outputModifier ;
1820
21+ public static array $ statements = [];
22+
23+ public static int $ current = 0 ;
24+
1925 public function __construct (OutputModifier $ outputModifier , Configuration $ config )
2026 {
2127 $ this ->output = new BufferedOutput ;
@@ -25,23 +31,41 @@ public function __construct(OutputModifier $outputModifier, Configuration $confi
2531 $ this ->outputModifier = $ outputModifier ;
2632 }
2733
28- public function execute (string $ phpCode ): string
34+ public function execute (string $ rawPHPCode ): array
2935 {
30- $ phpCode = $ this -> removeComments ( $ phpCode );
31-
32- $ this -> shell -> addInput ( $ phpCode );
36+ if ( strpos ( $ rawPHPCode , ' <?php ' ) === false ) {
37+ $ rawPHPCode = " <?php \n" . $ rawPHPCode ;
38+ }
3339
34- if (defined ('PHP_WINDOWS_VERSION_BUILD ' )) {
35- $ this ->shell ->addInput ("\necho('TWEAKPHP_END'); exit(); " );
40+ $ parser = (new ParserFactory )->createForHostVersion ();
41+ $ prettyPrinter = new Standard ;
42+ foreach ($ parser ->parse ($ rawPHPCode ) as $ key => $ stmt ) {
43+ $ code = $ prettyPrinter ->prettyPrint ([$ stmt ]);
44+ self ::$ current = $ key ;
45+ self ::$ statements [] = [
46+ 'line ' => $ stmt ->getStartLine (),
47+ 'code ' => $ code ,
48+ ];
49+ $ output = $ this ->doExecute ($ code );
50+ self ::$ statements [$ key ]['output ' ] = $ output ;
3651 }
3752
38- $ closure = new ExecutionLoopClosure ($ this ->shell );
53+ return [
54+ 'output ' => self ::$ statements ,
55+ ];
56+ }
3957
58+ protected function doExecute (string $ code ): string
59+ {
60+ $ this ->shell ->addInput ($ code );
61+ $ this ->shell ->addInput ("\necho('TWEAKPHP_END'); exit(); " );
62+ $ this ->output = new BufferedOutput ;
63+ $ this ->shell ->setOutput ($ this ->output );
64+ $ closure = new ExecutionLoopClosure ($ this ->shell );
4065 $ closure ->execute ();
66+ $ result = $ this ->outputModifier ->modify ($ this ->cleanOutput ($ this ->output ->fetch ()));
4167
42- $ output = $ this ->cleanOutput ($ this ->output ->fetch ());
43-
44- return $ this ->outputModifier ->modify ($ output );
68+ return trim ($ result );
4569 }
4670
4771 protected function createShell (BufferedOutput $ output , Configuration $ config ): Shell
@@ -53,27 +77,6 @@ protected function createShell(BufferedOutput $output, Configuration $config): S
5377 return $ shell ;
5478 }
5579
56- public function removeComments (string $ code ): string
57- {
58- $ tokens = token_get_all ("<?php \n" .$ code .'?> ' );
59- $ result = '' ;
60-
61- foreach ($ tokens as $ token ) {
62- if (is_array ($ token )) {
63- [$ id , $ text ] = $ token ;
64-
65- if (in_array ($ id , [T_COMMENT , T_DOC_COMMENT , T_OPEN_TAG , T_CLOSE_TAG ])) {
66- continue ;
67- }
68- $ result .= $ text ;
69- } else {
70- $ result .= $ token ;
71- }
72- }
73-
74- return $ result ;
75- }
76-
7780 protected function cleanOutput (string $ output ): string
7881 {
7982 $ output = preg_replace ('/(?s)(<aside.*?<\/aside>)|Exit: Ctrl\+D/ms ' , '$2 ' , $ output );
0 commit comments