BaseIO.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. /*
  3. * This file is part of Composer.
  4. *
  5. * (c) Nils Adermann <naderman@naderman.de>
  6. * Jordi Boggiano <j.boggiano@seld.be>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Composer\IO;
  12. use Composer\Config;
  13. abstract class BaseIO implements IOInterface
  14. {
  15. protected $authentications = array();
  16. /**
  17. * {@inheritDoc}
  18. */
  19. public function getAuthentications()
  20. {
  21. return $this->authentications;
  22. }
  23. /**
  24. * {@inheritDoc}
  25. */
  26. public function hasAuthentication($repositoryName)
  27. {
  28. return isset($this->authentications[$repositoryName]);
  29. }
  30. /**
  31. * {@inheritDoc}
  32. */
  33. public function getAuthentication($repositoryName)
  34. {
  35. if (isset($this->authentications[$repositoryName])) {
  36. return $this->authentications[$repositoryName];
  37. }
  38. return array('username' => null, 'password' => null);
  39. }
  40. /**
  41. * {@inheritDoc}
  42. */
  43. public function setAuthentication($repositoryName, $username, $password = null)
  44. {
  45. $this->authentications[$repositoryName] = array('username' => $username, 'password' => $password);
  46. }
  47. /**
  48. * {@inheritDoc}
  49. */
  50. public function loadConfiguration(Config $config)
  51. {
  52. // reload oauth token from config if available
  53. if ($tokens = $config->get('github-oauth')) {
  54. foreach ($tokens as $domain => $token) {
  55. if (!preg_match('{^[a-z0-9]+$}', $token)) {
  56. throw new \UnexpectedValueException('Your github oauth token for '.$domain.' contains invalid characters: "'.$token.'"');
  57. }
  58. $this->setAuthentication($domain, $token, 'x-oauth-basic');
  59. }
  60. }
  61. if ($tokens = $config->get('gitlab-tokens')) {
  62. foreach ($tokens as $domain => $token) {
  63. $this->setAuthentication($domain, $token, 'gitlab-private-token');
  64. }
  65. }
  66. // reload http basic credentials from config if available
  67. if ($creds = $config->get('http-basic')) {
  68. foreach ($creds as $domain => $cred) {
  69. $this->setAuthentication($domain, $cred['username'], $cred['password']);
  70. }
  71. }
  72. }
  73. }