123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207 |
- <?php
- /**
- * @package Grav\Common\Assets
- *
- * @copyright Copyright (c) 2015 - 2023 Trilby Media, LLC. All rights reserved.
- * @license MIT License; see LICENSE file for details.
- */
- namespace Grav\Common\Assets;
- use Grav\Common\Assets;
- use Grav\Common\Config\Config;
- use Grav\Common\Grav;
- use Grav\Framework\ContentBlock\HtmlBlock;
- use function strlen;
- /**
- * Register block assets into Grav.
- */
- class BlockAssets
- {
- /**
- * @param HtmlBlock $block
- * @return void
- */
- public static function registerAssets(HtmlBlock $block): void
- {
- $grav = Grav::instance();
- /** @var Assets $assets */
- $assets = $grav['assets'];
- $types = $block->getAssets();
- foreach ($types as $type => $groups) {
- switch ($type) {
- case 'frameworks':
- static::registerFrameworks($assets, $groups);
- break;
- case 'styles':
- static::registerStyles($assets, $groups);
- break;
- case 'scripts':
- static::registerScripts($assets, $groups);
- break;
- case 'links':
- static::registerLinks($assets, $groups);
- break;
- case 'html':
- static::registerHtml($assets, $groups);
- break;
- }
- }
- }
- /**
- * @param Assets $assets
- * @param array $list
- * @return void
- */
- protected static function registerFrameworks(Assets $assets, array $list): void
- {
- if ($list) {
- throw new \RuntimeException('Not Implemented');
- }
- }
- /**
- * @param Assets $assets
- * @param array $groups
- * @return void
- */
- protected static function registerStyles(Assets $assets, array $groups): void
- {
- $grav = Grav::instance();
- /** @var Config $config */
- $config = $grav['config'];
- foreach ($groups as $group => $styles) {
- foreach ($styles as $style) {
- switch ($style[':type']) {
- case 'file':
- $options = [
- 'priority' => $style[':priority'],
- 'group' => $group,
- 'type' => $style['type'],
- 'media' => $style['media']
- ] + $style['element'];
- $assets->addCss(static::getRelativeUrl($style['href'], $config->get('system.assets.css_pipeline')), $options);
- break;
- case 'inline':
- $options = [
- 'priority' => $style[':priority'],
- 'group' => $group,
- 'type' => $style['type'],
- ] + $style['element'];
- $assets->addInlineCss($style['content'], $options);
- break;
- }
- }
- }
- }
- /**
- * @param Assets $assets
- * @param array $groups
- * @return void
- */
- protected static function registerScripts(Assets $assets, array $groups): void
- {
- $grav = Grav::instance();
- /** @var Config $config */
- $config = $grav['config'];
- foreach ($groups as $group => $scripts) {
- $group = $group === 'footer' ? 'bottom' : $group;
- foreach ($scripts as $script) {
- switch ($script[':type']) {
- case 'file':
- $options = [
- 'group' => $group,
- 'priority' => $script[':priority'],
- 'src' => $script['src'],
- 'type' => $script['type'],
- 'loading' => $script['loading'],
- 'defer' => $script['defer'],
- 'async' => $script['async'],
- 'handle' => $script['handle']
- ] + $script['element'];
- $assets->addJs(static::getRelativeUrl($script['src'], $config->get('system.assets.js_pipeline')), $options);
- break;
- case 'inline':
- $options = [
- 'priority' => $script[':priority'],
- 'group' => $group,
- 'type' => $script['type'],
- 'loading' => $script['loading']
- ] + $script['element'];
- $assets->addInlineJs($script['content'], $options);
- break;
- }
- }
- }
- }
- /**
- * @param Assets $assets
- * @param array $groups
- * @return void
- */
- protected static function registerLinks(Assets $assets, array $groups): void
- {
- foreach ($groups as $group => $links) {
- foreach ($links as $link) {
- $href = $link['href'];
- $options = [
- 'group' => $group,
- 'priority' => $link[':priority'],
- 'rel' => $link['rel'],
- ] + $link['element'];
- $assets->addLink($href, $options);
- }
- }
- }
- /**
- * @param Assets $assets
- * @param array $groups
- * @return void
- */
- protected static function registerHtml(Assets $assets, array $groups): void
- {
- if ($groups) {
- throw new \RuntimeException('Not Implemented');
- }
- }
- /**
- * @param string $url
- * @param bool $pipeline
- * @return string
- */
- protected static function getRelativeUrl($url, $pipeline)
- {
- $grav = Grav::instance();
- $base = rtrim($grav['base_url'], '/') ?: '/';
- if (strpos($url, $base) === 0) {
- if ($pipeline) {
- // Remove file timestamp if CSS pipeline has been enabled.
- $url = preg_replace('|[?#].*|', '', $url);
- }
- return substr($url, strlen($base) - 1);
- }
- return $url;
- }
- }
|