/** * @test */ public function itShouldShowUsageWithNoArgs() { $this->argumentManager->parse(Argument::type('array'))->willThrow(\Exception::class); $args = ['phpa']; $this->climate->usage($args)->shouldBeCalled(); $this->cli->handle($args); }
/** * @param array $args */ public function handle(array $args) { $this->cli->out(sprintf('PHPAssumptions analyser v%s by @rskuipers', Cli::VERSION))->br(); try { $this->cli->arguments->parse($args); } catch (\Exception $e) { $this->cli->usage($args); return; } switch ($this->cli->arguments->get('format')) { case 'xml': $output = new XmlOutput($this->cli, $this->cli->arguments->get('output')); break; default: $output = new PrettyOutput($this->cli); break; } $nodeTraverser = new NodeTraverser(); $analyser = new Analyser($this->parser, $nodeTraverser); $nodeTraverser->addVisitor(new NodeVisitor($analyser, new Detector())); $target = $this->cli->arguments->get('path'); $targets = []; if (is_file($target)) { $targets[] = $target; } else { $directory = new \RecursiveDirectoryIterator($target); $iterator = new \RecursiveIteratorIterator($directory); $regex = new \RegexIterator($iterator, '/^.+\\.php$/i', \RecursiveRegexIterator::GET_MATCH); foreach ($regex as $file) { $targets[] = $file[0]; } } $result = $analyser->analyse($targets); $output->output($result); }
/** * Prompt user to provide the config data. * * @return array */ protected function getConfig() { $this->console->arguments->parse(); if ($this->console->arguments->defined('help')) { $this->console->usage(); exit; } return ['deep' => $this->console->arguments->get('deep'), 'timeout' => $this->console->arguments->get('timeout'), 'savePath' => $this->console->arguments->get('outputPath')]; }
/** * Constructor. */ public function __construct() { $this->opt = new \Banago\PHPloy\Options(new \League\CLImate\CLImate()); $this->cli = $this->opt->cli; $this->cli->backgroundGreen()->bold()->out('-------------------------------------------------'); $this->cli->backgroundGreen()->bold()->out('| PHPloy |'); $this->cli->backgroundGreen()->bold()->out('-------------------------------------------------'); // Setup PHPloy $this->setup(); if ($this->cli->arguments->defined('help')) { $this->cli->usage(); return; } if ($this->cli->arguments->defined('init')) { $this->createSampleIniFile(); return; } if ($this->cli->arguments->defined('version')) { $this->cli->bold()->info('PHPloy v' . $this->version); return; } if (file_exists("{$this->repo}/.git")) { $this->git = new \Banago\PHPloy\Git($this->repo); $this->deploy(); } else { throw new \Exception("'{$this->repo}' is not a Git repository."); } }
/** * Command constructor. * * @param CLImate $climate */ public function __construct(CLImate $climate) { $this->climate = $climate; $this->createCommandLineArguments(); $this->climate->description(self::DESCRIPTION); $this->climate->arguments->parse(); if ($this->climate->arguments->defined('help')) { $this->climate->usage(); exit; } if ($this->climate->arguments->defined('list')) { $this->showList(); exit; } $config_file = $this->climate->arguments->defined('config') ? $this->climate->arguments->get('config') : self::DEFAULT_CONFIG; if (!file_exists($config_file)) { die("Couldn't read configuration file. Make sure the path is correct."); } $this->prepareConfig($config_file); }
use alexlvcom\TaskRunner\ParamValidator; use alexlvcom\ServiceContainer\Container as ServiceContainer; $climate = new CLImate(); $container = new ServiceContainer(); $climate->out('<green>Task Runner by AlexLV. (c) 2015 .</green> <white>Version <yellow>' . ALEXLVCOM_TASK_RUNNER_VERSION . '</yellow>'); $climate->br(); $climate->arguments->add(['name' => ['longPrefix' => 'name', 'description' => 'Task Name', 'required' => true]]); try { $climate->arguments->parse(); } catch (Exception $e) { $climate->error($e->getMessage())->br()->usage(); $climate->br(); exit; } if ($climate->arguments->defined('name') === false || $climate->arguments->get('name') === '') { $climate->usage(); exit; } $time_start = microtime(true); $taskName = $climate->arguments->get('name'); if (substr($taskName, 0, 1) === '\\') { // Absolute namespace provided $className = substr($taskName, 1); } else { // relative path goes with prepended alexlvcom\TaskRunner\Tasks namespace $className = 'alexlvcom\\TaskRunner\\Tasks\\' . $climate->arguments->get('name'); } if (class_exists($className)) { $task = $container->make($className); $task->setServiceContainer($container); $task->setDocroot(ROOTPATH);
/** * Call cli * * @param array $argv * * @throws BaseException * @throws MissingMethodException */ private function callCli(array $argv) { if ($this->getRouter()->isMatched()) { $cliMethod = 'cliMethod'; $cliConfig = 'cliConfig'; $actionNamespace = $this->getRouter()->getActionNamespace(); $reflection = new ReflectionClass($actionNamespace); if (!$reflection->implementsInterface('App\\Action\\AppAction')) { throw new BaseException(sprintf('"%s" action must extend "App\\Action\\AppAction"', $actionNamespace)); } // Check Action::cliMethod exist or callable if (method_exists($actionNamespace, $cliMethod) && is_callable([$actionNamespace, $cliMethod])) { $responderNamespace = $this->getRouter()->getResponderNamespace(); $responder = null; $reflection = new ReflectionClass($responderNamespace); if (class_exists($responderNamespace) && $reflection->implementsInterface('App\\Responder\\AppResponder')) { $responder = new $responderNamespace(); } /** @var ContainerAwareInterface|EventSubscriberInterface $actionInstance */ $actionInstance = new $actionNamespace($responder); $actionInstance->setContainer($this->container); $this->getEventManager()->addSubscriber($actionInstance); $climate = new CLImate(); if (method_exists($actionNamespace, $cliConfig) && is_callable([$actionNamespace, $cliConfig])) { $argumentManager = new Manager(); $climate->setArgumentManager($argumentManager); $this->getEventManager()->dispatch(Action::EVENT_BEFORE_CLI_CONFIG); call_user_func([$actionInstance, 'cliConfig'], $argumentManager); $this->getEventManager()->dispatch(Action::EVENT_AFTER_CLI_CONFIG); try { $argumentManager->parse($argv); } catch (\Exception $e) { $climate->error($e->getMessage()); $climate->usage(); } } $this->getEventManager()->dispatch(Action::EVENT_BEFORE_CLI_METHOD); call_user_func([$actionInstance, $cliMethod], $climate); $this->getEventManager()->dispatch(Action::EVENT_AFTER_CLI_METHOD); // Check Responder::cliMethod exist or callable if (method_exists($responder, $cliMethod) && is_callable([$responder, $cliMethod])) { $this->getEventManager()->dispatch(self::EVENT_BEFORE_RESPONDER); call_user_func([$responder, $cliMethod]); $this->getEventManager()->dispatch(self::EVENT_AFTER_RESPONDER); } } else { throw new MissingMethodException(sprintf('Method %s::%s() could not be found, or is not accessible.', $actionNamespace, $cliMethod)); } } else { throw new BaseException(sprintf('Route "%s" does not found', $argv[0])); } }