| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* JCH Optimize - Performs several front-end optimizations for fast downloads |
| 5 |
* |
| 6 |
* @package jchoptimize/joomla-platform |
| 7 |
* @author Samuel Marshall <samuel@jch-optimize.net> |
| 8 |
* @copyright Copyright (c) 2024 Samuel Marshall / JCH Optimize |
| 9 |
* @license GNU/GPLv3, or later. See LICENSE file |
| 10 |
* |
| 11 |
* If LICENSE file missing, see <http://www.gnu.org/licenses/>. |
| 12 |
*/ |
| 13 |
|
| 14 |
namespace JchOptimize\Core; |
| 15 |
|
| 16 |
use _JchOptimizeVendor\V91\Psr\Http\Message\UriInterface; |
| 17 |
use JchOptimize\Core\Css\Components\ImportAtRule; |
| 18 |
use JchOptimize\Core\Css\CssComponents; |
| 19 |
use JchOptimize\Core\Exception\PropertyNotFoundException; |
| 20 |
use JchOptimize\Core\Html\Elements\Link; |
| 21 |
use JchOptimize\Core\Html\Elements\Script; |
| 22 |
use JchOptimize\Core\Html\Elements\Style; |
| 23 |
use JchOptimize\Core\Html\HtmlElementInterface; |
| 24 |
use JchOptimize\Core\Uri\Utils; |
| 25 |
|
| 26 |
class FileInfo |
| 27 |
{ |
| 28 |
protected ?UriInterface $uri = null; |
| 29 |
|
| 30 |
protected string $content = ''; |
| 31 |
|
| 32 |
protected string $media = ''; |
| 33 |
|
| 34 |
private string $layer = ''; |
| 35 |
|
| 36 |
private string $supports = ''; |
| 37 |
|
| 38 |
private bool $alreadyProcessed = false; |
| 39 |
|
| 40 |
private ?bool $aboveFold = null; |
| 41 |
|
| 42 |
public function __construct( |
| 43 |
protected HtmlElementInterface|CssComponents $element, |
| 44 |
protected ?bool $isSensitive = false |
| 45 |
) { |
| 46 |
$this->applyParts($element); |
| 47 |
} |
| 48 |
|
| 49 |
private function applyParts(CssComponents|HtmlElementInterface $element): void |
| 50 |
{ |
| 51 |
if ($element instanceof Script) { |
| 52 |
if (($src = $element->getSrc()) instanceof UriInterface) { |
| 53 |
$this->uri = $src; |
| 54 |
} else { |
| 55 |
$this->content = $element->getChildren()[0] ?? ''; |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
if ($element instanceof Link) { |
| 60 |
$href = $element->getHref(); |
| 61 |
if ($href instanceof UriInterface) { |
| 62 |
$this->uri = $href; |
| 63 |
} |
| 64 |
|
| 65 |
if (!empty($media = $element->getMedia())) { |
| 66 |
$this->media = $media; |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
if ($element instanceof Style) { |
| 71 |
$this->content = $element->getChildren()[0] ?? ''; |
| 72 |
|
| 73 |
if (!empty($media = $element->getMedia())) { |
| 74 |
$this->media = $media; |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
if ($element instanceof ImportAtRule) { |
| 79 |
$this->uri = $element->getUri(); |
| 80 |
$this->media = $element->getMediaQueriesList(); |
| 81 |
$this->layer = $element->getLayer(); |
| 82 |
$this->supports = $element->getSupports(); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
public function hasUri(): bool |
| 87 |
{ |
| 88 |
return $this->uri instanceof UriInterface; |
| 89 |
} |
| 90 |
|
| 91 |
public function getUri(): UriInterface |
| 92 |
{ |
| 93 |
if ($this->uri instanceof UriInterface) { |
| 94 |
return $this->uri; |
| 95 |
} |
| 96 |
|
| 97 |
throw new PropertyNotFoundException('Uri not set'); |
| 98 |
} |
| 99 |
|
| 100 |
public function getContent(): string |
| 101 |
{ |
| 102 |
return $this->content; |
| 103 |
} |
| 104 |
|
| 105 |
public function getMedia(): string |
| 106 |
{ |
| 107 |
return $this->media; |
| 108 |
} |
| 109 |
|
| 110 |
public function getLayer(): string |
| 111 |
{ |
| 112 |
return $this->layer; |
| 113 |
} |
| 114 |
|
| 115 |
public function getSupports(): string |
| 116 |
{ |
| 117 |
return $this->supports; |
| 118 |
} |
| 119 |
|
| 120 |
public function getElement(): HtmlElementInterface|CssComponents |
| 121 |
{ |
| 122 |
return $this->element; |
| 123 |
} |
| 124 |
|
| 125 |
public function display(): string |
| 126 |
{ |
| 127 |
if ($this->hasUri()) { |
| 128 |
return (string)$this->uri; |
| 129 |
} elseif ($this->getType() == 'js') { |
| 130 |
return 'Script Declaration'; |
| 131 |
} else { |
| 132 |
return 'Style Declaration'; |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
public function getType(): string |
| 137 |
{ |
| 138 |
return $this->element instanceof Script ? 'js' : 'css'; |
| 139 |
} |
| 140 |
|
| 141 |
public function isAlreadyProcessed(): bool |
| 142 |
{ |
| 143 |
return $this->alreadyProcessed; |
| 144 |
} |
| 145 |
|
| 146 |
public function setAlreadyProcessed(bool $alreadyProcessed): void |
| 147 |
{ |
| 148 |
$this->alreadyProcessed = $alreadyProcessed; |
| 149 |
} |
| 150 |
|
| 151 |
public function __serialize(): array |
| 152 |
{ |
| 153 |
return [ |
| 154 |
'uri' => (string)$this->uri, |
| 155 |
'content' => $this->content, |
| 156 |
'media' => $this->media, |
| 157 |
'layer' => $this->layer, |
| 158 |
'supports' => $this->supports, |
| 159 |
]; |
| 160 |
} |
| 161 |
|
| 162 |
public function __unserialize(array $data): void |
| 163 |
{ |
| 164 |
$this->uri = !empty($data['uri']) ? Utils::uriFor($data['uri']) : null; |
| 165 |
$this->content = $data['content'] ?? ''; |
| 166 |
$this->media = $data['media'] ?? ''; |
| 167 |
$this->layer = $data['layer'] ?? ''; |
| 168 |
$this->supports = $data['supports'] ?? ''; |
| 169 |
} |
| 170 |
|
| 171 |
public function isAboveFold(): ?bool |
| 172 |
{ |
| 173 |
return $this->aboveFold; |
| 174 |
} |
| 175 |
|
| 176 |
public function setAboveFold(bool $aboveFold): void |
| 177 |
{ |
| 178 |
$this->aboveFold = $aboveFold; |
| 179 |
} |
| 180 |
} |
| 181 |
|