|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * Author: eramitgupta |
| 5 | + * Email: info.eramitgupta@gmail.com |
| 6 | + * |
| 7 | + * Copyright (c) 2024 by eramitgupta. |
| 8 | + * All rights reserved. |
| 9 | + * |
| 10 | + * You cannot steal and copy my code, I have the copyright, I do not give authority. |
| 11 | + */ |
| 12 | + |
| 13 | +namespace LaravelSetupLayout\Core; |
| 14 | + |
| 15 | +class Theme |
| 16 | +{ |
| 17 | + public static $htmlAttributes = []; |
| 18 | + public static $htmlClasses = []; |
| 19 | + |
| 20 | + public static $javascriptFiles = []; |
| 21 | + public static $cssFiles = []; |
| 22 | + public static $vendorFiles = []; |
| 23 | + public static $webAssets = []; |
| 24 | + |
| 25 | + |
| 26 | + function addHtmlAttribute($scope, $name, $value) |
| 27 | + { |
| 28 | + self::$htmlAttributes[$scope][$name] = $value; |
| 29 | + // dump(self::$htmlAttributes); |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * Add multiple HTML attributes by scope |
| 34 | + * |
| 35 | + * @param $scope |
| 36 | + * @param $attributes |
| 37 | + * |
| 38 | + * @return void |
| 39 | + */ |
| 40 | + function addHtmlAttributes($scope, $attributes) |
| 41 | + { |
| 42 | + foreach ($attributes as $key => $value) { |
| 43 | + self::$htmlAttributes[$scope][$key] = $value; |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Add HTML class by scope |
| 49 | + * |
| 50 | + * @param $scope |
| 51 | + * @param $value |
| 52 | + * |
| 53 | + * @return void |
| 54 | + */ |
| 55 | + function addHtmlClass($scope, $value) |
| 56 | + { |
| 57 | + self::$htmlClasses[$scope] = $value; |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Print HTML attributes for the HTML template |
| 62 | + * |
| 63 | + * @param $scope |
| 64 | + * |
| 65 | + * @return string |
| 66 | + */ |
| 67 | + function printHtmlAttributes($scope) |
| 68 | + { |
| 69 | + $attributes = []; |
| 70 | + if (isset(self::$htmlAttributes[$scope])) { |
| 71 | + foreach (self::$htmlAttributes[$scope] as $key => $value) { |
| 72 | + $attributes[] = sprintf('%s="%s"', $key, $value); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + return join(' ', $attributes); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Print HTML classes for the HTML template |
| 81 | + * |
| 82 | + * @param $scope |
| 83 | + * @param $full |
| 84 | + * |
| 85 | + * @return string |
| 86 | + */ |
| 87 | + function printHtmlClasses($scope, $full = true) |
| 88 | + { |
| 89 | + if (empty(self::$htmlClasses)) { |
| 90 | + return ''; |
| 91 | + } |
| 92 | + |
| 93 | + $classes = ''; |
| 94 | + if (isset(self::$htmlClasses[$scope])) { |
| 95 | + $classes = self::$htmlClasses[$scope]; |
| 96 | + } |
| 97 | + |
| 98 | + if ($full) { |
| 99 | + return sprintf('class="%s"', $classes); |
| 100 | + } |
| 101 | + |
| 102 | + return $classes; |
| 103 | + } |
| 104 | + |
| 105 | + function getGlobalAssets($type = 'js') |
| 106 | + { |
| 107 | + // $this->extendCssFilename() |
| 108 | + return config('layout-assets.THEME_ASSETS.global.' . $type); |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Add multiple vendors to the page by name. Refer to layout-assets THEME_VENDORS |
| 113 | + * |
| 114 | + * @param $vendors |
| 115 | + * |
| 116 | + * @return array |
| 117 | + */ |
| 118 | + function addVendors($vendors) |
| 119 | + { |
| 120 | + foreach ($vendors as $value) { |
| 121 | + self::$vendorFiles[] = $value; |
| 122 | + } |
| 123 | + return array_unique(self::$vendorFiles); |
| 124 | + } |
| 125 | + |
| 126 | + |
| 127 | + /** |
| 128 | + * Add custom javascript file to the page |
| 129 | + * |
| 130 | + * @param $file |
| 131 | + * |
| 132 | + * @return void |
| 133 | + */ |
| 134 | + function addJavascriptFile($file) |
| 135 | + { |
| 136 | + self::$javascriptFiles[] = $file; |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * Add custom CSS file to the page |
| 141 | + * |
| 142 | + * @param $file |
| 143 | + * |
| 144 | + * @return void |
| 145 | + */ |
| 146 | + function addCssFile($file) |
| 147 | + { |
| 148 | + self::$cssFiles[] = $file; |
| 149 | + } |
| 150 | + |
| 151 | + /** |
| 152 | + * Get vendor files from layout-assets. Refer to settings |
| 153 | + * |
| 154 | + * @param $type |
| 155 | + * |
| 156 | + * @return array |
| 157 | + */ |
| 158 | + function getVendors($type) |
| 159 | + { |
| 160 | + $files = []; |
| 161 | + foreach (self::$vendorFiles as $vendor) { |
| 162 | + $vendors = config('layout-assets.THEME_VENDORS.' . $vendor); |
| 163 | + if (isset($vendors[$type])) { |
| 164 | + foreach ($vendors[$type] as $path) { |
| 165 | + $files[] = $path; |
| 166 | + } |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + return array_unique($files); |
| 171 | + } |
| 172 | + |
| 173 | + |
| 174 | + |
| 175 | + // |
| 176 | + |
| 177 | + function addWebAsset($webAssets) |
| 178 | + { |
| 179 | + foreach ($webAssets as $value) { |
| 180 | + self::$webAssets[] = $value; |
| 181 | + } |
| 182 | + return array_unique(self::$webAssets); |
| 183 | + } |
| 184 | + |
| 185 | + function getWebAssets($type) |
| 186 | + { |
| 187 | + $files = []; |
| 188 | + foreach (self::$webAssets as $webAsset) { |
| 189 | + $assets = config('layout-assets.THEME_WEB_ASSETS.' . $webAsset); |
| 190 | + if (isset($assets[$type])) { |
| 191 | + foreach ($assets[$type] as $path) { |
| 192 | + $files[] = $path; |
| 193 | + } |
| 194 | + } |
| 195 | + } |
| 196 | + |
| 197 | + return array_unique($files); |
| 198 | + } |
| 199 | + |
| 200 | + /** |
| 201 | + * Get custom js files from the settings |
| 202 | + * |
| 203 | + * @return array |
| 204 | + */ |
| 205 | + function getCustomJs() |
| 206 | + { |
| 207 | + return self::$javascriptFiles; |
| 208 | + } |
| 209 | + |
| 210 | + /** |
| 211 | + * Get custom css files from the settings |
| 212 | + * |
| 213 | + * @return array |
| 214 | + */ |
| 215 | + function getCustomCss() |
| 216 | + { |
| 217 | + return self::$cssFiles; |
| 218 | + } |
| 219 | + |
| 220 | + /** |
| 221 | + * Get HTML attribute based on the scope |
| 222 | + * |
| 223 | + * @param $scope |
| 224 | + * @param $attribute |
| 225 | + * |
| 226 | + * @return array |
| 227 | + */ |
| 228 | + function getHtmlAttribute($scope, $attribute) |
| 229 | + { |
| 230 | + return self::$htmlAttributes[$scope][$attribute] ?? []; |
| 231 | + } |
| 232 | +} |
0 commit comments