| Total Complexity | 102 |
| Total Lines | 812 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like CoreExtension often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use CoreExtension, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 38 | class CoreExtension extends SlugifyExtension |
||
| 39 | { |
||
| 40 | /** @var Builder */ |
||
| 41 | protected $builder; |
||
| 42 | |||
| 43 | /** @var Config */ |
||
| 44 | protected $config; |
||
| 45 | |||
| 46 | /** @var Slugify */ |
||
| 47 | private static $slugifier; |
||
| 48 | |||
| 49 | public function __construct(Builder $builder) |
||
| 50 | { |
||
| 51 | if (!self::$slugifier instanceof Slugify) { |
||
| 52 | self::$slugifier = Slugify::create(['regexp' => Page::SLUGIFY_PATTERN]); |
||
| 53 | } |
||
| 54 | |||
| 55 | parent::__construct(self::$slugifier); |
||
| 56 | |||
| 57 | $this->builder = $builder; |
||
| 58 | $this->config = $this->builder->getConfig(); |
||
| 59 | } |
||
| 60 | |||
| 61 | /** |
||
| 62 | * {@inheritdoc} |
||
| 63 | */ |
||
| 64 | public function getName() |
||
| 65 | { |
||
| 66 | return 'CoreExtension'; |
||
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * {@inheritdoc} |
||
| 71 | */ |
||
| 72 | public function getFunctions() |
||
| 73 | { |
||
| 74 | return [ |
||
| 75 | new \Twig\TwigFunction('url', [$this, 'url']), |
||
| 76 | // assets |
||
| 77 | new \Twig\TwigFunction('asset', [$this, 'asset']), |
||
| 78 | new \Twig\TwigFunction('integrity', [$this, 'integrity']), |
||
| 79 | // content |
||
| 80 | new \Twig\TwigFunction('readtime', [$this, 'readtime']), |
||
| 81 | // others |
||
| 82 | new \Twig\TwigFunction('getenv', [$this, 'getEnv']), |
||
| 83 | // deprecated |
||
| 84 | new \Twig\TwigFunction( |
||
| 85 | 'hash', |
||
| 86 | [$this, 'integrity'], |
||
| 87 | ['deprecated' => true, 'alternative' => 'integrity'] |
||
| 88 | ), |
||
| 89 | new \Twig\TwigFunction( |
||
| 90 | 'minify', |
||
| 91 | [$this, 'minify'], |
||
| 92 | ['deprecated' => true, 'alternative' => 'minify filter'] |
||
| 93 | ), |
||
| 94 | new \Twig\TwigFunction( |
||
| 95 | 'toCSS', |
||
| 96 | [$this, 'toCss'], |
||
| 97 | ['deprecated' => true, 'alternative' => 'to_css filter'] |
||
| 98 | ), |
||
| 99 | ]; |
||
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * {@inheritdoc} |
||
| 104 | */ |
||
| 105 | public function getFilters() |
||
| 106 | { |
||
| 107 | return [ |
||
| 108 | new \Twig\TwigFilter('url', [$this, 'url']), |
||
| 109 | // collections |
||
| 110 | new \Twig\TwigFilter('sort_by_title', [$this, 'sortByTitle']), |
||
| 111 | new \Twig\TwigFilter('sort_by_weight', [$this, 'sortByWeight']), |
||
| 112 | new \Twig\TwigFilter('sort_by_date', [$this, 'sortByDate']), |
||
| 113 | new \Twig\TwigFilter('filter_by', [$this, 'filterBy']), |
||
| 114 | // assets |
||
| 115 | new \Twig\TwigFilter('html', [$this, 'html']), |
||
| 116 | new \Twig\TwigFilter('inline', [$this, 'inline']), |
||
| 117 | new \Twig\TwigFilter('fingerprint', [$this, 'fingerprint']), |
||
| 118 | new \Twig\TwigFilter('to_css', [$this, 'toCss']), |
||
| 119 | new \Twig\TwigFilter('minify', [$this, 'minify']), |
||
| 120 | new \Twig\TwigFilter('minify_css', [$this, 'minifyCss']), |
||
| 121 | new \Twig\TwigFilter('minify_js', [$this, 'minifyJs']), |
||
| 122 | new \Twig\TwigFilter('scss_to_css', [$this, 'scssToCss']), |
||
| 123 | new \Twig\TwigFilter('sass_to_css', [$this, 'scssToCss']), |
||
| 124 | new \Twig\TwigFilter('resize', [$this, 'resize']), |
||
| 125 | new \Twig\TwigFilter('dataurl', [$this, 'dataurl']), |
||
| 126 | new \Twig\TwigFilter('dominant_color', [$this, 'dominantColor']), |
||
| 127 | // content |
||
| 128 | new \Twig\TwigFilter('slugify', [$this, 'slugifyFilter']), |
||
| 129 | new \Twig\TwigFilter('excerpt', [$this, 'excerpt']), |
||
| 130 | new \Twig\TwigFilter('excerpt_html', [$this, 'excerptHtml']), |
||
| 131 | new \Twig\TwigFilter('markdown_to_html', [$this, 'markdownToHtml']), |
||
| 132 | new \Twig\TwigFilter('toc', [$this, 'markdownToToc']), |
||
| 133 | new \Twig\TwigFilter('json_decode', [$this, 'jsonDecode']), |
||
| 134 | new \Twig\TwigFilter('yaml_parse', [$this, 'yamlParse']), |
||
| 135 | new \Twig\TwigFilter('preg_split', [$this, 'pregSplit']), |
||
| 136 | new \Twig\TwigFilter('preg_match_all', [$this, 'pregMatchAll']), |
||
| 137 | new \Twig\TwigFilter('hex_to_rgb', [$this, 'hexToRgb']), |
||
| 138 | // deprecated |
||
| 139 | new \Twig\TwigFilter( |
||
| 140 | 'filterBySection', |
||
| 141 | [$this, 'filterBySection'], |
||
| 142 | ['deprecated' => true, 'alternative' => 'filter_by'] |
||
| 143 | ), |
||
| 144 | new \Twig\TwigFilter( |
||
| 145 | 'filterBy', |
||
| 146 | [$this, 'filterBy'], |
||
| 147 | ['deprecated' => true, 'alternative' => 'filter_by'] |
||
| 148 | ), |
||
| 149 | new \Twig\TwigFilter( |
||
| 150 | 'sortByTitle', |
||
| 151 | [$this, 'sortByTitle'], |
||
| 152 | ['deprecated' => true, 'alternative' => 'sort_by_title'] |
||
| 153 | ), |
||
| 154 | new \Twig\TwigFilter( |
||
| 155 | 'sortByWeight', |
||
| 156 | [$this, 'sortByWeight'], |
||
| 157 | ['deprecated' => true, 'alternative' => 'sort_by_weight'] |
||
| 158 | ), |
||
| 159 | new \Twig\TwigFilter( |
||
| 160 | 'sortByDate', |
||
| 161 | [$this, 'sortByDate'], |
||
| 162 | ['deprecated' => true, 'alternative' => 'sort_by_date'] |
||
| 163 | ), |
||
| 164 | new \Twig\TwigFilter( |
||
| 165 | 'minifyCSS', |
||
| 166 | [$this, 'minifyCss'], |
||
| 167 | ['deprecated' => true, 'alternative' => 'minifyCss'] |
||
| 168 | ), |
||
| 169 | new \Twig\TwigFilter( |
||
| 170 | 'minifyJS', |
||
| 171 | [$this, 'minifyJs'], |
||
| 172 | ['deprecated' => true, 'alternative' => 'minifyJs'] |
||
| 173 | ), |
||
| 174 | new \Twig\TwigFilter( |
||
| 175 | 'SCSStoCSS', |
||
| 176 | [$this, 'scssToCss'], |
||
| 177 | ['deprecated' => true, 'alternative' => 'scss_to_css'] |
||
| 178 | ), |
||
| 179 | new \Twig\TwigFilter( |
||
| 180 | 'excerptHtml', |
||
| 181 | [$this, 'excerptHtml'], |
||
| 182 | ['deprecated' => true, 'alternative' => 'excerpt_html'] |
||
| 183 | ), |
||
| 184 | new \Twig\TwigFilter( |
||
| 185 | 'urlize', |
||
| 186 | [$this, 'slugifyFilter'], |
||
| 187 | ['deprecated' => true, 'alternative' => 'slugify'] |
||
| 188 | ), |
||
| 189 | ]; |
||
| 190 | } |
||
| 191 | |||
| 192 | /** |
||
| 193 | * {@inheritdoc} |
||
| 194 | */ |
||
| 195 | public function getTests() |
||
| 196 | { |
||
| 197 | return [ |
||
| 198 | new \Twig\TwigTest('asset', [$this, 'isAsset']), |
||
| 199 | ]; |
||
| 200 | } |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Filters by Section. |
||
| 204 | */ |
||
| 205 | public function filterBySection(PagesCollection $pages, string $section): CollectionInterface |
||
| 206 | { |
||
| 207 | return $this->filterBy($pages, 'section', $section); |
||
| 208 | } |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Filters by variable's name/value. |
||
| 212 | */ |
||
| 213 | public function filterBy(PagesCollection $pages, string $variable, string $value): CollectionInterface |
||
| 214 | { |
||
| 215 | $filteredPages = $pages->filter(function (Page $page) use ($variable, $value) { |
||
| 216 | // is a dedicated getter exists? |
||
| 217 | $method = 'get'.ucfirst($variable); |
||
| 218 | if (method_exists($page, $method) && $page->$method() == $value) { |
||
| 219 | return $page->getType() == Type::PAGE() && !$page->isVirtual() && true; |
||
| 220 | } |
||
| 221 | // or a classic variable |
||
| 222 | if ($page->getVariable($variable) == $value) { |
||
| 223 | return $page->getType() == Type::PAGE() && !$page->isVirtual() && true; |
||
| 224 | } |
||
| 225 | }); |
||
| 226 | |||
| 227 | return $filteredPages; |
||
| 228 | } |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Sorts by title. |
||
| 232 | */ |
||
| 233 | public function sortByTitle(\Traversable $collection): array |
||
| 234 | { |
||
| 235 | $collection = iterator_to_array($collection); |
||
| 236 | /** @var \array $collection */ |
||
| 237 | array_multisort(array_keys(/** @scrutinizer ignore-type */ $collection), \SORT_ASC, \SORT_NATURAL | \SORT_FLAG_CASE, $collection); |
||
| 238 | |||
| 239 | return $collection; |
||
| 240 | } |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Sorts by weight. |
||
| 244 | */ |
||
| 245 | public function sortByWeight(\Traversable $collection): array |
||
| 246 | { |
||
| 247 | $callback = function ($a, $b) { |
||
| 248 | if (!isset($a['weight'])) { |
||
| 249 | $a['weight'] = 0; |
||
| 250 | } |
||
| 251 | if (!isset($b['weight'])) { |
||
| 252 | $a['weight'] = 0; |
||
| 253 | } |
||
| 254 | if ($a['weight'] == $b['weight']) { |
||
| 255 | return 0; |
||
| 256 | } |
||
| 257 | |||
| 258 | return ($a['weight'] < $b['weight']) ? -1 : 1; |
||
| 259 | }; |
||
| 260 | |||
| 261 | $collection = iterator_to_array($collection); |
||
| 262 | /** @var \array $collection */ |
||
| 263 | usort(/** @scrutinizer ignore-type */ $collection, $callback); |
||
| 264 | |||
| 265 | return $collection; |
||
| 266 | } |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Sorts by creation date (or 'updated' date): the most recent first. |
||
| 270 | */ |
||
| 271 | public function sortByDate(\Traversable $collection, string $variable = 'date'): array |
||
| 272 | { |
||
| 273 | $callback = function ($a, $b) use ($variable) { |
||
| 274 | if ($a[$variable] == $b[$variable]) { |
||
| 275 | return 0; |
||
| 276 | } |
||
| 277 | |||
| 278 | return ($a[$variable] > $b[$variable]) ? -1 : 1; |
||
| 279 | }; |
||
| 280 | |||
| 281 | $collection = iterator_to_array($collection); |
||
| 282 | /** @var \array $collection */ |
||
| 283 | usort(/** @scrutinizer ignore-type */ $collection, $callback); |
||
| 284 | |||
| 285 | return $collection; |
||
| 286 | } |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Creates an URL. |
||
| 290 | * |
||
| 291 | * $options[ |
||
| 292 | * 'canonical' => false, |
||
| 293 | * 'format' => 'html', |
||
| 294 | * 'language' => null, |
||
| 295 | * ]; |
||
| 296 | * |
||
| 297 | * @param Page|Asset|string|null $value |
||
| 298 | * @param array|null $options |
||
| 299 | */ |
||
| 300 | public function url($value = null, array $options = null): string |
||
| 301 | { |
||
| 302 | return (new Url($this->builder, $value, $options))->getUrl(); |
||
| 303 | } |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Creates an Asset (CSS, JS, images, etc.) from a path or an array of paths. |
||
| 307 | * |
||
| 308 | * @param string|array $path File path or array of files path (relative from `assets/` or `static/` dir). |
||
| 309 | * @param array|null $options |
||
| 310 | * |
||
| 311 | * @return Asset |
||
| 312 | */ |
||
| 313 | public function asset($path, array $options = null): Asset |
||
| 314 | { |
||
| 315 | return new Asset($this->builder, $path, $options); |
||
| 316 | } |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Compiles a SCSS asset. |
||
| 320 | * |
||
| 321 | * @param string|Asset $asset |
||
| 322 | * |
||
| 323 | * @return Asset |
||
| 324 | */ |
||
| 325 | public function toCss($asset): Asset |
||
| 326 | { |
||
| 327 | if (!$asset instanceof Asset) { |
||
| 328 | $asset = new Asset($this->builder, $asset); |
||
| 329 | } |
||
| 330 | |||
| 331 | return $asset->compile(); |
||
| 332 | } |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Minifying an asset (CSS or JS). |
||
| 336 | * |
||
| 337 | * @param string|Asset $asset |
||
| 338 | * |
||
| 339 | * @return Asset |
||
| 340 | */ |
||
| 341 | public function minify($asset): Asset |
||
| 342 | { |
||
| 343 | if (!$asset instanceof Asset) { |
||
| 344 | $asset = new Asset($this->builder, $asset); |
||
| 345 | } |
||
| 346 | |||
| 347 | return $asset->minify(); |
||
| 348 | } |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Fingerprinting an asset. |
||
| 352 | * |
||
| 353 | * @param string|Asset $asset |
||
| 354 | * |
||
| 355 | * @return Asset |
||
| 356 | */ |
||
| 357 | public function fingerprint($asset): Asset |
||
| 358 | { |
||
| 359 | if (!$asset instanceof Asset) { |
||
| 360 | $asset = new Asset($this->builder, $asset); |
||
| 361 | } |
||
| 362 | |||
| 363 | return $asset->fingerprint(); |
||
| 364 | } |
||
| 365 | |||
| 366 | /** |
||
| 367 | * Resizes an image. |
||
| 368 | * |
||
| 369 | * @param string|Asset $asset |
||
| 370 | * |
||
| 371 | * @return Asset |
||
| 372 | */ |
||
| 373 | public function resize($asset, int $size): Asset |
||
| 374 | { |
||
| 375 | if (!$asset instanceof Asset) { |
||
| 376 | $asset = new Asset($this->builder, $asset); |
||
| 377 | } |
||
| 378 | |||
| 379 | return $asset->resize($size); |
||
| 380 | } |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Returns the data URL of an image. |
||
| 384 | * |
||
| 385 | * @param string|Asset $asset |
||
| 386 | * |
||
| 387 | * @return string |
||
| 388 | */ |
||
| 389 | public function dataurl($asset): string |
||
| 390 | { |
||
| 391 | if (!$asset instanceof Asset) { |
||
| 392 | $asset = new Asset($this->builder, $asset); |
||
| 393 | } |
||
| 394 | |||
| 395 | return $asset->dataurl(); |
||
| 396 | } |
||
| 397 | |||
| 398 | /** |
||
| 399 | * Hashing an asset with algo (sha384 by default). |
||
| 400 | * |
||
| 401 | * @param string|Asset $path |
||
| 402 | * @param string $algo |
||
| 403 | * |
||
| 404 | * @return string |
||
| 405 | */ |
||
| 406 | public function integrity($asset, string $algo = 'sha384'): string |
||
| 407 | { |
||
| 408 | if (!$asset instanceof Asset) { |
||
| 409 | $asset = new Asset($this->builder, $asset); |
||
| 410 | } |
||
| 411 | |||
| 412 | return $asset->getIntegrity($algo); |
||
| 413 | } |
||
| 414 | |||
| 415 | /** |
||
| 416 | * Minifying a CSS string. |
||
| 417 | */ |
||
| 418 | public function minifyCss(?string $value): string |
||
| 435 | } |
||
| 436 | |||
| 437 | /** |
||
| 438 | * Minifying a JavaScript string. |
||
| 439 | */ |
||
| 440 | public function minifyJs(?string $value): string |
||
| 441 | { |
||
| 442 | $value = $value ?? ''; |
||
| 443 | |||
| 444 | if ($this->builder->isDebug()) { |
||
| 445 | return $value; |
||
| 446 | } |
||
| 447 | |||
| 448 | $cache = new Cache($this->builder); |
||
| 449 | $cacheKey = $cache->createKeyFromString($value); |
||
| 450 | if (!$cache->has($cacheKey)) { |
||
| 451 | $minifier = new Minify\JS($value); |
||
| 452 | $value = $minifier->minify(); |
||
| 453 | $cache->set($cacheKey, $value); |
||
| 454 | } |
||
| 455 | |||
| 456 | return $cache->get($cacheKey, $value); |
||
| 457 | } |
||
| 458 | |||
| 459 | /** |
||
| 460 | * Compiles a SCSS string. |
||
| 461 | * |
||
| 462 | * @throws RuntimeException |
||
| 463 | */ |
||
| 464 | public function scssToCss(?string $value): string |
||
| 465 | { |
||
| 466 | $value = $value ?? ''; |
||
| 467 | |||
| 468 | $cache = new Cache($this->builder); |
||
| 469 | $cacheKey = $cache->createKeyFromString($value); |
||
| 470 | if (!$cache->has($cacheKey)) { |
||
| 471 | $scssPhp = new Compiler(); |
||
| 472 | $outputStyles = ['expanded', 'compressed']; |
||
| 473 | $outputStyle = strtolower((string) $this->config->get('assets.compile.style')); |
||
| 474 | if (!in_array($outputStyle, $outputStyles)) { |
||
| 475 | throw new RuntimeException(\sprintf('Scss output style "%s" doesn\'t exists.', $outputStyle)); |
||
| 476 | } |
||
| 477 | $scssPhp->setOutputStyle($outputStyle); |
||
| 478 | $variables = $this->config->get('assets.compile.variables') ?? []; |
||
| 479 | if (!empty($variables)) { |
||
| 480 | $variables = array_map('ScssPhp\ScssPhp\ValueConverter::parseValue', $variables); |
||
| 481 | $scssPhp->replaceVariables($variables); |
||
| 482 | } |
||
| 483 | $value = $scssPhp->compileString($value)->getCss(); |
||
| 484 | $cache->set($cacheKey, $value); |
||
| 485 | } |
||
| 486 | |||
| 487 | return $cache->get($cacheKey, $value); |
||
| 488 | } |
||
| 489 | |||
| 490 | /** |
||
| 491 | * Creates the HTML element of an asset. |
||
| 492 | * |
||
| 493 | * $options[ |
||
| 494 | * 'preload' => false, |
||
| 495 | * 'responsive' => false, |
||
| 496 | * 'webp' => false, |
||
| 497 | * ]; |
||
| 498 | * |
||
| 499 | * @throws RuntimeException |
||
| 500 | */ |
||
| 501 | public function html(Asset $asset, array $attributes = [], array $options = []): string |
||
| 502 | { |
||
| 503 | $htmlAttributes = ''; |
||
| 504 | $preload = false; |
||
| 505 | $responsive = $this->config->get('assets.images.responsive.enabled') ?? false; |
||
| 506 | $webp = $this->config->get('assets.images.webp.enabled') ?? false; |
||
| 507 | extract($options, EXTR_IF_EXISTS); |
||
| 508 | |||
| 509 | // builds HTML attributes |
||
| 510 | foreach ($attributes as $name => $value) { |
||
| 511 | $attribute = \sprintf(' %s="%s"', $name, $value); |
||
| 512 | if (empty($value)) { |
||
| 513 | $attribute = \sprintf(' %s', $name); |
||
| 514 | } |
||
| 515 | $htmlAttributes .= $attribute; |
||
| 516 | } |
||
| 517 | |||
| 518 | // be sure Asset file is saved |
||
| 519 | $asset->save(); |
||
| 520 | |||
| 521 | // CSS or JavaScript |
||
| 522 | switch ($asset['ext']) { |
||
| 523 | case 'css': |
||
| 524 | if ($preload) { |
||
| 525 | return \sprintf( |
||
| 526 | '<link href="%s" rel="preload" as="style" onload="this.onload=null;this.rel=\'stylesheet\'"%s><noscript><link rel="stylesheet" href="%1$s"%2$s></noscript>', |
||
| 527 | $this->url($asset, $options), |
||
| 528 | $htmlAttributes |
||
| 529 | ); |
||
| 530 | } |
||
| 531 | |||
| 532 | return \sprintf('<link rel="stylesheet" href="%s"%s>', $this->url($asset, $options), $htmlAttributes); |
||
| 533 | case 'js': |
||
| 534 | return \sprintf('<script src="%s"%s></script>', $this->url($asset, $options), $htmlAttributes); |
||
| 535 | } |
||
| 536 | // image |
||
| 537 | if ($asset['type'] == 'image') { |
||
| 538 | // responsive |
||
| 539 | if ($responsive && $srcset = Image::buildSrcset( |
||
| 540 | $asset, |
||
| 541 | $this->config->get('assets.images.responsive.widths') ?? [480, 640, 768, 1024, 1366, 1600, 1920] |
||
| 542 | )) { |
||
| 543 | $htmlAttributes .= \sprintf(' srcset="%s"', $srcset); |
||
| 544 | $htmlAttributes .= \sprintf(' sizes="%s"', $this->config->get('assets.images.responsive.sizes.default') ?? '100vw'); |
||
| 545 | } |
||
| 546 | |||
| 547 | // <img> element |
||
| 548 | $img = \sprintf( |
||
| 549 | '<img src="%s" width="'.($asset->getWidth() ?: '').'" height="'.($asset->getHeight() ?: '').'"%s>', |
||
| 550 | $this->url($asset, $options), |
||
| 551 | $htmlAttributes |
||
| 552 | ); |
||
| 553 | |||
| 554 | // WebP conversion? |
||
| 555 | if ($webp && $asset['subtype'] != 'image/webp' && !Image::isAnimatedGif($asset)) { |
||
| 556 | try { |
||
| 557 | $assetWebp = Image::convertTopWebp($asset, $this->config->get('assets.images.quality') ?? 75); |
||
| 558 | // <source> element |
||
| 559 | $source = \sprintf('<source type="image/webp" srcset="%s">', $assetWebp); |
||
| 560 | // responsive |
||
| 561 | if ($responsive) { |
||
| 562 | $srcset = Image::buildSrcset( |
||
| 563 | $assetWebp, |
||
| 564 | $this->config->get('assets.images.responsive.widths') ?? [480, 640, 768, 1024, 1366, 1600, 1920] |
||
| 565 | ) ?: (string) $assetWebp; |
||
| 566 | // <source> element |
||
| 567 | $source = \sprintf( |
||
| 568 | '<source type="image/webp" srcset="%s" sizes="%s">', |
||
| 569 | $srcset, |
||
| 570 | $this->config->get('assets.images.responsive.sizes.default') ?? '100vw' |
||
| 571 | ); |
||
| 572 | } |
||
| 573 | |||
| 574 | return \sprintf("<picture>\n %s\n %s\n</picture>", $source, $img); |
||
| 575 | } catch (\Exception $e) { |
||
| 576 | $this->builder->getLogger()->debug($e->getMessage()); |
||
| 577 | } |
||
| 578 | } |
||
| 579 | |||
| 580 | return $img; |
||
| 581 | } |
||
| 582 | |||
| 583 | throw new RuntimeException(\sprintf('%s is available for CSS, JavaScript and images files only.', '"html" filter')); |
||
| 584 | } |
||
| 585 | |||
| 586 | /** |
||
| 587 | * Returns the content of an asset. |
||
| 588 | */ |
||
| 589 | public function inline(Asset $asset): string |
||
| 590 | { |
||
| 591 | return $asset['content']; |
||
| 592 | } |
||
| 593 | |||
| 594 | /** |
||
| 595 | * Reads $length first characters of a string and adds a suffix. |
||
| 596 | */ |
||
| 597 | public function excerpt(?string $string, int $length = 450, string $suffix = ' …'): string |
||
| 598 | { |
||
| 599 | $string = $string ?? ''; |
||
| 600 | |||
| 601 | $string = str_replace('</p>', '<br /><br />', $string); |
||
| 602 | $string = trim(strip_tags($string, '<br>'), '<br />'); |
||
| 603 | if (mb_strlen($string) > $length) { |
||
| 604 | $string = mb_substr($string, 0, $length); |
||
| 605 | $string .= $suffix; |
||
| 606 | } |
||
| 607 | |||
| 608 | return $string; |
||
| 609 | } |
||
| 610 | |||
| 611 | /** |
||
| 612 | * Reads characters before '<!-- excerpt|break -->'. |
||
| 613 | * Options: |
||
| 614 | * - separator: string to use as separator |
||
| 615 | * - capture: string to capture, 'before' (default) or 'after'. |
||
| 616 | */ |
||
| 617 | public function excerptHtml(?string $string, array $options = []): string |
||
| 618 | { |
||
| 619 | $string = $string ?? ''; |
||
| 620 | |||
| 621 | $separator = 'excerpt|break'; |
||
| 622 | $capture = 'before'; |
||
| 623 | extract($options, EXTR_IF_EXISTS); |
||
| 624 | |||
| 625 | // https://regex101.com/r/n9TWHF/1 |
||
| 626 | $pattern = '(.*)<!--[[:blank:]]?('.$separator.')[[:blank:]]?-->(.*)'; |
||
| 627 | preg_match('/'.$pattern.'/is', $string, $matches); |
||
| 628 | |||
| 629 | if (empty($matches)) { |
||
| 630 | return $string; |
||
| 631 | } |
||
| 632 | if ($capture == 'after') { |
||
| 633 | return trim($matches[3]); |
||
| 634 | } |
||
| 635 | // remove footnotes |
||
| 636 | return preg_replace('/<sup[^>]*>[^u]*<\/sup>/', '', trim($matches[1])); |
||
| 637 | } |
||
| 638 | |||
| 639 | /** |
||
| 640 | * Converts a Markdown string to HTML. |
||
| 641 | * |
||
| 642 | * @throws RuntimeException |
||
| 643 | */ |
||
| 644 | public function markdownToHtml(?string $markdown): ?string |
||
| 645 | { |
||
| 646 | $markdown = $markdown ?? ''; |
||
| 647 | |||
| 648 | try { |
||
| 649 | $parsedown = new Parsedown($this->builder); |
||
| 650 | $html = $parsedown->text($markdown); |
||
| 651 | } catch (\Exception $e) { |
||
| 652 | throw new RuntimeException('"markdown_to_html" filter can not convert supplied Markdown.'); |
||
| 653 | } |
||
| 654 | |||
| 655 | return $html; |
||
| 656 | } |
||
| 657 | |||
| 658 | /** |
||
| 659 | * Extract table of content of a Markdown string, |
||
| 660 | * in the given format ("html" or "json", "html" by default). |
||
| 661 | * |
||
| 662 | * @throws RuntimeException |
||
| 663 | */ |
||
| 664 | public function markdownToToc(?string $markdown, $format = 'html'): ?string |
||
| 665 | { |
||
| 666 | $markdown = $markdown ?? ''; |
||
| 667 | |||
| 668 | try { |
||
| 669 | $parsedown = new Parsedown($this->builder, ['selectors' => ['h2']]); |
||
| 670 | $parsedown->body($markdown); |
||
| 671 | $return = $parsedown->contentsList($format); |
||
| 672 | } catch (\Exception $e) { |
||
| 673 | throw new RuntimeException('"toc" filter can not convert supplied Markdown.'); |
||
| 674 | } |
||
| 675 | |||
| 676 | return $return; |
||
| 677 | } |
||
| 678 | |||
| 679 | /** |
||
| 680 | * Converts a JSON string to an array. |
||
| 681 | * |
||
| 682 | * @throws RuntimeException |
||
| 683 | */ |
||
| 684 | public function jsonDecode(?string $json): ?array |
||
| 685 | { |
||
| 686 | $json = $json ?? ''; |
||
| 687 | |||
| 688 | try { |
||
| 689 | $array = json_decode($json, true); |
||
| 690 | if ($array === null && json_last_error() !== JSON_ERROR_NONE) { |
||
| 691 | throw new \Exception('JSON error.'); |
||
| 692 | } |
||
| 693 | } catch (\Exception $e) { |
||
| 694 | throw new RuntimeException('"json_decode" filter can not parse supplied JSON.'); |
||
| 695 | } |
||
| 696 | |||
| 697 | return $array; |
||
| 698 | } |
||
| 699 | |||
| 700 | /** |
||
| 701 | * Converts a YAML string to an array. |
||
| 702 | * |
||
| 703 | * @throws RuntimeException |
||
| 704 | */ |
||
| 705 | public function yamlParse(?string $yaml): ?array |
||
| 706 | { |
||
| 707 | $yaml = $yaml ?? ''; |
||
| 708 | |||
| 709 | try { |
||
| 710 | $array = Yaml::parse($yaml); |
||
| 711 | if (!is_array($array)) { |
||
| 712 | throw new ParseException('YAML error.'); |
||
| 713 | } |
||
| 714 | } catch (ParseException $e) { |
||
| 715 | throw new RuntimeException(\sprintf('"yaml_parse" filter can not parse supplied YAML: %s', $e->getMessage())); |
||
| 716 | } |
||
| 717 | |||
| 718 | return $array; |
||
| 719 | } |
||
| 720 | |||
| 721 | /** |
||
| 722 | * Split a string into an array using a regular expression. |
||
| 723 | * |
||
| 724 | * @throws RuntimeException |
||
| 725 | */ |
||
| 726 | public function pregSplit(?string $value, string $pattern, int $limit = 0): ?array |
||
| 727 | { |
||
| 728 | $value = $value ?? ''; |
||
| 729 | |||
| 730 | try { |
||
| 731 | $array = preg_split($pattern, $value, $limit); |
||
| 732 | if ($array === false) { |
||
| 733 | throw new RuntimeException('PREG split error.'); |
||
| 734 | } |
||
| 735 | } catch (\Exception $e) { |
||
| 736 | throw new RuntimeException('"preg_split" filter can not split supplied string.'); |
||
| 737 | } |
||
| 738 | |||
| 739 | return $array; |
||
| 740 | } |
||
| 741 | |||
| 742 | /** |
||
| 743 | * Perform a regular expression match and return the group for all matches. |
||
| 744 | * |
||
| 745 | * @throws RuntimeException |
||
| 746 | */ |
||
| 747 | public function pregMatchAll(?string $value, string $pattern, int $group = 0): ?array |
||
| 748 | { |
||
| 749 | $value = $value ?? ''; |
||
| 750 | |||
| 751 | try { |
||
| 752 | $array = preg_match_all($pattern, $value, $matches, PREG_PATTERN_ORDER); |
||
| 753 | if ($array === false) { |
||
| 754 | throw new RuntimeException('PREG match all error.'); |
||
| 755 | } |
||
| 756 | } catch (\Exception $e) { |
||
| 757 | throw new RuntimeException('"preg_match_all" filter can not match in supplied string.'); |
||
| 758 | } |
||
| 759 | |||
| 760 | return $matches[$group]; |
||
| 761 | } |
||
| 762 | |||
| 763 | /** |
||
| 764 | * Calculates estimated time to read a text. |
||
| 765 | */ |
||
| 766 | public function readtime(?string $text): string |
||
| 777 | } |
||
| 778 | |||
| 779 | /** |
||
| 780 | * Gets the value of an environment variable. |
||
| 781 | */ |
||
| 782 | public function getEnv(?string $var): ?string |
||
| 783 | { |
||
| 784 | $var = $var ?? ''; |
||
| 785 | |||
| 786 | return getenv($var) ?: null; |
||
| 787 | } |
||
| 788 | |||
| 789 | /** |
||
| 790 | * Tests if a variable is an Asset. |
||
| 791 | */ |
||
| 792 | public function isAsset($variable): bool |
||
| 793 | { |
||
| 794 | return $variable instanceof Asset; |
||
| 795 | } |
||
| 796 | |||
| 797 | /** |
||
| 798 | * Returns the dominant hex color of an image asset. |
||
| 799 | * |
||
| 800 | * @param string|Asset $path |
||
| 801 | * |
||
| 802 | * @return string |
||
| 803 | */ |
||
| 804 | public function dominantColor($asset): string |
||
| 811 | } |
||
| 812 | |||
| 813 | /** |
||
| 814 | * Converts an hexadecimal color to RGB. |
||
| 815 | * |
||
| 816 | * @throws RuntimeException |
||
| 817 | */ |
||
| 818 | public function hexToRgb(?string $variable): array |
||
| 819 | { |
||
| 820 | $variable = $variable ?? ''; |
||
| 821 | |||
| 822 | if (!self::isHex($variable)) { |
||
| 823 | throw new RuntimeException(\sprintf('"%s" is not a valid hexadecimal value.', $variable)); |
||
| 835 | ]; |
||
| 836 | } |
||
| 837 | |||
| 838 | /** |
||
| 839 | * Is a hexadecimal color is valid? |
||
| 840 | */ |
||
| 841 | private static function isHex(string $hex): bool |
||
| 842 | { |
||
| 843 | $valid = is_string($hex); |
||
| 844 | $hex = ltrim($hex, '#'); |
||
| 845 | $length = strlen($hex); |
||
| 846 | $valid = $valid && ($length === 3 || $length === 6); |
||
| 847 | $valid = $valid && ctype_xdigit($hex); |
||
| 850 | } |
||
| 851 | } |
||
| 852 |