From cfbe050bbf0cfe261f4aa44b0853ee75d85f6e08 Mon Sep 17 00:00:00 2001 From: brandonkelly Date: Wed, 11 Jan 2023 14:27:36 -0800 Subject: [PATCH 1/2] `exec` command --- src/console/controllers/ExecController.php | 65 ++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 src/console/controllers/ExecController.php diff --git a/src/console/controllers/ExecController.php b/src/console/controllers/ExecController.php new file mode 100644 index 00000000000..8e29fab121a --- /dev/null +++ b/src/console/controllers/ExecController.php @@ -0,0 +1,65 @@ + + * @since 4.4.0 + */ +class ExecController extends Controller +{ + /** + * @inheritdoc + */ + public $defaultAction = 'exec'; + + /** + * Executes a PHP statement and outputs the result. + * + * @param string $command + * @return int + */ + public function actionExec(string $command): int + { + ob_start(); + + try { + eval("\$result = $command;"); + $showResult = true; + } catch (ParseError) { + eval("$command;"); + $showResult = false; + } + + $output = ob_get_clean(); + + if ($showResult) { + // Dump the result + $this->stdout('= ', Console::FG_GREY); + /** @var mixed $result */ + /** @phpstan-ignore-next-line */ + $dump = Craft::dump($result, return: true); + $this->stdout(trim(preg_replace('/^/m', ' ', trim($dump))) . "\n\n"); + } + + if ($output !== '') { + $this->stdout("Output:\n", Console::FG_GREY); + $this->stdout("$output\n\n"); + } + + return ExitCode::OK; + } +} From c93769de45d1275416082886f07f5ae2c001fb42 Mon Sep 17 00:00:00 2001 From: brandonkelly Date: Wed, 11 Jan 2023 14:33:50 -0800 Subject: [PATCH 2/2] Release note [ci skip] --- CHANGELOG-WIP.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG-WIP.md b/CHANGELOG-WIP.md index 4714b42262d..dabcfefccb5 100644 --- a/CHANGELOG-WIP.md +++ b/CHANGELOG-WIP.md @@ -26,6 +26,7 @@ - The `plugin/install`, `plugin/uninstall`, `plugin/enable`, and `plugin/disabled` commands now support an `--all` option, which applies the action to all applicable Composer-installed plugins. ([#11373](https://github.com/craftcms/cms/discussions/11373), [#12218](https://github.com/craftcms/cms/pull/12218)) ### Development +- Added the `exec` command, which executes an individual PHP statement and outputs the result. ([#12528](https://github.com/craftcms/cms/pull/12528)) - Added the `editable` and `savable` asset query params. ([#12266](https://github.com/craftcms/cms/pull/12266)) - Added the `savable` entry query param. ([#12266](https://github.com/craftcms/cms/pull/12266)) - The `editable` entry query param can now be set to `false` to only show entries that _can’t_ be viewed by the current user. ([#12266](https://github.com/craftcms/cms/pull/12266))