PageRenderTime 24ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Composer/IO/BaseIO.php

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