Skip to content

Commit a158afd

Browse files
committed
upload
1 parent 16c91fa commit a158afd

11 files changed

+718
-0
lines changed

composer.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "erag/laravel-setup-layout",
3+
"description": "A package for setting up layout in Laravel applications.",
4+
"license": "MIT",
5+
"authors": [
6+
{
7+
"name": "Er Amiit Gupta",
8+
"email": "[email protected]"
9+
}
10+
],
11+
"require": {
12+
"php": ">=8.0.0"
13+
},
14+
"autoload": {
15+
"files": [
16+
"src/helper.php"
17+
],
18+
"psr-4": {
19+
"LaravelSetupLayout\\": "src/"
20+
}
21+
}
22+
}

src/Components/AppLayout.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace LaravelSetupLayout\Components;
4+
5+
use Closure;
6+
use Illuminate\Contracts\View\View;
7+
use Illuminate\View\Component;
8+
9+
class AppLayout extends Component
10+
{
11+
/**
12+
* Create a new component instance.
13+
*/
14+
public function __construct()
15+
{
16+
//
17+
}
18+
19+
/**
20+
* Get the view / contents that represent the component.
21+
*/
22+
public function render(): View|Closure|string
23+
{
24+
return view('vendor.setup-layout.components.app-layout');
25+
}
26+
}

src/Components/WebAppLayout.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace LaravelSetupLayout\Components;
4+
5+
use Closure;
6+
use Illuminate\Contracts\View\View;
7+
use Illuminate\View\Component;
8+
9+
class WebAppLayout extends Component
10+
{
11+
/**
12+
* Create a new component instance.
13+
*/
14+
public function __construct()
15+
{
16+
//
17+
}
18+
19+
/**
20+
* Get the view / contents that represent the component.
21+
*/
22+
public function render(): View|Closure|string
23+
{
24+
return view('vendor.setup-layout.components.web-app-layout');
25+
}
26+
}

src/Config/layout-assets.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
return [
4+
5+
#THEME_VENDORS <x-web-app-layout> Define web assets for different pages
6+
'THEME_WEB_ASSETS' => [
7+
'home' => [
8+
// CSS files for the home page
9+
'css' => [
10+
'assets/css/demo.css',
11+
],
12+
// JavaScript files for the home page
13+
'js' => [
14+
'assets/js/demo.js',
15+
],
16+
],
17+
// You can add more as per your requirement
18+
],
19+
20+
21+
#THEME_ASSETS <x-app-layout> Define global assets used across all pages
22+
'THEME_ASSETS' => [
23+
'global' => [
24+
// Global CSS files, such as Bootstrap
25+
'css' => [
26+
'https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css',
27+
// You can add more global CSS files here
28+
],
29+
// Global JavaScript files, such as Bootstrap JS
30+
'js' => [
31+
'https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js',
32+
// You can add more global JavaScript files here
33+
],
34+
],
35+
],
36+
37+
#THEME_VENDORS <x-app-layout> Define vendor assets specific to certain pages or components
38+
'THEME_VENDORS' => [
39+
'demo' => [
40+
// CSS files for the login page or component
41+
'css' => [
42+
'assets/css/demo.css',
43+
],
44+
// JavaScript files for the login page or component
45+
'js' => [
46+
'assets/js/demo.js',
47+
],
48+
],
49+
// You can add more as per your requirement
50+
],
51+
];

src/Core/Theme.php

Lines changed: 232 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,232 @@
1+
<?php
2+
3+
/*
4+
* Author: eramitgupta
5+
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

Comments
 (0)