/** * Decode a received message * * @param string $json Received json message * @return MessageInterface|void * @throws ComponentException */ protected function jsonDecode($json) { $obj = @json_decode($json); if (json_last_error() !== JSON_ERROR_NONE) { $strErr = 'JSON ERROR: ' . $json; if ($this->application->getVerboseLevel() == 2) { Output::err($strErr); } throw new ComponentException($strErr); } return $obj; }
/** * Runs the application * * @return void */ public function run() { if (!self::$defaultApplication) { self::$defaultApplication = $this; } $application = $this; if (OsDetector::isMacOS()) { $processName = './phpgui-i386-darwin'; $processPath = __DIR__ . '/../lazarus/phpgui-i386-darwin.app/Contents/MacOS/'; } elseif (OsDetector::isFreeBSD()) { $processName = './phpgui-x86_64-freebsd'; $processPath = __DIR__ . '/../lazarus/'; } elseif (OsDetector::isUnix()) { $processName = './phpgui-x86_64-linux'; $processPath = __DIR__ . '/../lazarus/'; } elseif (OsDetector::isWindows()) { $processName = '.\\phpgui-x86_64-win64'; $processPath = __DIR__ . '\\..\\lazarus\\'; } else { throw new RuntimeException('Operational System not identified by PHP-GUI.'); } $this->process = $process = new Process($processName, $processPath); $this->process->on('exit', function () use($application) { $application->loop->stop(); }); $this->receiver = $receiver = new Receiver($this); $this->sender = $sender = new Sender($this, $receiver); $this->loop->addTimer(0.001, function ($timer) use($process, $application, $receiver) { $process->start($timer->getLoop()); // We need to pause all default streams // The react/loop uses fread to read data from streams // On Windows, fread always is blocking // Stdin is paused, we use our own way to write on it $process->stdin->pause(); // Stdout is paused, we use our own way to read it $process->stdout->pause(); // Stderr is paused for avoiding fread $process->stderr->pause(); $process->stdout->on('data', function ($data) use($receiver) { $receiver->onData($data); }); $process->stderr->on('data', function ($data) { if (!empty($data)) { Output::err($data); } }); $application->running = true; // Bootstrap the application $application->fire('start'); }); $this->loop->addPeriodicTimer(0.001, function () use($application) { $application->sender->tick(); if (@is_resource($application->process->stdout->stream)) { $application->receiver->tick(); } }); $this->loop->run(); }