Advertisement
Serafim

Untitled

Dec 3rd, 2014
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.28 KB | None | 0 0
  1. <?php namespace Illuminate\Foundation\Console;
  2.  
  3. use Boris\Boris;
  4. use Illuminate\Console\Command;
  5.  
  6. class TinkerCommand extends Command
  7. {
  8.  
  9.     /**
  10.      * The console command name.
  11.      *
  12.      * @var string
  13.      */
  14.     protected $name = 'tinker';
  15.  
  16.     /**
  17.      * The console command description.
  18.      *
  19.      * @var string
  20.      */
  21.     protected $description = "Interact with your application";
  22.  
  23.     /**
  24.      * Execute the console command.
  25.      *
  26.      * @return void
  27.      */
  28.     public function fire()
  29.     {
  30.         if ($this->supportsBoris()) {
  31.             $this->runBorisShell();
  32.         } else {
  33.             $this->comment('Full REPL not supported. Falling back to simple shell.');
  34.  
  35.             $this->runPlainShell();
  36.         }
  37.     }
  38.  
  39.     /**
  40.      * Run the Boris REPL with the current context.
  41.      *
  42.      * @return void
  43.      */
  44.     protected function runBorisShell()
  45.     {
  46.         $this->setupBorisErrorHandling();
  47.  
  48.         (new Boris('> '))->start();
  49.     }
  50.  
  51.     /**
  52.      * Setup the Boris exception handling.
  53.      *
  54.      * @return void
  55.      */
  56.     protected function setupBorisErrorHandling()
  57.     {
  58.         restore_error_handler();
  59.         restore_exception_handler();
  60.  
  61.         $this->laravel->make('artisan')->setCatchExceptions(false);
  62.  
  63.         $this->laravel->error(function () {
  64.                 return '';
  65.             });
  66.     }
  67.  
  68.  
  69.     public function dump($obj, $ext = 0)
  70.     {
  71.         if ($ext == 1) {
  72.             $extends = get_parent_class($obj)
  73.                 ? ':' . get_parent_class($obj)
  74.                 : '';
  75.             echo 'class(' . print_r($obj, 1) . ')' . $extends . "\n";
  76.         } elseif ($ext == 2) {
  77.             echo 'function(' . print_r($obj, 1) . ")\n";
  78.         } else {
  79.             if (!is_object($obj)) {
  80.                 var_dump($obj);
  81.             } else {
  82.                 ob_start();
  83.                 var_dump($obj);
  84.                 $objectId = preg_replace('~.+#(\d+).+~s', '$1', ob_get_clean());
  85.                 echo 'object(' . get_class($obj) . '#' . $objectId . ")\n";
  86.             }
  87.         }
  88.  
  89.     }
  90.  
  91.     /**
  92.      * Run the plain Artisan tinker shell.
  93.      *
  94.      * @return void
  95.      */
  96.     protected function runPlainShell()
  97.     {
  98.         $input = $this->prompt();
  99.  
  100.         while ($input != 'quit') {
  101.             $input = preg_replace('/\becho\b/is', '', $input);
  102.             if (!trim($input)) {
  103.                 $input = $this->prompt();
  104.                 continue;
  105.             }
  106.  
  107.             if ($input == 'reload') {
  108.                 passthru('php ' . base_path('artisan') . ' tinker');
  109.                 die;
  110.             } elseif ($input == 'new') {
  111.                 system('start php ' . base_path('artisan') . ' tinker');
  112.                 $input = $this->prompt();
  113.                 continue;
  114.             }
  115.  
  116.             // We will wrap the execution of the command in a try / catch block so we
  117.             // can easily display the errors in a convenient way instead of having
  118.             // them bubble back out to the CLI and stop the entire command loop.
  119.             try {
  120.                 eval('$query = ' . $input . '; $this->dump($query);');
  121.             } catch (\Exception $e) {
  122.                 if (class_exists(trim($input))) {
  123.                     $input = '"' . str_replace('"', '\"', trim($input)) . '"';
  124.                     eval('$query = ' . $input . '; $this->dump($query, 1);');
  125.                 } elseif (function_exists(trim($input))) {
  126.                     $input = '"' . str_replace('"', '\"', trim($input)) . '"';
  127.                     eval('$query = ' . $input . '; $this->dump($query, 2);');
  128.                 } else {
  129.                     $this->error($e->getMessage());
  130.                 }
  131.             }
  132.             $input = $this->prompt();
  133.         }
  134.     }
  135.  
  136.     /**
  137.      * Prompt the developer for a command.
  138.      *
  139.      * @return string
  140.      */
  141.     protected function prompt()
  142.     {
  143.         $dialog = $this->getHelperSet()->get('dialog');
  144.  
  145.         return $dialog->ask($this->output, "<info>></info>", null);
  146.     }
  147.  
  148.     /**
  149.      * Determine if the current environment supports Boris.
  150.      *
  151.      * @return bool
  152.      */
  153.     protected function supportsBoris()
  154.     {
  155.         return extension_loaded('readline') && extension_loaded('posix') && extension_loaded('pcntl');
  156.     }
  157.  
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement