Roboroads / laravel-tinker

Adds a way to write php and run it directly in Laravels' Artisan Tinker.
Apache License 2.0
208 stars 15 forks source link

LARAVEL_START constant is undefined #18

Closed daniser closed 4 years ago

daniser commented 4 years ago

Bug description Plugin can't see LARAVEL_START constant declared in "artisan" script.

To Reproduce <?php echo LARAVEL_START; in tinker console.

Expected behavior LARAVEL_START is predefined Laravel constant and should be visible by the plugin console (it works in artisan tinker BTW).

Environment

Roboroads commented 4 years ago

I'm not running your console through the exact php artisan tinker command, just using the way it processes input in a wrapper script I wrote myself (inspired from web-tinker). Where do you need this constant for?

daniser commented 4 years ago

It is used in my project to measure script execution time. It's large project with tons of legacy code. Is there a way to tinker (sic!) with this wrapper script to add custom bootstrap code to it? P.S. I can't even execute simplest code in tinker console because some service provider uses this constant in it...

Roboroads commented 4 years ago

I'll add the constant in the following version - for now you could prepend your script to work around the problem.

<?php
define('LARAVEL_START', microtime(true));
//Tinker code

I realized all my inspiration sources do set this constant somewhere as well :P

daniser commented 4 years ago

I'll add the constant in the following version - for now you could prepend your script to work around the problem.

<?php
define('LARAVEL_START', microtime(true));
//Tinker code

Just FYI: this doesn't help because, as I said, this constant is used inside service provider that boots before tinker script execution: tinker

But it's no problem, I'll just wait for the next version. Thank you.

Roboroads commented 4 years ago

Ah yea, gotcha. It's queued for 1.1.2 ✌️

Roboroads commented 4 years ago

@daniser This fix got released this week, is it now fixed for you?

daniser commented 4 years ago

@daniser This fix got released this week, is it now fixed for you?

Yeah, already checked it out, works great. Thank you!

P.S. Some little annoying bugs remained though, when trying to execute empty shell: <?php 1; gives me output:

// Laravel Tinker started at 2020-09-20 21:46:56
=> 1
*** Execution finished ***

... looks good, but this: <?php; doubles notification:

// Laravel Tinker started at 2020-09-20 21:51:04
*** Execution finished ***
*** Execution finished ***

... and when I omit semicolon entirely like that: <?php it gives me this error:

// Laravel Tinker started at 2020-09-20 21:53:14
=> null
 PHP Warning:  array_filter() expects parameter 1 to be array, null given in D:/OSPanel/domains/ttb/lib/lozovsky/vendor/psy/psysh/src/Shell.php on line 1012 
 PHP Warning:  implode(): Invalid arguments passed in D:/OSPanel/domains/ttb/lib/lozovsky/vendor/psy/psysh/src/Shell.php on line 1014 
*** Execution finished ***

Not that it's huge problem (who in their sane mind installs Tinker console to execute nothing, lol :), but I still think it'll be good to add some consistency in output in that case.

Roboroads commented 4 years ago

Adding <?php is not even necessary, you can run php code without it, it's just there to make the ide know that you are in php code. double execution finish message is probably because you press the shortcut twice. That is indeed a bug I wanted to fix but forgot.