PageRenderTime 30ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/laravel/framework/src/Illuminate/Remote/Connection.php

https://gitlab.com/thugside/clicktocall-laravel
PHP | 269 lines | 95 code | 34 blank | 140 comment | 7 complexity | cf1256c2fa8d75c16212d41937c3450f MD5 | raw file
  1. <?php namespace Illuminate\Remote;
  2. use Closure;
  3. use Illuminate\Filesystem\Filesystem;
  4. use Symfony\Component\Console\Output\NullOutput;
  5. use Symfony\Component\Console\Output\OutputInterface;
  6. class Connection implements ConnectionInterface {
  7. /**
  8. * The SSH gateway implementation.
  9. *
  10. * @var \Illuminate\Remote\GatewayInterface
  11. */
  12. protected $gateway;
  13. /**
  14. * The name of the connection.
  15. *
  16. * @var string
  17. */
  18. protected $name;
  19. /**
  20. * The host name of the server.
  21. *
  22. * @var string
  23. */
  24. protected $host;
  25. /**
  26. * The username for the connection.
  27. *
  28. * @var string
  29. */
  30. protected $username;
  31. /**
  32. * The authentication credential set.
  33. *
  34. * @var array
  35. */
  36. protected $auth;
  37. /**
  38. * All of the defined tasks.
  39. *
  40. * @var array
  41. */
  42. protected $tasks = array();
  43. /**
  44. * The output implementation for the connection.
  45. *
  46. * @var \Symfony\Component\Console\Output\OutputInterface
  47. */
  48. protected $output;
  49. /**
  50. * Create a new SSH connection instance.
  51. *
  52. * @param string $name
  53. * @param string $host
  54. * @param string $username
  55. * @param array $auth
  56. * @param \Illuminate\Remote\GatewayInterface
  57. * @param
  58. */
  59. public function __construct($name, $host, $username, array $auth, GatewayInterface $gateway = null)
  60. {
  61. $this->name = $name;
  62. $this->host = $host;
  63. $this->username = $username;
  64. $this->gateway = $gateway ?: new SecLibGateway($host, $auth, new Filesystem);
  65. }
  66. /**
  67. * Define a set of commands as a task.
  68. *
  69. * @param string $task
  70. * @param string|array $commands
  71. * @return void
  72. */
  73. public function define($task, $commands)
  74. {
  75. $this->tasks[$task] = $commands;
  76. }
  77. /**
  78. * Run a task against the connection.
  79. *
  80. * @param string $task
  81. * @param \Closure $callback
  82. * @return void
  83. */
  84. public function task($task, Closure $callback = null)
  85. {
  86. if (isset($this->tasks[$task]))
  87. {
  88. return $this->run($this->tasks[$task], $callback);
  89. }
  90. }
  91. /**
  92. * Run a set of commands against the connection.
  93. *
  94. * @param string|array $commands
  95. * @param \Closure $callback
  96. * @return void
  97. */
  98. public function run($commands, Closure $callback = null)
  99. {
  100. // First, we will initialize the SSH gateway, and then format the commands so
  101. // they can be run. Once we have the commands formatted and the server is
  102. // ready to go we will just fire off these commands against the server.
  103. $gateway = $this->getGateway();
  104. $callback = $this->getCallback($callback);
  105. $gateway->run($this->formatCommands($commands));
  106. // After running the commands against the server, we will continue to ask for
  107. // the next line of output that is available, and write it them out using
  108. // our callback. Once we hit the end of output, we'll bail out of here.
  109. while (true)
  110. {
  111. if (is_null($line = $gateway->nextLine())) break;
  112. call_user_func($callback, $line, $this);
  113. }
  114. }
  115. /**
  116. * Download the contents of a remote file.
  117. *
  118. * @param string $remote
  119. * @param string $local
  120. * @return void
  121. */
  122. public function get($remote, $local)
  123. {
  124. $this->getGateway()->get($remote, $local);
  125. }
  126. /**
  127. * Get the contents of a remote file.
  128. *
  129. * @param string $remote
  130. * @return string
  131. */
  132. public function getString($remote)
  133. {
  134. return $this->getGateway()->getString($remote);
  135. }
  136. /**
  137. * Upload a local file to the server.
  138. *
  139. * @param string $local
  140. * @param string $remote
  141. * @return void
  142. */
  143. public function put($local, $remote)
  144. {
  145. $this->getGateway()->put($local, $remote);
  146. }
  147. /**
  148. * Upload a string to to the given file on the server.
  149. *
  150. * @param string $remote
  151. * @param string $contents
  152. * @return void
  153. */
  154. public function putString($remote, $contents)
  155. {
  156. $this->getGateway()->putString($remote, $contents);
  157. }
  158. /**
  159. * Display the given line using the default output.
  160. *
  161. * @param string $line
  162. * @return void
  163. */
  164. public function display($line)
  165. {
  166. $server = $this->username.'@'.$this->host;
  167. $lead = '<comment>['.$server.']</comment> <info>('.$this->name.')</info>';
  168. $this->getOutput()->writeln($lead.' '.$line);
  169. }
  170. /**
  171. * Format the given command set.
  172. *
  173. * @param string|array $commands
  174. * @return string
  175. */
  176. protected function formatCommands($commands)
  177. {
  178. return is_array($commands) ? implode(' && ', $commands) : $commands;
  179. }
  180. /**
  181. * Get the display callback for the connection.
  182. *
  183. * @param \Closure|null $callback
  184. * @return \Closure
  185. */
  186. protected function getCallback($callback)
  187. {
  188. if ( ! is_null($callback)) return $callback;
  189. return function($line) { $this->display($line); };
  190. }
  191. /**
  192. * Get the exit status of the last command.
  193. *
  194. * @return int|bool
  195. */
  196. public function status()
  197. {
  198. return $this->gateway->status();
  199. }
  200. /**
  201. * Get the gateway implementation.
  202. *
  203. * @return \Illuminate\Remote\GatewayInterface
  204. *
  205. * @throws \RuntimeException
  206. */
  207. public function getGateway()
  208. {
  209. if ( ! $this->gateway->connected() && ! $this->gateway->connect($this->username))
  210. {
  211. throw new \RuntimeException("Unable to connect to remote server.");
  212. }
  213. return $this->gateway;
  214. }
  215. /**
  216. * Get the output implementation for the connection.
  217. *
  218. * @return \Symfony\Component\Console\Output\OutputInterface
  219. */
  220. public function getOutput()
  221. {
  222. if (is_null($this->output)) $this->output = new NullOutput;
  223. return $this->output;
  224. }
  225. /**
  226. * Set the output implementation.
  227. *
  228. * @param \Symfony\Component\Console\Output\OutputInterface $output
  229. * @return void
  230. */
  231. public function setOutput(OutputInterface $output)
  232. {
  233. $this->output = $output;
  234. }
  235. }