View source
<?php
namespace Drupal\advagg\Form;
use Drupal\advagg\AdvaggSettersTrait;
use Drupal\Component\Utility\Xss;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class InfoForm extends ConfigFormBase {
use AdvaggSettersTrait;
protected $themeRegistry;
protected $requestStack;
protected $dateFormatter;
protected $translation;
protected $cache;
protected $messenger;
public static function create(ContainerInterface $container) {
$instance = parent::create($container);
$instance
->setThemeRegistry($container
->get('theme.registry'));
$instance
->setRequestStack($container
->get('request_stack'));
$instance
->setDateFomatter($container
->get('date.formatter'));
$instance
->setStringTranslation($container
->get('string_translation'));
$instance
->setCache($container
->get('cache.advagg'));
return $instance;
}
public function getFormId() {
return 'advagg_info';
}
protected function getEditableConfigNames() {
return [];
}
public function buildForm(array $form, FormStateInterface $form_state) {
$form['tip'] = [
'#markup' => '<p>' . $this
->t('This page provides debugging information. There are no configuration options here.') . '</p>',
];
$core_hooks = $this->themeRegistry
->get();
$advagg_hooks = advagg_hooks_implemented();
$form['theme_info'] = [
'#type' => 'details',
'#open' => TRUE,
'#title' => $this
->t('Hook Theme Info'),
];
$data = implode("\n", $core_hooks['html']['preprocess functions']);
$form['theme_info']['advagg_theme_info'] = [
'#markup' => '<p>preprocess functions on html.</p><pre>' . $data . '</pre>',
];
$form['hooks_implemented'] = [
'#type' => 'details',
'#open' => TRUE,
'#title' => $this
->t('Core asset hooks implemented by modules'),
];
foreach ($advagg_hooks as $hook => $values) {
if (empty($values)) {
$form['hooks_implemented'][$hook] = [
'#markup' => '<div><strong>' . $hook . ':</strong> 0</div>',
];
}
else {
$form['hooks_implemented'][$hook] = [
'#markup' => '<div><strong>' . $hook . ':</strong> ' . count($values) . $this
->formatList($values) . '</div>',
];
}
}
$form['get_info_about_agg'] = [
'#type' => 'details',
'#open' => TRUE,
'#title' => $this
->t('Get detailed info about an optimized file'),
];
$form['get_info_about_agg']['filename'] = [
'#type' => 'textfield',
'#size' => 170,
'#maxlength' => 256,
'#default_value' => '',
'#title' => $this
->t('Filename'),
];
$form['get_info_about_agg']['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Lookup Details'),
'#submit' => [
'::getFileInfoSubmit',
],
'#validate' => [
'::getFileInfoValidate',
],
'#ajax' => [
'callback' => '::getFileInfoAjax',
'wrapper' => 'advagg-file-info-ajax',
'effect' => 'fade',
],
];
if ($tip = $this
->getRandomFile()) {
$form['get_info_about_agg']['tip'] = [
'#markup' => '<p>' . $this
->t('Input an optimized filename like "@css_file".', [
'@css_file' => $tip,
]) . '</p>',
];
}
$form['get_info_about_agg']['wrapper'] = [
'#prefix' => "<div id='advagg-file-info-ajax'>",
'#suffix' => "</div>",
];
$form = parent::buildForm($form, $form_state);
unset($form['actions']);
return $form;
}
private function formatList(array $list, $depth = 1) {
$spacer = '<br />' . str_repeat(' ', 2 * $depth);
$output = $spacer . Xss::filter(implode($spacer, $list), [
'br',
]);
return $output;
}
public function getFileInfoSubmit(array &$form, FormStateInterface $form_state) {
$this
->messenger()
->addMessage($this
->getFileInfo($form_state
->getValue('filename')));
}
public function getFileInfoAjax(array &$form) {
return $form['get_info_about_agg']['wrapper'];
}
public function getFileInfoValidate(array $form, FormStateInterface $form_state) {
if (empty($form_state
->getValue('filename'))) {
$form_state
->setErrorByName('filename', $this
->t('Please input a valid optimized filename.'));
}
}
private function getFileInfo($filename) {
if (substr_compare($filename, 'css_', 0) || substr_compare($filename, 'js_', 0)) {
$cid = str_replace([
'css_',
'js_',
'.css',
'.js',
], '', $filename);
$cid = substr($cid, 0, strpos($cid, '.'));
if ($cached = $this->cache
->get($cid, TRUE)) {
return print_r($cached->data, TRUE);
}
}
return $this
->t('Optimized file information not found, confirm spelling of the path. Alternatively, that could be an outdated file.');
}
private function getRandomFile() {
$dir = 'public://js/optimized/';
if (!is_dir($dir)) {
return FALSE;
}
if ($handler = opendir($dir)) {
while (($file = readdir($handler)) !== FALSE) {
if (is_file($dir . $file) && pathinfo($file, PATHINFO_EXTENSION) == 'js') {
closedir($handler);
return $file;
}
}
closedir($handler);
}
return FALSE;
}
}