From b54f2d625231f052394b9260964d8a6dc82d6d24 Mon Sep 17 00:00:00 2001 From: Saeed Vaziry Date: Sat, 11 Jan 2025 23:36:29 +0100 Subject: [PATCH 1/5] fix --- src/Loader.php | 4 ++++ src/Loaders/BaseLoader.php | 4 +++- src/Loaders/LaravelLoader.php | 14 ------------- src/Loaders/LoaderInterface.php | 2 +- src/Loaders/PimcoreLoader.php | 20 +++++++++++-------- src/Loaders/SymfonyLoader.php | 35 +++++++++------------------------ src/Tinker.php | 12 ++++++----- 7 files changed, 36 insertions(+), 55 deletions(-) diff --git a/src/Loader.php b/src/Loader.php index bfa799c..cdff4b7 100644 --- a/src/Loader.php +++ b/src/Loader.php @@ -29,6 +29,10 @@ public static function load(string $path) return new WordPressLoader($path); } + if (PimcoreLoader::supports($path)) { + return new PimcoreLoader($path); + } + if (ComposerLoader::supports($path)) { return new ComposerLoader($path); } diff --git a/src/Loaders/BaseLoader.php b/src/Loaders/BaseLoader.php index 1750dd7..ed5bf64 100644 --- a/src/Loaders/BaseLoader.php +++ b/src/Loaders/BaseLoader.php @@ -17,6 +17,9 @@ public function init() 'configFile' => null, ]); $config->setUpdateCheck(Checker::NEVER); + $config->setRawOutput(true); + $config->setInteractiveMode(Configuration::INTERACTIVE_MODE_DISABLED); + if (class_exists('Illuminate\Support\Collection') && class_exists('Laravel\Tinker\TinkerCaster')) { $config->getPresenter()->addCasters([ \Illuminate\Support\Collection::class => 'Laravel\Tinker\TinkerCaster::castCollection', @@ -32,7 +35,6 @@ public function init() \Illuminate\Foundation\Application::class => 'Laravel\Tinker\TinkerCaster::castApplication', ]); } - $config->setRawOutput(true); $this->tinker = new Tinker(new CustomOutputModifier(), $config); } diff --git a/src/Loaders/LaravelLoader.php b/src/Loaders/LaravelLoader.php index c4227d8..7055204 100644 --- a/src/Loaders/LaravelLoader.php +++ b/src/Loaders/LaravelLoader.php @@ -25,20 +25,6 @@ public function __construct(string $path) parent::__construct($path); $this->app = require_once $path . '/bootstrap/app.php'; $this->app->make('Illuminate\Contracts\Console\Kernel')->bootstrap(); - $classAliases = require $path . '/vendor/composer/autoload_classmap.php'; - $vendorPath = dirname($path . '/vendor/composer/autoload_classmap.php', 2); - foreach ($classAliases as $class => $path) { - if (!str_contains($class, '\\')) { - continue; - } - if (str_starts_with($path, $vendorPath)) { - continue; - } - try { - class_alias($class, class_basename($class)); - } catch (Throwable $e) { - } - } } /** diff --git a/src/Loaders/LoaderInterface.php b/src/Loaders/LoaderInterface.php index b48db39..cc58f84 100644 --- a/src/Loaders/LoaderInterface.php +++ b/src/Loaders/LoaderInterface.php @@ -27,7 +27,7 @@ public function init(); /** * @param string $code - * @return void + * @return string */ public function execute(string $code); } diff --git a/src/Loaders/PimcoreLoader.php b/src/Loaders/PimcoreLoader.php index e774ab0..895f47a 100644 --- a/src/Loaders/PimcoreLoader.php +++ b/src/Loaders/PimcoreLoader.php @@ -1,22 +1,22 @@ kernel->boot(); } - /** - * Find the application's Kernel class dynamically. - * - * @param string $path - * @return string - */ private function findKernelClass(string $path): string { - $kernelFile = $path . '/src/Kernel.php'; - if (!file_exists($kernelFile)) { - throw new \RuntimeException('Kernel.php file not found in the src directory.'); - } + require_once $path . '/src/Kernel.php'; - require_once $kernelFile; return 'App\\Kernel'; } - /** - * @return string - */ public function name(): string { return 'Symfony'; } - /** - * @return string - */ public function version(): string { - return \Symfony\Component\HttpKernel\Kernel::VERSION; + if (class_exists('Symfony\Component\HttpKernel\Kernel')) { + return \Symfony\Component\HttpKernel\Kernel::VERSION; + } + + return ''; } } diff --git a/src/Tinker.php b/src/Tinker.php index b841ab0..374bf70 100644 --- a/src/Tinker.php +++ b/src/Tinker.php @@ -27,12 +27,14 @@ public function __construct(OutputModifier $outputModifier, $config) public function execute(string $phpCode): string { $phpCode = $this->removeComments($phpCode); - - $this->shell->addInput($phpCode); - $closure = new ExecutionLoopClosure($this->shell); - - $closure->execute(); + if (defined('PHP_WINDOWS_VERSION_BUILD')) { + $this->shell->execute($phpCode); + } else { + $this->shell->addInput($phpCode); + $closure = new ExecutionLoopClosure($this->shell); + $closure->execute(); + } $output = $this->cleanOutput($this->output->fetch()); From 83ec37dd3a165fa97a7f5b4cd78c1752caad6fbe Mon Sep 17 00:00:00 2001 From: Saeed Vaziry Date: Sun, 12 Jan 2025 14:12:50 +0100 Subject: [PATCH 2/5] wip --- src/Loaders/BaseLoader.php | 7 +++++++ src/Tinker.php | 17 ++++++----------- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/src/Loaders/BaseLoader.php b/src/Loaders/BaseLoader.php index ed5bf64..518e17c 100644 --- a/src/Loaders/BaseLoader.php +++ b/src/Loaders/BaseLoader.php @@ -19,6 +19,13 @@ public function init() $config->setUpdateCheck(Checker::NEVER); $config->setRawOutput(true); $config->setInteractiveMode(Configuration::INTERACTIVE_MODE_DISABLED); + $config->setColorMode(Configuration::COLOR_MODE_DISABLED); + $config->setTheme([ + 'prompt' => '', + ]); + $config->setVerbosity(Configuration::VERBOSITY_QUIET); + $config->setHistoryFile(defined('PHP_WINDOWS_VERSION_BUILD') ? 'null' : '/dev/null'); + $config->setRawOutput(false); if (class_exists('Illuminate\Support\Collection') && class_exists('Laravel\Tinker\TinkerCaster')) { $config->getPresenter()->addCasters([ diff --git a/src/Tinker.php b/src/Tinker.php index 374bf70..425b293 100644 --- a/src/Tinker.php +++ b/src/Tinker.php @@ -2,6 +2,7 @@ namespace TweakPHP\Client; +use Psy\Configuration; use Psy\ExecutionLoopClosure; use Psy\Shell; use Symfony\Component\Console\Output\BufferedOutput; @@ -15,7 +16,7 @@ class Tinker protected OutputModifier $outputModifier; - public function __construct(OutputModifier $outputModifier, $config) + public function __construct(OutputModifier $outputModifier, Configuration $config) { $this->output = new BufferedOutput(); @@ -28,23 +29,17 @@ public function execute(string $phpCode): string { $phpCode = $this->removeComments($phpCode); - if (defined('PHP_WINDOWS_VERSION_BUILD')) { - $this->shell->execute($phpCode); - } else { - $this->shell->addInput($phpCode); - $closure = new ExecutionLoopClosure($this->shell); - $closure->execute(); - } + $this->shell->addInput($phpCode); + $closure = new ExecutionLoopClosure($this->shell); + $closure->execute(); $output = $this->cleanOutput($this->output->fetch()); return $this->outputModifier->modify($output); } - protected function createShell(BufferedOutput $output, $config): Shell + protected function createShell(BufferedOutput $output, Configuration $config): Shell { - $config->setHistoryFile(defined('PHP_WINDOWS_VERSION_BUILD') ? 'null' : '/dev/null'); - $shell = new Shell($config); $shell->setOutput($output); From 2f716cdc3dabb26d0016ebb448d958e1a08b5942 Mon Sep 17 00:00:00 2001 From: Saeed Vaziry Date: Mon, 13 Jan 2025 16:46:34 +0100 Subject: [PATCH 3/5] disable pcntl --- src/Loaders/BaseLoader.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Loaders/BaseLoader.php b/src/Loaders/BaseLoader.php index 518e17c..95271db 100644 --- a/src/Loaders/BaseLoader.php +++ b/src/Loaders/BaseLoader.php @@ -26,6 +26,7 @@ public function init() $config->setVerbosity(Configuration::VERBOSITY_QUIET); $config->setHistoryFile(defined('PHP_WINDOWS_VERSION_BUILD') ? 'null' : '/dev/null'); $config->setRawOutput(false); + $config->setUsePcntl(false); if (class_exists('Illuminate\Support\Collection') && class_exists('Laravel\Tinker\TinkerCaster')) { $config->getPresenter()->addCasters([ From 0c1837a8e92e075813e439eae193af05ffe3cf64 Mon Sep 17 00:00:00 2001 From: Saeed Vaziry Date: Mon, 13 Jan 2025 17:50:24 +0100 Subject: [PATCH 4/5] lint --- .github/workflows/code-style.yml | 41 +++++++++++ composer.json | 5 +- composer.lock | 71 +++++++++++++++++++- index.php | 18 ++--- src/Loader.php | 1 - src/Loaders/BaseLoader.php | 4 +- src/Loaders/ComposerLoader.php | 16 +---- src/Loaders/LaravelLoader.php | 19 +----- src/Loaders/LoaderInterface.php | 11 --- src/Loaders/PimcoreLoader.php | 4 +- src/Loaders/SymfonyLoader.php | 10 +-- src/Loaders/WordPressLoader.php | 20 ++---- src/OutputModifiers/CustomOutputModifier.php | 2 +- src/OutputModifiers/OutputModifier.php | 2 +- src/Tinker.php | 26 ++++--- 15 files changed, 162 insertions(+), 88 deletions(-) create mode 100644 .github/workflows/code-style.yml diff --git a/.github/workflows/code-style.yml b/.github/workflows/code-style.yml new file mode 100644 index 0000000..22addf3 --- /dev/null +++ b/.github/workflows/code-style.yml @@ -0,0 +1,41 @@ +name: code-style + +on: + push: + branches: + - main + pull_request: + +jobs: + code-style: + runs-on: ubuntu-22.04 + + strategy: + fail-fast: true + matrix: + php: [ 8.2 ] + node-version: [ "20.x" ] + + steps: + - uses: actions/checkout@v2 + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php }} + + - name: Cache Composer packages + id: composer-cache + uses: actions/cache@v2 + with: + path: vendor + key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }} + restore-keys: | + ${{ runner.os }}-php- + + - name: Install dependencies + if: steps.composer-cache.outputs.cache-hit != 'true' + run: composer install --prefer-dist --no-progress --no-suggest + + - name: Run pint + run: ./vendor/bin/pint --test diff --git a/composer.json b/composer.json index 90a932a..ea27361 100644 --- a/composer.json +++ b/composer.json @@ -18,5 +18,8 @@ "psy/psysh": "*" }, "minimum-stability": "stable", - "prefer-stable": true + "prefer-stable": true, + "require-dev": { + "laravel/pint": "^1.19" + } } diff --git a/composer.lock b/composer.lock index 2a5a09e..8d63d0e 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "206f824071dc8fdf6bc7dcd684dd5aab", + "content-hash": "1130d41e7e62baf860f3bb9f06a09765", "packages": [ { "name": "nikic/php-parser", @@ -928,7 +928,74 @@ "time": "2024-11-08T15:48:14+00:00" } ], - "packages-dev": [], + "packages-dev": [ + { + "name": "laravel/pint", + "version": "v1.19.0", + "source": { + "type": "git", + "url": "https://github.com/laravel/pint.git", + "reference": "8169513746e1bac70c85d6ea1524d9225d4886f0" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/laravel/pint/zipball/8169513746e1bac70c85d6ea1524d9225d4886f0", + "reference": "8169513746e1bac70c85d6ea1524d9225d4886f0", + "shasum": "" + }, + "require": { + "ext-json": "*", + "ext-mbstring": "*", + "ext-tokenizer": "*", + "ext-xml": "*", + "php": "^8.1.0" + }, + "require-dev": { + "friendsofphp/php-cs-fixer": "^3.66.0", + "illuminate/view": "^10.48.25", + "larastan/larastan": "^2.9.12", + "laravel-zero/framework": "^10.48.25", + "mockery/mockery": "^1.6.12", + "nunomaduro/termwind": "^1.17.0", + "pestphp/pest": "^2.36.0" + }, + "bin": [ + "builds/pint" + ], + "type": "project", + "autoload": { + "psr-4": { + "App\\": "app/", + "Database\\Seeders\\": "database/seeders/", + "Database\\Factories\\": "database/factories/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Nuno Maduro", + "email": "enunomaduro@gmail.com" + } + ], + "description": "An opinionated code formatter for PHP.", + "homepage": "https://laravel.com", + "keywords": [ + "format", + "formatter", + "lint", + "linter", + "php" + ], + "support": { + "issues": "https://github.com/laravel/pint/issues", + "source": "https://github.com/laravel/pint" + }, + "time": "2024-12-30T16:20:10+00:00" + } + ], "aliases": [], "minimum-stability": "stable", "stability-flags": {}, diff --git a/index.php b/index.php index b120846..09a040c 100644 --- a/index.php +++ b/index.php @@ -2,12 +2,12 @@ use TweakPHP\Client\Loader; -require __DIR__ . '/vendor/autoload.php'; +require __DIR__.'/vendor/autoload.php'; $arguments = $argv; if (count($arguments) < 3) { - echo 'Invalid arguments' . PHP_EOL; + echo 'Invalid arguments'.PHP_EOL; exit(1); } @@ -23,7 +23,7 @@ function dd(...$args) $loader = Loader::load($arguments[1]); if ($loader === null) { - echo 'Invalid path' . PHP_EOL; + echo 'Invalid path'.PHP_EOL; exit(1); } @@ -34,8 +34,8 @@ function dd(...$args) 'execute', ]; -if (!in_array($arguments[2], $supportedCommands)) { - echo 'Invalid command' . PHP_EOL; +if (! in_array($arguments[2], $supportedCommands)) { + echo 'Invalid command'.PHP_EOL; exit(1); } @@ -46,13 +46,13 @@ function dd(...$args) 'version' => $loader->version(), 'php_version' => phpversion(), ]); - echo $info . PHP_EOL; + echo $info.PHP_EOL; break; case 'execute': if (count($arguments) < 4) { - echo 'Invalid arguments' . PHP_EOL; + echo 'Invalid arguments'.PHP_EOL; exit(1); } - echo $loader->execute(base64_decode($arguments[3])) . PHP_EOL; + echo $loader->execute(base64_decode($arguments[3])).PHP_EOL; break; -} \ No newline at end of file +} diff --git a/src/Loader.php b/src/Loader.php index cdff4b7..483957a 100644 --- a/src/Loader.php +++ b/src/Loader.php @@ -12,7 +12,6 @@ class Loader { /** - * @param string $path * @return null|LoaderInterface */ public static function load(string $path) diff --git a/src/Loaders/BaseLoader.php b/src/Loaders/BaseLoader.php index 518e17c..8d5c972 100644 --- a/src/Loaders/BaseLoader.php +++ b/src/Loaders/BaseLoader.php @@ -43,7 +43,7 @@ public function init() ]); } - $this->tinker = new Tinker(new CustomOutputModifier(), $config); + $this->tinker = new Tinker(new CustomOutputModifier, $config); } public function execute(string $code) @@ -52,4 +52,4 @@ public function execute(string $code) echo trim($output); } -} \ No newline at end of file +} diff --git a/src/Loaders/ComposerLoader.php b/src/Loaders/ComposerLoader.php index 8a07505..5be2fa4 100644 --- a/src/Loaders/ComposerLoader.php +++ b/src/Loaders/ComposerLoader.php @@ -4,21 +4,14 @@ class ComposerLoader extends BaseLoader { - /** - * @param string $path - * @return bool - */ public static function supports(string $path): bool { - return file_exists($path . '/vendor/autoload.php'); + return file_exists($path.'/vendor/autoload.php'); } - /** - * @param string $path - */ public function __construct(string $path) { - require $path . '/vendor/autoload.php'; + require $path.'/vendor/autoload.php'; } public function name(): string @@ -26,11 +19,8 @@ public function name(): string return 'Composer Project'; } - /** - * @return string - */ public function version(): string { - return ""; + return ''; } } diff --git a/src/Loaders/LaravelLoader.php b/src/Loaders/LaravelLoader.php index 7055204..19ba83c 100644 --- a/src/Loaders/LaravelLoader.php +++ b/src/Loaders/LaravelLoader.php @@ -2,42 +2,27 @@ namespace TweakPHP\Client\Loaders; -use Throwable; - class LaravelLoader extends ComposerLoader { private $app; - /** - * @param string $path - * @return bool - */ public static function supports(string $path): bool { - return file_exists($path . '/vendor/autoload.php') && file_exists($path . '/bootstrap/app.php'); + return file_exists($path.'/vendor/autoload.php') && file_exists($path.'/bootstrap/app.php'); } - /** - * @param string $path - */ public function __construct(string $path) { parent::__construct($path); - $this->app = require_once $path . '/bootstrap/app.php'; + $this->app = require_once $path.'/bootstrap/app.php'; $this->app->make('Illuminate\Contracts\Console\Kernel')->bootstrap(); } - /** - * @return string - */ public function name(): string { return 'Laravel'; } - /** - * @return string - */ public function version(): string { return $this->app->version(); diff --git a/src/Loaders/LoaderInterface.php b/src/Loaders/LoaderInterface.php index cc58f84..5fbfb2f 100644 --- a/src/Loaders/LoaderInterface.php +++ b/src/Loaders/LoaderInterface.php @@ -4,20 +4,10 @@ interface LoaderInterface { - /** - * @param string $path - * @return bool - */ public static function supports(string $path): bool; - /** - * @return string - */ public function name(): string; - /** - * @return string - */ public function version(): string; /** @@ -26,7 +16,6 @@ public function version(): string; public function init(); /** - * @param string $code * @return string */ public function execute(string $code); diff --git a/src/Loaders/PimcoreLoader.php b/src/Loaders/PimcoreLoader.php index 895f47a..329aacc 100644 --- a/src/Loaders/PimcoreLoader.php +++ b/src/Loaders/PimcoreLoader.php @@ -8,7 +8,7 @@ class PimcoreLoader extends BaseLoader { public function __construct(string $path) { - require_once $path . '/vendor/autoload.php'; + require_once $path.'/vendor/autoload.php'; if (class_exists('\Pimcore\Bootstrap')) { \Pimcore\Bootstrap::setProjectRoot(); @@ -29,6 +29,6 @@ public function version(): string public static function supports(string $path): bool { - return file_exists($path . '/vendor/autoload.php') && file_exists($path . '/vendor/pimcore/pimcore'); + return file_exists($path.'/vendor/autoload.php') && file_exists($path.'/vendor/pimcore/pimcore'); } } diff --git a/src/Loaders/SymfonyLoader.php b/src/Loaders/SymfonyLoader.php index 92cbe7d..575b659 100644 --- a/src/Loaders/SymfonyLoader.php +++ b/src/Loaders/SymfonyLoader.php @@ -8,9 +8,9 @@ class SymfonyLoader extends ComposerLoader public static function supports(string $path): bool { - return file_exists($path . '/vendor/autoload.php') && - file_exists($path . '/symfony.lock') && - file_exists($path . '/src/Kernel.php'); + return file_exists($path.'/vendor/autoload.php') && + file_exists($path.'/symfony.lock') && + file_exists($path.'/src/Kernel.php'); } public function __construct(string $path) @@ -18,7 +18,7 @@ public function __construct(string $path) parent::__construct($path); // Include the Composer autoloader - require_once $path . '/vendor/autoload.php'; + require_once $path.'/vendor/autoload.php'; // Initialize the Symfony Kernel $env = $_SERVER['APP_ENV'] ?? 'dev'; @@ -31,7 +31,7 @@ public function __construct(string $path) private function findKernelClass(string $path): string { - require_once $path . '/src/Kernel.php'; + require_once $path.'/src/Kernel.php'; return 'App\\Kernel'; } diff --git a/src/Loaders/WordPressLoader.php b/src/Loaders/WordPressLoader.php index 9a34fbb..63c5249 100644 --- a/src/Loaders/WordPressLoader.php +++ b/src/Loaders/WordPressLoader.php @@ -6,23 +6,16 @@ class WordPressLoader extends BaseLoader { - /** - * @param string $path - * @return bool - */ public static function supports(string $path): bool { - return file_exists($path . '/wp-load.php'); + return file_exists($path.'/wp-load.php'); } - /** - * @param string $path - */ public function __construct(string $path) { - require_once $path . '/wp-load.php'; - require_once $path . '/wp-admin/includes/admin.php'; - require_once $path . '/wp-includes/pluggable.php'; + require_once $path.'/wp-load.php'; + require_once $path.'/wp-admin/includes/admin.php'; + require_once $path.'/wp-includes/pluggable.php'; } public function name(): string @@ -30,9 +23,6 @@ public function name(): string return 'WordPress'; } - /** - * @return string - */ public function version(): string { try { @@ -41,6 +31,6 @@ public function version(): string // } - return ""; + return ''; } } diff --git a/src/OutputModifiers/CustomOutputModifier.php b/src/OutputModifiers/CustomOutputModifier.php index 90fd116..6cf06aa 100644 --- a/src/OutputModifiers/CustomOutputModifier.php +++ b/src/OutputModifiers/CustomOutputModifier.php @@ -9,4 +9,4 @@ public function modify(string $output = ''): string // remove only the first tab from each line return preg_replace('/^ {2}/m', '', $output); } -} \ No newline at end of file +} diff --git a/src/OutputModifiers/OutputModifier.php b/src/OutputModifiers/OutputModifier.php index 8e45782..d2beadb 100644 --- a/src/OutputModifiers/OutputModifier.php +++ b/src/OutputModifiers/OutputModifier.php @@ -5,4 +5,4 @@ interface OutputModifier { public function modify(string $output = ''): string; -} \ No newline at end of file +} diff --git a/src/Tinker.php b/src/Tinker.php index 425b293..c432906 100644 --- a/src/Tinker.php +++ b/src/Tinker.php @@ -18,7 +18,7 @@ class Tinker public function __construct(OutputModifier $outputModifier, Configuration $config) { - $this->output = new BufferedOutput(); + $this->output = new BufferedOutput; $this->shell = $this->createShell($this->output, $config); @@ -29,13 +29,23 @@ public function execute(string $phpCode): string { $phpCode = $this->removeComments($phpCode); - $this->shell->addInput($phpCode); - $closure = new ExecutionLoopClosure($this->shell); - $closure->execute(); + $phpCode = explode("\n", $phpCode); - $output = $this->cleanOutput($this->output->fetch()); + $output = ''; - return $this->outputModifier->modify($output); + foreach ($phpCode as $key => $line) { + $this->shell->addInput($line); + $closure = new ExecutionLoopClosure($this->shell); + $closure->execute(); + $result = $this->outputModifier->modify($this->cleanOutput($this->output->fetch())); + if (trim($result) !== '' && trim($result) !== 'null') { + $output .= 'Line '.$key + 1 .' Result:'.PHP_EOL; + $output .= $result; + $output .= PHP_EOL; + } + } + + return $output; } protected function createShell(BufferedOutput $output, Configuration $config): Shell @@ -49,12 +59,12 @@ protected function createShell(BufferedOutput $output, Configuration $config): S public function removeComments(string $code): string { - $tokens = token_get_all("'); + $tokens = token_get_all("'); $result = ''; foreach ($tokens as $token) { if (is_array($token)) { - list($id, $text) = $token; + [$id, $text] = $token; if (in_array($id, [T_COMMENT, T_DOC_COMMENT, T_OPEN_TAG, T_CLOSE_TAG])) { continue; From 99fa9e90ed72010713ae1ef29e6b61ff3452012a Mon Sep 17 00:00:00 2001 From: Saeed Vaziry Date: Mon, 13 Jan 2025 19:58:07 +0100 Subject: [PATCH 5/5] tinker --- src/Loaders/BaseLoader.php | 10 ++++++---- src/Loaders/LoaderInterface.php | 10 ++-------- src/Loaders/WordPressLoader.php | 4 +++- src/Tinker.php | 24 ++++++++---------------- 4 files changed, 19 insertions(+), 29 deletions(-) diff --git a/src/Loaders/BaseLoader.php b/src/Loaders/BaseLoader.php index 2930105..d58db38 100644 --- a/src/Loaders/BaseLoader.php +++ b/src/Loaders/BaseLoader.php @@ -11,7 +11,7 @@ abstract class BaseLoader implements LoaderInterface { protected Tinker $tinker; - public function init() + public function init(): void { $config = new Configuration([ 'configFile' => null, @@ -26,7 +26,9 @@ public function init() $config->setVerbosity(Configuration::VERBOSITY_QUIET); $config->setHistoryFile(defined('PHP_WINDOWS_VERSION_BUILD') ? 'null' : '/dev/null'); $config->setRawOutput(false); - $config->setUsePcntl(false); + if (getenv('KUBERNETES_SERVICE_HOST') || defined('PHP_WINDOWS_VERSION_BUILD')) { + $config->setUsePcntl(false); + } if (class_exists('Illuminate\Support\Collection') && class_exists('Laravel\Tinker\TinkerCaster')) { $config->getPresenter()->addCasters([ @@ -47,10 +49,10 @@ public function init() $this->tinker = new Tinker(new CustomOutputModifier, $config); } - public function execute(string $code) + public function execute(string $code): string { $output = $this->tinker->execute($code); - echo trim($output); + return trim($output); } } diff --git a/src/Loaders/LoaderInterface.php b/src/Loaders/LoaderInterface.php index 5fbfb2f..e445ea7 100644 --- a/src/Loaders/LoaderInterface.php +++ b/src/Loaders/LoaderInterface.php @@ -10,13 +10,7 @@ public function name(): string; public function version(): string; - /** - * @return void - */ - public function init(); + public function init(): void; - /** - * @return string - */ - public function execute(string $code); + public function execute(string $code): string; } diff --git a/src/Loaders/WordPressLoader.php b/src/Loaders/WordPressLoader.php index 63c5249..87f9883 100644 --- a/src/Loaders/WordPressLoader.php +++ b/src/Loaders/WordPressLoader.php @@ -26,7 +26,9 @@ public function name(): string public function version(): string { try { - return get_bloginfo('version'); + if (function_exists('get_bloginfo')) { + return get_bloginfo('version'); + } } catch (Throwable $e) { // } diff --git a/src/Tinker.php b/src/Tinker.php index c432906..865557c 100644 --- a/src/Tinker.php +++ b/src/Tinker.php @@ -29,23 +29,15 @@ public function execute(string $phpCode): string { $phpCode = $this->removeComments($phpCode); - $phpCode = explode("\n", $phpCode); - - $output = ''; - - foreach ($phpCode as $key => $line) { - $this->shell->addInput($line); - $closure = new ExecutionLoopClosure($this->shell); - $closure->execute(); - $result = $this->outputModifier->modify($this->cleanOutput($this->output->fetch())); - if (trim($result) !== '' && trim($result) !== 'null') { - $output .= 'Line '.$key + 1 .' Result:'.PHP_EOL; - $output .= $result; - $output .= PHP_EOL; - } - } + $this->shell->addInput($phpCode); + + $closure = new ExecutionLoopClosure($this->shell); + + $closure->execute(); + + $output = $this->cleanOutput($this->output->fetch()); - return $output; + return $this->outputModifier->modify($output); } protected function createShell(BufferedOutput $output, Configuration $config): Shell