Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

exec command #12528

Merged
merged 2 commits into from
Jan 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG-WIP.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
65 changes: 65 additions & 0 deletions src/console/controllers/ExecController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/

namespace craft\console\controllers;

use Craft;
use craft\console\Controller;
use craft\helpers\Console;
use ParseError;
use yii\console\ExitCode;

/**
* Executes a PHP statement and outputs the result.
*
* @author Pixel & Tonic, Inc. <[email protected]>
* @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;
}
}