laravel-theme icon indicating copy to clipboard operation
laravel-theme copied to clipboard

Adding ThemeRepository

Open duckzland opened this issue 3 years ago • 0 comments

Adding ThemeRepository capability to allow other developer to create theme as a separated Laravel package and then register them via ThemeRepository.

At the custom theme ServiceProvider, one could register it to the LaravelTheme repository:

namespace MyPrefix\MyTheme;

use Illuminate\Support\ServiceProvider;


/**
 * Service class for registering the theme into the main Theme repository
 */
class ThemeServiceProvider extends ServiceProvider
{

    /**
     * Bootstrap the application services.
     */
    public function boot()
    {

        // Registering theme to theme repository
        // Inject the content of theme.json here directly to save file access time
        $relative_path = str_replace(base_path(), '', __DIR__);
        optional(app('themes'))
            ->register('my_slug', [
                'path' => rtrim(ltrim($relative_path, '/')) . '/theme',
                'slug' => 'my_slug',
                'name' => 'My Theme',
                'version' => '1.0',
                'description' => 'My Awesome Theme',
                'mode' => 'public'
            ]);


        // For Theme developer, publish the dist folder to laravel main public folder
        // Invoke this with command: php artisan vendor:publish --tag=theme_assets --force
        $this->publishes([
            __DIR__ . '/../dist' => public_path('theme/my_theme')
        ], 'theme_assets');
    }
}

Additional helper to get the right themepath

    /**
     * Generate path to theme
     *
     * @param string $theme
     * @param string $path
     * @return string
     */
    function theme_path(string $theme = '', string $path = ''): string
    {
        return app('themes')->path($theme, $path);
    }

It will return the correct vendor/path or the resources/path based on what path did the theme registered to.

duckzland avatar Jul 23 '21 03:07 duckzland