From f27ad7c421f70833314aae006cdffb46f4ca6e37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Kv=C3=A1=C4=8Dek?= Date: Mon, 4 Apr 2016 20:20:33 +0200 Subject: [PATCH] FIX: fix theme:set command. This command did not set theme properly, when THEME was in .env file, but with no value (THEME= only - as default in .env.example). --- app/Console/Commands/SetTheme.php | 25 +++++++++++++---- app/Providers/AppServiceProvider.php | 40 ++++++++++++++-------------- config/app.php | 8 +++--- 3 files changed, 44 insertions(+), 29 deletions(-) diff --git a/app/Console/Commands/SetTheme.php b/app/Console/Commands/SetTheme.php index 943602bf..dd788a79 100644 --- a/app/Console/Commands/SetTheme.php +++ b/app/Console/Commands/SetTheme.php @@ -40,16 +40,31 @@ class SetTheme extends Command throw new NotFoundHttpException('Invalid theme ' . $theme . '!'); } - $env = file_get_contents(base_path('.env')); + // load .env file + $envFile = base_path('.env'); + $env = file_get_contents($envFile); - $env = preg_replace('/THEME=(.+)/i', 'THEME=' . $theme, $env); - - if (!Str::contains($env, 'THEME')) { + if (!Str::contains($env, 'THEME=')) { $env = sprintf("%s\nTHEME=%s\n", $env, $theme); + } else { + $env = preg_replace('/THEME=(.*)/i', 'THEME=' . $theme, $env, -1, $cnt); } - file_put_contents(base_path('.env'), $env); + // try to write content + $bytesWritten = file_put_contents($envFile, $env); + // check if write was successfull + if (!$bytesWritten) { + throw new \Exception('Cannot write to ' . base_path('.env') . '. Add THEME=' . $theme . ' to your .env file manualy.'); + } + + // theme written $this->line('Theme successfully setup.'); + $this->line(''); + $this->line(' ** PUBLISHING ASSETS AND VIEWS **'); + // publish assets and views for module and theme + $this->call('vendor:publish', ['--tag' => ['public-assets'], '--force' => true]); + $this->call('vendor:publish', ['--tag' => ['module-views'], '--force' => true]); + $this->call('vendor:publish'); } } diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index 3ee85967..1f1933e7 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -13,12 +13,12 @@ use App\Handlers\Search\Presenters\AdminContent as AdminContentPresenter; use App\Handlers\Search\Presenters\AdminFiles; use App\Models\Template; use App\Widgets\HtmlCode\HtmlCode; -use App\Widgets\Menu\Extensions\URL; use App\Widgets\Menu\Menu; use App\Widgets\OrbitSlider\OrbitSlider; use Illuminate\Support\Facades\App; use Illuminate\Support\Facades\File; use Illuminate\Support\ServiceProvider; +use Symfony\Component\Process\Exception\InvalidArgumentException; class AppServiceProvider extends ServiceProvider { @@ -39,6 +39,13 @@ class AppServiceProvider extends ServiceProvider $this->registerTemplates(); $this->registerSettings(); $this->registerSearch(); + + // check if given template is valid + $theme = config('app.theme'); + $themeDir = resource_path('views/visitor/' . $theme); + if (!$theme || empty($theme) || !is_dir($themeDir)) { + throw new InvalidArgumentException('No or invalid theme set! Use artisan theme:set or edit .env file manually and include THEME option, please'); + } } /** @@ -95,8 +102,8 @@ class AppServiceProvider extends ServiceProvider $registrator->register(OrbitSlider::class); // register some menu extensions -// $menuExtensions = $this->app->make('registerMenuExtension'); -// $menuExtensions->register(new URL()); + // $menuExtensions = $this->app->make('registerMenuExtension'); + // $menuExtensions->register(new URL()); } /** @@ -140,35 +147,28 @@ class AppServiceProvider extends ServiceProvider */ private function publishViewResources() { - $themes = File::directories(resource_path('views/visitor')); + $theme = config('app.theme'); + $themeDir = resource_path('views/visitor/' . $theme); - $assets = []; - foreach ($themes as $theme) { - $themeName = basename($theme); - - if (is_dir($theme . '/'.Template::ASSETS_DIRECTORY)) { - $assets[$theme . '/'.Template::ASSETS_DIRECTORY] = public_path('assets/' . $themeName); - } - - if (is_dir($theme . '/'.Template::MODULES_DIRECTORY)) { - $this->publishModuleViews($theme . '/'.Template::MODULES_DIRECTORY, $themeName); - } + // publish assets if exists for current theme + if (is_dir($themeDir . '/' . Template::ASSETS_DIRECTORY)) { + $this->publishes([$themeDir . '/' . Template::ASSETS_DIRECTORY => public_path('assets/' . $theme)], 'public-assets'); } - if (!empty($assets)) { - $this->publishes($assets, 'public-assets'); + if (is_dir($themeDir . '/' . Template::MODULES_DIRECTORY)) { +// $this->publishModuleViews($themeDir . '/' . Template::MODULES_DIRECTORY); } } - private function publishModuleViews($themeModules, $themeName) + private function publishModuleViews($themeModules) { + $theme = config('app.theme'); $modules = File::directories($themeModules); $moduleViews = []; foreach ($modules as $module) { $moduleName = strtolower(basename($module)); - - $moduleViews[$module] = resource_path('views/modules/' . $moduleName . '/visitor/' . $themeName); + $moduleViews[$module] = resource_path('views/modules/' . $moduleName . '/visitor/' . $theme); } if (!empty($moduleViews)) { diff --git a/config/app.php b/config/app.php index 57525eed..7fdee8dd 100644 --- a/config/app.php +++ b/config/app.php @@ -37,7 +37,7 @@ return [ | */ - 'url' => 'http://localhost', + 'url' => env('SITE_URL', 'http://localhost'), /* |-------------------------------------------------------------------------- | Application Timezone @@ -49,7 +49,7 @@ return [ | */ - 'timezone' => 'UTC', + 'timezone' => env('TIMEZONE', 'UTC'), /* |-------------------------------------------------------------------------- | Application Locale Configuration @@ -61,7 +61,7 @@ return [ | */ - 'locale' => 'en', + 'locale' => env('LOCALES', 'en'), /* |-------------------------------------------------------------------------- | Application Fallback Locale @@ -97,7 +97,7 @@ return [ */ 'upload_url' => env('UPLOAD_URL', PHP_SAPI === 'cli' ? '' : url('uploads')), 'thumbnail_url' => env('THUMBNAIL_URL', PHP_SAPI === 'cli' ? '' : url('uploads/thumbnails')), - 'theme' => env('THEME', 'default_theme'), + 'theme' => env('THEME'), /* |-------------------------------------------------------------------------- | Logging Configuration