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,21 +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 );
36+ if (strpos ($ rawPHPCode , '<?php ' ) === false ) {
37+ $ rawPHPCode = "<?php \n" .$ rawPHPCode ;
38+ }
39+
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 ;
51+ }
3152
32- $ this ->shell ->addInput ($ phpCode );
53+ return [
54+ 'output ' => self ::$ statements ,
55+ ];
56+ }
3357
58+ protected function doExecute (string $ code ): string
59+ {
60+ $ this ->shell ->addInput ($ code );
3461 $ this ->shell ->addInput ("\necho('TWEAKPHP_END'); exit(); " );
35-
62+ $ this ->output = new BufferedOutput ;
63+ $ this ->shell ->setOutput ($ this ->output );
3664 $ closure = new ExecutionLoopClosure ($ this ->shell );
37-
3865 $ closure ->execute ();
66+ $ result = $ this ->outputModifier ->modify ($ this ->cleanOutput ($ this ->output ->fetch ()));
3967
40- $ output = $ this ->cleanOutput ($ this ->output ->fetch ());
41-
42- return $ this ->outputModifier ->modify ($ output );
68+ return trim ($ result );
4369 }
4470
4571 protected function createShell (BufferedOutput $ output , Configuration $ config ): Shell
@@ -51,27 +77,6 @@ protected function createShell(BufferedOutput $output, Configuration $config): S
5177 return $ shell ;
5278 }
5379
54- public function removeComments (string $ code ): string
55- {
56- $ tokens = token_get_all ("<?php \n" .$ code .'?> ' );
57- $ result = '' ;
58-
59- foreach ($ tokens as $ token ) {
60- if (is_array ($ token )) {
61- [$ id , $ text ] = $ token ;
62-
63- if (in_array ($ id , [T_COMMENT , T_DOC_COMMENT , T_OPEN_TAG , T_CLOSE_TAG ])) {
64- continue ;
65- }
66- $ result .= $ text ;
67- } else {
68- $ result .= $ token ;
69- }
70- }
71-
72- return $ result ;
73- }
74-
7580 protected function cleanOutput (string $ output ): string
7681 {
7782 $ output = preg_replace ('/(?s)(<aside.*?<\/aside>)|Exit: Ctrl\+D/ms ' , '$2 ' , $ output );
0 commit comments