From a167b6d21af091c55bf85045f3b82f9bc1b51b7c Mon Sep 17 00:00:00 2001 From: Yonas Habteab Date: Wed, 13 Sep 2023 10:37:14 +0200 Subject: [PATCH] Rename migration list item classes --- .../Widget/ItemList/MigrationFileListItem.php | 92 +++++++++++ .../Web/Widget/ItemList/MigrationList.php | 8 +- .../Web/Widget/ItemList/MigrationListItem.php | 129 +++++++++++---- .../ItemList/MigrationListItemMinimal.php | 151 ------------------ 4 files changed, 190 insertions(+), 190 deletions(-) create mode 100644 library/Icinga/Web/Widget/ItemList/MigrationFileListItem.php delete mode 100644 library/Icinga/Web/Widget/ItemList/MigrationListItemMinimal.php diff --git a/library/Icinga/Web/Widget/ItemList/MigrationFileListItem.php b/library/Icinga/Web/Widget/ItemList/MigrationFileListItem.php new file mode 100644 index 000000000..9ce63a881 --- /dev/null +++ b/library/Icinga/Web/Widget/ItemList/MigrationFileListItem.php @@ -0,0 +1,92 @@ +item->getLastState()) { + $visual->getAttributes()->add('class', 'upgrade-failed'); + $visual->addHtml(new Icon('circle-xmark')); + } + } + + protected function assembleTitle(BaseHtmlElement $title): void + { + $scriptPath = $this->item->getScriptPath(); + /** @var string $parentDirs */ + $parentDirs = substr($scriptPath, (int) strpos($scriptPath, 'schema')); + $parentDirs = substr($parentDirs, 0, strrpos($parentDirs, '/') + 1); + + $title->addHtml( + new HtmlElement('span', null, Text::create($parentDirs)), + new HtmlElement( + 'span', + Attributes::create(['class' => 'version']), + Text::create($this->item->getVersion() . '.sql') + ) + ); + + if ($this->item->getLastState()) { + $title->addHtml( + new HtmlElement( + 'span', + Attributes::create(['class' => 'upgrade-failed']), + Text::create($this->translate('Upgrade failed')) + ) + ); + } + } + + protected function assembleHeader(BaseHtmlElement $header): void + { + $header->addHtml($this->createTitle()); + } + + protected function assembleCaption(BaseHtmlElement $caption): void + { + if ($this->item->getDescription()) { + $caption->addHtml(Text::create($this->item->getDescription())); + } else { + $caption->addHtml(new EmptyState(Text::create($this->translate('No description provided.')))); + } + } + + protected function assembleFooter(BaseHtmlElement $footer): void + { + if ($this->item->getLastState()) { + $footer->addHtml( + new HtmlElement( + 'section', + Attributes::create(['class' => 'caption']), + new HtmlElement('pre', null, new HtmlString(Html::escape($this->item->getLastState()))) + ) + ); + } + } + + protected function assembleMain(BaseHtmlElement $main): void + { + $main->addHtml($this->createHeader(), $this->createCaption()); + } +} diff --git a/library/Icinga/Web/Widget/ItemList/MigrationList.php b/library/Icinga/Web/Widget/ItemList/MigrationList.php index d1836cd98..b8a153b31 100644 --- a/library/Icinga/Web/Widget/ItemList/MigrationList.php +++ b/library/Icinga/Web/Widget/ItemList/MigrationList.php @@ -69,10 +69,10 @@ class MigrationList extends BaseItemList protected function getItemClass(): string { if ($this->isMinimal()) { - return MigrationListItemMinimal::class; + return MigrationListItem::class; } - return MigrationListItem::class; + return MigrationFileListItem::class; } protected function assemble(): void @@ -84,9 +84,9 @@ class MigrationList extends BaseItemList /** @var MigrationHook $data */ foreach ($this->data as $data) { - /** @var MigrationListItem|MigrationListItemMinimal $item */ + /** @var MigrationFileListItem|MigrationListItem $item */ $item = new $itemClass($data, $this); - if ($item instanceof MigrationListItemMinimal && $this->migrationForm) { + if ($item instanceof MigrationListItem && $this->migrationForm) { $migrateButton = $this->migrationForm->createElement( 'submit', sprintf('migrate-%s', $data->getModuleName()), diff --git a/library/Icinga/Web/Widget/ItemList/MigrationListItem.php b/library/Icinga/Web/Widget/ItemList/MigrationListItem.php index 89ceade82..d9010db22 100644 --- a/library/Icinga/Web/Widget/ItemList/MigrationListItem.php +++ b/library/Icinga/Web/Widget/ItemList/MigrationListItem.php @@ -5,81 +5,131 @@ namespace Icinga\Web\Widget\ItemList; use Icinga\Application\Hook\Common\DbMigration; +use Icinga\Application\Hook\MigrationHook; use ipl\Html\Attributes; use ipl\Html\BaseHtmlElement; +use ipl\Html\Contract\FormElement; +use ipl\Html\FormattedString; use ipl\Html\Html; use ipl\Html\HtmlElement; use ipl\Html\HtmlString; use ipl\Html\Text; use ipl\I18n\Translation; use ipl\Web\Common\BaseListItem; +use ipl\Web\Url; use ipl\Web\Widget\EmptyState; use ipl\Web\Widget\Icon; +use ipl\Web\Widget\Link; +use LogicException; class MigrationListItem extends BaseListItem { use Translation; - /** @var DbMigration Just for type hint */ + /** @var ?FormElement */ + protected $migrateButton; + + /** @var MigrationHook Just for type hint */ protected $item; - protected function assembleVisual(BaseHtmlElement $visual): void + /** + * Set a migration form of this list item + * + * @param FormElement $migrateButton + * + * @return $this + */ + public function setMigrateButton(FormElement $migrateButton): self { - if ($this->item->getLastState()) { - $visual->getAttributes()->add('class', 'upgrade-failed'); - $visual->addHtml(new Icon('circle-xmark')); - } + $this->migrateButton = $migrateButton; + + return $this; } protected function assembleTitle(BaseHtmlElement $title): void { - $scriptPath = $this->item->getScriptPath(); - /** @var string $parentDirs */ - $parentDirs = substr($scriptPath, (int) strpos($scriptPath, 'schema')); - $parentDirs = substr($parentDirs, 0, strrpos($parentDirs, '/') + 1); - $title->addHtml( - new HtmlElement('span', null, Text::create($parentDirs)), - new HtmlElement( - 'span', - Attributes::create(['class' => 'version']), - Text::create($this->item->getVersion() . '.sql') + FormattedString::create( + t('%s ', ''), + HtmlElement::create('span', ['class' => 'subject'], $this->item->getName()) ) ); + } - if ($this->item->getLastState()) { + protected function assembleHeader(BaseHtmlElement $header): void + { + if ($this->migrateButton === null) { + throw new LogicException('Please set the migrate submit button beforehand'); + } + + $header->addHtml($this->createTitle()); + $header->addHtml($this->migrateButton); + } + + protected function assembleCaption(BaseHtmlElement $caption): void + { + $migrations = $this->item->getMigrations(); + /** @var DbMigration $migration */ + $migration = array_shift($migrations); + if ($migration->getLastState()) { + if ($migration->getDescription()) { + $caption->addHtml(Text::create($migration->getDescription())); + } else { + $caption->addHtml(new EmptyState(Text::create($this->translate('No description provided.')))); + } + + $scriptPath = $migration->getScriptPath(); + /** @var string $parentDirs */ + $parentDirs = substr($scriptPath, (int) strpos($scriptPath, 'schema')); + $parentDirs = substr($parentDirs, 0, strrpos($parentDirs, '/') + 1); + + $title = new HtmlElement('div', Attributes::create(['class' => 'title'])); $title->addHtml( + new HtmlElement('span', null, Text::create($parentDirs)), + new HtmlElement( + 'span', + Attributes::create(['class' => 'version']), + Text::create($migration->getVersion() . '.sql') + ), new HtmlElement( 'span', Attributes::create(['class' => 'upgrade-failed']), Text::create($this->translate('Upgrade failed')) ) ); - } - } - protected function assembleHeader(BaseHtmlElement $header): void - { - $header->addHtml($this->createTitle()); - } + $error = new HtmlElement('div', Attributes::create([ + 'class' => 'collapsible', + 'data-visible-height' => '58', + ])); + $error->addHtml(new HtmlElement('pre', null, new HtmlString(Html::escape($migration->getLastState())))); - protected function assembleCaption(BaseHtmlElement $caption): void - { - if ($this->item->getDescription()) { - $caption->addHtml(Text::create($this->item->getDescription())); - } else { - $caption->addHtml(new EmptyState(Text::create($this->translate('No description provided.')))); + $errorSection = new HtmlElement('div', Attributes::create(['class' => 'errors-section',])); + $errorSection->addHtml( + new HtmlElement('header', null, new Icon('circle-xmark', ['class' => 'status-icon']), $title), + $caption, + $error + ); + + $caption->prependWrapper($errorSection); } } protected function assembleFooter(BaseHtmlElement $footer): void { - if ($this->item->getLastState()) { + $footer->addHtml((new MigrationList($this->item->getLatestMigrations(3)))->setMinimal(false)); + if ($this->item->count() > 3) { $footer->addHtml( - new HtmlElement( - 'section', - Attributes::create(['class' => 'caption']), - new HtmlElement('pre', null, new HtmlString(Html::escape($this->item->getLastState()))) + new Link( + sprintf($this->translate('Show all %d migrations'), $this->item->count()), + Url::fromPath( + 'migrations/migration', + [MigrationHook::MIGRATION_PARAM => $this->item->getModuleName()] + ), + [ + 'data-base-target' => '_next', + 'class' => 'show-more' + ] ) ); } @@ -87,6 +137,15 @@ class MigrationListItem extends BaseListItem protected function assembleMain(BaseHtmlElement $main): void { - $main->addHtml($this->createHeader(), $this->createCaption()); + $main->addHtml($this->createHeader()); + $caption = $this->createCaption(); + if (! $caption->isEmpty()) { + $main->addHtml($caption); + } + + $footer = $this->createFooter(); + if ($footer) { + $main->addHtml($footer); + } } } diff --git a/library/Icinga/Web/Widget/ItemList/MigrationListItemMinimal.php b/library/Icinga/Web/Widget/ItemList/MigrationListItemMinimal.php deleted file mode 100644 index a45288808..000000000 --- a/library/Icinga/Web/Widget/ItemList/MigrationListItemMinimal.php +++ /dev/null @@ -1,151 +0,0 @@ -migrateButton = $migrateButton; - - return $this; - } - - protected function assembleTitle(BaseHtmlElement $title): void - { - $title->addHtml( - FormattedString::create( - t('%s ', ''), - HtmlElement::create('span', ['class' => 'subject'], $this->item->getName()) - ) - ); - } - - protected function assembleHeader(BaseHtmlElement $header): void - { - if ($this->migrateButton === null) { - throw new LogicException('Please set the migrate submit button beforehand'); - } - - $header->addHtml($this->createTitle()); - $header->addHtml($this->migrateButton); - } - - protected function assembleCaption(BaseHtmlElement $caption): void - { - $migrations = $this->item->getMigrations(); - /** @var DbMigration $migration */ - $migration = array_shift($migrations); - if ($migration->getLastState()) { - if ($migration->getDescription()) { - $caption->addHtml(Text::create($migration->getDescription())); - } else { - $caption->addHtml(new EmptyState(Text::create($this->translate('No description provided.')))); - } - - $scriptPath = $migration->getScriptPath(); - /** @var string $parentDirs */ - $parentDirs = substr($scriptPath, (int) strpos($scriptPath, 'schema')); - $parentDirs = substr($parentDirs, 0, strrpos($parentDirs, '/') + 1); - - $title = new HtmlElement('div', Attributes::create(['class' => 'title'])); - $title->addHtml( - new HtmlElement('span', null, Text::create($parentDirs)), - new HtmlElement( - 'span', - Attributes::create(['class' => 'version']), - Text::create($migration->getVersion() . '.sql') - ), - new HtmlElement( - 'span', - Attributes::create(['class' => 'upgrade-failed']), - Text::create($this->translate('Upgrade failed')) - ) - ); - - $error = new HtmlElement('div', Attributes::create([ - 'class' => 'collapsible', - 'data-visible-height' => '58', - ])); - $error->addHtml(new HtmlElement('pre', null, new HtmlString(Html::escape($migration->getLastState())))); - - $errorSection = new HtmlElement('div', Attributes::create(['class' => 'errors-section',])); - $errorSection->addHtml( - new HtmlElement('header', null, new Icon('circle-xmark', ['class' => 'status-icon']), $title), - $caption, - $error - ); - - $caption->prependWrapper($errorSection); - } - } - - protected function assembleFooter(BaseHtmlElement $footer): void - { - $footer->addHtml((new MigrationList($this->item->getLatestMigrations(3)))->setMinimal(false)); - if ($this->item->count() > 3) { - $footer->addHtml( - new Link( - sprintf($this->translate('Show all %d migrations'), $this->item->count()), - Url::fromPath( - 'migrations/migration', - [MigrationHook::MIGRATION_PARAM => $this->item->getModuleName()] - ), - [ - 'data-base-target' => '_next', - 'class' => 'show-more' - ] - ) - ); - } - } - - protected function assembleMain(BaseHtmlElement $main): void - { - $main->addHtml($this->createHeader()); - $caption = $this->createCaption(); - if (! $caption->isEmpty()) { - $main->addHtml($caption); - } - - $footer = $this->createFooter(); - if ($footer) { - $main->addHtml($footer); - } - } -}