Skip to content

Commit 6e0586b

Browse files
committed
Added helper method to obtain an instance of a class present in the current theme
1 parent e6ffeb5 commit 6e0586b

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

src/Theme.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Codions\ThemesManager\Traits\HasProviders;
1414
use Codions\ThemesManager\Traits\HasTranslations;
1515
use Codions\ThemesManager\Traits\HasViews;
16+
use Exception;
1617
use Illuminate\Filesystem\Filesystem;
1718
use Illuminate\Support\Facades\Config;
1819
use Illuminate\Support\Facades\Log;
@@ -208,6 +209,21 @@ public function getNamespace(string $path = null): string
208209
return "Themes\\$vendor\\$name\\" . $path;
209210
}
210211

212+
public function getInstance(string $path)
213+
{
214+
if (! $this->enabled()) {
215+
$this->requireClass($path);
216+
}
217+
218+
$class = $this->getNamespace($path);
219+
220+
if (! class_exists($class)) {
221+
throw new Exception("Class not found: {$class}");
222+
}
223+
224+
return new $class;
225+
}
226+
211227
/**
212228
* Check if has parent Theme.
213229
*/

src/Traits/Autoloader.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,20 @@ trait Autoloader
1313
public function registerAutoloader()
1414
{
1515
spl_autoload_register(function ($class) {
16-
$class = str_replace($this->getNamespace(), '', $class);
16+
$this->requireClass($class);
17+
});
18+
}
19+
20+
protected function requireClass($class)
21+
{
22+
$class = str_replace($this->getNamespace(), '', $class);
1723

18-
$class = str_replace('\\', DIRECTORY_SEPARATOR, $class);
19-
$file = $this->getPath("src/{$class}.php");
24+
$class = str_replace('\\', DIRECTORY_SEPARATOR, $class);
25+
$file = $this->getPath("src/{$class}.php");
2026

21-
if (file_exists($file)) {
22-
require_once $file;
23-
}
24-
});
27+
if (file_exists($file)) {
28+
require_once $file;
29+
}
2530
}
2631

2732
protected function registerLivewireComponents()

0 commit comments

Comments
 (0)