1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
|
<?php
declare(strict_types=1);
namespace Fig\Link\Builder;
const REGISTRY_FILE = 'https://www.iana.org/assignments/link-relations/link-relations.xml';
run();
function run() {
$compiler = new RegistryCompiler();
$records = new Records();
$out = fopen('src/Relations.php', 'w');
$compiler->compile($records(), $out);
}
class Records
{
/**
* Loads the XML object from the remote source.
*
* IANA requires a valid User-Agent string for all requests, and non-curl stream options for PHP seem to all
* fail at sending a User-Agent, even when told to. So we use the ugly way.
*/
protected function getXml(): \SimpleXMLElement
{
$ch = curl_init(REGISTRY_FILE);
curl_setopt($ch, CURLOPT_USERAGENT, 'php-fig/fig-utils');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$xml = curl_exec($ch);
return simplexml_load_string($xml);
}
/**
* Fetches the records for the relation registry.
*
* @return iterable<\SimpleXMLElement>
*/
public function __invoke()
{
foreach ($this->getXml()->registry->children() as $element) {
if ($element->getName() == 'record') {
yield $element;
}
}
}
}
/**
* Compiles a list of SimpleXmlElements to a Relations interface.
*/
class RegistryCompiler
{
/**
* Writes a Relations index class based on the provided records.
*
* @param iterable<\SimpleXMLElement> $records
* An iterable of SimpleXml elements from the Link Relations XML file.
* @param $stream
* An open file stream to which to write the generated code.
* @param string $class
* The name of the interface to produce.
* @param string $namespace
* The namespace of the interface to generate.
*/
public function compile(iterable $records, $stream, string $class = 'Relations', string $namespace = 'Fig\\Link') : void
{
fwrite($stream, $this->createPreamble($class, $namespace));
foreach ($records as $record) {
$item = $this->createEntry($record);
fwrite($stream, $item);
}
fwrite($stream, $this->createClosing());
}
/**
* Processes a SimpleXml record into a constant definition.
*
* @param \SimpleXMLElement $record
* The record to process.
* @return string
*/
protected function createEntry(\SimpleXMLElement $record) : string
{
$value = (string)$record->value;
$name = $this->buildName($value);
$description = $this->rewrap((string)$record->description);
$note = $this->rewrap((string)$record->note);
$seeUri = $this->xrefLink($record->spec->xref);
if ($note) {
return <<<END
/**
* {$description}
*
* {$note}
*
* @see {$seeUri}
*/
const REL_$name = '$value';
END;
}
else {
return <<<END
/**
* {$description}
*
* @see {$seeUri}
*/
const REL_$name = '$value';
END;
}
}
/**
* Maps an Xref element to a full URL.
*
* @param \SimpleXMLElement $xref
* The xref argument of a record.
* @return string
* A web URL.
* @throws \Exception
*/
protected function xrefLink(\SimpleXMLElement $xref) : string
{
if ($xref['type'] == 'uri') {
return (string)$xref['data'];
}
if ($xref['type'] == 'rfc') {
return 'https://tools.ietf.org/html/' . $xref['data'];
}
// This seems to never happen.
throw new \InvalidArgumentException("Unhandled type: {$xref['type']}");
}
/**
* Fixes the word wrapping of a text string to fit in a docblock.
*
* @param string $text
* The string to rewrap.
* @return string
* The rewrapped string.
*/
protected function rewrap(string $text) : string
{
$text = preg_replace('/\s\s+/', ' ', $text);
$text = wordwrap($text, 120);
$text = str_replace("\n", "\n * ", $text);
return $text;
}
/**
* Maps a rel name to the appropriate constant name.
*
* @param string $value
* The rel name from the record.
* @return string
* A const-name-safe string.
*/
protected function buildName(string $value) : string
{
return str_replace(['-', '.'], '_', strtoupper($value));
}
/**
* Returns the opening of the file.
*
* @param string $class
* The name of the class/interface to generate.
* @param string $namespace
* The namspace for the generated class.
* @return string
* A portion of a valid PHP class file.
*/
protected function createPreamble(string $class, string $namespace) : string
{
return <<<END
<?php
/**
* Standard relation names.
*
* This file is auto-generated. Do not edit directly. Edit or re-run `rebuild-rels.php` if necessary.
*/
declare(strict_types=1);
namespace $namespace;
/**
* Standard relation names.
*
* This interface provides convenience constants for standard relationships defined by IANA. They are not required,
* but are useful for avoiding typos and similar such errors.
*
* This interface may be referenced directly like so:
*
* Relations::REL_UP
*
* Or you may implement this interface in your class and then refer to the constants locally:
*
* static::REL_UP
*/
interface $class
{
END;
}
/**
* Returns the closing of a file.
*
* @return string
*/
protected function createClosing() : string
{
return <<<'END'
}
END;
}
}
|