Commands autloaded, api client

This commit is contained in:
Simon Vieille 2015-02-10 01:17:29 +01:00
parent ca7a5c2de6
commit a02ba75555
11 changed files with 233 additions and 0 deletions

3
app/config.yml Normal file
View file

@ -0,0 +1,3 @@
auth:
uid:
token:

15
composer.json Normal file
View file

@ -0,0 +1,15 @@
{
"autoload": {
"psr-0": {
"": "src/"
}
},
"require": {
"symfony/console": "2.*",
"psr/log": "1.0.0",
"symfony/finder": "~2.6",
"symfony/yaml": "~2.6",
"symfony/filesystem": "3.0.x-dev",
"guzzlehttp/guzzle": "~5.2"
}
}

9
console Executable file
View file

@ -0,0 +1,9 @@
#!/usr/bin/env php
<?php
require_once __DIR__ . '/vendor/autoload.php';
use Console\Application;
$app = new Application();
$app->run();

Binary file not shown.

51
src/Api/Client.php Normal file
View file

@ -0,0 +1,51 @@
<?php
namespace Api;
use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\Exception\RequestException;
class Client
{
const API_HOST = 'https://api.t411.me/';
protected $client;
public function __construct()
{
$this->client = new GuzzleClient();
}
public function getAuthorization($username, $password)
{
return new ClientResponse($this->get(
'auth/',
array(
'username' => $username,
'password' => $password,
)
);
}
public function get($uri, array $options = array())
{
try {
return new ClientResponse($this->client->get(API_HOST.$uri, $options));
} catch (RequestException $e) {
throw new ApiClientException(sprintf('Request exception (GET): %s', $e->getMessage()));
} catch (HttpConnectException $e) {
throw new ApiClientException(sprintf('HTTP Connection exception: %s', $e->getMessage()));
}
}
public function post($uri, array $options = array())
{
try {
return new ClientResponse($this->client->post(API_HOST.$uri, $options));
} catch (RequestException $e) {
throw new ApiClientException(sprintf('Request exception (POST): %s', $e->getMessage()));
} catch (HttpConnectException $e) {
throw new ApiClientException(sprintf('HTTP Connection exception: %s', $e->getMessage()));
}
}
}

View file

@ -0,0 +1,7 @@
<?php
namespace Api;
class ClientException extends \Exception
{
}

View file

@ -0,0 +1,41 @@
<?php
namespace Api;
class ClientResponse
{
protected $response = null;
public function __construct(FutureInterface $response)
{
$this->response = $response;
}
public function hasError()
{
return isset($this->response->json()['error']);
}
public function getErrorCode()
{
if ($this->hasError()) {
return null;
}
return $this->response->json()['code'];
}
public function getErrorMessage()
{
if ($this->hasError()) {
return null;
}
return $this->response->json()['error'];
}
public function getData()
{
return $this->response->json();
}
}

22
src/Api/ConfigLoader.php Normal file
View file

@ -0,0 +1,22 @@
<?php
namespace Api;
use Symfony\Component\Filesystem\Filesystem;
class ConfigLoader
{
protected $configFile = 'app/config.yml';
protected $filesystem = null;
protected function __construct()
{
$this->filesystem = new Filesystem();
}
public function configExists()
{
return $this->filesystem->exists($this->configFile);
}
}

View file

@ -0,0 +1,44 @@
<?php
namespace Console;
use Symfony\Component\Console\Application as BaseApplication;
use Symfony\Component\Finder\Finder;
class Application extends BaseApplication
{
protected $commandsPaths = array(
'src/Console/Command' => 'Console\\Command',
);
public function __construct($name = 'UNKNOWN', $version = 'UNKNOWN')
{
parent::__construct($name, $version);
$this->loadCommands();
}
public function addCommandsPath($path, $namespace)
{
$this->commandsPaths[$path] = trim($namespace, '/');
return $this;
}
protected function loadCommands()
{
$finder = new Finder();
foreach ($this->commandsPaths as $path => $namespace) {
$finder->files('*Command.php')->in($path);
foreach ($finder as $file) {
$className = $namespace.'\\'.str_replace('.php', '', $file->getFilename());
$this->addCommands(array(
new $className(),
));
}
}
}
}

Binary file not shown.

View file

@ -0,0 +1,41 @@
<?php
namespace Console\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Command\Command;
class AuthCommand extends Command
{
protected function configure()
{
$this
->setName('auth:login')
->setDescription('Login on t411')
->setHelp("The <info>%command.name%</info> ");
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$configLoader = new ConfigLoader();
if ($configLoader->configExists()) {
$continue = $dialog->askConfirmation(
$output,
$dialog->getQuestion(
'The configuration file already exists. Do you want to continue',
'yes',
'?'
),
true
);
if (!$continue) {
$output->writeln('Aborded.');
return;
}
}
}
}