Exemplo n.º 1
0
 /**
  * @param Request $request
  * @param array|Option[] $options
  * @return $this
  * @throws Command\Exception
  */
 public function read(Request $request, array $options)
 {
     foreach ($options as $option) {
         $publicName = $this->getPublicName($option->name);
         if (false !== ($value = $request->request($publicName, false))) {
             if (Option::TYPE_ENUM === $option->type && !isset($option->values[$value])) {
                 throw new Command\Exception('Invalid value for ' . $publicName, Command\Exception::INVALID_VALUE);
             }
             if (!$value && Option::TYPE_VALUE === $option->type) {
                 throw new Command\Exception('Value required for ' . $publicName, Command\Exception::VALUE_REQUIRED);
             }
             if ($option->isVariadic) {
                 if (!is_array($value)) {
                     $value = array($value);
                 }
             }
             if (Option::TYPE_BOOL === $option->type) {
                 $value = (bool) $value;
             }
             $this->values[$option->name] = $value;
         } else {
             if ($option->isRequired) {
                 throw new Command\Exception('Option ' . $publicName . ' required', Command\Exception::OPTION_REQUIRED);
             }
         }
     }
     return $this;
 }
Exemplo n.º 2
0
 public function route(Request $request)
 {
     $path = new StringValue($request->path());
     if ($path->starts('/api/')) {
         Api\Router::create('/api/')->route($request);
         return;
     }
     if ($path->starts('/')) {
     }
 }
Exemplo n.º 3
0
 public function route()
 {
     $path = new StringValue($this->request->path());
     switch (true) {
         case $path->starts('/?') || '/' === $path->value:
             IndexController::ActionIndex();
             break;
         case $path->value === '/albums':
             AlbumController::create($this->request)->actionIndex();
             break;
         case $path->starts('/albums/details'):
             AlbumController::create($this->request)->actionDetails();
             break;
         case $path->starts('/albums/create'):
             AlbumController::create($this->request)->actionCreate();
             break;
         case $path->starts('/albums/upload/receive'):
             AlbumController::create($this->request)->actionUploadReceive();
             break;
         case $path->starts('/auth/vk'):
             AuthController::create($this->request)->oauthEndPoint();
             break;
         case $path->value === '/auth/register':
             AuthController::create($this->request)->actionRegister();
             break;
         case $path->value === '/auth/sign-in':
             AuthController::create($this->request)->actionSignIn();
             break;
         case $path->value === '/auth/register/receive':
             AuthController::create($this->request)->actionRegisterReceive();
             break;
         case $path->value === '/upload':
             UploadController::create($this->request)->indexAction();
             break;
         case $path->starts('/upload/receive'):
             UploadController::create($this->request)->receiveAction();
             break;
         default:
             IndexController::NotFoundAction();
             break;
     }
 }
Exemplo n.º 4
0
 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();
 }
Exemplo n.º 5
0
 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();
 }
Exemplo n.º 6
0
 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;
 }
Exemplo n.º 7
0
 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;
 }
Exemplo n.º 8
0
 public function read(Request $request, array $options)
 {
     $this->def = new PrepareDefinition($options);
     $tokens = $request->server()->argv;
     for ($i = 0; $i < $this->skipFirstTokens; ++$i) {
         array_shift($tokens);
     }
     $this->scriptName = array_shift($tokens);
     $tokens = array_values($tokens);
     $argc = count($tokens);
     if ($argc === 1) {
         foreach (array(Runner::HELP, Runner::VERSION, Runner::BASH_COMPLETION, Runner::INSTALL) as $builtIn) {
             if ($tokens[0] === Runner::OPTION_NAME . $builtIn) {
                 $this->values[$builtIn] = true;
                 return $this;
             }
         }
     }
     $this->option = null;
     for ($index = 0; $index < $argc; ++$index) {
         $this->token = new StringValue($tokens[$index]);
         $this->processToken();
     }
     if ($this->variadicStarted) {
         $this->finishVariadic();
     }
     if (!empty($this->def->requiredArguments)) {
         foreach ($this->def->requiredArguments as $this->option) {
             throw new Exception('Missing required argument: ' . $this->option->getUsage(), Exception::ARGUMENT_REQUIRED);
         }
     }
     if (!empty($this->def->requiredOptions)) {
         foreach ($this->def->requiredOptions as $this->option) {
             throw new Exception('Option required: ' . $this->option->getUsage(), Exception::OPTION_REQUIRED);
         }
     }
     return $this;
 }
Exemplo n.º 9
0
 /**
  * @param Request $request
  * @return Identity
  * @throws \Exception
  * @deprecated
  */
 public static function findIdentityByRequest(Request $request)
 {
     $login = $request->post('login');
     $password = $request->post('password');
     return self::findIdentity($login, $password);
 }
Exemplo n.º 10
0
 /**
  * @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);
 }