From e7e358ea7fc5bb26514cb2e40d237941139a3ef5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20Gu=CC=88nther?= Date: Mon, 27 Jun 2022 22:17:04 +0200 Subject: [PATCH] Code style fixes --- index.php | 90 ++++++++++++------------ lib/collection.php | 35 +++++----- lib/fields/redirect.php | 24 +++---- lib/functions.php | 143 +++++++++++++++++++-------------------- lib/models.php | 74 ++++++++++---------- lib/routes.php | 74 ++++++++++---------- lib/sections/modules.php | 62 ++++++++--------- lib/translations.php | 16 ++--- 8 files changed, 253 insertions(+), 265 deletions(-) diff --git a/index.php b/index.php index 69688f8..0793a76 100755 --- a/index.php +++ b/index.php @@ -9,49 +9,49 @@ $moduleRegistry = createModuleRegistry(); Kirby::plugin('medienbaecker/modules', [ - 'templates' => $moduleRegistry['templates'], - 'pageModels' => $moduleRegistry['pageModels'], - 'blueprints' => $moduleRegistry['blueprints'], - 'sections' => [ - 'modules' => include __DIR__ . '/lib/sections/modules.php' - ], - 'fields' => [ - 'modules_redirect' => include __DIR__ . '/lib/fields/redirect.php' - ], - 'pageMethods' => [ - 'renderModules' => function () { - foreach ($this->modules() as $module) { - $moduleTemplate = new Template($module->intendedTemplate()); - echo $moduleTemplate->render([ - 'page' => $this, - 'module' => $module, - 'site' => $this->site() - ]); - } - }, - 'hasModules' => function () { - $modules = array_filter($this->blueprint()->sections(), function ($section) { - return 'modules' === $section->type(); - }); - return count($modules) > 0; - }, - 'modules' => function () { - $modules = new ModulesCollection; - if ($rawModules = $this->find('modules')) { - foreach($rawModules->childrenAndDrafts() as $module) { - if (!$module->isListed() && !$module->isDraft()) continue; - if ($module->isDraft && !$module->isVerified(get('token'))) continue; - $modules->append($module); - } - } - return $modules; - }, - 'isModule' => function () { - return Str::startsWith($this->intendedTemplate(), 'module.'); - } - ], - 'api' => [ - 'routes' => include __DIR__ . '/lib/routes.php', - ], - 'translations' => include __DIR__ . '/lib/translations.php', + 'templates' => $moduleRegistry['templates'], + 'pageModels' => $moduleRegistry['pageModels'], + 'blueprints' => $moduleRegistry['blueprints'], + 'sections' => [ + 'modules' => include __DIR__ . '/lib/sections/modules.php' + ], + 'fields' => [ + 'modules_redirect' => include __DIR__ . '/lib/fields/redirect.php' + ], + 'pageMethods' => [ + 'renderModules' => function () { + foreach ($this->modules() as $module) { + $moduleTemplate = new Template($module->intendedTemplate()); + echo $moduleTemplate->render([ + 'page' => $this, + 'module' => $module, + 'site' => $this->site() + ]); + } + }, + 'hasModules' => function () { + $modules = array_filter($this->blueprint()->sections(), function ($section) { + return 'modules' === $section->type(); + }); + return count($modules) > 0; + }, + 'modules' => function () { + $modules = new ModulesCollection; + if ($rawModules = $this->find('modules')) { + foreach ($rawModules->childrenAndDrafts() as $module) { + if (!$module->isListed() && !$module->isDraft()) continue; + if ($module->isDraft && !$module->isVerified(get('token'))) continue; + $modules->append($module); + } + } + return $modules; + }, + 'isModule' => function () { + return Str::startsWith($this->intendedTemplate(), 'module.'); + } + ], + 'api' => [ + 'routes' => include __DIR__ . '/lib/routes.php', + ], + 'translations' => include __DIR__ . '/lib/translations.php', ]); diff --git a/lib/collection.php b/lib/collection.php index a4b1c85..7a58c73 100755 --- a/lib/collection.php +++ b/lib/collection.php @@ -4,24 +4,23 @@ use Kirby\Cms\Template; class ModulesCollection extends Pages { - /** - * Converts the object to a string - * - * @return string - */ - public function toString(): string - { - $html = ''; + /** + * Converts the object to a string + * + * @return string + */ + public function toString(): string { + $html = ''; - foreach ($this->data() as $module) { - $moduleTemplate = new Template($module->intendedTemplate()); - $html .= $moduleTemplate->render([ - 'page' => $module->parent(), - 'module' => $module, - 'site' => site(), - ]); - } + foreach ($this->data() as $module) { + $moduleTemplate = new Template($module->intendedTemplate()); + $html .= $moduleTemplate->render([ + 'page' => $module->parent(), + 'module' => $module, + 'site' => site(), + ]); + } - return $html; - } + return $html; + } } diff --git a/lib/fields/redirect.php b/lib/fields/redirect.php index 4a85b34..93d6bf3 100644 --- a/lib/fields/redirect.php +++ b/lib/fields/redirect.php @@ -1,19 +1,13 @@ [ - 'redirect' => function () { - if ($this->model()->isHomePage()) { - return $this->model() - ->site() - ->panel() - ->url(); - } else { - return $this->model() - ->parent() - ->panel() - ->url(); - } - } - ] + 'computed' => [ + 'redirect' => function () { + if ($this->model()->isHomePage()) { + return $this->model()->site()->panel()->url(); + } else { + return $this->model()->parent()->panel()->url(); + } + } + ] ]; diff --git a/lib/functions.php b/lib/functions.php index 92628f2..09b7e71 100644 --- a/lib/functions.php +++ b/lib/functions.php @@ -1,77 +1,76 @@ [], 'templates' => [], 'pageModels' => []]; - - // Add modules in site/modules - $modulesFolder = kirby()->root('site') . "/modules"; - foreach (Dir::dirs($modulesFolder) as $folder) { - $blueprint = $modulesFolder . "/". $folder . "/" . $folder . ".yml"; - $template = $modulesFolder . "/". $folder . "/" . $folder . ".php"; - if(F::exists($blueprint)) { - $blueprintArray = Yaml::read($blueprint); - if(!array_key_exists('status', $blueprintArray)) { - $blueprintArray['status'] = [ - 'draft' => true, - 'listed' => true, - ]; - } - $registry['blueprints']['pages/module.'. $folder] = $blueprintArray; - $registry['templates']['module.'. $folder] = $template; - $registry['pageModels']['module.'. $folder] = option('medienbaecker.modules.model', 'ModulePage'); - } - } - - // Add legacy modules to registry - $moduleBlueprints = array_filter(kirby()->blueprints(), function($blueprint) { - return Str::startsWith($blueprint, 'module.'); - }); - if(!empty($moduleBlueprints)) { - $blueprintsFolder = kirby()->root('blueprints'); - $snippetsFolder = kirby()->root('snippets'); - foreach($moduleBlueprints as $moduleBlueprint) { - $blueprint = $blueprintsFolder . "/pages/" . $moduleBlueprint . ".yml"; - $template = $snippetsFolder . "/modules/" . $moduleBlueprint . ".php"; - if(F::exists($blueprint)) { - $blueprintArray = Yaml::read($blueprint); - if(!array_key_exists('status', $blueprintArray)) { - $blueprintArray['status'] = [ - 'draft' => true, - 'listed' => true, - ]; - } - if(!array_key_exists('pages/'. $moduleBlueprint, $registry['blueprints'])) { - $registry['blueprints']['pages/'. $moduleBlueprint] = $blueprintArray; - } - if(!array_key_exists($moduleBlueprint, $registry['templates'])) { - $registry['templates'][$moduleBlueprint] = $template; - } - if(!array_key_exists($moduleBlueprint, $registry['pageModels'])) { - $registry['pageModels'][$moduleBlueprint] = option('medienbaecker.modules.model', 'ModulePage'); - } - } - } - } - - // Add modules container blueprint with redirect field - $registry['blueprints']['pages/modules'] = [ - 'title' => 'Modules', - 'options' => [ - 'changeSlug' => false, - 'changeStatus' => false, - 'changeTemplate' => false - ], - 'fields' => [ - 'modules_redirect' => true - ] - ]; - - // Add modules container model - $registry['pageModels']['modules'] = 'ModulesPage'; - - - return $registry; + // Set up registry array + $registry = ['blueprints' => [], 'templates' => [], 'pageModels' => []]; + + // Add modules in site/modules + $modulesFolder = kirby()->root('site') . "/modules"; + foreach (Dir::dirs($modulesFolder) as $folder) { + $blueprint = $modulesFolder . "/". $folder . "/" . $folder . ".yml"; + $template = $modulesFolder . "/". $folder . "/" . $folder . ".php"; + if(F::exists($blueprint)) { + $blueprintArray = Yaml::read($blueprint); + if(!array_key_exists('status', $blueprintArray)) { + $blueprintArray['status'] = [ + 'draft' => true, + 'listed' => true, + ]; + } + $registry['blueprints']['pages/module.'. $folder] = $blueprintArray; + $registry['templates']['module.'. $folder] = $template; + $registry['pageModels']['module.'. $folder] = option('medienbaecker.modules.model', 'ModulePage'); + } + } + + // Add legacy modules to registry + $moduleBlueprints = array_filter(kirby()->blueprints(), function($blueprint) { + return Str::startsWith($blueprint, 'module.'); + }); + if(!empty($moduleBlueprints)) { + $blueprintsFolder = kirby()->root('blueprints'); + $snippetsFolder = kirby()->root('snippets'); + foreach($moduleBlueprints as $moduleBlueprint) { + $blueprint = $blueprintsFolder . "/pages/" . $moduleBlueprint . ".yml"; + $template = $snippetsFolder . "/modules/" . $moduleBlueprint . ".php"; + if(F::exists($blueprint)) { + $blueprintArray = Yaml::read($blueprint); + if(!array_key_exists('status', $blueprintArray)) { + $blueprintArray['status'] = [ + 'draft' => true, + 'listed' => true, + ]; + } + if(!array_key_exists('pages/'. $moduleBlueprint, $registry['blueprints'])) { + $registry['blueprints']['pages/'. $moduleBlueprint] = $blueprintArray; + } + if(!array_key_exists($moduleBlueprint, $registry['templates'])) { + $registry['templates'][$moduleBlueprint] = $template; + } + if(!array_key_exists($moduleBlueprint, $registry['pageModels'])) { + $registry['pageModels'][$moduleBlueprint] = option('medienbaecker.modules.model', 'ModulePage'); + } + } + } + } + + // Add modules container blueprint with redirect field + $registry['blueprints']['pages/modules'] = [ + 'title' => 'Modules', + 'options' => [ + 'changeSlug' => false, + 'changeStatus' => false, + 'changeTemplate' => false + ], + 'fields' => [ + 'modules_redirect' => true + ] + ]; + + // Add modules container model + $registry['pageModels']['modules'] = 'ModulesPage'; + + + return $registry; } \ No newline at end of file diff --git a/lib/models.php b/lib/models.php index 0a74d08..9dd54fb 100644 --- a/lib/models.php +++ b/lib/models.php @@ -3,47 +3,43 @@ use Kirby\Cms\Template; class ModulePage extends Page { - public static function create(array $props) { - if (option('medienbaecker.modules.autopublish', false)) { - $props['num'] = 9999; - } - return parent::create($props); - } - public function url($options = null): string { - return $this->parents()->filterBy('intendedTemplate', '!=', 'modules')->first()->url() . '#' . $this->slug(); - } - public function render(array $data = [], $contentType = 'html'): string { - go($this->parents()->filterBy('intendedTemplate', '!=', 'modules')->first()->url() . '#' . $this->slug()); - } - public function renderModule() { - $moduleTemplate = new Template($this->intendedTemplate()); - echo $moduleTemplate->render([ - 'page' => $this->parent()->parent(), - 'module' => $this, - 'site' => $this->site() - ]); - } - public function moduleName() { - return $this->blueprint()->title(); - } - public function moduleId() { - return str_replace('.', '__', $this->intendedTemplate()); - } - public function parents(){ - $parents = parent::parents(); - return $parents->filter('slug', '!=', 'modules'); + public static function create(array $props) { + if (option('medienbaecker.modules.autopublish', false)) { + $props['num'] = 9999; } + return parent::create($props); + } + public function url($options = null): string { + return $this->parents()->filterBy('intendedTemplate', '!=', 'modules')->first()->url() . '#' . $this->slug(); + } + public function render(array $data = [], $contentType = 'html'): string { + go($this->parents()->filterBy('intendedTemplate', '!=', 'modules')->first()->url() . '#' . $this->slug()); + } + public function renderModule() { + $moduleTemplate = new Template($this->intendedTemplate()); + echo $moduleTemplate->render([ + 'page' => $this->parent()->parent(), + 'module' => $this, + 'site' => $this->site() + ]); + } + public function moduleName() { + return $this->blueprint()->title(); + } + public function moduleId() { + return str_replace('.', '__', $this->intendedTemplate()); + } + public function parents(){ + $parents = parent::parents(); + return $parents->filter('slug', '!=', 'modules'); + } } class ModulesPage extends Page { - public function url($options = null): string { - return $this->parent()->url(); - } - public function render(array $data = [], $contentType = 'html'): string { - go($this->parent()->url()); - } - public function parents(){ - $parents = parent::parents(); - return $parents->filter('slug', '!=', 'modules'); - } + public function url($options = null): string { + return $this->parent()->url(); + } + public function render(array $data = [], $contentType = 'html'): string { + go($this->parent()->url()); + } } diff --git a/lib/routes.php b/lib/routes.php index 2562395..263cc56 100644 --- a/lib/routes.php +++ b/lib/routes.php @@ -1,44 +1,44 @@ 'pages/(:any)/modules', - 'method' => 'POST', - 'action' => function (string $id) use ($kirby) { - $page = $this->page($id); + return [ + [ + 'pattern' => 'pages/(:any)/modules', + 'method' => 'POST', + 'action' => function (string $id) use ($kirby) { + $page = $this->page($id); - if (!$page) { - return false; - } + if (!$page) { + return false; + } - if (!$page->hasModules() || $page->find('modules')) { - return [ - 'status' => 'ok', - 'created' => false, - ]; - } - - try { - $kirby->impersonate('kirby'); - $modulesPage = $page->createChild([ - 'content' => ['title' => 'Modules'], - 'slug' => 'modules', - 'template' => 'modules', - ]); + if (!$page->hasModules() || $page->find('modules')) { + return [ + 'status' => 'ok', + 'created' => false, + ]; + } + + try { + $kirby->impersonate('kirby'); + $modulesPage = $page->createChild([ + 'content' => ['title' => 'Modules'], + 'slug' => 'modules', + 'template' => 'modules', + ]); - if ($modulesPage) { - $modulesPage->publish(); - } - - return [ - 'status' => 'ok', - 'created' => true, - ]; - } catch (Exception $error) { - throw new Exception($error); - } - } - ], - ]; + if ($modulesPage) { + $modulesPage->publish(); + } + + return [ + 'status' => 'ok', + 'created' => true, + ]; + } catch (Exception $error) { + throw new Exception($error); + } + } + ], + ]; }; \ No newline at end of file diff --git a/lib/sections/modules.php b/lib/sections/modules.php index b98e985..3d16632 100644 --- a/lib/sections/modules.php +++ b/lib/sections/modules.php @@ -1,49 +1,49 @@ $file) { - if (Str::startsWith($blueprint, 'pages/module.')) { - $blueprints[] = str_replace('pages/', '', $blueprint); - } + if (Str::startsWith($blueprint, 'pages/module.')) { + $blueprints[] = str_replace('pages/', '', $blueprint); + } } $default = array_search('module.' . option('medienbaecker.modules.default', 'text'), $blueprints); if ($default !== false) { - $module_text = $blueprints[$default]; - unset($blueprints[$default]); - array_unshift($blueprints, $module_text); + $module_text = $blueprints[$default]; + unset($blueprints[$default]); + array_unshift($blueprints, $module_text); } $base = Section::$types['pages']; if (is_string($base)) { - $base = include $base; + $base = include $base; } return array_replace_recursive($base, [ - 'props' => [ - 'create' => function ($create = null) use ($blueprints) { - return $create ?? $blueprints; - }, - 'empty' => function ($empty = null) { - return $empty ?? I18n::translate('modules.empty'); - }, - 'headline' => function ($headline = null) { - return $headline ?? I18n::translate('modules'); - }, - 'info' => function (string $info = '{{ page.moduleName }}') { - return $info; - }, - 'image' => false, - 'parent' => function ($parent = null) { - return $this->model()->find('modules') - ? 'page.find("modules")' - : $parent; - }, - 'layout' => function (string $layout = 'list') { - $layouts = ['list', 'cardlets', 'cards', 'table', 'module']; - return in_array($layout, $layouts) ? $layout : 'list'; - } - ] + 'props' => [ + 'create' => function ($create = null) use ($blueprints) { + return $create ?? $blueprints; + }, + 'empty' => function ($empty = null) { + return $empty ?? I18n::translate('modules.empty'); + }, + 'headline' => function ($headline = null) { + return $headline ?? I18n::translate('modules'); + }, + 'info' => function (string $info = '{{ page.moduleName }}') { + return $info; + }, + 'image' => false, + 'parent' => function ($parent = null) { + return $this->model()->find('modules') ? 'page.find("modules")' : $parent; + }, + 'layout' => function (string $layout = 'list') { + $layouts = ['list', 'cardlets', 'cards', 'table', 'module']; + return in_array($layout, $layouts) ? $layout : 'list'; + } + ] ]); diff --git a/lib/translations.php b/lib/translations.php index c935850..dfa7fbe 100644 --- a/lib/translations.php +++ b/lib/translations.php @@ -1,12 +1,12 @@ [ - 'modules' => 'Modules', - 'modules.empty' => 'No modules yet', - ], - 'de' => [ - 'modules' => 'Module', - 'modules.empty' => 'Keine Module', - ], + 'en' => [ + 'modules' => 'Modules', + 'modules.empty' => 'No modules yet', + ], + 'de' => [ + 'modules' => 'Module', + 'modules.empty' => 'Keine Module', + ], ]; \ No newline at end of file