Css.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. /**
  3. * @package Grav\Common\Assets
  4. *
  5. * @copyright Copyright (c) 2015 - 2021 Trilby Media, LLC. All rights reserved.
  6. * @license MIT License; see LICENSE file for details.
  7. */
  8. namespace Grav\Common\Assets;
  9. use Grav\Common\Utils;
  10. /**
  11. * Class Css
  12. * @package Grav\Common\Assets
  13. */
  14. class Css extends BaseAsset
  15. {
  16. /**
  17. * Css constructor.
  18. * @param array $elements
  19. * @param string|null $key
  20. */
  21. public function __construct(array $elements = [], $key = null)
  22. {
  23. $base_options = [
  24. 'asset_type' => 'css',
  25. 'attributes' => [
  26. 'type' => 'text/css',
  27. 'rel' => 'stylesheet'
  28. ]
  29. ];
  30. $merged_attributes = Utils::arrayMergeRecursiveUnique($base_options, $elements);
  31. parent::__construct($merged_attributes, $key);
  32. }
  33. /**
  34. * @return string
  35. */
  36. public function render()
  37. {
  38. if (isset($this->attributes['loading']) && $this->attributes['loading'] === 'inline') {
  39. $buffer = $this->gatherLinks([$this], self::CSS_ASSET);
  40. return "<style>\n" . trim($buffer) . "\n</style>\n";
  41. }
  42. return '<link href="' . trim($this->asset) . $this->renderQueryString() . '"' . $this->renderAttributes() . $this->integrityHash($this->asset) . ">\n";
  43. }
  44. }