TerminalController.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. <?php
  2. namespace Encore\Admin\Helpers\Controllers;
  3. use Encore\Admin\Facades\Admin;
  4. use Encore\Admin\Layout\Content;
  5. use Exception;
  6. use Illuminate\Contracts\Support\Arrayable;
  7. use Illuminate\Routing\Controller;
  8. use Illuminate\Support\Facades\Artisan;
  9. use Illuminate\Support\Facades\DB;
  10. use Illuminate\Support\Facades\Redis;
  11. use Illuminate\Support\Facades\Request;
  12. use Illuminate\Support\Str;
  13. use MongoDB\Driver\Command;
  14. use MongoDB\Driver\Manager;
  15. use Symfony\Component\Console\Helper\Table;
  16. use Symfony\Component\Console\Input\ArgvInput;
  17. use Symfony\Component\Console\Output\Output;
  18. class TerminalController extends Controller
  19. {
  20. public function artisan()
  21. {
  22. return Admin::content(function (Content $content) {
  23. $content->header('Artisan terminal');
  24. $content->row(view('laravel-admin-helpers::artisan', ['commands' => $this->organizedCommands()]));
  25. });
  26. }
  27. public function runArtisan()
  28. {
  29. $command = Request::get('c', 'list');
  30. // If Exception raised.
  31. if (1 === Artisan::handle(
  32. new ArgvInput(explode(' ', 'artisan '.trim($command))),
  33. $output = new StringOutput()
  34. )) {
  35. return $this->renderException(new Exception($output->getContent()));
  36. }
  37. return sprintf('<pre>%s</pre>', $output->getContent());
  38. }
  39. public function database()
  40. {
  41. return Admin::content(function (Content $content) {
  42. $content->header('Database terminal');
  43. $content->row(view('laravel-admin-helpers::database', ['connections' => $this->connections()]));
  44. });
  45. }
  46. public function runDatabase()
  47. {
  48. $query = Request::get('q');
  49. $connection = Request::get('c', config('database.default'));
  50. return $this->dispatchQuery($connection, $query);
  51. }
  52. protected function getDumpedHtml($var)
  53. {
  54. ob_start();
  55. dump($var);
  56. $content = ob_get_contents();
  57. ob_get_clean();
  58. return substr($content, strpos($content, '<pre '));
  59. }
  60. protected function connections()
  61. {
  62. $dbs = $redis = [];
  63. foreach (config('database.connections') as $name => $_) {
  64. $dbs[] = [
  65. 'option' => $name,
  66. 'value' => "db:$name",
  67. 'selected' => $name == config('database.default'),
  68. ];
  69. }
  70. $connections = array_filter(config('database.redis'), function ($config) {
  71. return is_array($config);
  72. });
  73. foreach ($connections as $name => $_) {
  74. $redis[] = [
  75. 'value' => "redis:$name",
  76. 'option' => $name,
  77. ];
  78. }
  79. return compact('dbs', 'redis');
  80. }
  81. protected function table(array $headers, $rows, $style = 'default')
  82. {
  83. $output = new StringOutput();
  84. $table = new Table($output);
  85. if ($rows instanceof Arrayable) {
  86. $rows = $rows->toArray();
  87. }
  88. $table->setHeaders($headers)->setRows($rows)->setStyle($style)->render();
  89. return $output->getContent();
  90. }
  91. protected function dispatchQuery($connection, $query)
  92. {
  93. list($type, $connection) = explode(':', $connection);
  94. if ($type == 'redis') {
  95. return $this->execRedisCommand($connection, $query);
  96. }
  97. $config = config('database.connections.'.$connection);
  98. if ($config['driver'] == 'mongodb') {
  99. return $this->execMongodbQuery($config, $query);
  100. }
  101. /* @var \Illuminate\Database\Connection $connection */
  102. $connection = DB::connection($connection);
  103. $connection->enableQueryLog();
  104. try {
  105. $result = $connection->select(str_replace([';', "\G"], '', $query));
  106. } catch (Exception $exception) {
  107. return $this->renderException($exception);
  108. }
  109. $log = current($connection->getQueryLog());
  110. if (empty($result)) {
  111. return sprintf("<pre>Empty set (%s sec)</pre>\r\n", number_format($log['time'] / 1000, 2));
  112. }
  113. $result = json_decode(json_encode($result), true);
  114. if (Str::contains($query, "\G")) {
  115. return $this->getDumpedHtml($result);
  116. }
  117. return sprintf(
  118. "<pre>%s \n%d %s in set (%s sec)</pre>\r\n",
  119. $this->table(array_keys(current($result)), $result),
  120. count($result),
  121. count($result) == 1 ? 'row' : 'rows',
  122. number_format($log['time'] / 1000, 2)
  123. );
  124. }
  125. protected function execMongodbQuery($config, $query)
  126. {
  127. if (Str::contains($query, '.find(') && !Str::contains($query, '.toArray(')) {
  128. $query .= '.toArray()';
  129. }
  130. $manager = new Manager("mongodb://{$config['host']}:{$config['port']}");
  131. $command = new Command(['eval' => $query]);
  132. try {
  133. $cursor = $manager->executeCommand($config['database'], $command);
  134. } catch (Exception $exception) {
  135. return $this->renderException($exception);
  136. }
  137. $result = $cursor->toArray()[0];
  138. $result = json_decode(json_encode($result), true);
  139. if (isset($result['errmsg'])) {
  140. return $this->renderException(new Exception($result['errmsg']));
  141. }
  142. return $this->getDumpedHtml($result['retval']);
  143. }
  144. protected function execRedisCommand($connection, $command)
  145. {
  146. $command = explode(' ', $command);
  147. try {
  148. $result = Redis::connection($connection)->executeRaw($command);
  149. } catch (Exception $exception) {
  150. return $this->renderException($exception);
  151. }
  152. if (is_string($result) && Str::startsWith($result, ['ERR ', 'WRONGTYPE '])) {
  153. return $this->renderException(new Exception($result));
  154. }
  155. return $this->getDumpedHtml($result);
  156. }
  157. protected function organizedCommands()
  158. {
  159. $commands = array_keys(Artisan::all());
  160. $groups = $others = [];
  161. foreach ($commands as $command) {
  162. $parts = explode(':', $command);
  163. if (isset($parts[1])) {
  164. $groups[$parts[0]][] = $command;
  165. } else {
  166. $others[] = $command;
  167. }
  168. }
  169. foreach ($groups as $key => $group) {
  170. if (count($group) === 1) {
  171. $others[] = $group[0];
  172. unset($groups[$key]);
  173. }
  174. }
  175. ksort($groups);
  176. sort($others);
  177. return compact('groups', 'others');
  178. }
  179. protected function renderException(Exception $exception)
  180. {
  181. return sprintf(
  182. "<div class='callout callout-warning'><i class='icon fa fa-warning'></i>&nbsp;&nbsp;&nbsp;%s</div>",
  183. str_replace("\n", '<br />', $exception->getMessage())
  184. );
  185. }
  186. }
  187. class StringOutput extends Output
  188. {
  189. public $output = '';
  190. public function clear()
  191. {
  192. $this->output = '';
  193. }
  194. protected function doWrite($message, $newline)
  195. {
  196. $this->output .= $message.($newline ? "\n" : '');
  197. }
  198. public function getContent()
  199. {
  200. return trim($this->output);
  201. }
  202. }