itsgoingd / clockwork

Clockwork - php dev tools in your browser - server-side component
https://underground.works/clockwork
MIT License
5.69k stars 321 forks source link

Use with laravel tinker? #571

Open freestyledork opened 2 years ago

freestyledork commented 2 years ago

Is it possible to log the queries when testing with laravel tinker? If so, can you provide instructions on how to enable it? I reviewed the docs and searched the issues, I don't see any other mention of using Tinker.

itsgoingd commented 2 years ago

I don't think so, unless it works with collecting artisan commands? Have you tried turning it on?

freestyledork commented 2 years ago

I tried messing with the settings to see if that was the reason it wasn't working. I can confirm it doesn't log anything when using tinker with artisan commands enabled. I'd like to suggest it as a feature. Love the package anyway!

tminich commented 2 years ago

I had a similar need. I solved it with a custom Psy-Shell command:

use Psy\Command\ReflectingCommand;
use Psy\VarDumper\Presenter;
use Psy\VarDumper\PresenterAware;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class MonitorDb extends ReflectingCommand implements PresenterAware
{
    private ?Presenter $presenter = null;

    /**
     * PresenterAware interface.
     *
     * @param Presenter $presenter
     */
    public function setPresenter(Presenter $presenter): void
    {
        $this->presenter = $presenter;
    }

    /**
     * {@inheritdoc}
     */
    protected function configure(): void
    {
        $this
            ->setName('monitor_db')
            ->setAliases(['mdb', 'debug_db', 'ddb'])
            ->setDescription('Start monitoring database queries');
    }

    /**
     * {@inheritdoc}
     */
    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        \DB::listen(function ($query) use ($output){
            $data = [
                'sql' => $query->sql,
                'bindings' => $query->bindings,
                'execution_time' => $query->time,
            ];

            $output->writeln($this->presenter->present($data));
        });
        return 0;
    }

}

You need to load the command in your config.tinker.php (which you need to enable in you .env).

fgilio commented 2 years ago

Would be useful to also have it working with Tinkerwell