| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace RedMoon\TinkerServer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Psy\Shell; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Clue\React\Stdio\Stdio; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use React\EventLoop\Factory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use React\EventLoop\LoopInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use React\Socket\ConnectionInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use React\Socket\Server as SocketServer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use RedMoon\TinkerServer\Shell\ExecutionClosure; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Symfony\Component\Console\Output\BufferedOutput; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | class Server | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     /** @var string */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     protected $host; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     /** @var LoopInterface */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     protected $loop; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     /** @var BufferedOutput */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     protected $shellOutput; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     /** @var Shell */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     protected $shell; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     /** @var BufferedOutput */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     protected $output; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     /** @var Stdio */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     protected $stdio; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * Server constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * @param string             $host | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * @param Shell              $shell | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * @param BufferedOutput     $output | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * @param LoopInterface|null $loop | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * @param Stdio|null         $stdio | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     public function __construct(string $host, Shell $shell, BufferedOutput $output, LoopInterface $loop = null, Stdio $stdio = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         $this->host = $host; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         $this->loop = $loop ?? Factory::create(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         $this->shell = $shell; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         $this->output = $output; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         $this->shellOutput = new BufferedOutput(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         $this->stdio = $stdio ?? $this->createStdio(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * Start Tinker Server. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * @return void | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 57 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |     public function start() : void | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |         $this->shell->setOutput($this->shellOutput); | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |         $this->createSocketServer(); | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |         $this->loop->run(); | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      * Create SocketServer. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     protected function createSocketServer() : void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         $socket = new SocketServer($this->host, $this->loop); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         $socket->on('connection', function (ConnectionInterface $connection) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             $connection->on('data', function ($data) use ($connection) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |                 $unserializedData = unserialize(base64_decode($data)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |                 $this->shell->setScopeVariables(array_merge($unserializedData, $this->shell->getScopeVariables())); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |                 $this->stdio->write(PHP_EOL); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |                 collect($unserializedData)->keys()->map(function ($variableName) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |                     $this->output->writeln('>> $'.$variableName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |                     $this->executeCode('$'.$variableName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |                     $this->output->write($this->shellOutput->fetch()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |                     $this->stdio->write($this->output->fetch()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |                 }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |             }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      * Create Stdio. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |      * @return Stdio | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     protected function createStdio() : Stdio | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         $stdio = new Stdio($this->loop); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         $stdio->getReadline()->setPrompt('>> '); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         $stdio->on('data', function ($line) use ($stdio) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |             $line = rtrim($line, "\r\n"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |             $stdio->getReadline()->addHistory($line); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |             $this->executeCode($line); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |             $this->output->write(PHP_EOL.$this->shellOutput->fetch()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |             $this->stdio->write($this->output->fetch()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         return $stdio; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |      * Execute Code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |      * @param  [type] $code | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |      * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |     protected function executeCode($code) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |         (new ExecutionClosure($this->shell, $code))->execute(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 123 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 124 |  |  |  | 
            
                        
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.