Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php namespace Illuminate\Foundation\Console;
- use Boris\Boris;
- use Illuminate\Console\Command;
- class TinkerCommand extends Command
- {
- /**
- * The console command name.
- *
- * @var string
- */
- protected $name = 'tinker';
- /**
- * The console command description.
- *
- * @var string
- */
- protected $description = "Interact with your application";
- /**
- * Execute the console command.
- *
- * @return void
- */
- public function fire()
- {
- if ($this->supportsBoris()) {
- $this->runBorisShell();
- } else {
- $this->comment('Full REPL not supported. Falling back to simple shell.');
- $this->runPlainShell();
- }
- }
- /**
- * Run the Boris REPL with the current context.
- *
- * @return void
- */
- protected function runBorisShell()
- {
- $this->setupBorisErrorHandling();
- (new Boris('> '))->start();
- }
- /**
- * Setup the Boris exception handling.
- *
- * @return void
- */
- protected function setupBorisErrorHandling()
- {
- restore_error_handler();
- restore_exception_handler();
- $this->laravel->make('artisan')->setCatchExceptions(false);
- $this->laravel->error(function () {
- return '';
- });
- }
- public function dump($obj, $ext = 0)
- {
- if ($ext == 1) {
- $extends = get_parent_class($obj)
- ? ':<comment>' . get_parent_class($obj) . '</comment>'
- : '';
- $this->output->writeln('class(<info>' . print_r($obj, 1) . '</info>)' . $extends . "\n");
- } elseif ($ext == 2) {
- $this->output->writeln('function(<info>' . print_r($obj, 1) . "</info>)\n");
- } else {
- if (!is_object($obj)) {
- var_dump($obj);
- } else {
- ob_start();
- var_dump($obj);
- $objectId = preg_replace('~.+#(\d+).+~s', '$1', ob_get_clean());
- $this->output->writeln('object(<info>' . get_class($obj) . '</info>#' . $objectId . ")\n");
- }
- }
- }
- /**
- * Run the plain Artisan tinker shell.
- *
- * @return void
- */
- protected function runPlainShell()
- {
- $input = $this->prompt();
- while ($input != 'quit') {
- $input = preg_replace('/\becho\b/is', '', $input);
- if (!trim($input)) {
- $input = $this->prompt();
- continue;
- }
- if ($input == 'reload') {
- passthru('php ' . base_path('artisan') . ' tinker');
- die;
- } elseif ($input == 'new') {
- system('start php ' . base_path('artisan') . ' tinker');
- $input = $this->prompt();
- continue;
- }
- // We will wrap the execution of the command in a try / catch block so we
- // can easily display the errors in a convenient way instead of having
- // them bubble back out to the CLI and stop the entire command loop.
- try {
- eval('$query = ' . $input . '; $this->dump($query);');
- } catch (\Exception $e) {
- if (class_exists(trim($input))) {
- $input = '"' . str_replace('"', '\"', trim($input)) . '"';
- eval('$query = ' . $input . '; $this->dump($query, 1);');
- } elseif (function_exists(trim($input))) {
- $input = '"' . str_replace('"', '\"', trim($input)) . '"';
- eval('$query = ' . $input . '; $this->dump($query, 2);');
- } else {
- $this->error($e->getMessage());
- }
- }
- $input = $this->prompt();
- }
- }
- /**
- * Prompt the developer for a command.
- *
- * @return string
- */
- protected function prompt()
- {
- $dialog = $this->getHelperSet()->get('dialog');
- return $dialog->ask($this->output, "<info>></info>", null);
- }
- /**
- * Determine if the current environment supports Boris.
- *
- * @return bool
- */
- protected function supportsBoris()
- {
- return extension_loaded('readline') && extension_loaded('posix') && extension_loaded('pcntl');
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement