Skip to content

Commit 0e46f69

Browse files
Merge pull request #2 from codions/develop
Register theme autoloader
2 parents 53de085 + 4eee34a commit 0e46f69

File tree

4 files changed

+36
-20
lines changed

4 files changed

+36
-20
lines changed

.gitignore

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
vendor/
2-
.idea/
3-
build/
1+
vendor
2+
.idea
3+
.vscode
4+
build
45
.DS_Store
5-
*.cache
6+
*.cache

resources/stubs/template/src/Helpers/functions.php

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,6 @@
1010
|
1111
*/
1212

13-
if (! function_exists('theme_load')) {
14-
15-
/**
16-
* This function is called on theme loading. Put in all the rules that
17-
* you want to be executedas a settings override or any other action.
18-
*/
19-
function theme_load()
20-
{
21-
// Unleash your imagination here
22-
}
23-
}
24-
2513
if (! function_exists('theme_inspire')) {
2614

2715
/**

src/Theme.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Codions\ThemesManager\Events\ThemeEnabled;
88
use Codions\ThemesManager\Events\ThemeEnabling;
99
use Codions\ThemesManager\Facades\ThemesManager;
10+
use Codions\ThemesManager\Traits\Autoloader;
1011
use Codions\ThemesManager\Traits\HasConfigs;
1112
use Codions\ThemesManager\Traits\HasHelpers;
1213
use Codions\ThemesManager\Traits\HasTranslations;
@@ -24,6 +25,7 @@ class Theme
2425
use HasViews;
2526
use HasHelpers;
2627
use HasConfigs;
28+
use Autoloader;
2729

2830
/**
2931
* The theme name.
@@ -196,6 +198,14 @@ public function getVendor(): string
196198
return $this->vendor;
197199
}
198200

201+
public function getNamespace(string $path = null): string
202+
{
203+
$vendor = Str::studly($this->vendor);
204+
$name = Str::studly($this->name);
205+
206+
return "Themes\\$vendor\\$name\\" . $path;
207+
}
208+
199209
/**
200210
* Check if has parent Theme.
201211
*/
@@ -275,14 +285,11 @@ public function enable(bool $withEvent = true): self
275285
}
276286

277287
$this->enabled = true;
288+
$this->registerAutoloader();
278289
$this->loadViews();
279290
$this->loadTranlastions();
280291
$this->loadHelpers();
281292

282-
if (function_exists('theme_load')) {
283-
theme_load();
284-
}
285-
286293
if ($withEvent) {
287294
event(new ThemeEnabled($this->name));
288295
}

src/Traits/Autoloader.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Codions\ThemesManager\Traits;
4+
5+
trait Autoloader
6+
{
7+
public function registerAutoloader()
8+
{
9+
spl_autoload_register(function ($class) {
10+
$class = str_replace($this->getNamespace(), '', $class);
11+
12+
$class = str_replace('\\', DIRECTORY_SEPARATOR, $class);
13+
$file = $this->getPath("src/{$class}.php");
14+
15+
if (file_exists($file)) {
16+
require_once $file;
17+
}
18+
});
19+
}
20+
}

0 commit comments

Comments
 (0)