Update website

This commit is contained in:
Guilhem Lavaux 2024-11-19 08:02:04 +01:00
parent 4413528994
commit 1d90fbf296
6865 changed files with 1091082 additions and 0 deletions

View file

@ -0,0 +1,47 @@
<?php
declare(strict_types=1);
namespace Doctrine\RST\References;
use Doctrine\RST\Environment;
final class Doc extends Reference
{
/** @var string */
private $name;
/** @var Resolver */
private $resolver;
/**
* Used with "ref" - it means the dependencies added in found()
* must be resolved to their final path later (they are not
* already document names).
*
* @var bool
*/
private $dependenciesMustBeResolved;
public function __construct(string $name = 'doc', bool $dependenciesMustBeResolved = false)
{
$this->name = $name;
$this->resolver = new Resolver();
$this->dependenciesMustBeResolved = $dependenciesMustBeResolved;
}
public function getName(): string
{
return $this->name;
}
public function resolve(Environment $environment, string $data): ?ResolvedReference
{
return $this->resolver->resolve($environment, $data);
}
public function found(Environment $environment, string $data): void
{
$environment->addDependency($data, $this->dependenciesMustBeResolved);
}
}

View file

@ -0,0 +1,41 @@
<?php
declare(strict_types=1);
namespace Doctrine\RST\References;
use Doctrine\RST\Environment;
/**
* A reference is something that can be resolved in the document, for instance:
*
* :method:`helloWorld()`
*
* Will be resolved as a reference of type method and the given reference will
* be called to resolve it
*/
abstract class Reference
{
/**
* The name of the reference, i.e the :something:
*/
abstract public function getName(): string;
/**
* Resolve the reference and returns an array
*
* @param Environment $environment the Environment in use
* @param string $data the data of the reference
*/
abstract public function resolve(Environment $environment, string $data): ?ResolvedReference;
/**
* Called when a reference is just found
*
* @param Environment $environment the Environment in use
* @param string $data the data of the reference
*/
public function found(Environment $environment, string $data): void
{
}
}

View file

@ -0,0 +1,81 @@
<?php
declare(strict_types=1);
namespace Doctrine\RST\References;
use RuntimeException;
use function is_string;
use function preg_match;
use function sprintf;
final class ResolvedReference
{
/** @var ?string */
private $file;
/** @var string|null */
private $title;
/** @var string|null */
private $url;
/** @var string[][]|string[][][] */
private $titles;
/** @var string[] */
private $attributes;
/**
* @param string[][]|string[][][] $titles
* @param string[] $attributes
*/
public function __construct(?string $file, ?string $title, ?string $url, array $titles = [], array $attributes = [])
{
$this->file = $file;
$this->title = $title;
$this->url = $url;
$this->titles = $titles;
$this->validateAttributes($attributes);
$this->attributes = $attributes;
}
public function getFile(): ?string
{
return $this->file;
}
public function getTitle(): ?string
{
return $this->title;
}
public function getUrl(): ?string
{
return $this->url;
}
/** @return string[][]|string[][][] */
public function getTitles(): array
{
return $this->titles;
}
/** @return string[] */
public function getAttributes(): array
{
return $this->attributes;
}
/** @param string[] $attributes */
private function validateAttributes(array $attributes): void
{
foreach ($attributes as $attribute => $value) {
if (! is_string($attribute) || $attribute === 'href' || ! (bool) preg_match('/^[a-zA-Z\_][\w\.\-_]+$/', $attribute)) {
throw new RuntimeException(sprintf('Attribute with name "%s" is not allowed', $attribute));
}
}
}
}

View file

@ -0,0 +1,91 @@
<?php
declare(strict_types=1);
namespace Doctrine\RST\References;
use Doctrine\RST\Environment;
use Doctrine\RST\Meta\MetaEntry;
final class Resolver
{
/** @param string[] $attributes */
public function resolve(
Environment $environment,
string $data,
array $attributes = []
): ?ResolvedReference {
$resolvedFileReference = $this->resolveFileReference($environment, $data, $attributes);
if ($resolvedFileReference !== null) {
return $resolvedFileReference;
}
$resolvedAnchorReference = $this->resolveAnchorReference($environment, $data, $attributes);
if ($resolvedAnchorReference !== null) {
return $resolvedAnchorReference;
}
return null;
}
/** @param string[] $attributes */
private function resolveFileReference(
Environment $environment,
string $data,
array $attributes = []
): ?ResolvedReference {
$entry = null;
$file = $environment->canonicalUrl($data);
if ($file !== null) {
$entry = $environment->getMetas()->get($file);
}
if ($entry === null) {
return null;
}
return $this->createResolvedReference($file, $environment, $entry, $attributes);
}
/** @param string[] $attributes */
private function resolveAnchorReference(
Environment $environment,
string $data,
array $attributes = []
): ?ResolvedReference {
$entry = $environment->getMetas()->findLinkMetaEntry($data);
if ($entry !== null) {
return $this->createResolvedReference($entry->getFile(), $environment, $entry, $attributes, $data);
}
return null;
}
/** @param string[] $attributes */
private function createResolvedReference(
?string $file,
Environment $environment,
MetaEntry $entry,
array $attributes = [],
?string $anchor = null
): ResolvedReference {
$url = $entry->getUrl();
if ($url !== '') {
$url = $environment->relativeUrl('/' . $url) . ($anchor !== null ? '#' . $anchor : '');
}
return new ResolvedReference(
$file,
$entry->getTitle(),
$url,
$entry->getTitles(),
$attributes
);
}
}