PageRenderTime 55ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/src/Composer/IO/BaseIO.php

https://github.com/naderman/composer
PHP | 285 lines | 135 code | 32 blank | 118 comment | 10 complexity | fe63a2f07e5f3767595d66e96d303b88 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\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. * {@inheritDoc}
  58. */
  59. public function writeRaw($messages, $newline = true, $verbosity = self::NORMAL)
  60. {
  61. $this->write($messages, $newline, $verbosity);
  62. }
  63. /**
  64. * {@inheritDoc}
  65. */
  66. public function writeErrorRaw($messages, $newline = true, $verbosity = self::NORMAL)
  67. {
  68. $this->writeError($messages, $newline, $verbosity);
  69. }
  70. /**
  71. * Check for overwrite and set the authentication information for the repository.
  72. *
  73. * @param string $repositoryName The unique name of repository
  74. * @param string $username The username
  75. * @param string $password The password
  76. */
  77. protected function checkAndSetAuthentication($repositoryName, $username, $password = null)
  78. {
  79. if ($this->hasAuthentication($repositoryName)) {
  80. $auth = $this->getAuthentication($repositoryName);
  81. if ($auth['username'] === $username && $auth['password'] === $password) {
  82. return;
  83. }
  84. $this->writeError(
  85. sprintf(
  86. "<warning>Warning: You should avoid overwriting already defined auth settings for %s.</warning>",
  87. $repositoryName
  88. )
  89. );
  90. }
  91. $this->setAuthentication($repositoryName, $username, $password);
  92. }
  93. /**
  94. * {@inheritDoc}
  95. */
  96. public function loadConfiguration(Config $config)
  97. {
  98. $bitbucketOauth = $config->get('bitbucket-oauth') ?: array();
  99. $githubOauth = $config->get('github-oauth') ?: array();
  100. $gitlabOauth = $config->get('gitlab-oauth') ?: array();
  101. $gitlabToken = $config->get('gitlab-token') ?: array();
  102. $httpBasic = $config->get('http-basic') ?: array();
  103. $bearerToken = $config->get('bearer') ?: array();
  104. // reload oauth tokens from config if available
  105. foreach ($bitbucketOauth as $domain => $cred) {
  106. $this->checkAndSetAuthentication($domain, $cred['consumer-key'], $cred['consumer-secret']);
  107. }
  108. foreach ($githubOauth as $domain => $token) {
  109. if (!preg_match('{^[.a-z0-9]+$}', $token)) {
  110. throw new \UnexpectedValueException('Your github oauth token for '.$domain.' contains invalid characters: "'.$token.'"');
  111. }
  112. $this->checkAndSetAuthentication($domain, $token, 'x-oauth-basic');
  113. }
  114. foreach ($gitlabOauth as $domain => $token) {
  115. $this->checkAndSetAuthentication($domain, $token, 'oauth2');
  116. }
  117. foreach ($gitlabToken as $domain => $token) {
  118. $username = is_array($token) && array_key_exists("username", $token) ? $token["username"] : $token;
  119. $password = is_array($token) && array_key_exists("token", $token) ? $token["token"] : 'private-token';
  120. $this->checkAndSetAuthentication($domain, $username, $password);
  121. }
  122. // reload http basic credentials from config if available
  123. foreach ($httpBasic as $domain => $cred) {
  124. $this->checkAndSetAuthentication($domain, $cred['username'], $cred['password']);
  125. }
  126. foreach ($bearerToken as $domain => $token) {
  127. $this->checkAndSetAuthentication($domain, $token, 'bearer');
  128. }
  129. // setup process timeout
  130. ProcessExecutor::setTimeout((int) $config->get('process-timeout'));
  131. }
  132. /**
  133. * System is unusable.
  134. *
  135. * @param string $message
  136. * @param array $context
  137. * @return null
  138. */
  139. public function emergency($message, array $context = array())
  140. {
  141. return $this->log(LogLevel::EMERGENCY, $message, $context);
  142. }
  143. /**
  144. * Action must be taken immediately.
  145. *
  146. * Example: Entire website down, database unavailable, etc. This should
  147. * trigger the SMS alerts and wake you up.
  148. *
  149. * @param string $message
  150. * @param array $context
  151. * @return null
  152. */
  153. public function alert($message, array $context = array())
  154. {
  155. return $this->log(LogLevel::ALERT, $message, $context);
  156. }
  157. /**
  158. * Critical conditions.
  159. *
  160. * Example: Application component unavailable, unexpected exception.
  161. *
  162. * @param string $message
  163. * @param array $context
  164. * @return null
  165. */
  166. public function critical($message, array $context = array())
  167. {
  168. return $this->log(LogLevel::CRITICAL, $message, $context);
  169. }
  170. /**
  171. * Runtime errors that do not require immediate action but should typically
  172. * be logged and monitored.
  173. *
  174. * @param string $message
  175. * @param array $context
  176. * @return null
  177. */
  178. public function error($message, array $context = array())
  179. {
  180. return $this->log(LogLevel::ERROR, $message, $context);
  181. }
  182. /**
  183. * Exceptional occurrences that are not errors.
  184. *
  185. * Example: Use of deprecated APIs, poor use of an API, undesirable things
  186. * that are not necessarily wrong.
  187. *
  188. * @param string $message
  189. * @param array $context
  190. * @return null
  191. */
  192. public function warning($message, array $context = array())
  193. {
  194. return $this->log(LogLevel::WARNING, $message, $context);
  195. }
  196. /**
  197. * Normal but significant events.
  198. *
  199. * @param string $message
  200. * @param array $context
  201. * @return null
  202. */
  203. public function notice($message, array $context = array())
  204. {
  205. return $this->log(LogLevel::NOTICE, $message, $context);
  206. }
  207. /**
  208. * Interesting events.
  209. *
  210. * Example: User logs in, SQL logs.
  211. *
  212. * @param string $message
  213. * @param array $context
  214. * @return null
  215. */
  216. public function info($message, array $context = array())
  217. {
  218. return $this->log(LogLevel::INFO, $message, $context);
  219. }
  220. /**
  221. * Detailed debug information.
  222. *
  223. * @param string $message
  224. * @param array $context
  225. * @return null
  226. */
  227. public function debug($message, array $context = array())
  228. {
  229. return $this->log(LogLevel::DEBUG, $message, $context);
  230. }
  231. /**
  232. * Logs with an arbitrary level.
  233. *
  234. * @param mixed $level
  235. * @param string $message
  236. * @param array $context
  237. * @return null
  238. */
  239. public function log($level, $message, array $context = array())
  240. {
  241. if (in_array($level, array(LogLevel::EMERGENCY, LogLevel::ALERT, LogLevel::CRITICAL, LogLevel::ERROR))) {
  242. $this->writeError('<error>'.$message.'</error>');
  243. } elseif ($level === LogLevel::WARNING) {
  244. $this->writeError('<warning>'.$message.'</warning>');
  245. } elseif ($level === LogLevel::NOTICE) {
  246. $this->writeError('<info>'.$message.'</info>', true, self::VERBOSE);
  247. } elseif ($level === LogLevel::INFO) {
  248. $this->writeError('<info>'.$message.'</info>', true, self::VERY_VERBOSE);
  249. } else {
  250. $this->writeError($message, true, self::DEBUG);
  251. }
  252. }
  253. }