class CssW3Form in Advanced CSS/JS Aggregation 8.2
Same name and namespace in other branches
- 8.4 advagg_validator/src/Form/CssW3Form.php \Drupal\advagg_validator\Form\CssW3Form
- 8.3 advagg_validator/src/Form/CssW3Form.php \Drupal\advagg_validator\Form\CssW3Form
Configure form for W3C validation of CSS files.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\Core\Form\ConfigFormBase uses ConfigFormBaseTrait
- class \Drupal\advagg\Form\AdvaggFormBase
- class \Drupal\advagg_validator\Form\BaseValidatorForm
- class \Drupal\advagg_validator\Form\CssW3Form
- class \Drupal\advagg_validator\Form\BaseValidatorForm
- class \Drupal\advagg\Form\AdvaggFormBase
- class \Drupal\Core\Form\ConfigFormBase uses ConfigFormBaseTrait
Expanded class hierarchy of CssW3Form
1 string reference to 'CssW3Form'
- advagg_validator.routing.yml in advagg_validator/
advagg_validator.routing.yml - advagg_validator/advagg_validator.routing.yml
File
- advagg_validator/
src/ Form/ CssW3Form.php, line 19
Namespace
Drupal\advagg_validator\FormView source
class CssW3Form extends BaseValidatorForm {
/**
* The Guzzle HTTP Client.
*
* @var \GuzzleHttp\Client
*/
protected $httpClient;
/**
* The Drupal renderer.
*
* @var \Drupal\Core\Render\RendererInterface
*/
protected $renderer;
/**
* {@inheritdoc}
*
* @param \GuzzleHttp\Client $http_client
* The Guzzle HTTP Client.
* @param \Drupal\Core\Render\RendererInterface $renderer
* The Drupal renderer.
*/
public function __construct(ConfigFactoryInterface $config_factory, StateInterface $advagg_files, StateInterface $advagg_aggregates, RequestStack $request_stack, Client $http_client, RendererInterface $renderer) {
parent::__construct($config_factory, $advagg_files, $advagg_aggregates, $request_stack);
$this->requestStack = $request_stack;
$this->httpClient = $http_client;
$this->renderer = $renderer;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('config.factory'), $container
->get('state.advagg.files'), $container
->get('state.advagg.aggregates'), $container
->get('request_stack'), $container
->get('http_client'), $container
->get('renderer'));
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'advagg_validator_cssw3';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$form = parent::generateForm('css', FALSE);
$form['notice'] = [
'#markup' => '<div>' . t('Notice: The form below will submit files to the <a href="http://jigsaw.w3.org/css-validator/">http://jigsaw.w3.org/css-validator/</a> service if used.') . '</div>',
'#weight' => -1,
];
$form = parent::buildForm($form, $form_state);
unset($form['actions']);
return $form;
}
/**
* {@inheritdoc}
*/
public function submitCheckAll(array &$form, FormStateInterface $form_state) {
$dir = $form_state
->getTriggeringElement()['#name'];
$files = [];
foreach ($form_state
->getValues() as $key => $value) {
if (strpos($key, 'hidden') === FALSE || strpos($value, $dir) === FALSE || $dir === '.' && substr_count($value, '/') > 0) {
continue;
}
$files[] = $value;
}
// Check list.
$info = $this
->testFiles($files);
$info = $this
->hideGoodFiles($info);
$output = [
'#theme' => 'item_list',
'#items' => $info,
];
drupal_set_message($this->renderer
->render($output));
}
/**
* Display validation 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 ajaxCheck(array &$form, FormStateInterface $form_state) {
$dir = $form_state
->getTriggeringElement()['#name'];
return $this
->getElement($form, explode('/', $dir))['wrapper'];
}
/**
* {@inheritdoc}
*/
public function submitCheckDirectory(array &$form, FormStateInterface $form_state) {
$dir = $form_state
->getTriggeringElement()['#name'];
$files = [];
$slash_count = substr_count('/' . $dir, '/');
foreach ($form_state
->getValues() as $key => $value) {
if (strpos($key, 'hidden') === FALSE || strpos($value, $dir) === FALSE || substr_count($value, '/') > $slash_count || $dir === '.' && substr_count($value, '/') > 0) {
continue;
}
$files[] = $value;
}
// Check list.
$info = $this
->testFiles($files);
$info = $this
->hideGoodFiles($info);
$output = [
'#theme' => 'item_list',
'#items' => $info,
];
drupal_set_message($this->renderer
->render($output));
}
/**
* {@inheritdoc}
*/
protected function testFiles(array $files, array $options = []) {
$output = [];
$file_info = $this->advaggFiles
->getMultiple($files);
foreach ($files as $filename) {
// Skip missing files.
if (!file_exists($filename)) {
continue;
}
$file_contents = file_get_contents($filename);
$lines = file($filename);
$content_hash = Crypt::hashBase64($file_contents);
// If saved file information not current update filestore.
if ($file_info[$filename]['content_hash'] != $content_hash) {
$this->advagg_files
->scanFile($filename, $file_info[$filename], $file_contents);
}
// If saved validation results available use them rather than re-run.
if (isset($file_info[$filename]['validation']['w3'])) {
$output[$filename]['jigsaw.w3.org'] = $file_info[$filename]['validation']['w3'];
continue;
}
// Run jigsaw.w3.org validator.
$output[$filename]['jigsaw.w3.org'] = $this
->testW3C($filename, $options);
// Get extra context for errors.
if (!empty($output[$filename]['jigsaw.w3.org']['errors'])) {
foreach ($output[$filename]['jigsaw.w3.org']['errors'] as &$value) {
if (isset($value['line'])) {
$value['linedata'] = $lines[$value['line'] - 1];
if (strlen($value['linedata']) > 512) {
unset($value['linedata']);
}
}
}
unset($value);
}
if (!empty($output[$filename]['jigsaw.w3.org']['warnings'])) {
foreach ($output[$filename]['jigsaw.w3.org']['warnings'] as &$value) {
if (isset($value['line'])) {
$value['linedata'] = $lines[$value['line'] - 1];
if (strlen($value['linedata']) > 512) {
unset($value['linedata']);
}
}
}
unset($value);
}
// Save data.
$file_info[$filename]['validation']['w3'] = $output[$filename]['jigsaw.w3.org'];
$this->advaggFiles
->set($filename, $file_info[$filename]);
}
return $output;
}
/**
* Given a CSS file, test to make sure it is valid CSS.
*
* @param string $filename
* The name of the file.
* @param array $validator_options
* List of options to pass along to the CSS Validator.
*
* @return array
* Info from the w3c server.
*/
private function testW3C($filename, array &$validator_options = []) {
// Get CSS files contents.
$validator_options['text'] = file_get_contents($filename);
if (strlen($validator_options['text']) > 50000) {
unset($validator_options['text']);
$validator_options['uri'] = $this->requestStack
->getCurrentRequest()
->getBaseUrl() . $filename;
}
// Add in defaults.
$validator_options += [
'output' => 'soap12',
'warning' => '1',
'profile' => 'css3',
'usermedium' => 'all',
'lang' => 'en',
];
// Build request URL.
// API Documentation http://jigsaw.w3.org/css-validator/api.html
$request_url = 'http://jigsaw.w3.org/css-validator/validator';
$query = http_build_query($validator_options, '', '&');
$url = $request_url . '?' . $query;
try {
$data = $this->httpClient
->get($url)
->getBody();
} catch (RequestException $e) {
watchdog_exception('AdvAgg Validator', $e);
} catch (\Exception $e) {
watchdog_exception('AdvAgg Validator', $e);
}
if (!empty($data)) {
// Parse XML and return info.
$return = $this
->parseSoapResponse($data);
$return['filename'] = $filename;
if (isset($validator_options['text'])) {
unset($validator_options['text']);
}
elseif (isset($validator_options['uri'])) {
unset($validator_options['uri']);
}
$return['options'] = $validator_options;
return $return;
}
return [
'error' => t('W3C Server did not return a 200 or request data was empty.'),
];
}
/**
* {@inheritdoc}
*/
private function parseSoapResponse($xml) {
$doc = new DOMDocument();
$response = [];
// Try to load soap 1.2 XML response, and suppress warning reports if any.
if (!@$doc
->loadXML($xml)) {
// Could not load the XML document.
return $response;
}
// Get the standard CDATA elements.
$cdata = [
'uri',
'checkedby',
'csslevel',
'date',
];
foreach ($cdata as $var) {
$element = $doc
->getElementsByTagName($var);
if ($element->length) {
$response[$var] = $element
->item(0)->nodeValue;
}
}
// Handle the element validity and get errors if not valid.
$element = $doc
->getElementsByTagName('validity');
if ($element->length && $element
->item(0)->nodeValue === 'true') {
$response['validity'] = TRUE;
}
else {
$response['validity'] = FALSE;
$errors = $doc
->getElementsByTagName('error');
foreach ($errors as $error) {
$response['errors'][] = $this
->domExtractor($error);
}
}
// Get warnings.
$warnings = $doc
->getElementsByTagName('warning');
foreach ($warnings as $warning) {
$response['warnings'][] = $this
->domExtractor($warning);
}
// Return response array.
return $response;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
AdvaggFormBase:: |
protected | property | The AdvAgg aggregates state information storage service. | |
AdvaggFormBase:: |
protected | property | The AdvAgg file status state information storage service. | |
AdvaggFormBase:: |
protected | property |
The request stack. Overrides FormBase:: |
|
AdvaggFormBase:: |
protected | function | Checks if the form was submitted by AJAX. | |
BaseValidatorForm:: |
protected | function | Extract info from the DOMNode Object. | |
BaseValidatorForm:: |
public | function | Generate a heirachical form sorted by path from asset files. | |
BaseValidatorForm:: |
protected | function |
Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait:: |
|
BaseValidatorForm:: |
protected | function | Get array element that corresponds to directory. | |
BaseValidatorForm:: |
protected | function | Do not display info on a file if it is valid. | |
BaseValidatorForm:: |
protected | function | Recursively scan the drupal webroot for files matching the given extension. | |
BaseValidatorForm:: |
protected | function | Finds all files that match a given mask in a given directory. | |
ConfigFormBase:: |
public | function |
Form submission handler. Overrides FormInterface:: |
26 |
ConfigFormBaseTrait:: |
protected | function | Retrieves a configuration object. | |
CssW3Form:: |
protected | property | The Guzzle HTTP Client. | |
CssW3Form:: |
protected | property | The Drupal renderer. | |
CssW3Form:: |
public | function | Display validation info via ajax callback. | |
CssW3Form:: |
public | function |
Form constructor. Overrides ConfigFormBase:: |
|
CssW3Form:: |
public static | function |
Instantiates a new instance of this class. Overrides AdvaggFormBase:: |
|
CssW3Form:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
CssW3Form:: |
private | function | ||
CssW3Form:: |
public | function | ||
CssW3Form:: |
public | function | ||
CssW3Form:: |
protected | function |
Perform server side test(s) on all given files. Overrides BaseValidatorForm:: |
|
CssW3Form:: |
private | function | Given a CSS file, test to make sure it is valid CSS. | |
CssW3Form:: |
public | function |
Overrides AdvaggFormBase:: |
|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
FormBase:: |
protected | property | The config factory. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 1 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
protected | function | Gets the current user. | |
FormBase:: |
protected | function | Gets the request object. | |
FormBase:: |
protected | function | Gets the route match. | |
FormBase:: |
protected | function | Gets the logger for a specific channel. | |
FormBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
FormBase:: |
public | function | Resets the configuration factory. | |
FormBase:: |
public | function | Sets the config factory for this form. | |
FormBase:: |
public | function | Sets the request stack object to use. | |
FormBase:: |
public | function |
Form validation handler. Overrides FormInterface:: |
62 |
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |