Skip to content

Commit

Permalink
Using Robo Runner
Browse files Browse the repository at this point in the history
  • Loading branch information
Sara McCutcheon committed Sep 8, 2016
1 parent b588e6b commit 1cad5ed
Show file tree
Hide file tree
Showing 9 changed files with 196 additions and 152 deletions.
19 changes: 15 additions & 4 deletions bin/terminus.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
#!/usr/bin/env php
<?php

require __DIR__ . '/../vendor/autoload.php';

use Pantheon\Terminus\Runner;
$phar_path = \Phar::running(true);
if ($phar_path) {
include_once "$phar_path/vendor/autoload.php";
} else {
if (file_exists(__DIR__ . '/../vendor/autoload.php')) {
include_once __DIR__ . '/../vendor/autoload.php';
} else if (file_exists(__DIR__ . '/../../autoload.php')) {
include_once __DIR__ . '/../../autoload.php';
}
}

$runner = new Runner();
$runner->run();
use Pantheon\Terminus\Terminus;

$terminus = new Terminus();
$status_code = $terminus->run($_SERVER['argv']);
exit($status_code);
17 changes: 10 additions & 7 deletions src/Commands/Auth/LogoutCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,22 @@

use Pantheon\Terminus\Commands\TerminusCommand;

class LogoutCommand extends TerminusCommand
class LoginCommand extends TerminusCommand
{

/**
* Logs the currently logged-in user out of Pantheon
* Logs a user into Pantheon
*
* @name auth:logout
* @aliases logout
* @name auth:login
* @aliases login
*
* @usage terminus auth:logout
* Logs you out of Pantheon by removing your saved session
* @option machine_token A machine token to be saved for future logins
* @usage terminus auth:login --machine-token=111111111111111111111111111111111111111111111
* Logs in the user granted machine token "111111111111111111111111111111111111111111111"
* @usage terminus auth:login
* Logs in your user with a previously saved machine token
*/
public function logOut() {
public function logIn(array $options = []) {

}

Expand Down
38 changes: 32 additions & 6 deletions src/Commands/TerminusCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

namespace Pantheon\Terminus\Commands;

use Pantheon\Terminus\Config;
use Psr\Log\LoggerInterface;
use Symfony\Component\Console\Input\InputAwareInterface;
use Symfony\Component\Console\Logger\ConsoleLogger;
use Robo\Contract\OutputAwareInterface;
use Robo\Common\OutputAwareTrait;
use Robo\Common\InputAwareTrait;
Expand All @@ -21,20 +24,33 @@ abstract class TerminusCommand implements InputAwareInterface, OutputAwareInterf
* @var boolean True if the command requires the user to be logged in
*/
protected $authorized = false;
/**
* @var LoggerInterface
*/
private $logger;

/**
* TerminusCommand constructor
*
* @param Config $config Terminus configuration object
*/
public function __construct($config)
public function __construct()
{
$this->config = $config;
$this->config = new Config();
$this->setLog();
if ($this->authorized) {
$this->ensureLogin();
}
}

/**
* Returns a logger object for use
*
* @return LoggerInterface
*/
protected function log()
{
return $this->logger;
}

/**
* Logs the user in or errs
*
Expand All @@ -52,11 +68,21 @@ private function ensureLogin() {
) {
$auth->logInViaMachineToken(compact('email'));
} else {
$message = 'You are not logged in. Run `auth login` to ';
$message .= 'authenticate or `help auth login` for more info.';
$message = 'You are not logged in. Run `auth:login` to ';
$message .= 'authenticate or `help auth:login` for more info.';
$this->log()->error($message);
}
}
return true;
}

/**
* Sets the given LoggerInterface to this class' logger property
*
* @return void
*/
private function setLog()
{
$this->logger = new ConsoleLogger($this->output());
}
}
32 changes: 16 additions & 16 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,22 +79,6 @@ public static function getHomeDir()
return $home;
}

/**
* Ensures that directory paths work in any system
*
* @param string $path A path to set the directory separators for
* @return string
*/
public static function fixDirectorySeparators($path)
{
$fixed_path = str_replace(
['/', '\\',],
DIRECTORY_SEPARATOR,
$path
);
return $fixed_path;
}

/**
* Sets constants necessary for the proper functioning of Terminus
*
Expand Down Expand Up @@ -227,6 +211,22 @@ private static function getTerminusScript()
return $script_name;
}

/**
* Ensures that directory paths work in any system
*
* @param string $path A path to set the directory separators for
* @return string
*/
private static function fixDirectorySeparators($path)
{
$fixed_path = str_replace(
['/', '\\',],
DIRECTORY_SEPARATOR,
$path
);
return $fixed_path;
}

/**
* Imports environment variables
*
Expand Down
88 changes: 0 additions & 88 deletions src/Runner.php

This file was deleted.

87 changes: 63 additions & 24 deletions src/Terminus.php
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,86 @@

namespace Pantheon\Terminus;

use Symfony\Component\Console\Application;
use Symfony\Component\Console\Input\InputOption;
use Consolidation\AnnotatedCommand\CommandFileDiscovery;
use Robo\Runner;

class Terminus extends Application
class Terminus
{
/**
* @var string[]
*/
private $args;
/**
* @var string
*/
private $commands_directory;
/**
* @var \Robo\Runner
*/
private $runner;
/**
* @var string
*/
private $top_namespace = 'Pantheon\Terminus\Commands';

/**
* @inheritdoc
* Runner constructor
*/
public function __construct()
{
$config = new Config();
parent::__construct('Terminus', $config->get('version'));
$this->commands_directory = __DIR__ . '/Commands';
$this->setRunner();
}

/**
* @inheritdoc
* Runs the instantiated Terminus application
*
* @return integer The exiting status code of the application
*/
protected function getDefaultHelperSet()
public function run(array $arguments)
{
$helper_set = parent::getDefaultHelperSet();
//$helper_set->set(new Pantheon\Terminus\Helpers\FileHelper());
return $helper_set;
$config = new Config();
$status_code = $this->runner->execute($arguments, null, 'Terminus', $config->get('version'));
return $status_code;
}

/**
* @inheritdoc
* Adds command files to the application
*
* @return void
*/
protected function getDefaultInputDefinition()
private function setRunner()
{
$definition = parent::getDefaultInputDefinition();
$definition->addOptions(
[
new InputOption(
'--yes',
'-y',
InputOption::VALUE_NONE,
'Answer yes to all prompts'
),
]
$discovery = new CommandFileDiscovery();
$discovery->setSearchPattern('*Command.php')->setSearchLocations(['.']);
$command_classes = $this->fixNames($discovery->discover($this->commands_directory, $this->top_namespace));
$this->runner = new Runner($command_classes);
}

/**
* Fixes the extraneous '\.' and '/.' currently inserted by the discovery and removes abstract classes
*
* @param string[] $command_files A hash of command file names and paths
* @return string[] The param array, fixed
*/
private function fixNames($command_files) {
$commands = array_filter(
array_combine(
array_map(
function ($file_name) {return str_replace('/./', '/', $file_name);},
array_keys($command_files)
),
array_map(
function ($class_name) {return str_replace('\\.\\', '\\', $class_name);},
array_values($command_files)
)
),
function ($class_name) {
$reflection_class = new \ReflectionClass($class_name);
return !$reflection_class->isAbstract();
}
);
return $definition;
return $commands;
}

}
File renamed without changes.

0 comments on commit 1cad5ed

Please sign in to comment.