From 7a51c913d3bc5765daa792b367a7600f9e693ac6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Kv=C3=A1=C4=8Dek?= Date: Thu, 31 Mar 2016 16:47:40 +0200 Subject: [PATCH] FEATURE #1524: merge views into one directory --- .gitignore | 1 + .../Registrators/TemplateRegistrator.php | 12 +++-- app/Models/Template.php | 3 ++ app/Providers/AppServiceProvider.php | 47 ++++++++++++++++++- 4 files changed, 56 insertions(+), 7 deletions(-) diff --git a/.gitignore b/.gitignore index cd97728e..9c93cf6e 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ /resources/views/modules /public/modules /resources/views/vendor +/public/assets # directories with custom templates **/views/**/custom/* diff --git a/app/Handlers/Registrators/TemplateRegistrator.php b/app/Handlers/Registrators/TemplateRegistrator.php index 99c27a6e..0a7f5480 100644 --- a/app/Handlers/Registrators/TemplateRegistrator.php +++ b/app/Handlers/Registrators/TemplateRegistrator.php @@ -21,7 +21,6 @@ class TemplateRegistrator protected $namespace; /** * @var string absolute path to currently registered directory - * * This variable is set in register() method as $baseDirectory/$path */ protected $registeredPath; @@ -78,7 +77,6 @@ class TemplateRegistrator /** * Registers new template path. - * * This method registeres given path itself with all subdirectories * * @param string $path path relatively to root of the project @@ -140,7 +138,13 @@ class TemplateRegistrator { // skip custom directories (contains updated templates only) $dirName = basename($path); - if ($dirName == Template::EDITED_TEMPLATES_DIRECTORY) { + $ignoredDirectories = [ + Template::EDITED_TEMPLATES_DIRECTORY, + Template::ASSETS_DIRECTORY, + Template::MODULES_DIRECTORY + ]; + + if (in_array($dirName, $ignoredDirectories)) { return; } @@ -156,7 +160,6 @@ class TemplateRegistrator /** * Registers all templates - * * This method takes all templates in given directory and registers them into database. Templates registered in * this way may be used for further manipulation. * @@ -259,7 +262,6 @@ class TemplateRegistrator /** * Get template name - * * Converts path to template to corresponding name * * @param $template diff --git a/app/Models/Template.php b/app/Models/Template.php index 2d92ed11..81370bbf 100644 --- a/app/Models/Template.php +++ b/app/Models/Template.php @@ -21,6 +21,9 @@ class Template extends Model protected $guarded = []; protected $pathModel = null; const EDITED_TEMPLATES_DIRECTORY = 'custom'; + const ASSETS_DIRECTORY = 'assets'; + const MODULES_DIRECTORY = 'modules'; + const TEMPLATE_SUFFIX = '.blade.php'; const CUSTOM_EDIT_DIRECTORY = 1; const ORIGINAL_DIRECTORY = 2; diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index 05860312..8cddb90f 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -10,21 +10,24 @@ use App\Handlers\Search\Content; use App\Handlers\Search\Files; use App\Handlers\Search\Presenters\AdminContent as AdminContentPresenter; use App\Handlers\Search\Presenters\AdminFiles; +use App\Models\Template; use App\Widgets\HtmlCode\HtmlCode; use Illuminate\Support\Facades\App; +use Illuminate\Support\Facades\File; use Illuminate\Support\ServiceProvider; class AppServiceProvider extends ServiceProvider { /** * Bootstrap any application services. - * * @return void */ public function boot() { // ignore running from artisan if (App::runningInConsole()) { + $this->publishViewResources(); + return; } @@ -36,7 +39,6 @@ class AppServiceProvider extends ServiceProvider /** * Register any application services. - * * @return void */ public function register() @@ -118,4 +120,45 @@ class AppServiceProvider extends ServiceProvider $registrator->register(new Content(), new AdminContentPresenter()); $registrator->register(new Files(), new AdminFiles()); } + + /** + * Publish all public assets from all themes + */ + private function publishViewResources() + { + $themes = File::directories(resource_path('views/visitor')); + + $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); + } + } + + if (!empty($assets)) { + $this->publishes($assets, 'public-assets'); + } + } + + private function publishModuleViews($themeModules, $themeName) + { + $modules = File::directories($themeModules); + + $moduleViews = []; + foreach ($modules as $module) { + $moduleName = strtolower(basename($module)); + + $moduleViews[$module] = resource_path('views/modules/' . $moduleName . '/visitor/' . $themeName); + } + + if (!empty($moduleViews)) { + $this->publishes($moduleViews, 'module-views'); + } + } }