From a02ba755550cdbf273b50cf74e6d5d053f80e22a Mon Sep 17 00:00:00 2001 From: Simon Vieille Date: Tue, 10 Feb 2015 01:17:29 +0100 Subject: [PATCH] Commands autloaded, api client --- app/config.yml | 3 ++ composer.json | 15 +++++++ console | 9 ++++ src/Api/.ConfigLoader.php.swp | Bin 0 -> 12288 bytes src/Api/Client.php | 51 +++++++++++++++++++++++ src/Api/ClientException.php | 7 ++++ src/Api/ClientResponse.php | 41 ++++++++++++++++++ src/Api/ConfigLoader.php | 22 ++++++++++ src/Console/Application.php | 44 +++++++++++++++++++ src/Console/Command/.AuthCommand.php.swp | Bin 0 -> 12288 bytes src/Console/Command/AuthCommand.php | 41 ++++++++++++++++++ 11 files changed, 233 insertions(+) create mode 100644 app/config.yml create mode 100644 composer.json create mode 100755 console create mode 100644 src/Api/.ConfigLoader.php.swp create mode 100644 src/Api/Client.php create mode 100644 src/Api/ClientException.php create mode 100644 src/Api/ClientResponse.php create mode 100644 src/Api/ConfigLoader.php create mode 100644 src/Console/Application.php create mode 100644 src/Console/Command/.AuthCommand.php.swp create mode 100644 src/Console/Command/AuthCommand.php diff --git a/app/config.yml b/app/config.yml new file mode 100644 index 0000000..21229e7 --- /dev/null +++ b/app/config.yml @@ -0,0 +1,3 @@ +auth: + uid: + token: diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..98b17a5 --- /dev/null +++ b/composer.json @@ -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" + } +} diff --git a/console b/console new file mode 100755 index 0000000..aab334c --- /dev/null +++ b/console @@ -0,0 +1,9 @@ +#!/usr/bin/env php +run(); diff --git a/src/Api/.ConfigLoader.php.swp b/src/Api/.ConfigLoader.php.swp new file mode 100644 index 0000000000000000000000000000000000000000..ff79c38034ff691d97ae45bd7f87e341ced01953 GIT binary patch literal 12288 zcmeI2zi-n(6vtl}k@%q^m=CDbWJugVC6EF|B`OjUOteguBG)++3;XWmJIjqqVFEGn zCom%Z2lx+|82J-mhJmv1E;cworOd$hq>s<`J->JN*`mDN)%&;aiYvV};&GbjaPz~~ zqnpQDFV7O$B-6^jUoceiFmd66AYOhvOBk49H%+3^#ARtvmSvDjqXW0r?{_1utWITM z^C;LbNiftZP9EIR;aKK9Gck)xAOZ$p;3xxyi@R4((B(`0Rlj`k!g+CKY5S;1yn_K4 zfB_hQ0T_S*7=Qs7_(u&m)1oJQwqy1A&ee14x4C}A1OqSt126ysFaQHE00S@p126ys zFz^=|2**TACyAah{{R0UzyA+U5q)JoF&~+?%p2x4^NQJHN@kncVgjbaeB(S{n9t06 z<{k5bdCvG6PZ#(-ixoinuP6 zEQL>7m2Ll;J=ab~PL7)bqmgf2cX<)HcAek+j9PB??=u%^I;gfV7t$PrLsmj#g6d6g znx!iP+NUTDtrf$$hIpdFN^x(R#ac~whB`A^N#%B$ZO`3uMHOb!nlO@rTkHAx+6~`P F{~fF@v)BLt literal 0 HcmV?d00001 diff --git a/src/Api/Client.php b/src/Api/Client.php new file mode 100644 index 0000000..ce29504 --- /dev/null +++ b/src/Api/Client.php @@ -0,0 +1,51 @@ +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())); + } + } +} diff --git a/src/Api/ClientException.php b/src/Api/ClientException.php new file mode 100644 index 0000000..c9c5637 --- /dev/null +++ b/src/Api/ClientException.php @@ -0,0 +1,7 @@ +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(); + } +} diff --git a/src/Api/ConfigLoader.php b/src/Api/ConfigLoader.php new file mode 100644 index 0000000..2dbca74 --- /dev/null +++ b/src/Api/ConfigLoader.php @@ -0,0 +1,22 @@ +filesystem = new Filesystem(); + } + + public function configExists() + { + return $this->filesystem->exists($this->configFile); + } +} diff --git a/src/Console/Application.php b/src/Console/Application.php new file mode 100644 index 0000000..a14f715 --- /dev/null +++ b/src/Console/Application.php @@ -0,0 +1,44 @@ + '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(), + )); + } + } + } + +} diff --git a/src/Console/Command/.AuthCommand.php.swp b/src/Console/Command/.AuthCommand.php.swp new file mode 100644 index 0000000000000000000000000000000000000000..7fd81e8736bbda85511d99fb9a8ac61deda606d4 GIT binary patch literal 12288 zcmeI2L2DC16vwB67qwLs&rWOVZlFn8Ehx68)K;+2Dq`zF3$opPn+)B|EIYHLp;Qlk z2l3?1gZMG@6X?lb$R5mlPaW+j1GRm}9% zRLNITs|YGO@hc1YctJH9o(wAUsj27Va$JuGe;aua0V2>LkeaY`ZJ1rTJT+-;7sp2V zxwC7X$21@UM1Tko0U|&IhyW2F0z}~dCt%_s_7t~yEZ=G|?}z&Od`cY=AOb{y2oM1x zKm>>Y5g-CYfCvx)BJd9q@B+rlCmH*Svj6|@zyJR{&DamrF6tBNHALF?xwR?)Ox2QL$7pP|_gVLxql!Lm2 z8bLAC9_INi&-aVXnHmuw0z`la5CI}U1c(3;AOc4a*kK%7`?Gg+cxEjLm^6_yjM+(d zE2z}OsVP;Pi7*gJ*O}i?NdQ6FDbBENPZ4r=w1AbpM#vP3e7iSTpSN9Uk`#JESYvVG zkja{zIp^>87HH>)C98Gdm`^BbY2q27Bo9Rd+=~+Mf){gbuTkOHxcf61UyWopJ9P5@KXcslEkB4k_M8{ z^MMzHolo$)*%j!rx9v&i(ejU$6$S$9PsSC}l_mSIf zBTW-setName('auth:login') + ->setDescription('Login on t411') + ->setHelp("The %command.name% "); + } + + 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; + } + } + } +}