/** * @param InputInterface $input * @param OutputInterface $output * * @return int|void */ protected function execute(InputInterface $input, OutputInterface $output) { $initialized = false; try { $this->detectMagento($output); $initialized = $this->initMagento(); } catch (Exception $e) { // do nothing } $parser = new Parser(new Lexer()); $cleaner = new CodeCleaner($parser); $consoleOutput = new ShellOutput(); $config = new Configuration(); $config->setCodeCleaner($cleaner); $shell = new Shell($config); $shell->setScopeVariables(['di' => $this->getObjectManager()]); if ($initialized) { $ok = Charset::convertInteger(Charset::UNICODE_CHECKMARK_CHAR); $edition = $this->productMeta->getEdition(); $magentoVersion = $this->productMeta->getVersion(); $consoleOutput->writeln('<fg=black;bg=green>Magento ' . $magentoVersion . ' ' . $edition . ' initialized.</fg=black;bg=green> ' . $ok); } else { $consoleOutput->writeln('<fg=black;bg=yellow>Magento is not initialized.</fg=black;bg=yellow>'); } $help = <<<'help' At the prompt, type <comment>help</comment> for some help. To exit the shell, type <comment>^D</comment>. help; $consoleOutput->writeln($help); $shell->run($input, $consoleOutput); }
/** * Run the execution loop. * * Forks into a master and a loop process. The loop process will handle the * evaluation of all instructions, then return its state via a socket upon * completion. * * @param Shell $shell */ public function run(Shell $shell) { list($up, $down) = stream_socket_pair(STREAM_PF_UNIX, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP); if (!$up) { throw new \RuntimeException('Unable to create socket pair.'); } $pid = pcntl_fork(); if ($pid < 0) { throw new \RuntimeException('Unable to start execution loop.'); } elseif ($pid > 0) { // This is the main thread. We'll just wait for a while. // We won't be needing this one. fclose($up); // Wait for a return value from the loop process. $read = array($down); $write = null; $except = null; if (stream_select($read, $write, $except, null) === false) { throw new \RuntimeException('Error waiting for execution loop.'); } $content = stream_get_contents($down); fclose($down); if ($content) { $shell->setScopeVariables(@unserialize($content)); } return; } // This is the child process. It's going to do all the work. if (function_exists('setproctitle')) { setproctitle('psysh (loop)'); } // We won't be needing this one. fclose($down); // Let's do some processing. parent::run($shell); // Send the scope variables back up to the main thread fwrite($up, $this->serializeReturn($shell->getScopeVariables())); fclose($up); exit; }
/** * @return callable */ private function getClosure() { $closure = function () { extract($this->shellSoul->getScopeVariables()); try { $this->shellSoul->addCode($this->code); // evaluate the current code buffer ob_start([$this->shellSoul, 'writeStdout'], version_compare(PHP_VERSION, '5.4', '>=') ? 1 : 2); set_error_handler([$this->shellSoul, 'handleError']); $_ = eval($this->shellSoul->flushCode() ?: Loop::NOOP_INPUT); restore_error_handler(); ob_end_flush(); $this->shellSoul->writeReturnValue($_); } catch (BreakException $_e) { restore_error_handler(); if (ob_get_level() > 0) { ob_end_clean(); } $this->shellSoul->writeException($_e); return; } catch (ThrowUpException $_e) { restore_error_handler(); if (ob_get_level() > 0) { ob_end_clean(); } $this->shellSoul->writeException($_e); throw $_e; } catch (\Exception $_e) { restore_error_handler(); if (ob_get_level() > 0) { ob_end_clean(); } $this->shellSoul->writeException($_e); } $this->shellSoul->setScopeVariables(get_defined_vars()); }; return $closure; }
/** * @expectedException \InvalidArgumentException */ public function testUnknownScopeVariablesThrowExceptions() { $shell = new Shell($this->getConfig()); $shell->setScopeVariables(array('foo' => 'FOO', 'bar' => 1)); $shell->getScopeVariable('baz'); }
/** * {@inheritdoc} */ protected function execute(InputInterface $input, OutputInterface $output) { $shell = new Shell(); $shell->setScopeVariables(['app' => $this->container, 'config' => $this->container->get(ConfigurationInterface::class)]); $shell->run(); }
protected function initialize(InputInterface $input, OutputInterface $output) { $this->psysh = $this->getContainer()->get('shell.psysh'); $this->psysh->setScopeVariables(['container' => $this->getContainer()->get('service_container'), 'kernel' => $this->getContainer()->get('kernel')]); }