-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 18d7afc
Showing
5 changed files
with
328 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
name: Integrity check | ||
|
||
on: [push] | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@master | ||
|
||
- name: Install PHP | ||
uses: shivammathur/setup-php@master | ||
with: | ||
php-version: 7.4 | ||
|
||
- name: Install composer deps | ||
run: | | ||
composer create-project nette/code-checker temp/code-checker ^3 --no-progress | ||
composer create-project nette/coding-standard temp/coding-standard ^2 --no-progress | ||
# Install app deps | ||
composer install --no-interaction --prefer-dist | ||
# Check code checker and coding standards | ||
- name: Check coding standards | ||
run: | | ||
php temp/code-checker/code-checker --short-arrays --strict-types --fix --no-progress | ||
php temp/coding-standard/ecs check src --config temp/coding-standard/coding-standard-php71.yml | ||
- name: Check PHPStan rules | ||
run: composer phpstan |
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,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2021 Baraja packages | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
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,31 @@ | ||
{ | ||
"name": "baraja-core/html-header", | ||
"description": "Renders the complete valid header HTML content based on MetaBuilder.", | ||
"homepage": "https://github.com/baraja-core/html-header", | ||
"authors": [ | ||
{ | ||
"name": "Jan Barášek", | ||
"homepage": "https://baraja.cz" | ||
} | ||
], | ||
"require": { | ||
"php": ">=7.4.0" | ||
}, | ||
"require-dev": { | ||
"phpstan/phpstan": "^0.12.18", | ||
"tracy/tracy": "^2.7", | ||
"phpstan/phpstan-nette": "^0.12.6", | ||
"symplify/easy-coding-standard": "^7.2" | ||
}, | ||
"autoload": { | ||
"classmap": [ | ||
"src/" | ||
] | ||
}, | ||
"scripts": { | ||
"phpstan": [ | ||
"vendor/bin/phpstan analyse src -c phpstan.neon --level 8 --no-progress" | ||
] | ||
}, | ||
"minimum-stability": "stable" | ||
} |
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,3 @@ | ||
includes: | ||
- vendor/phpstan/phpstan-nette/extension.neon | ||
- vendor/phpstan/phpstan-nette/rules.neon |
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,241 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Baraja\HtmlHeader; | ||
|
||
|
||
/** | ||
* Renders the complete valid header HTML content based on MetaBuilder. | ||
* Simply define individual tags by calling methods and then get the entire contents of the header: | ||
* | ||
* $header = new HtmlHeader; | ||
* $header->title('My webpage'); | ||
* $header->meta('description', 'Awesome page...'); | ||
* // or shortcut: | ||
* $header->metaDescription('Awesome page...'); | ||
* | ||
* $header->link('canonical', 'https://baraja.cz'); | ||
* $header->link('alternate', [ | ||
* 'hreflang' => 'cs', | ||
* 'href' => 'https://baraja.cz', | ||
* ]); | ||
* | ||
* $header->jsonld([ | ||
* '@context' => 'http://schema.org', | ||
* '@type' => 'Person', | ||
* 'name' => 'Jan Barášek', | ||
* ]); | ||
*/ | ||
final class HtmlHeader | ||
{ | ||
/** @var string[] */ | ||
private array $order = ['title', 'meta', 'og', 'twitter', 'link', 'json-ld']; | ||
|
||
/** @var string[][]|string[][][] */ | ||
private array $tags = []; | ||
|
||
|
||
public function __toString(): string | ||
{ | ||
return $this->render(); | ||
} | ||
|
||
|
||
/** | ||
* Render all or a specific group of HTML meta tags. | ||
* | ||
* @param string[] $groups render specific groups or keep empty for all records. | ||
*/ | ||
public function render(?array $groups = null): string | ||
{ | ||
$items = []; | ||
foreach (($groups ?? $this->order) as $group) { | ||
$items[] = $this->renderGroup($group); | ||
} | ||
|
||
return trim(implode('', $items)); | ||
} | ||
|
||
|
||
/** | ||
* @param string[] $order | ||
*/ | ||
public function setCustomOrderingStrategy(array $order): void | ||
{ | ||
$this->order = $order; | ||
} | ||
|
||
|
||
/** | ||
* Build an HTML link tag. | ||
* | ||
* @param string|string[]|null $value | ||
*/ | ||
public function link(string $key, $value): void | ||
{ | ||
if (!empty($value)) { | ||
$attributes = ['rel' => $key]; | ||
if (is_array($value)) { | ||
foreach ($value as $valueKey => $v) { | ||
$attributes[$valueKey] = $v; | ||
} | ||
} else { | ||
$attributes['href'] = $value; | ||
} | ||
|
||
$this->addToTagsGroup('link', $key, $this->createTag('link', $attributes)); | ||
} | ||
} | ||
|
||
|
||
public function metaDescription(string $content): void | ||
{ | ||
$this->meta('description', $content); | ||
} | ||
|
||
|
||
/** | ||
* Build an HTML meta tag. | ||
* | ||
* @param string|string[]|null $value | ||
*/ | ||
public function meta(string $key, $value): void | ||
{ | ||
if (!empty($value)) { | ||
$attributes = ['name' => $key]; | ||
if (is_array($value)) { | ||
foreach ($value as $valueKey => $v) { | ||
$attributes[$valueKey] = $v; | ||
} | ||
} else { | ||
$attributes['content'] = $value; | ||
} | ||
|
||
$this->addToTagsGroup('meta', $key, $this->createTag('meta', $attributes)); | ||
} | ||
} | ||
|
||
|
||
/** Build an Open Graph meta tag. */ | ||
public function og(string $key, string $value, bool $prefixed = true): void | ||
{ | ||
if (!empty($value)) { | ||
$key = $prefixed ? 'og:' . $key : $key; | ||
$this->addToTagsGroup('og', $key, $this->createTag('meta', [ | ||
'property' => $key, | ||
'content' => $value, | ||
])); | ||
} | ||
} | ||
|
||
|
||
/** | ||
* Build an JSON linked data meta tag. | ||
* | ||
* @param mixed[] $schema | ||
*/ | ||
public function jsonld(array $schema): void | ||
{ | ||
if ($schema === []) { | ||
return; | ||
} | ||
|
||
try { | ||
$json = json_encode($schema, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR); | ||
} catch (\Throwable $e) { | ||
throw new \InvalidArgumentException('Invalid json: ' . $e->getMessage(), $e->getCode(), $e); | ||
} | ||
$this->tags['json-ld'][] = '<script type="application/ld+json">' . "\n" . $json . "\n" . '</script>'; | ||
} | ||
|
||
|
||
/** Build a Title HTML tag. */ | ||
public function title(?string $value): void | ||
{ | ||
if ($value === null) { | ||
return; | ||
} | ||
if (($value = trim($value)) !== '') { | ||
$this->tags['title'][] = '<title>' . htmlspecialchars($value, ENT_NOQUOTES | ENT_SUBSTITUTE, 'UTF-8') . '</title>'; | ||
} | ||
} | ||
|
||
|
||
/** Build a Twitter Card meta tag. */ | ||
public function twitter(string $key, string $value, bool $prefixed = true): void | ||
{ | ||
if (!empty($value)) { | ||
$key = $prefixed ? 'twitter:' . $key : $key; | ||
$this->addToTagsGroup('twitter', $key, $this->createTag('meta', [ | ||
'name' => $key, | ||
'content' => $value, | ||
])); | ||
} | ||
} | ||
|
||
|
||
/** Render all HTML meta tags from a specific group. */ | ||
private function renderGroup(string $group): string | ||
{ | ||
if (!isset($this->tags[$group])) { | ||
return ''; | ||
} | ||
|
||
$html = []; | ||
foreach ($this->tags[$group] as $tag) { | ||
if (is_array($tag)) { | ||
foreach ($tag as $t) { | ||
$html[] = $t; | ||
} | ||
} else { | ||
$html[] = $tag; | ||
} | ||
} | ||
|
||
return count($html) > 0 | ||
? implode("\n", $html) . "\n" | ||
: ''; | ||
} | ||
|
||
|
||
/** Add single HTML element to tags group. */ | ||
private function addToTagsGroup(string $group, string $key, string $tag): void | ||
{ | ||
if (isset($this->tags[$group][$key])) { | ||
if (is_array($this->tags[$group][$key])) { | ||
$this->tags[$group][$key][] = $tag; | ||
} else { | ||
$this->tags[$group][$key] = [$this->tags[$group][$key], $tag]; | ||
} | ||
} else { | ||
$this->tags[$group][$key] = $tag; | ||
} | ||
} | ||
|
||
|
||
/** | ||
* Build an HTML tag | ||
* | ||
* @param string[] $attributes | ||
*/ | ||
private function createTag(string $tagName, array $attributes = []): string | ||
{ | ||
$escapeAttr = static function (string $s): string { | ||
if (strpos($s, '`') !== false && strpbrk($s, ' <>"\'') === false) { | ||
$s .= ' '; // protection against innerHTML mXSS vulnerability nette/nette#1496 | ||
} | ||
|
||
return htmlspecialchars($s, ENT_QUOTES | ENT_HTML5 | ENT_SUBSTITUTE, 'UTF-8', true); | ||
}; | ||
|
||
$attrItems = []; | ||
foreach ($attributes as $key => $value) { | ||
if ($value !== null) { | ||
$attrItems[] = $escapeAttr((string) $key) . '="' . $escapeAttr($value) . '"'; | ||
} | ||
} | ||
|
||
return '<' . $tagName . (count($attrItems) > 0 ? ' ' . implode(' ', $attrItems) : '') . '>'; | ||
} | ||
} |