BaseIO.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. use Composer\Util\ProcessExecutor;
  14. use Psr\Log\LogLevel;
  15. abstract class BaseIO implements IOInterface
  16. {
  17. protected $authentications = array();
  18. /**
  19. * {@inheritDoc}
  20. */
  21. public function getAuthentications()
  22. {
  23. return $this->authentications;
  24. }
  25. /**
  26. * {@inheritDoc}
  27. */
  28. public function resetAuthentications()
  29. {
  30. $this->authentications = array();
  31. }
  32. /**
  33. * {@inheritDoc}
  34. */
  35. public function hasAuthentication($repositoryName)
  36. {
  37. return isset($this->authentications[$repositoryName]);
  38. }
  39. /**
  40. * {@inheritDoc}
  41. */
  42. public function getAuthentication($repositoryName)
  43. {
  44. if (isset($this->authentications[$repositoryName])) {
  45. return $this->authentications[$repositoryName];
  46. }
  47. return array('username' => null, 'password' => null);
  48. }
  49. /**
  50. * {@inheritDoc}
  51. */
  52. public function setAuthentication($repositoryName, $username, $password = null)
  53. {
  54. $this->authentications[$repositoryName] = array('username' => $username, 'password' => $password);
  55. }
  56. /**
  57. * Check for overwrite and set the authentication information for the repository.
  58. *
  59. * @param string $repositoryName The unique name of repository
  60. * @param string $username The username
  61. * @param string $password The password
  62. */
  63. protected function checkAndSetAuthentication($repositoryName, $username, $password = null)
  64. {
  65. if ($this->hasAuthentication($repositoryName)) {
  66. $auth = $this->getAuthentication($repositoryName);
  67. if ($auth['username'] === $username && $auth['password'] === $password) {
  68. return;
  69. }
  70. $this->writeError(
  71. sprintf(
  72. "<warning>Warning: You should avoid overwriting already defined auth settings for %s.</warning>",
  73. $repositoryName
  74. )
  75. );
  76. }
  77. $this->setAuthentication($repositoryName, $username, $password);
  78. }
  79. /**
  80. * {@inheritDoc}
  81. */
  82. public function loadConfiguration(Config $config)
  83. {
  84. $bitbucketOauth = $config->get('bitbucket-oauth') ?: array();
  85. $githubOauth = $config->get('github-oauth') ?: array();
  86. $gitlabOauth = $config->get('gitlab-oauth') ?: array();
  87. $gitlabToken = $config->get('gitlab-token') ?: array();
  88. $httpBasic = $config->get('http-basic') ?: array();
  89. // reload oauth tokens from config if available
  90. foreach ($bitbucketOauth as $domain => $cred) {
  91. $this->checkAndSetAuthentication($domain, $cred['consumer-key'], $cred['consumer-secret']);
  92. }
  93. foreach ($githubOauth as $domain => $token) {
  94. if (!preg_match('{^[.a-z0-9]+$}', $token)) {
  95. throw new \UnexpectedValueException('Your github oauth token for '.$domain.' contains invalid characters: "'.$token.'"');
  96. }
  97. $this->checkAndSetAuthentication($domain, $token, 'x-oauth-basic');
  98. }
  99. foreach ($gitlabOauth as $domain => $token) {
  100. $this->checkAndSetAuthentication($domain, $token, 'oauth2');
  101. }
  102. foreach ($gitlabToken as $domain => $token) {
  103. $this->checkAndSetAuthentication($domain, $token, 'private-token');
  104. }
  105. // reload http basic credentials from config if available
  106. foreach ($httpBasic as $domain => $cred) {
  107. $this->checkAndSetAuthentication($domain, $cred['username'], $cred['password']);
  108. }
  109. // setup process timeout
  110. ProcessExecutor::setTimeout((int) $config->get('process-timeout'));
  111. }
  112. /**
  113. * System is unusable.
  114. *
  115. * @param string $message
  116. * @param array $context
  117. * @return null
  118. */
  119. public function emergency($message, array $context = array())
  120. {
  121. return $this->log(LogLevel::EMERGENCY, $message, $context);
  122. }
  123. /**
  124. * Action must be taken immediately.
  125. *
  126. * Example: Entire website down, database unavailable, etc. This should
  127. * trigger the SMS alerts and wake you up.
  128. *
  129. * @param string $message
  130. * @param array $context
  131. * @return null
  132. */
  133. public function alert($message, array $context = array())
  134. {
  135. return $this->log(LogLevel::ALERT, $message, $context);
  136. }
  137. /**
  138. * Critical conditions.
  139. *
  140. * Example: Application component unavailable, unexpected exception.
  141. *
  142. * @param string $message
  143. * @param array $context
  144. * @return null
  145. */
  146. public function critical($message, array $context = array())
  147. {
  148. return $this->log(LogLevel::CRITICAL, $message, $context);
  149. }
  150. /**
  151. * Runtime errors that do not require immediate action but should typically
  152. * be logged and monitored.
  153. *
  154. * @param string $message
  155. * @param array $context
  156. * @return null
  157. */
  158. public function error($message, array $context = array())
  159. {
  160. return $this->log(LogLevel::ERROR, $message, $context);
  161. }
  162. /**
  163. * Exceptional occurrences that are not errors.
  164. *
  165. * Example: Use of deprecated APIs, poor use of an API, undesirable things
  166. * that are not necessarily wrong.
  167. *
  168. * @param string $message
  169. * @param array $context
  170. * @return null
  171. */
  172. public function warning($message, array $context = array())
  173. {
  174. return $this->log(LogLevel::WARNING, $message, $context);
  175. }
  176. /**
  177. * Normal but significant events.
  178. *
  179. * @param string $message
  180. * @param array $context
  181. * @return null
  182. */
  183. public function notice($message, array $context = array())
  184. {
  185. return $this->log(LogLevel::NOTICE, $message, $context);
  186. }
  187. /**
  188. * Interesting events.
  189. *
  190. * Example: User logs in, SQL logs.
  191. *
  192. * @param string $message
  193. * @param array $context
  194. * @return null
  195. */
  196. public function info($message, array $context = array())
  197. {
  198. return $this->log(LogLevel::INFO, $message, $context);
  199. }
  200. /**
  201. * Detailed debug information.
  202. *
  203. * @param string $message
  204. * @param array $context
  205. * @return null
  206. */
  207. public function debug($message, array $context = array())
  208. {
  209. return $this->log(LogLevel::DEBUG, $message, $context);
  210. }
  211. /**
  212. * Logs with an arbitrary level.
  213. *
  214. * @param mixed $level
  215. * @param string $message
  216. * @param array $context
  217. * @return null
  218. */
  219. public function log($level, $message, array $context = array())
  220. {
  221. if (in_array($level, array(LogLevel::EMERGENCY, LogLevel::ALERT, LogLevel::CRITICAL, LogLevel::ERROR))) {
  222. $this->writeError('<error>'.$message.'</error>', true, self::NORMAL);
  223. } elseif ($level === LogLevel::WARNING) {
  224. $this->writeError('<warning>'.$message.'</warning>', true, self::NORMAL);
  225. } elseif ($level === LogLevel::NOTICE) {
  226. $this->writeError('<info>'.$message.'</info>', true, self::VERBOSE);
  227. } elseif ($level === LogLevel::INFO) {
  228. $this->writeError('<info>'.$message.'</info>', true, self::VERY_VERBOSE);
  229. } else {
  230. $this->writeError($message, true, self::DEBUG);
  231. }
  232. }
  233. }