You are here

class ConfigForm in XHProf 8

Provides a form to configure profiling settings.

Hierarchy

Expanded class hierarchy of ConfigForm

1 string reference to 'ConfigForm'
xhprof.routing.yml in ./xhprof.routing.yml
xhprof.routing.yml

File

src/Form/ConfigForm.php, line 12

Namespace

Drupal\xhprof\Form
View source
class ConfigForm extends ConfigFormBase {

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {
    return [
      'xhprof.config',
    ];
  }

  /**
   * The storage.
   *
   * @var \Drupal\xhprof\XHProfLib\Storage\StorageManager
   */
  private $storageManager;

  /**
   * The profiler.
   *
   * @var \Drupal\xhprof\ProfilerInterface
   */
  private $profiler;

  /**
   * The module handler.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  private $moduleHandler;

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    $instance = parent::create($container);
    $instance->storageManager = $container
      ->get('xhprof.storage_manager');
    $instance->profiler = $container
      ->get('xhprof.profiler');
    $instance->moduleHandler = $container
      ->get('module_handler');
    return $instance;
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'xhprof_config';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $config = $this
      ->config('xhprof.config');
    $extension_loaded = $this->profiler
      ->isLoaded();
    if ($extension_loaded) {
      $help = $this
        ->t('Profile requests with the XHProf or Tideways php extension.');
    }
    else {
      $help = $this
        ->t('You must enable the <a href=":xhprof">XHProf</a> or <a href=":tideways">Tideways</a> php extension.', [
        ':xhprof' => 'https://www.drupal.org/node/946182',
        ':tideways' => 'https://github.com/tideways/php-xhprof-extension',
      ]);
    }
    $form['help'] = [
      '#type' => 'inline_template',
      '#template' => '<span class="warning">{{ help }}</span>',
      '#context' => [
        'help' => $help,
      ],
    ];
    $form['enabled'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Enable profiling of page views.'),
      '#default_value' => $extension_loaded & $config
        ->get('enabled'),
      '#disabled' => !$extension_loaded,
    ];
    $form['settings'] = [
      '#title' => $this
        ->t('Profiling settings'),
      '#type' => 'details',
      '#open' => TRUE,
      '#states' => [
        'invisible' => [
          'input[name="enabled"]' => [
            'checked' => FALSE,
          ],
        ],
      ],
    ];
    $form['settings']['extension'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Extension'),
      '#options' => $this->profiler
        ->getExtensions(),
      '#default_value' => $config
        ->get('extension'),
      '#description' => $this
        ->t('Choose the extension to use for profiling. The recommended extension is <a href=":url">%profiler</a> because it is actively maintained.', [
        ':url' => 'https://github.com/longxinH/xhprof',
        '%profiler' => 'XHProf',
      ]),
    ];
    $form['settings']['exclude'] = [
      '#type' => 'textarea',
      '#title' => $this
        ->t('Exclude'),
      '#default_value' => $config
        ->get('exclude'),
      '#description' => $this
        ->t('Path to exclude for profiling. One path per line.'),
    ];
    $form['settings']['interval'] = [
      '#type' => 'number',
      '#title' => 'Profiling interval',
      '#min' => 0,
      '#default_value' => $config
        ->get('interval'),
      '#description' => $this
        ->t('The approximate number of requests between XHProf samples. Leave zero to profile all requests.'),
    ];
    $flags = [
      'FLAGS_CPU' => $this
        ->t('Cpu'),
      'FLAGS_MEMORY' => $this
        ->t('Memory'),
      'FLAGS_NO_BUILTINS' => $this
        ->t('Exclude PHP builtin functions'),
    ];
    $form['settings']['flags'] = [
      '#type' => 'checkboxes',
      '#title' => 'Profile',
      '#options' => $flags,
      '#default_value' => $config
        ->get('flags') ?: [],
      '#description' => $this
        ->t('Flags to choose what profile.'),
    ];
    $form['settings']['exclude_indirect_functions'] = [
      '#type' => 'checkbox',
      '#title' => 'Exclude indirect functions',
      '#default_value' => $config
        ->get('exclude_indirect_functions'),
      '#description' => $this
        ->t('Exclude functions like %call_user_func and %call_user_func_array.', [
        '%call_user_func' => 'call_user_func',
        '%call_user_func_array' => 'call_user_func_array',
      ]),
    ];
    $options = $this->storageManager
      ->getStorages();
    $form['settings']['storage'] = [
      '#type' => 'radios',
      '#title' => $this
        ->t('Profile storage'),
      '#default_value' => $config
        ->get('storage'),
      '#options' => $options,
      '#description' => $this
        ->t('Choose the storage class.'),
    ];
    if ($this->moduleHandler
      ->moduleExists('webprofiler')) {
      $form['webprofiler'] = [
        '#title' => $this
          ->t('Webprofiler integration'),
        '#type' => 'details',
        '#open' => TRUE,
        '#states' => [
          'invisible' => [
            'input[name="enabled"]' => [
              'checked' => FALSE,
            ],
          ],
        ],
      ];
      $form['webprofiler']['show_summary_toolbar'] = [
        '#type' => 'checkbox',
        '#title' => $this
          ->t('Show summary data in toolbar.'),
        '#default_value' => $config
          ->get('show_summary_toolbar'),
        '#description' => $this
          ->t('Show data from the overall summary directly into the Webprofiler toolbar. May slow down the toolbar rendering.'),
      ];
    }
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $this
      ->config('xhprof.config')
      ->set('enabled', $form_state
      ->getValue('enabled'))
      ->set('extension', $form_state
      ->getValue('extension'))
      ->set('exclude', $form_state
      ->getValue('exclude'))
      ->set('interval', $form_state
      ->getValue('interval'))
      ->set('storage', $form_state
      ->getValue('storage'))
      ->set('flags', $form_state
      ->getValue('flags'))
      ->set('exclude_indirect_functions', $form_state
      ->getValue('exclude_indirect_functions'))
      ->set('show_summary_toolbar', $form_state
      ->getValue('show_summary_toolbar'))
      ->save();
    parent::submitForm($form, $form_state);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigForm::$moduleHandler private property The module handler.
ConfigForm::$profiler private property The profiler.
ConfigForm::$storageManager private property The storage.
ConfigForm::buildForm public function Form constructor. Overrides ConfigFormBase::buildForm
ConfigForm::create public static function Instantiates a new instance of this class. Overrides ConfigFormBase::create
ConfigForm::getEditableConfigNames protected function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
ConfigForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
ConfigForm::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm
ConfigFormBase::__construct public function Constructs a \Drupal\system\ConfigFormBase object. 11
ConfigFormBaseTrait::config protected function Retrieves a configuration object.
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FormBase::$configFactory protected property The config factory. 1
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::configFactory protected function Gets the config factory for this form. 1
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
FormBase::validateForm public function Form validation handler. Overrides FormInterface::validateForm 62
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.