public function __construct(Request $request = null) { if (null === $request) { $request = Request::createAuto(); } $this->requestRef = DependencyRepository::add($request); $undefined = Undefined::get(); foreach (static::optionsArray() as $option) { $this->{$option->name} = $undefined; } $this->layout = new Layout(); }
public static function run(Command\Definition $definition, Request $request = null) { if (null === $request) { $request = Request::createAuto(); } $requestMapper = new Command\Web\RequestMapper($request); $response = new Response(); $layout = new Layout(); $layout->pushMain($response); try { $io = new Command\Io($definition, $requestMapper, $response); $io->getCommand()->performAction(); } catch (\Exception $exception) { $response->error($exception->getMessage()); } $layout->render(); }
public function run(Request $request = null) { if (null === $request) { $request = Request::createAuto(); } $this->request = $request; try { if (!$this->command instanceof Application) { throw new Command\Exception('Application required', Command\Exception::INVALID_ARGUMENT); } $this->reader = new RequestMapper(); $this->reader->read($request, $this->command->optionsArray()); } catch (Command\Exception $exception) { if (empty($this->reader->values['action'])) { // TODO symbolize 'action' literal $this->response->error($exception->getMessage()); $this->response->addContent('Use --help to show information.'); return $this; } } foreach ($this->reader->values as $name => $value) { $this->command->{$name} = $value; } if (isset($this->command->action)) { $action = $this->command->action; $commandDefinition = $this->command->definition()->actions[$action]; $command = new $commandDefinition->commandClass(); $runner = new \Yaoi\Cli\Command\Runner($command); $runner->commandName = $this->commandName . ' ' . $action; $runner->commandVersion = $this->commandVersion; $runner->commandDescription = $this->commandDescription . ($runner->commandDescription ? PHP_EOL . $runner->commandDescription : ''); $runner->skipFirstTokens = 1; $runner->run($request); } elseif (!empty($this->reader->values[self::HELP])) { $this->showHelp(); } elseif (!empty($this->reader->values[self::VERSION])) { $this->showVersion(); } elseif (!empty($this->reader->values[self::BASH_COMPLETION])) { $this->showBashCompletion(); } elseif (!empty($this->reader->values[self::INSTALL])) { $this->install(); } // @codeCoverageIgnoreEnd return $this; }
public function run(Request $request = null) { if (null === $request) { $request = Request::createAuto(); } $this->request = $request; try { $this->reader = new RequestMapper(); $this->reader->read($request, $this->command->optionsArray()); } catch (Exception $exception) { $this->response->error($exception->getMessage()); return $this; } foreach ($this->reader->values as $name => $value) { $this->command->{$name} = $value; } $this->command->performAction(); return $this; }
/** * @codeCoverageIgnore */ public function install() { $this->response->addContent('Installing'); $request = Request::createAuto(); if (!$request->isCli()) { $this->response->error('CLI mode required'); return; } $scriptFilename = realpath($request->server()->SCRIPT_NAME); $basename = basename($scriptFilename); ob_start(); $this->showBashCompletion(); $completion = ob_get_clean(); $completionDirs = array('/usr/local/etc/bash_completion.d/', '/etc/bash_completion.d/'); $completionDir = null; foreach ($completionDirs as $dir) { if (file_exists($dir)) { $completionDir = $dir; break; } } if (null === $completionDir) { $this->response->error('bash_completion.d not found'); return; } $result = file_put_contents($completionDir . $basename, $completion) && chmod($completionDir . $basename, 0755); if (!$result) { $this->response->error('Unable to save bash completion'); return; } $scriptFilenameInstall = '/usr/local/bin/' . $basename; if (!file_exists($scriptFilenameInstall)) { $cmd = 'ln -s ' . $scriptFilename . ' ' . $scriptFilenameInstall; $this->response->addContent($cmd); system($cmd, $result); if ($result) { $this->response->error('Unable to create symlink to ' . $scriptFilenameInstall); } } system('. ' . $completionDir . $basename); }