Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #33122 [WebLink] implement PSR-13 directly (nicolas-grekas)
This PR was merged into the 4.4 branch. Discussion ---------- [WebLink] implement PSR-13 directly | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - Implementing PSR-13 is simple enough and the repo we're using is [freezed](https://github.com/php-fig/link-util/pulls). This also allows us to add some type declarations. We're going to need them before merging #30323. Commits ------- b570ee1 [WebLink] implement PSR-13 directly
- Loading branch information
Showing
15 changed files
with
454 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,11 @@ | ||
CHANGELOG | ||
========= | ||
|
||
4.4.0 | ||
----- | ||
|
||
* implement PSR-13 directly | ||
|
||
3.3.0 | ||
----- | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\WebLink; | ||
|
||
use Psr\Link\EvolvableLinkProviderInterface; | ||
use Psr\Link\LinkInterface; | ||
|
||
class GenericLinkProvider implements EvolvableLinkProviderInterface | ||
{ | ||
/** | ||
* @var LinkInterface[] | ||
*/ | ||
private $links = []; | ||
|
||
/** | ||
* @param LinkInterface[] $links | ||
*/ | ||
public function __construct(array $links = []) | ||
{ | ||
$that = $this; | ||
|
||
foreach ($links as $link) { | ||
$that = $that->withLink($link); | ||
} | ||
|
||
$this->links = $that->links; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getLinks(): array | ||
{ | ||
return array_values($this->links); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getLinksByRel($rel): array | ||
{ | ||
$links = []; | ||
|
||
foreach ($this->links as $link) { | ||
if (\in_array($rel, $link->getRels())) { | ||
$links[] = $link; | ||
} | ||
} | ||
|
||
return $links; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function withLink(LinkInterface $link) | ||
{ | ||
$that = clone $this; | ||
$that->links[spl_object_id($link)] = $link; | ||
|
||
return $that; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function withoutLink(LinkInterface $link) | ||
{ | ||
$that = clone $this; | ||
unset($that->links[spl_object_id($link)]); | ||
|
||
return $that; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\WebLink; | ||
|
||
use Psr\Link\EvolvableLinkInterface; | ||
|
||
class Link implements EvolvableLinkInterface | ||
{ | ||
// Relations defined in https://www.w3.org/TR/html5/links.html#links and applicable on link elements | ||
public const REL_ALTERNATE = 'alternate'; | ||
public const REL_AUTHOR = 'author'; | ||
public const REL_HELP = 'help'; | ||
public const REL_ICON = 'icon'; | ||
public const REL_LICENSE = 'license'; | ||
public const REL_SEARCH = 'search'; | ||
public const REL_STYLESHEET = 'stylesheet'; | ||
public const REL_NEXT = 'next'; | ||
public const REL_PREV = 'prev'; | ||
|
||
// Relation defined in https://www.w3.org/TR/preload/ | ||
public const REL_PRELOAD = 'preload'; | ||
|
||
// Relations defined in https://www.w3.org/TR/resource-hints/ | ||
public const REL_DNS_PREFETCH = 'dns-prefetch'; | ||
public const REL_PRECONNECT = 'preconnect'; | ||
public const REL_PREFETCH = 'prefetch'; | ||
public const REL_PRERENDER = 'prerender'; | ||
|
||
// Extra relations | ||
public const REL_MERCURE = 'mercure'; | ||
|
||
private $href = ''; | ||
|
||
/** | ||
* @var string[] | ||
*/ | ||
private $rel = []; | ||
|
||
/** | ||
* @var string[] | ||
*/ | ||
private $attributes = []; | ||
|
||
public function __construct(string $rel = null, string $href = '') | ||
{ | ||
if (null !== $rel) { | ||
$this->rel[$rel] = $rel; | ||
} | ||
$this->href = $href; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getHref(): string | ||
{ | ||
return $this->href; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function isTemplated(): bool | ||
{ | ||
return $this->hrefIsTemplated($this->href); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getRels(): array | ||
{ | ||
return array_values($this->rel); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getAttributes(): array | ||
{ | ||
return $this->attributes; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function withHref($href) | ||
{ | ||
$that = clone $this; | ||
$that->href = $href; | ||
$that->templated = $this->hrefIsTemplated($href); | ||
|
||
return $that; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function withRel($rel) | ||
{ | ||
$that = clone $this; | ||
$that->rel[$rel] = $rel; | ||
|
||
return $that; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function withoutRel($rel) | ||
{ | ||
$that = clone $this; | ||
unset($that->rel[$rel]); | ||
|
||
return $that; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function withAttribute($attribute, $value) | ||
{ | ||
$that = clone $this; | ||
$that->attributes[$attribute] = $value; | ||
|
||
return $that; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function withoutAttribute($attribute) | ||
{ | ||
$that = clone $this; | ||
unset($that->attributes[$attribute]); | ||
|
||
return $that; | ||
} | ||
|
||
private function hrefIsTemplated(string $href): bool | ||
{ | ||
return false !== strpos($href, '{') || false !== strpos($href, '}'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.