forked from deblan/t411-console
Commands autloaded, api client
This commit is contained in:
parent
ca7a5c2de6
commit
a02ba75555
3
app/config.yml
Normal file
3
app/config.yml
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
auth:
|
||||||
|
uid:
|
||||||
|
token:
|
15
composer.json
Normal file
15
composer.json
Normal 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
9
console
Executable file
|
@ -0,0 +1,9 @@
|
||||||
|
#!/usr/bin/env php
|
||||||
|
<?php
|
||||||
|
|
||||||
|
require_once __DIR__ . '/vendor/autoload.php';
|
||||||
|
|
||||||
|
use Console\Application;
|
||||||
|
|
||||||
|
$app = new Application();
|
||||||
|
$app->run();
|
BIN
src/Api/.ConfigLoader.php.swp
Normal file
BIN
src/Api/.ConfigLoader.php.swp
Normal file
Binary file not shown.
51
src/Api/Client.php
Normal file
51
src/Api/Client.php
Normal 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()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
7
src/Api/ClientException.php
Normal file
7
src/Api/ClientException.php
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Api;
|
||||||
|
|
||||||
|
class ClientException extends \Exception
|
||||||
|
{
|
||||||
|
}
|
41
src/Api/ClientResponse.php
Normal file
41
src/Api/ClientResponse.php
Normal 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
22
src/Api/ConfigLoader.php
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
44
src/Console/Application.php
Normal file
44
src/Console/Application.php
Normal 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(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
BIN
src/Console/Command/.AuthCommand.php.swp
Normal file
BIN
src/Console/Command/.AuthCommand.php.swp
Normal file
Binary file not shown.
41
src/Console/Command/AuthCommand.php
Normal file
41
src/Console/Command/AuthCommand.php
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue