Skip to content

Commit

Permalink
Merge pull request #8 from codions/develop
Browse files Browse the repository at this point in the history
Added helper method to obtain an instance of a class present in the current theme
  • Loading branch information
fabioassuncao authored May 31, 2023
2 parents eb833e1 + 6e0586b commit 9ac055e
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

<!-- Scripts -->
<script src="https://cdn.jsdelivr.net/gh/alpinejs/[email protected]/dist/alpine.js" defer></script>
@livewireStyles
</head>

<body class="font-sans antialiased">
Expand All @@ -37,6 +38,8 @@
</div>

@stack('modals')

@livewireScripts
</body>

</html>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,16 @@

<!-- Scripts -->
<script src="https://cdn.jsdelivr.net/gh/alpinejs/[email protected]/dist/alpine.js" defer></script>

@livewireStyles
</head>

<body>
<div class="font-sans text-gray-900 antialiased">
{{ $slot }}
</div>

@livewireScripts
</body>

</html>
</html>
16 changes: 16 additions & 0 deletions src/Theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Codions\ThemesManager\Traits\HasProviders;
use Codions\ThemesManager\Traits\HasTranslations;
use Codions\ThemesManager\Traits\HasViews;
use Exception;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Log;
Expand Down Expand Up @@ -208,6 +209,21 @@ public function getNamespace(string $path = null): string
return "Themes\\$vendor\\$name\\" . $path;
}

public function getInstance(string $path)
{
if (! $this->enabled()) {
$this->requireClass($path);
}

$class = $this->getNamespace($path);

if (! class_exists($class)) {
throw new Exception("Class not found: {$class}");
}

return new $class;
}

/**
* Check if has parent Theme.
*/
Expand Down
19 changes: 12 additions & 7 deletions src/Traits/Autoloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,20 @@ trait Autoloader
public function registerAutoloader()
{
spl_autoload_register(function ($class) {
$class = str_replace($this->getNamespace(), '', $class);
$this->requireClass($class);
});
}

protected function requireClass($class)
{
$class = str_replace($this->getNamespace(), '', $class);

$class = str_replace('\\', DIRECTORY_SEPARATOR, $class);
$file = $this->getPath("src/{$class}.php");
$class = str_replace('\\', DIRECTORY_SEPARATOR, $class);
$file = $this->getPath("src/{$class}.php");

if (file_exists($file)) {
require_once $file;
}
});
if (file_exists($file)) {
require_once $file;
}
}

protected function registerLivewireComponents()
Expand Down

0 comments on commit 9ac055e

Please sign in to comment.