X-Git-Url: http://www.aleph1.co.uk/gitweb/?a=blobdiff_plain;f=web%2Fmodules%2Fcontrib%2Fadvagg%2Fsrc%2FForm%2FInfoForm.php;fp=web%2Fmodules%2Fcontrib%2Fadvagg%2Fsrc%2FForm%2FInfoForm.php;h=fb93a55ea0d3d0e4fb9db49fc1a81f9ed9c8dd91;hb=a2bd1bf0c2c1f1a17d188f4dc0726a45494cefae;hp=0000000000000000000000000000000000000000;hpb=57c063afa3f66b07c4bbddc2d6129a96d90f0aad;p=yaffs-website diff --git a/web/modules/contrib/advagg/src/Form/InfoForm.php b/web/modules/contrib/advagg/src/Form/InfoForm.php new file mode 100644 index 000000000..fb93a55ea --- /dev/null +++ b/web/modules/contrib/advagg/src/Form/InfoForm.php @@ -0,0 +1,389 @@ +themeRegistry = $theme_registry; + $this->advaggFiles = $advagg_files; + $this->advaggAggregates = $advagg_aggregates; + $this->requestStack = $request_stack; + $this->dateFormatter = $date_formatter; + $this->translation = $string_translation; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container) { + return new static( + $container->get('config.factory'), + $container->get('theme.registry'), + $container->get('state.advagg.files'), + $container->get('state.advagg.aggregates'), + $container->get('request_stack'), + $container->get('date.formatter'), + $container->get('string_translation') + ); + } + + /** + * {@inheritdoc} + */ + public function getFormId() { + return 'advagg_info'; + } + + /** + * {@inheritdoc} + */ + protected function getEditableConfigNames() { + return []; + } + + /** + * {@inheritdoc} + */ + public function buildForm(array $form, FormStateInterface $form_state) { + $form = []; + $form['tip'] = [ + '#markup' => '

' . $this->t('This page provides debugging information. There are no configuration options here.') . '

', + ]; + + // Get all hooks and variables. + $core_hooks = $this->themeRegistry->get(); + $advagg_hooks = advagg_hooks_implemented(); + + // Output html preprocess functions hooks. + $form['theme_info'] = [ + '#type' => 'details', + '#title' => $this->t('Hook Theme Info'), + ]; + $data = implode("\n", $core_hooks['html']['preprocess functions']); + $form['theme_info']['advagg_theme_info'] = [ + '#markup' => '

preprocess functions on html.

' . $data . '
', + ]; + + $file_data = $this->advaggFiles->getAll(); + + // Get all parent css and js files. + $types = ['css', 'js']; + foreach ($types as $type) { + $form[$type] = [ + '#type' => 'details', + '#title' => $this->t('@type files', ['@type' => Unicode::strtoupper($type)]), + ]; + } + foreach ($file_data as $name => $info) { + if (!in_array($info['fileext'], $types)) { + continue; + } + $form[$info['fileext']][$info['filename_hash']] = [ + '#markup' => '
' . $this->translation->formatPlural($info['changes'], 'changed 1 time - %file
', 'changed %changes times - %file
', [ + '%changes' => $info['changes'], + '%file' => $name, + ]) . '
' . print_r($info, TRUE) . '
', + ]; + } + + // Display as module -> hook instead of hook -> module. + ksort($advagg_hooks); + $module_hooks = []; + foreach ($advagg_hooks as $hook => $values) { + if (!empty($values)) { + foreach ($values as $module_name) { + if (!isset($module_hooks[$module_name])) { + $module_hooks[$module_name] = []; + } + $module_hooks[$module_name][] = $hook; + } + } + else { + $module_hooks['not in use'][] = $hook; + } + } + ksort($module_hooks); + + $form['modules_implementing_advagg'] = [ + '#type' => 'details', + '#title' => $this->t('Modules implementing aggregate hooks'), + ]; + $form['hooks_implemented'] = [ + '#type' => 'details', + '#title' => $this->t('AdvAgg CSS/JS hooks implemented by modules'), + ]; + + // Output all advagg hooks implemented. + foreach ($module_hooks as $hook => $values) { + if (empty($values)) { + $form['modules_implementing_advagg'][$hook] = [ + '#markup' => '
' . $hook . ': 0
', + ]; + } + else { + $form['modules_implementing_advagg'][$hook] = [ + '#markup' => '
' . $hook . ': ' . count($values) . $this->formatList($values) . '
', + ]; + } + } + + // Output all advagg hooks implemented. + foreach ($advagg_hooks as $hook => $values) { + if (empty($values)) { + $form['hooks_implemented'][$hook] = [ + '#markup' => '
' . $hook . ': 0
', + ]; + } + else { + $form['hooks_implemented'][$hook] = [ + '#markup' => '
' . $hook . ': ' . count($values) . $this->formatList($values) . '
', + ]; + } + } + + // Output what is used inside of advagg_get_current_hooks_hash(). + $form['hooks_variables_hash'] = [ + '#type' => 'details', + '#title' => $this->t('Hooks And Variables Used In Hash'), + ]; + $form['hooks_variables_hash']['description'] = [ + '#markup' => $this->t('Current Value: %value. Below is the listing of variables and hooks used to generate the 3rd hash of an aggregates filename.', ['%value' => advagg_get_current_hooks_hash()]), + ]; + $form['hooks_variables_hash']['output'] = [ + // @ignore production_php + '#markup' => '
' . print_r(advagg_current_hooks_hash_array(), TRUE) . '
', + ]; + // Get info about a file. + $form['get_info_about_agg'] = [ + '#type' => 'details', + '#open' => TRUE, + '#title' => $this->t('Get detailed info about an aggregate 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', + ], + ]; + $form['get_info_about_agg']['tip'] = [ + '#markup' => '

' . $this->t('Takes input like "@css_file" or a full aggregate name like "@advagg_js"', [ + '@css_file' => $this->advaggFiles->getRandomKey(), + '@advagg_js' => $this->advaggAggregates->getRandom()['uid'], + ]) . '

', + ]; + $form['get_info_about_agg']['wrapper'] = [ + '#prefix' => "
", + '#suffix' => "
", + ]; + $form = parent::buildForm($form, $form_state); + unset($form['actions']); + return $form; + } + + /** + * Format an indented list from array. + * + * @param array $list + * The array to convert to a string. + * @param int $depth + * (optional) Depth multiplier for indentation. + * + * @return string + * The imploded and spaced array. + */ + private function formatList(array $list, $depth = 1) { + $spacer = '
' . str_repeat(' ', 2 * $depth); + $output = $spacer . Xss::filter(implode($spacer, $list), ['br']); + return $output; + } + + /** + * Display file info in a drupal message. + * + * @param array $form + * An associative array containing the structure of the form. + * @param \Drupal\Core\Form\FormStateInterface $form_state + * The current state of the form. + */ + public function getFileInfoSubmit(array &$form, FormStateInterface $form_state) { + $info = $this->getFileInfo($form_state->getValue('filename')); + $output = '
' . print_r($info, TRUE) . '
'; + if (!$this->isAjax()) { + drupal_set_message($output); + } + } + + /** + * Display file info via ajax callback. + * + * @param array $form + * An associative array containing the structure of the form. + * @param \Drupal\Core\Form\FormStateInterface $form_state + * The current state of the form. + */ + public function getFileInfoAjax(array &$form, FormStateInterface $form_state) { + $element = $form['get_info_about_agg']['wrapper']; + if ($form_state->hasAnyErrors()) { + return $element; + } + $info = $this->getFileInfo($form_state->getValue('filename')); + if (empty($info)) { + $form_state->setErrorByName('filename', $this->t('Please input a valid aggregate filename.')); + return $element; + } + else { + $element['#markup'] = '
' . print_r($info, TRUE) . '
'; + return $element; + } + } + + /** + * Verify that the filename is correct. + * + * @param array $form + * An associative array containing the structure of the form. + * @param \Drupal\Core\Form\FormStateInterface $form_state + * The current state of the form. + */ + public function getFileInfoValidate(array &$form, FormStateInterface $form_state) { + if (empty($form_state->getValue('filename'))) { + $form_state->setErrorByName('filename', $this->t('Please input an aggregate filename.')); + } + } + + /** + * Get detailed info about the given filename. + * + * @param string $filename + * Name of file to lookup. + * + * @return array + * Returns an array of detailed info about this file. + */ + private function getFileInfo($filename) { + // Strip quotes and trim. + $filename = trim(str_replace(['"', "'"], '', $filename)); + if (substr_compare($filename, 'css_', 0) > 0 || substr_compare($filename, 'js_', 0) > 0) { + $results = array_column($this->advaggAggregates->getAll(), NULL, 'uid'); + if (isset($results[$filename])) { + return $results[$filename]; + } + else { + return "Aggregate name unrecognized, confirm spelling, otherwise likely a very old aggregate that has been expunged."; + } + } + elseif ($data = $this->advaggFiles->get($filename)) { + $data['File modification date'] = $this->dateFormatter->format($data['mtime'], 'html_datetime'); + $data['Information last update'] = $this->dateFormatter->format($data['updated'], 'html_datetime'); + return $data; + } + else { + return "File not found and AdvAgg has no record of it. Confirm spelling of the path."; + } + } + + /** + * Checks if the form was submitted by AJAX. + * + * @return bool + * TRUE if the form was submitted via AJAX, otherwise FALSE. + */ + private function isAjax() { + $request = $this->requestStack->getCurrentRequest(); + if ($request->query->has(FormBuilderInterface::AJAX_FORM_REQUEST)) { + return TRUE; + } + return FALSE; + } + +}