ExecutePHP.php in Devel PHP 8
File
src/Form/ExecutePHP.php
View source
<?php
declare (strict_types=1);
namespace Drupal\devel_php\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ExecutePHP extends FormBase {
protected $develDumper;
protected $session;
public static function create(ContainerInterface $container) : self {
$instance = parent::create($container);
$instance->develDumper = $container
->get('devel.dumper');
$instance->session = $container
->get('session');
return $instance;
}
public function getFormId() {
return 'devel_execute_form';
}
public function buildForm(array $form, FormStateInterface $form_state, bool $details_open = TRUE) : array {
$form['#redirect'] = FALSE;
$code = $this->session
->get('devel_execute_code', '');
$form['execute'] = [
'#type' => 'details',
'#title' => $this
->t('PHP code to execute'),
'#open' => !empty($code) || $details_open,
];
$form['execute']['code'] = [
'#type' => 'textarea',
'#title' => $this
->t('PHP code to execute'),
'#title_display' => 'invisible',
'#description' => $this
->t('Enter some code. Do not use <code><?php ?></code> tags.'),
'#default_value' => $code,
'#rows' => 20,
'#attributes' => [
'style' => 'font-family: monospace; font-size: 1.25em;',
],
];
$form['execute']['op'] = [
'#type' => 'submit',
'#value' => $this
->t('Execute'),
];
if ($this->session
->has('devel_execute_code')) {
$this->session
->remove('devel_execute_code');
}
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) : void {
$code = $form_state
->getValue('code');
try {
ob_start();
print eval($code);
$this->develDumper
->message(ob_get_clean());
} catch (\Throwable $error) {
$this
->messenger()
->addError($error
->getMessage());
}
$this->session
->set('devel_execute_code', $code);
}
}
Classes
Name |
Description |
ExecutePHP |
Defines a form that allows privileged users to execute arbitrary PHP code. |