/**
  * Called when this object is called as a function
  *
  * @param Context $context
  * @param OptionsFactory $optionsFactory
  * @throws \Exception if haltOnError setting is true
  */
 public function __invoke(Context $context, OptionsFactory $optionsFactory)
 {
     $getopt = $context->getopt(array_keys(self::$options));
     // Get directory list
     $dirListFactory = new DirectoryListFactory();
     $dirListFactory->loadFromCommandline($getopt->get(), 2);
     $dirListFactory->loadFromStdin(new StdinReader(3));
     $dirList = $dirListFactory->getList();
     $this->logger->addDebug('Found directories', [count($dirList)]);
     // Load base options
     $baseOptions = $optionsFactory->newInstance();
     $baseOptions->validateAllRequired();
     if ($baseOptions->preview) {
         $this->logger->addNotice('PREVIEW MODE');
     }
     foreach ($dirList as $dir) {
         try {
             $this->logger->addNotice('In directory', [$dir]);
             $this->processDirectory($dir, $baseOptions, $optionsFactory);
         } catch (\Exception $e) {
             if ($baseOptions->haltOnError) {
                 throw $e;
             } elseif ($baseOptions->verbosity == 2) {
                 $this->logger->addError($e, []);
             } else {
                 $this->logger->addError($e->getMessage(), []);
             }
         }
     }
 }
 /**
  * Creates and returns a new Options instance
  *
  * @param array $getopt Options data
  * @param array $validators Validators to validate the options
  * @param array $defaults Default values for the options
  * @return Options
  */
 public function newInstance(array $getopt = [], array $validators = [], array $defaults = [])
 {
     $this->error = null;
     // Extend the base config
     if (isset($this->config['getopt'])) {
         $getopt = array_merge($this->config['getopt'], $getopt);
     }
     if (isset($this->config['validators'])) {
         $validators = array_merge($this->config['validators'], $validators);
     }
     if (isset($this->config['defaults'])) {
         $defaults = array_merge($this->config['defaults'], $defaults);
     }
     // Read command line
     $cli = $this->context->getopt($getopt);
     $data = new OptionTransformer($cli->get());
     // Create the options container instance
     $options = new Options($validators, $defaults);
     $options->loadOptionData($data->getArrayCopy());
     // initial load so we can access the config option
     // Read config file
     $configLoader = new DotenvLoader((string) new TildeExpander($options->config));
     try {
         $configData = $configLoader->parse()->toArray();
         $options->loadOptionData($configData, false);
         // don't overwrite CLI data
     } catch (\InvalidArgumentException $e) {
         $this->error = $e;
     }
     return $options;
 }
Example #3
0
 public function __construct($version)
 {
     $this->version = $version;
     $cliFactory = new CliFactory();
     $this->context = $cliFactory->newContext($GLOBALS);
     $this->stdio = new \nochso\Phormat\CLI\Stdio(new Handle('php://stdin', 'r'), new Handle('php://stdout', 'w+'), new Handle('php://stderr', 'w+'), new Formatter());
     $this->opt = $this->context->getopt($this->getOptions());
 }
 /**
  * Called when this object is called as a function
  *
  * @param Context $context
  * @param OptionsFactory $optionsFactory
  * @throws \Exception if haltOnError setting is true
  */
 public function __invoke(Context $context, OptionsFactory $optionsFactory)
 {
     $getopt = $context->getopt(array_keys(self::$options));
     // Load base options
     $baseOptions = $optionsFactory->newInstance();
     $baseOptions->validateAllRequired();
     $result = $this->api->getBrands();
     $this->stdio->outln(json_encode($result['brands'], JSON_PRETTY_PRINT));
 }
Example #5
0
 public function run()
 {
     $this->stdio->outln($this->version->getInfo());
     $this->stdio->outln();
     try {
         $getopt = $this->context->getopt($this->getOptions());
         # For the interactive session.
         if ($getopt->get('--init')) {
             $doc = $this->interactiveTemplateToDocument();
             $doc->saveRaw();
             $this->stdio->outln();
             $this->stdio->outln('Customized template written to ' . $doc->getFilepath());
             $this->converter->convert($doc, $this->placeholders);
             $targetPath = $doc->saveTarget();
             $this->stdio->outln('Converted document written to ' . $targetPath);
             exit(Status::SUCCESS);
         }
         $this->validate($getopt);
         $sourceFile = $getopt->get(1);
         if ($sourceFile === null) {
             $this->showHelp();
             exit(Status::USAGE);
         }
         if (!is_file($sourceFile)) {
             throw new \RuntimeException('File not found: ' . $sourceFile);
         }
         $doc = Document::fromFile($sourceFile);
         $this->converter->convert($doc, $this->placeholders);
         $targetFile = $doc->getTargetFilepath($getopt->get('--target'));
         if ($getopt->get('--diff')) {
             $this->showDiff($doc, $targetFile);
         }
         if ($getopt->get('--dry-run')) {
             // Show full output when diff is not wanted
             if (!$getopt->get('--diff')) {
                 $this->stdio->outln('Output:');
                 $this->stdio->outln('<<green>>---<<reset>>');
                 $this->stdio->outln($doc->getContent());
                 $this->stdio->outln('<<green>>---<<reset>>');
             }
         } else {
             $doc->saveTarget($targetFile);
         }
         $this->stdio->outln(sprintf('Saved output from <<green>>%s<<reset>> to <<green>>%s<<reset>>.', $doc->getFilepath(), $targetFile));
     } catch (\Exception $e) {
         $this->stdio->exception($e);
         exit(Status::FAILURE);
     }
 }