Skip to content

Commit

Permalink
Merge pull request #2 from codions/develop
Browse files Browse the repository at this point in the history
Register theme autoloader
  • Loading branch information
fabioassuncao authored May 26, 2023
2 parents 53de085 + 4eee34a commit 0e46f69
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 20 deletions.
9 changes: 5 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
vendor/
.idea/
build/
vendor
.idea
.vscode
build
.DS_Store
*.cache
*.cache
12 changes: 0 additions & 12 deletions resources/stubs/template/src/Helpers/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,6 @@
|
*/

if (! function_exists('theme_load')) {

/**
* This function is called on theme loading. Put in all the rules that
* you want to be executedas a settings override or any other action.
*/
function theme_load()
{
// Unleash your imagination here
}
}

if (! function_exists('theme_inspire')) {

/**
Expand Down
15 changes: 11 additions & 4 deletions src/Theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Codions\ThemesManager\Events\ThemeEnabled;
use Codions\ThemesManager\Events\ThemeEnabling;
use Codions\ThemesManager\Facades\ThemesManager;
use Codions\ThemesManager\Traits\Autoloader;
use Codions\ThemesManager\Traits\HasConfigs;
use Codions\ThemesManager\Traits\HasHelpers;
use Codions\ThemesManager\Traits\HasTranslations;
Expand All @@ -24,6 +25,7 @@ class Theme
use HasViews;
use HasHelpers;
use HasConfigs;
use Autoloader;

/**
* The theme name.
Expand Down Expand Up @@ -196,6 +198,14 @@ public function getVendor(): string
return $this->vendor;
}

public function getNamespace(string $path = null): string
{
$vendor = Str::studly($this->vendor);
$name = Str::studly($this->name);

return "Themes\\$vendor\\$name\\" . $path;
}

/**
* Check if has parent Theme.
*/
Expand Down Expand Up @@ -275,14 +285,11 @@ public function enable(bool $withEvent = true): self
}

$this->enabled = true;
$this->registerAutoloader();
$this->loadViews();
$this->loadTranlastions();
$this->loadHelpers();

if (function_exists('theme_load')) {
theme_load();
}

if ($withEvent) {
event(new ThemeEnabled($this->name));
}
Expand Down
20 changes: 20 additions & 0 deletions src/Traits/Autoloader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Codions\ThemesManager\Traits;

trait Autoloader
{
public function registerAutoloader()
{
spl_autoload_register(function ($class) {
$class = str_replace($this->getNamespace(), '', $class);

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

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

0 comments on commit 0e46f69

Please sign in to comment.