diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..adbc151 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,15 @@ +# This file is for unifying the coding style for different editors and IDEs +# editorconfig.org + +# PHP PSR-2 Coding Standards +# http://www.php-fig.org/psr/psr-2/ + +root = true + +[*.php] +charset = utf-8 +end_of_line = lf +insert_final_newline = true +trim_trailing_whitespace = true +indent_style = space +indent_size = 4 \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index 49ade48..d49bcc0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,9 @@ All notable changes to this package will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this package adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [2.0.0] - 2021-01-31 +- Breaking change: Switched `themes` directory to `Themes` + ## [1.2.0] - 2021-01-03 - PHP 8 support with drop PHP 7.2 - Laravel 8 support with drop Laravel 6 diff --git a/composer.json b/composer.json index 2570d2d..e74b63d 100644 --- a/composer.json +++ b/composer.json @@ -1,43 +1,43 @@ { - "name": "afbora/larathemes", - "description": "Theme package for Laravel 7+|8+", - "keywords": [ - "themes", - "laravel", - "blade", - "theme", - "themes-package" - ], - "version": "1.2.0", - "license": "MIT", - "authors": [ - { - "name": "Ahmet Bora", - "email": "byybora@gmail.com" - } + "name": "afbora/larathemes", + "description": "Theme package for Laravel 7+|8+", + "keywords": [ + "themes", + "laravel", + "blade", + "theme", + "themes-package" + ], + "version": "2.0.0", + "license": "MIT", + "authors": [ + { + "name": "Ahmet Bora", + "email": "byybora@gmail.com" + } + ], + "minimum-stability": "dev", + "prefer-stable": true, + "require": { + "php": "^7.3|^8.0", + "illuminate/support": "^7.0|^8.0" + }, + "autoload": { + "files": [ + "helpers/themes.php" ], - "minimum-stability": "dev", - "prefer-stable": true, - "require": { - "php": "^7.3|^8.0", - "illuminate/support": "^7.0|^8.0" - }, - "autoload": { - "files": [ - "helpers/themes.php" - ], - "psr-4": { - "Afbora\\LaraThemes\\": "src/" - } - }, - "extra": { - "laravel": { - "providers": [ - "Afbora\\LaraThemes\\ThemesServiceProvider" - ], - "aliases": { - "Theme": "Afbora\\LaraThemes\\Facades\\Theme" - } - } + "psr-4": { + "Afbora\\LaraThemes\\": "src/" + } + }, + "extra": { + "laravel": { + "providers": [ + "Afbora\\LaraThemes\\ThemesServiceProvider" + ], + "aliases": { + "Theme": "Afbora\\LaraThemes\\Facades\\Theme" + } } + } } diff --git a/config/themes.php b/config/themes.php index 5671396..ec052b3 100644 --- a/config/themes.php +++ b/config/themes.php @@ -38,7 +38,7 @@ | may register your themes as a composer package as well. | */ - + 'vendor' => 'vendor', ]; diff --git a/helpers/themes.php b/helpers/themes.php index abc9b59..6fe3d5f 100644 --- a/helpers/themes.php +++ b/helpers/themes.php @@ -4,11 +4,11 @@ /** * Return the path to the given theme file. * - * @param string $file - * @param string $theme - * @return string + * @param string|null $file + * @param string|null $theme + * @return string|null */ - function theme_path($file = '', $theme = null) + function theme_path(string $file = null, string $theme = null): ?string { return Theme::path($file, $theme); } @@ -19,13 +19,18 @@ function theme_path($file = '', $theme = null) * Return the asset url to the given theme file. * * @param string $file - * @param string $theme - * @return string + * @param string|null $theme + * @return string|null * @author Ahmet Bora */ - function theme_asset($file = '', $theme = null) + function theme_asset(string $file, string $theme = null): ?string { $theme = $theme ?? Theme::getCurrent(); - return asset('themes/' . $theme . '/' . $file); + + if (empty($theme) === false) { + return asset('Themes/' . $theme . '/' . $file); + } + + return null; } } diff --git a/resources/stubs/theme/src/Providers/RouteServiceProvider.php b/resources/stubs/theme/src/Providers/RouteServiceProvider.php index 3987a45..e68d077 100644 --- a/resources/stubs/theme/src/Providers/RouteServiceProvider.php +++ b/resources/stubs/theme/src/Providers/RouteServiceProvider.php @@ -23,8 +23,6 @@ class RouteServiceProvider extends ServiceProvider */ public function boot() { - // - parent::boot(); } @@ -36,10 +34,7 @@ public function boot() public function map() { $this->mapApiRoutes(); - $this->mapWebRoutes(); - - // } /** diff --git a/src/Console/GenerateTheme.php b/src/Console/GenerateTheme.php index 2206fc4..5fe80b8 100644 --- a/src/Console/GenerateTheme.php +++ b/src/Console/GenerateTheme.php @@ -40,7 +40,7 @@ public function __construct() public function handle() { $options = $this->getOptions(); - $root = base_path('themes'); + $root = base_path('Themes'); $stubsPath = __DIR__ . '/../../resources/stubs/theme'; $slug = $options['slug']; $name = $this->format($slug); @@ -132,7 +132,7 @@ protected function replacePlaceholders($contents, $options) * @param string $name * @return string */ - private function format($name) + private function format(string $name): string { return ucfirst(Str::camel($name)); } diff --git a/src/Theme.php b/src/Theme.php index 69cdd0c..140cee2 100644 --- a/src/Theme.php +++ b/src/Theme.php @@ -11,21 +11,22 @@ class Theme extends Collection use RegistersViewLocations; /** - * @var string + * @var string|null */ protected $current; /** * @var string|null */ - protected $layout = null; + protected $layout; /** * Register and set the currently active theme. * * @param string $theme + * @return self */ - public function set($theme) + public function set(string $theme): self { list($theme, $parent) = $this->resolveTheme($theme); @@ -39,32 +40,38 @@ public function set($theme) $this->addRegisteredLocation($theme, $parent); $this->symlinkPublicDirectory(); $this->registerServiceProvider($this->format($theme->get('slug'))); + + return $this; } /** * Get the path of the given theme file. * - * @param string $file - * @param string $theme - * @return string + * @param string|null $file + * @param string|null $theme + * @return string|null */ - public function path($file = '', $theme = null) + public function path(string $file = null, string $theme = null): ?string { - if (is_null($theme)) { + if (empty($theme) === true) { $theme = $this->getCurrent(); } - $theme = $this->format($theme); + if (empty($theme) === false) { + $theme = $this->format($theme); + + return base_path('Themes/' . $theme . (empty($file) === false ? '/' . $file : null)); + } - return base_path("themes/{$theme}/{$file}"); + return null; } /** * Get the layout property. * - * @return string + * @return string|null */ - public function getLayout() + public function getLayout(): ?string { return $this->layout; } @@ -73,10 +80,13 @@ public function getLayout() * Set the layout property. * * @param string $layout + * @return self */ - public function setLayout($layout) + public function setLayout(string $layout): self { $this->layout = $layout; + + return $this; } /** @@ -84,9 +94,11 @@ public function setLayout($layout) * * @param string $theme */ - public function setCurrent($theme) + public function setCurrent(string $theme): self { $this->current = $theme; + + return $this; } /** @@ -94,7 +106,7 @@ public function setCurrent($theme) * * @return string */ - public function getCurrent() + public function getCurrent(): ?string { return $this->current; } @@ -105,7 +117,7 @@ public function getCurrent() * @param string $theme * @return bool */ - public function isCurrently($theme) + public function isCurrently(string $theme): bool { return $this->current === $theme; } @@ -116,26 +128,28 @@ public function isCurrently($theme) * @param string $name * @return string */ - protected function format($name) + protected function format(string $name): string { return ucfirst(Str::camel($name)); } /** - * Symlink the themes public directory so its accesible + * Symlink the themes public directory so its accessible * by the web. * * @return void */ - protected function symlinkPublicDirectory() + protected function symlinkPublicDirectory(): void { - if (!file_exists(public_path('themes/' . $this->getCurrent()))) { - if (!file_exists(public_path('themes'))) { - app()->make('files')->makeDirectory(public_path('themes')); + $theme = $this->format($this->getCurrent()); + + if (!file_exists(public_path('Themes/' . $theme))) { + if (!file_exists(public_path('Themes'))) { + app()->make('files')->makeDirectory(public_path('Themes')); } app()->make('files')->link( - $this->path('public'), public_path('themes/' . $this->getCurrent()) + $this->path('public'), public_path('Themes/' . $theme) ); } } @@ -146,7 +160,7 @@ protected function symlinkPublicDirectory() * @param string $theme * @return void */ - protected function registerServiceProvider($theme) + protected function registerServiceProvider(string $theme): void { app()->register("Themes\\$theme\\Providers\\ThemeServiceProvider"); } @@ -157,7 +171,7 @@ protected function registerServiceProvider($theme) * @param string $theme * @return void */ - protected function registerAutoload($theme) + protected function registerAutoload(string $theme): void { $base = 'Themes\\' . $theme . '\\'; $path = $this->path('src/'); @@ -165,7 +179,7 @@ protected function registerAutoload($theme) spl_autoload_register(function ($class) use ($base, $path) { $file = str_replace($base, '', $class); $file = str_replace('\\', '/', $file); - $file .= '.php'; + $file = $file . '.php'; if (file_exists($path . $file)) { include($path . $file); diff --git a/src/ThemesServiceProvider.php b/src/ThemesServiceProvider.php index 58cd0cd..bf48b7b 100644 --- a/src/ThemesServiceProvider.php +++ b/src/ThemesServiceProvider.php @@ -64,10 +64,10 @@ public function provides() protected function registerServices() { $this->app->singleton('afbora.larathemes', function ($app) { - $themes = []; $items = []; + $themes = []; - if ($path = base_path('themes')) { + if ($path = base_path('Themes')) { if (file_exists($path) && is_dir($path)) { $themes = $this->app['files']->directories($path); }