Skip to content

Commit

Permalink
Refactor doc section
Browse files Browse the repository at this point in the history
  • Loading branch information
schlessera committed May 26, 2021
1 parent 6bbb6c9 commit 131e2c4
Show file tree
Hide file tree
Showing 24 changed files with 866 additions and 82 deletions.
10 changes: 7 additions & 3 deletions bin/src/Validator/SpecGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public function generate($jsonSpec, $rootNamespace, $destination)
$this->generateEntityClass('DeclarationList', $fileManager);
$this->generateEntityClass('DescendantTagList', $fileManager);
$this->generateEntityClass('CssRuleset', $fileManager);
$this->generateEntityClass('DocRuleset', $fileManager);
$this->generateEntityClass('Tag', $fileManager);
$this->generateEntityClass('TagWithExtensionSpec', $fileManager, 'interface');
$this->generateEntityClass('ExtensionSpec', $fileManager, 'trait');
Expand Down Expand Up @@ -218,8 +219,11 @@ private function generateErrorCodeInterface($jsonSpec, FileManager $fileManager)
*/
private function adaptJsonSpec($jsonSpec)
{
$jsonSpec['cssRulesets'] = $jsonSpec['cssRulesets'];
unset($jsonSpec['cssRulesets']);
$jsonSpec['cssRulesets'] = $jsonSpec['css'];
unset($jsonSpec['css']);

$jsonSpec['docRulesets'] = $jsonSpec['doc'];
unset($jsonSpec['doc']);

$jsonSpec['attributeLists'] = $jsonSpec['attrLists'];
unset($jsonSpec['attrLists']);
Expand Down Expand Up @@ -272,7 +276,7 @@ private function collectSpecRuleKeys($jsonSpec)
}
break;
case 'cssRulesets':
case 'doc':
case 'docRulesets':
case 'errors':
foreach ($sectionData as $ruleset) {
foreach (array_keys($ruleset) as $specRuleKey) {
Expand Down
8 changes: 8 additions & 0 deletions bin/src/Validator/SpecGenerator/Dumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,14 @@ public function filterValueStrings($value)
||
strpos($value, 'AttributeList\\') === 0
||
strpos($value, 'CssRuleset::') === 0
||
strpos($value, 'CssRuleset\\') === 0
||
strpos($value, 'DocRuleset::') === 0
||
strpos($value, 'DocRuleset\\') === 0
||
strpos($value, 'DeclarationList::') === 0
||
strpos($value, 'DeclarationList\\') === 0
Expand Down
9 changes: 7 additions & 2 deletions bin/src/Validator/SpecGenerator/FileManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public function ensureDirectoriesExist()
"{$this->destination}/Spec",
"{$this->destination}/Spec/AttributeList",
"{$this->destination}/Spec/CssRuleset",
"{$this->destination}/Spec/DocRuleset",
"{$this->destination}/Spec/DeclarationList",
"{$this->destination}/Spec/DescendantTagList",
"{$this->destination}/Spec/Section",
Expand Down Expand Up @@ -300,6 +301,10 @@ private function getFullyQualifiedName($class)
return "AmpProject\\Validator\\Spec\\CssSpecRule";
}

if (strpos($class, 'DocSpecRule\\') === 0) {
return "AmpProject\\Validator\\Spec\\DocSpecRule";
}

if (strpos($class, 'AttributeList\\') === 0) {
return "AmpProject\\Validator\\Spec\\AttributeList";
}
Expand All @@ -315,7 +320,7 @@ private function getFullyQualifiedName($class)
if (
in_array(
$class,
['AttributeList', 'CssRuleset', 'DeclarationList', 'DescendantTagList', 'SpecRule'],
['AttributeList', 'CssRuleset', 'DocRuleset', 'DeclarationList', 'DescendantTagList', 'SpecRule'],
true
)
) {
Expand All @@ -325,7 +330,7 @@ private function getFullyQualifiedName($class)
if (
in_array(
$class,
['AttributeLists', 'CssRulesets', 'DeclarationLists', 'DescendantTagLists', 'Tags'],
['AttributeLists', 'CssRulesets', 'DocRulesets', 'DeclarationLists', 'DescendantTagLists', 'Tags'],
true
)
) {
Expand Down
8 changes: 0 additions & 8 deletions bin/src/Validator/SpecGenerator/Section/CssRulesets.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,6 @@ final class CssRulesets implements Section
use ClassNames;
use ConstantNames;

/**
* Associative array of CSS rulesets and their attributes.
*
* @var array<string,array>
*/
private $css = [];

/**
* Dumper instance to use.
*
Expand Down Expand Up @@ -54,7 +47,6 @@ public function process(FileManager $fileManager, $spec, PhpNamespace $namespace
$cssRulesets = [];
$byFormat = [];

$namespace->addUse("LogicException");
$namespace->addUse("{$fileManager->getRootNamespace()}\\Spec\\IterableSection");
$namespace->addUse("{$fileManager->getRootNamespace()}\\Spec\\Iteration");
$namespace->addUse("{$fileManager->getRootNamespace()}\\Spec\\CssRuleset");
Expand Down
12 changes: 0 additions & 12 deletions bin/src/Validator/SpecGenerator/Section/Doc.php

This file was deleted.

178 changes: 178 additions & 0 deletions bin/src/Validator/SpecGenerator/Section/DocRulesets.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
<?php

namespace AmpProject\Tooling\Validator\SpecGenerator\Section;

use AmpProject\Tooling\Validator\SpecGenerator\ArrayKeyFirstPolyfill;
use AmpProject\Tooling\Validator\SpecGenerator\ClassNames;
use AmpProject\Tooling\Validator\SpecGenerator\ConstantNames;
use AmpProject\Tooling\Validator\SpecGenerator\Dumper;
use AmpProject\Tooling\Validator\SpecGenerator\FileManager;
use AmpProject\Tooling\Validator\SpecGenerator\Section;
use AmpProject\Tooling\Validator\SpecGenerator\Template;
use Nette\PhpGenerator\ClassType;
use Nette\PhpGenerator\PhpNamespace;

final class DocRulesets implements Section
{
use ArrayKeyFirstPolyfill;
use ClassNames;
use ConstantNames;

/**
* Dumper instance to use.
*
* @var Dumper
*/
private $dumper;

/**
* DocRulesets constructor.
*/
public function __construct()
{
$this->dumper = new Dumper();
}

/**
* Process a section.
*
* @param FileManager $fileManager FileManager instance to use.
* @param array $spec Associative array of spec data that was decoded from the JSON file.
* @param PhpNamespace $namespace Namespace object of the section.
* @param ClassType $class Class object of the section.
* @return void
*/
public function process(FileManager $fileManager, $spec, PhpNamespace $namespace, ClassType $class)
{
$docRulesets = [];
$byFormat = [];

$namespace->addUse("{$fileManager->getRootNamespace()}\\Spec\\IterableSection");
$namespace->addUse("{$fileManager->getRootNamespace()}\\Spec\\Iteration");
$namespace->addUse("{$fileManager->getRootNamespace()}\\Spec\\DocRuleset");

$docRulesetsTemplateClass = ClassType::withBodiesFrom(Template\DocRulesets::class);
foreach ($docRulesetsTemplateClass->getMethods() as $method) {
$class->addMember($method);
}

$class->addImplement("{$fileManager->getRootNamespace()}\\Spec\\IterableSection");
$class->addTrait(
"{$fileManager->getRootNamespace()}\\Spec\\Iteration",
['Iteration::current as parentCurrent']
);

$class->addProperty('docRulesetsCache')
->setPrivate()
->addComment("Cache of instantiated DocRuleset objects.\n\n@var array<DocRuleset>")
->setValue([]);

$class->addProperty('iterationArray')
->setPrivate()
->addComment("Array used for storing the iteration index in.\n\n@var array<string>|null");

foreach ($spec as $attributes) {
$docRulesetId = $this->getNameForRuleset($attributes);
$docRulesets[$docRulesetId] = $attributes;
}

$docRulesetIds = array_keys($docRulesets);
natcasesort($docRulesetIds);

$rulesets = [];
foreach ($docRulesetIds as $docRulesetId) {
$docRulesetIdString = "DocRuleset\\{$this->getClassNameFromId($docRulesetId)}::ID";

$className = $this->generateDocRulesetSpecificClass(
$docRulesetId,
$docRulesets[$docRulesetId],
$fileManager
);

$rulesets["DocRuleset\\{$className}::ID"] = "DocRuleset\\{$className}::class";

if (array_key_exists('htmlFormat', $docRulesets[$docRulesetId])) {
$formats = $docRulesets[$docRulesetId]['htmlFormat'];
foreach ($formats as $format) {
$format = $this->getFormatConstant($this->getConstantName($format));
if (!array_key_exists($format, $byFormat)) {
$byFormat[$format] = [];
}
$byFormat[$format][] = $docRulesetIdString;
}
}
}

$class->addConstant('DOC_RULESETS', $rulesets)
->addComment("Mapping of document ruleset ID to document ruleset implementation.\n\n@var array<string>");

$class->addConstant('BY_FORMAT', $byFormat)
->addComment(
"Mapping of AMP format to array of document ruleset IDs.\n\n"
. "This is used to optimize querying by AMP format.\n\n"
. "@var array<array<string>>"
);
}

/**
* Get the name for a given ruleset.
*
* @param array $ruleSet Rule set to get the name for.
* @return string Name to use for the rule set.
*/
private function getNameForRuleSet($ruleSet)
{
static $index = 1;

if (!array_key_exists('htmlFormat', $ruleSet) || count($ruleSet['htmlFormat']) === 0) {
$name = "ruleset-{$index}";
$index++;

return $name;
}

$name = $ruleSet['htmlFormat'][0];

if (array_key_exists('enabledBy', $ruleSet) && count($ruleSet['enabledBy']) > 0) {
$name .= " ({$ruleSet['enabledBy'][0]})";
}

if (array_key_exists('disabledBy', $ruleSet) && count($ruleSet['disabledBy']) > 0) {
$name .= " (no-{$ruleSet['disabledBy'][0]})";
}

return $name;
}

/**
* Generate the document ruleset-specific class file.
*
* @param string $ruleset ID of the document ruleset to generate the class for.
* @param array $jsonSpec Array of spec data for the document ruleset.
* @param FileManager $fileManager File manager instance to use.
*/
private function generateDocRulesetSpecificClass($ruleset, $jsonSpec, FileManager $fileManager)
{
list($file, $namespace) = $fileManager->createNewNamespacedFile('Spec\\DocRuleset');

$className = $this->getClassNameFromId($ruleset);

$namespace->addUse("{$fileManager->getRootNamespace()}\\Spec\\SpecRule");
$namespace->addUse("{$fileManager->getRootNamespace()}\\Spec\\DocRuleset");

/** @var ClassType $class */
$class = $namespace->addClass($className)
->setFinal()
->addExtend('AmpProject\Validator\Spec\DocRuleset');

$class->addConstant('ID', $ruleset)
->addComment("ID of the ruleset.\n\n@var string");

$class->addConstant('SPEC', $jsonSpec)
->addComment("Array of spec rules.\n\n@var array");

$fileManager->saveFile($file, "Spec/DocRuleset/{$className}.php");

return $className;
}
}
4 changes: 1 addition & 3 deletions bin/src/Validator/SpecGenerator/Template/CssRuleset.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
/**
* Class CssRuleset.
*
* @package AmpProject\Tooling\Validator\SpecGenerator\Template
*
* @property-read bool $allowAllDeclarationInStyle
* @property-read bool $allowImportant
* @property-read bool $expandVendorPrefixes
Expand All @@ -32,7 +30,7 @@ class CssRuleset
*
* @var string
*/
const ID = '[cssRulesets ruleset base class]';
const ID = '[css ruleset base class]';

/**
* Spec data of the CSS ruleset.
Expand Down
Loading

0 comments on commit 131e2c4

Please sign in to comment.