Skip to content

Commit

Permalink
fix: Put all laravel functionality behind a check to see if the proje…
Browse files Browse the repository at this point in the history
…ct is a laravel project, making the plugin also work with plain PsySH without laravel or tinker installed.

Resolves #306
  • Loading branch information
Roboroads committed Jun 19, 2024
1 parent 7be6c52 commit 2e68aa1
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 12 deletions.
33 changes: 33 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

## [Unreleased]

### Fixed

- [[#349]](https://github.com/Roboroads/laravel-tinker/issues/349) Fixed a compatibility issue with PHP 7.0 (Laravel 5.5) and older versions of PsySH.
- [[#306]](https://github.com/Roboroads/laravel-tinker/issues/306) Put all laravel functionality behind a check to see if the project is a laravel project, making the plugin also
work with plain PsySH without laravel or tinker installed.

## [2.7.2] - 2024-06-17

### Fixed
Expand Down Expand Up @@ -371,30 +377,57 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- Everything.

[Unreleased]: https://github.com/Roboroads/laravel-tinker/compare/v2.7.2...HEAD

[2.7.2]: https://github.com/Roboroads/laravel-tinker/compare/v2.7.1...v2.7.2

[2.7.1]: https://github.com/Roboroads/laravel-tinker/compare/v2.7.0...v2.7.1

[2.7.0]: https://github.com/Roboroads/laravel-tinker/compare/v2.6.1...v2.7.0

[2.6.1]: https://github.com/Roboroads/laravel-tinker/compare/v2.6.0...v2.6.1

[2.6.0]: https://github.com/Roboroads/laravel-tinker/compare/v2.5.1...v2.6.0

[2.5.1]: https://github.com/Roboroads/laravel-tinker/compare/v2.5.0...v2.5.1

[2.5.0]: https://github.com/Roboroads/laravel-tinker/compare/v2.4.0...v2.5.0

[2.4.0]: https://github.com/Roboroads/laravel-tinker/compare/v2.3.4...v2.4.0

[2.3.4]: https://github.com/Roboroads/laravel-tinker/compare/v2.3.3...v2.3.4

[2.3.3]: https://github.com/Roboroads/laravel-tinker/compare/v2.3.2...v2.3.3

[2.3.2]: https://github.com/Roboroads/laravel-tinker/compare/v2.3.1...v2.3.2

[2.3.1]: https://github.com/Roboroads/laravel-tinker/compare/v2.3.0...v2.3.1

[2.3.0]: https://github.com/Roboroads/laravel-tinker/compare/v2.2.2...v2.3.0

[2.2.2]: https://github.com/Roboroads/laravel-tinker/compare/v2.2.1...v2.2.2

[2.2.1]: https://github.com/Roboroads/laravel-tinker/compare/v2.2.0...v2.2.1

[2.2.0]: https://github.com/Roboroads/laravel-tinker/compare/v2.1.0...v2.2.0

[2.1.0]: https://github.com/Roboroads/laravel-tinker/compare/v2.0.1...v2.1.0

[2.0.1]: https://github.com/Roboroads/laravel-tinker/compare/v2.0.0...v2.0.1

[2.0.0]: https://github.com/Roboroads/laravel-tinker/compare/v1.3.1...v2.0.0

[1.3.1]: https://github.com/Roboroads/laravel-tinker/compare/v1.3.0...v1.3.1

[1.3.0]: https://github.com/Roboroads/laravel-tinker/compare/v1.2.1...v1.3.0

[1.2.1]: https://github.com/Roboroads/laravel-tinker/compare/v1.2.0...v1.2.1

[1.2.0]: https://github.com/Roboroads/laravel-tinker/compare/v1.1.1...v1.2.0

[1.1.1]: https://github.com/Roboroads/laravel-tinker/compare/v1.1.0...v1.1.1

[1.1.0]: https://github.com/Roboroads/laravel-tinker/compare/v1.0.0...v1.1.0

[1.0.0]: https://github.com/Roboroads/laravel-tinker/compare/v0.0.1-ALPHA...v1.0.0

[0.0.1-ALPHA]: https://github.com/Roboroads/laravel-tinker/commits/v0.0.1-ALPHA
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Find all actions under `Tools -> Laravel Tinker` or use the default shortcut `ct
- [🔌 Plugin Page](https://plugins.jetbrains.com/plugin/14957-laravel-tinker/): Get the plugin for PHPStorm (or IDEA if that's your style)
- [🔗 Share](https://linktr.ee/laraveltinkerplugin): Share the plugin to your network

This plugin does work without your code being a laravel project as well - make sure you have psy/psysh required in your project for it to work, though with limited functionality.
<!-- Plugin description end -->

## Credits
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pluginGroup = nl.deschepers.laraveltinker
pluginName = Laravel Tinker
pluginRepositoryUrl = https://github.com/Roboroads/laravel-tinker
# SemVer format -> https://semver.org
pluginVersion = 2.7.2
pluginVersion = 2.7.3

# Supported build number ranges and IntelliJ Platform versions -> https://plugins.jetbrains.com/docs/intellij/build-number-ranges.html
pluginSinceBuild = 232
Expand Down
38 changes: 27 additions & 11 deletions src/main/resources/scripts/tinker_run.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,32 @@
use Psy\Shell;
use Symfony\Component\Console\Output\ConsoleOutput;

define('LARAVEL_START', microtime(true));

echo "%%START-OUTPUT%%";

$projectSettings = json_decode($argv[2]) ?? new stdClass();

$composerJsonFile = ($projectSettings->vendorRoot ?: __DIR__) . '/composer.json';
if (!file_exists($composerJsonFile)) {
throw new Exception($composerJsonFile . ' does not exist. Please check your project settings.');
}
$psyshComposerJsonFile = ($projectSettings->vendorRoot ?: __DIR__) . '/vendor/psy/psysh/composer.json';
if (!file_exists($psyshComposerJsonFile)) {
throw new Exception($psyshComposerJsonFile . ' does not exist. Make sure your composer dependencies are installed and laravel/tinker (or, at minimum, psy/psysh) is required.');
}

$composerJson = json_decode(file_get_contents($composerJsonFile));
$isLaravelProject = isset($composerJson->require->{"laravel/framework"});

if ($isLaravelProject) {
define('LARAVEL_START', microtime(true));
}

require ($projectSettings->vendorRoot ?: __DIR__) . '/vendor/autoload.php';
$app = require_once ($projectSettings->laravelRoot ?: __DIR__) . '/bootstrap/app.php';
$kernel = $app->make(Kernel::class);
$kernel->bootstrap();
if ($isLaravelProject) {
$app = require_once ($projectSettings->laravelRoot ?: __DIR__) . '/bootstrap/app.php';
$kernel = $app->make(Kernel::class);
$kernel->bootstrap();
}

$config = new Configuration([
'updateCheck' => 'never',
Expand All @@ -43,9 +59,9 @@
];

$existingCasters = [];
foreach($casters as $castableClass => $casterMethod) {
foreach ($casters as $castableClass => $casterMethod) {
list($casterClass, $casterClassMethod) = explode('::', $casterMethod);
if(class_exists($castableClass) && method_exists($casterClass, $casterClassMethod)) {
if (class_exists($castableClass) && method_exists($casterClass, $casterClassMethod)) {
$existingCasters[$castableClass] = $casterMethod;
}
}
Expand All @@ -59,13 +75,13 @@
$closure = new ExecutionLoopClosure($shell);

$autoloadClassMap = ($projectSettings->vendorRoot ?: __DIR__) . '/vendor/composer/autoload_classmap.php';
if(class_exists('\Laravel\Tinker\ClassAliasAutoloader')) {
if (class_exists('\Laravel\Tinker\ClassAliasAutoloader')) {
$loader = ClassAliasAutoloader::register($shell, $autoloadClassMap);
}

$unsanitizedRunCode = token_get_all($argv[1]);
$sanitizedRunCode = '';
foreach($unsanitizedRunCode as $token) {
foreach ($unsanitizedRunCode as $token) {
if (!is_string($token)) {
list($id, $token) = $token;
if (in_array($id, [T_COMMENT, T_DOC_COMMENT, T_OPEN_TAG, T_OPEN_TAG_WITH_ECHO, T_CLOSE_TAG], true)) {
Expand All @@ -77,13 +93,13 @@
}

$shell->addInput($sanitizedRunCode, true);
if($projectSettings->terminateApp) {
if ($isLaravelProject && $projectSettings->terminateApp) {
$shell->addInput('echo "%%END-OUTPUT%%";', true);
$shell->addInput('app()->terminate();', true);
}
$shell->addInput('usleep(250000); throw new \Psy\Exception\BreakException("%%END-OUTPUT%%");', true);
$closure->execute();

if(isset($loader)) {
if (isset($loader)) {
$loader->unregister();
}

0 comments on commit 2e68aa1

Please sign in to comment.