/* Decoded by unphp.net */ get("system.assets"); $locator = $grav["locator"]; $this->assets_dir = $locator->findResource("asset://"); $this->assets_url = $locator->findResource("asset://", false); $this->config($asset_config); foreach ((array) $this->collections as $name => $collection) { $this->registerCollection($name, (array) $collection); } } public function config(array $config) { foreach ($config as $key => $value) { if ($this->hasProperty($key)) { $this->setProperty($key, $value); } elseif (Utils::startsWith($key, "css_") || Utils::startsWith($key, "js_")) { $this->pipeline_options[$key] = $value; } } if ($this->enable_asset_timestamp) { $this->timestamp = Grav::instance()["cache"]->getKey(); } return $this; } public function add($asset) { if (!$asset) { return $this; } $args = func_get_args(); if (is_array($asset)) { foreach ($asset as $index => $location) { $params = array_slice($args, 1); if (is_array($location)) { $params = array_shift($params); if (is_numeric($params)) { $params = array("priority" => $params); } $params = array(array_replace_recursive(array(), $location, $params)); $location = $index; } $params = array_merge(array($location), $params); call_user_func_array(array($this, "add"), $params); } } elseif (isset($this->collections[$asset])) { array_shift($args); $args = array_merge(array($this->collections[$asset]), $args); call_user_func_array(array($this, "add"), $args); } else { $path = parse_url($asset, PHP_URL_PATH); $extension = $path ? Utils::pathinfo($path, PATHINFO_EXTENSION) : ''; if ($extension !== '') { $extension = strtolower($extension); if ($extension === "css") { call_user_func_array(array($this, "addCss"), $args); } elseif ($extension === "js") { call_user_func_array(array($this, "addJs"), $args); } elseif ($extension === "mjs") { call_user_func_array(array($this, "addJsModule"), $args); } } } return $this; } protected function addType($collection, $type, $asset, $options) { if (is_array($asset)) { foreach ($asset as $index => $location) { $assetOptions = $options; if (is_array($location)) { $assetOptions = array_replace_recursive(array(), $options, $location); $location = $index; } $this->addType($collection, $type, $location, $assetOptions); } return $this; } if ($this->isValidType($type) && isset($this->collections[$asset])) { $this->addType($collection, $type, $this->collections[$asset], $options); return $this; } if (isset($options["pipeline"])) { if ($options["pipeline"] === false) { $exclude_type = $this->getBaseType($type); $excludes = strtolower($exclude_type . "_pipeline_before_excludes"); if ($this->{$excludes}) { $default = "after"; } else { $default = "before"; } $options["position"] = $options["position"] ?? $default; } unset($options["pipeline"]); } $timestamp_override = $options["timestamp"] ?? true; if (filter_var($timestamp_override, FILTER_VALIDATE_BOOLEAN)) { $options["timestamp"] = $this->timestamp; } else { $options["timestamp"] = null; } $group = $options["group"] ?? "head"; $position = $options["position"] ?? "pipeline"; $orderKey = "{$type}|{$group}|{$position}"; if (!isset($this->order[$orderKey])) { $this->order[$orderKey] = 0; } $options["order"] = $this->order[$orderKey]++; $asset_object = new $type(); if ($asset_object->init($asset, $options)) { $this->{$collection[md5($asset)]} = $asset_object; } return $this; } public function addLink($asset) { return $this->addType($this::LINK_COLLECTION, $this::LINK_TYPE, $asset, $this->unifyLegacyArguments(func_get_args(), $this::LINK_TYPE)); } public function addCss($asset) { return $this->addType($this::CSS_COLLECTION, $this::CSS_TYPE, $asset, $this->unifyLegacyArguments(func_get_args(), $this::CSS_TYPE)); } public function addInlineCss($asset) { return $this->addType($this::CSS_COLLECTION, $this::INLINE_CSS_TYPE, $asset, $this->unifyLegacyArguments(func_get_args(), $this::INLINE_CSS_TYPE)); } public function addJs($asset) { return $this->addType($this::JS_COLLECTION, $this::JS_TYPE, $asset, $this->unifyLegacyArguments(func_get_args(), $this::JS_TYPE)); } public function addInlineJs($asset) { return $this->addType($this::JS_COLLECTION, $this::INLINE_JS_TYPE, $asset, $this->unifyLegacyArguments(func_get_args(), $this::INLINE_JS_TYPE)); } public function addJsModule($asset) { return $this->addType($this::JS_MODULE_COLLECTION, $this::JS_MODULE_TYPE, $asset, $this->unifyLegacyArguments(func_get_args(), $this::JS_MODULE_TYPE)); } public function addInlineJsModule($asset) { return $this->addType($this::JS_MODULE_COLLECTION, $this::INLINE_JS_MODULE_TYPE, $asset, $this->unifyLegacyArguments(func_get_args(), $this::INLINE_JS_MODULE_TYPE)); } public function registerCollection($collectionName, array $assets, $overwrite = false) { if ($overwrite || !isset($this->collections[$collectionName])) { $this->collections[$collectionName] = $assets; } return $this; } protected function filterAssets($assets, $key, $value, $sort = false) { $results = array_filter($assets, function ($asset) use($key, $value) { if ($key === "position" && $value === "pipeline") { $type = $asset->getType(); if ($type === "jsmodule") { $type = "js_module"; } if ($asset->getRemote() && $this->{strtolower($type) . "_pipeline_include_externals"} === false && $asset["position"] === "pipeline") { if ($this->{strtolower($type) . "_pipeline_before_excludes"}) { $asset->setPosition("after"); } else { $asset->setPosition("before"); } return false; } } if ($asset[$key] === $value) { return true; } return false; }); if ($sort && !empty($results)) { $results = $this->sortAssets($results); } return $results; } protected function sortAssets($assets) { uasort($assets, static function ($a, $b) { return $b["priority"] <=> $a["priority"] ?: $a["order"] <=> $b["order"]; }); return $assets; } public function render($type, $group = "head", $attributes = array()) { $before_output = ''; $pipeline_output = ''; $after_output = ''; $assets = "assets_" . $type; $pipeline_enabled = $type . "_pipeline"; $render_pipeline = "render" . ucfirst($type); $group_assets = $this->filterAssets($this->{$assets}, "group", $group); $pipeline_assets = $this->filterAssets($group_assets, "position", "pipeline", true); $before_assets = $this->filterAssets($group_assets, "position", "before", true); $after_assets = $this->filterAssets($group_assets, "position", "after", true); if ($this->{$pipeline_enabled} ?? false) { $options = array_merge($this->pipeline_options, array("timestamp" => $this->timestamp)); $pipeline = new Pipeline($options); $pipeline_output = $pipeline->{$render_pipeline}($pipeline_assets, $group, $attributes); } else { foreach ($pipeline_assets as $asset) { $pipeline_output .= $asset->render(); } } foreach ($before_assets as $asset) { $before_output .= $asset->render(); } foreach ($after_assets as $asset) { $after_output .= $asset->render(); } return $before_output . $pipeline_output . $after_output; } public function css($group = "head", $attributes = array(), $include_link = true) { $output = ''; if ($include_link) { $output = $this->link($group, $attributes); } $output .= $this->render(self::CSS, $group, $attributes); return $output; } public function link($group = "head", $attributes = array()) { return $this->render(self::LINK, $group, $attributes); } public function js($group = "head", $attributes = array(), $include_js_module = true) { $output = $this->render(self::JS, $group, $attributes); if ($include_js_module) { $output .= $this->jsModule($group, $attributes); } return $output; } public function jsModule($group = "head", $attributes = array()) { return $this->render(self::JS_MODULE, $group, $attributes); } public function all($group = "head", $attributes = array()) { $output = $this->css($group, $attributes, false); $output .= $this->link($group, $attributes); $output .= $this->js($group, $attributes, false); $output .= $this->jsModule($group, $attributes); return $output; } protected function isValidType($type) { return in_array($type, array(self::CSS_TYPE, self::JS_TYPE, self::JS_MODULE_TYPE)); } protected function getBaseType($type) { switch ($type) { case $this::JS_TYPE: case $this::INLINE_JS_TYPE: $base_type = $this::JS; break; case $this::JS_MODULE_TYPE: case $this::INLINE_JS_MODULE_TYPE: $base_type = $this::JS_MODULE; break; default: $base_type = $this::CSS; } return $base_type; } } ?>