File manager - Edit - /home/opticamezl/www/newok/view-metadata.tar
Back
src/View/MetadataManager.php 0000644 00000010247 15174335500 0012000 0 ustar 00 <?php namespace YOOtheme\View; use YOOtheme\Event; use YOOtheme\Metadata; /** * Manages HTML elements belonging to the metadata content category. * * @link https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Content_categories#Metadata_content */ class MetadataManager implements Metadata, \IteratorAggregate { /** * @var array */ protected $prefix = ['article', 'fb', 'og', 'twitter']; /** * @var array */ protected $metadata = []; /** * @inheritdoc */ public function all(...$names) { if (!$names) { return $this->metadata; } $result = []; foreach ($names as $name) { $prefix = str_ends_with($name, '*') ? substr($name, 0, -1) : false; foreach ($this->metadata as $metadata) { if ( isset($this->metadata[$name]) || ($prefix && str_starts_with($metadata->name, $prefix)) ) { $result[$metadata->name] = $metadata; } } } return $result; } /** * @inheritdoc */ public function get($name) { return $this->metadata[$name] ?? null; } /** * @inheritdoc */ public function set($name, $value, array $attributes = []) { if (is_array($value) && !is_callable($value)) { [$value, $attributes] = [null, array_merge($value, $attributes)]; } $metadata = new MetadataObject($name, $value, $attributes); $metadata = $this->resolveMetadata($metadata); $metadata = Event::emit('metadata.load|filter', $metadata, $this); return $this->metadata[$metadata->name] = $metadata; } /** * @inheritdoc */ public function del($name) { unset($this->metadata[$name]); } /** * @inheritdoc */ public function merge(array $metadata) { foreach ($metadata as $name => $value) { $this->set($name, $value); } } /** * @inheritdoc */ public function filter(callable $filter) { return array_filter($this->metadata, $filter); } /** * @inheritdoc */ public function render() { return join("\n", $this->metadata); } /** * Returns an iterator for metadata tags. * * @return \ArrayIterator */ #[\ReturnTypeWillChange] public function getIterator() { return new \ArrayIterator($this->metadata); } /** * Resolves the metadata. * * @param MetadataObject $metadata * * @return MetadataObject */ protected function resolveMetadata(MetadataObject $metadata) { if (is_string($metadata->value)) { $metadata = $this->resolveAttributes($metadata); } if ($metadata->tag === 'style' && !isset($metadata->value)) { return $metadata->withTag('link')->withAttribute('rel', 'stylesheet'); } if (in_array($metadata->tag, $this->prefix)) { return $metadata->withTag('meta'); } return $metadata; } /** * Resolve the metadata attributes. * * @param MetadataObject $metadata * * @return MetadataObject */ protected function resolveAttributes($metadata) { if ($metadata->tag === 'base') { return $metadata->withAttributes([ 'href' => $metadata->value, ]); } if ($metadata->tag === 'link') { return $metadata->withAttributes([ 'href' => $metadata->value, 'rel' => str_replace('link:', '', $metadata->name), ]); } if ($metadata->tag === 'meta') { return $metadata->withAttributes([ 'name' => str_replace('meta:', '', $metadata->name), 'content' => $metadata->value, ]); } if (in_array($metadata->tag, $this->prefix)) { return $metadata->withAttributes([ 'property' => $metadata->name, 'content' => $metadata->value, ]); } return $metadata; } } src/View/MetadataObject.php 0000644 00000007406 15174335500 0011637 0 ustar 00 <?php namespace YOOtheme\View; /** * @property string $href * @property string $src * @property string $defer * @property string $version */ class MetadataObject { /** * @var string */ public $tag; /** * @var string */ public $name; /** * @var mixed */ public $value; /** * @var array */ public $attributes; /** * Constructor. * * @param string $name * @param mixed $value * @param array $attributes */ public function __construct($name, $value, array $attributes = []) { $tag = substr($name, 0, strpos($name, ':')); $this->tag = $tag ?: $name; $this->name = $name; $this->value = $value; $this->attributes = $attributes; } /** * Gets an attribute value. * * @param string $name * * @return mixed */ public function __get($name) { return $this->attributes[$name] ?? null; } /** * Checks if an attribute value exists. * * @param string $name * * @return bool */ public function __isset($name) { return isset($this->attributes[$name]); } /** * Gets the rendered tag as string. * * @return string */ public function __toString() { return $this->render(); } /** * Renders the tag. * * @return string */ public function render() { $metadata = $this; if (is_callable($callback = $this->value)) { $metadata = $callback($this) ?: $this; } return HtmlElement::tag($metadata->tag, $metadata->attributes, $metadata->value); } /** * Gets the tag. * * @return string */ public function getTag() { return $this->tag; } /** * Sets the tag. * * @param string $tag * * @return static */ public function withTag($tag) { $clone = clone $this; $clone->tag = $tag; return $clone; } /** * Gets the name. * * @return string */ public function getName() { return $this->name; } /** * Sets the name. * * @param string $name * * @return static */ public function withName($name) { $clone = clone $this; $clone->name = $name; return $clone; } /** * Gets the value. * * @return string */ public function getValue() { return $this->value; } /** * Sets the value. * * @param string $value * * @return static */ public function withValue($value) { $clone = clone $this; $clone->value = $value; return $clone; } /** * Gets an attribute. * * @param string $name * @param mixed $default * * @return array */ public function getAttribute($name, $default = null) { return $this->$name ?? $default; } /** * Adds an attribute. * * @param string $name * @param mixed $value * * @return static */ public function withAttribute($name, $value) { $clone = clone $this; $clone->attributes[$name] = $value; return $clone; } /** * Gets attributes. * * @return array */ public function getAttributes() { return $this->attributes; } /** * Merges multiple attributes. * * @param array $attributes * * @return static */ public function withAttributes(array $attributes) { $clone = clone $this; $clone->attributes = array_merge($this->attributes, $attributes); return $clone; } } src/Metadata.php 0000644 00000002234 15174335500 0007570 0 ustar 00 <?php namespace YOOtheme; use YOOtheme\View\MetadataObject; interface Metadata { /** * Gets all metadata tags. * * @param string $names * * @return MetadataObject[] */ public function all(...$names); /** * Gets a metadata tag. * * @param string $name * * @return MetadataObject|null */ public function get($name); /** * Sets a metadata tag. * * @param string $name * @param mixed $value * @param array $attributes * * @return MetadataObject */ public function set($name, $value, array $attributes = []); /** * Deletes a metadata tag. * * @param string $name */ public function del($name); /** * Merges multiple metadata tags. * * @param array $metadata */ public function merge(array $metadata); /** * Filters metadata tags using a callback. * * @param callable $filter * * @return MetadataObject[] */ public function filter(callable $filter); /** * Renders metadata tags. * * @return string */ public function render(); } bootstrap.php 0000644 00000000610 15174335500 0007272 0 ustar 00 <?php namespace YOOtheme; use YOOtheme\View\MetadataManager; return [ 'extend' => [ View::class => function (View $view, $app) { $view->addFunction('metadata', $app->wrap(Metadata::class . '@set')); }, ], 'aliases' => [ Metadata::class => 'metadata', ], 'services' => [ Metadata::class => MetadataManager::class, ], ];
| ver. 1.4 |
Github
|
.
| PHP 8.3.23 | Generation time: 0 |
proxy
|
phpinfo
|
Settings