krubio/perfect-theme-solid

主题切换器

dev-master 2024-05-31 21:30 UTC

This package is auto-updated.

Last update: 2024-10-01 00:17:36 UTC


README

codecov Build

Coverage Reliability Rating Security Rating Maintainability Rating Bugs Vulnerabilities

Quality Gate Status Duplicated Lines (%) Technical Debt Code Smells

概述

ThemeSelector是一个PHP类,允许用户选择并应用不同的主题到他们的Web应用程序中。可用的主题是预定义的,并列入下拉菜单以供选择。

用法

要在PHP项目中使用ThemeSelector,请按照以下步骤操作

  1. 通过将Cookie对象作为参数传递给其构造函数来创建ThemeSelector的实例。
  2. 调用renderSelector方法以生成下拉菜单的HTML代码。
  3. 在您的Web应用程序中向用户显示生成的HTML代码。
  4. 检查POST请求并调用setTheme方法

示例用法

以下是如何在PHP项目中使用ThemeSelector的示例

创建Cookie对象

$cookie = new PerfectApp\Http\Cookie();

创建ThemeSelector实例

$themeSelector = new PerfectApp\ThemeSelector\ThemeSelector($cookie);

生成并显示下拉菜单的HTML代码

<form method="POST">
    <?php echo $theme->renderSelector(); ?>
    <button type="submit">Switch Theme</button>
</form>

示例用法

<?php

declare(strict_types=1);

use PerfectApp\Http\Cookie;
use PerfectApp\ThemeSelector\ThemeSelector;

require_once '../vendor/autoload.php';

$cookie = new Cookie($_COOKIE);

$theme = new ThemeSelector($cookie);

if (!empty($_POST['theme'])) {
    $theme->setTheme($_POST['theme']);
    header('Location: ./');
    exit;
}

API

以下是在ThemeSelector类中可用的方法

__construct(Cookie $cookie): This is the constructor method that takes a Cookie object as an argument. It initializes the $cookie property of the ThemeSelector object.

getTheme(): This method returns the current theme name that is stored in the cookie. If there is no theme stored in the cookie, it returns the default theme.

setTheme(string $themeName): This method sets the theme name in the cookie. If the theme name is not valid, it sets the default theme.

renderSelector(): This method generates the HTML code for the dropdown menu that lists all the available themes. It returns the generated HTML code as a string.

可用的主题:以下是在ThemeSelector中可用的主题

Default
Cerulean
Cosmo
Cyborg
Darkly
Flatly
Journal
Litera
Lumen
Lux
Materia
Minty
Morph
Pulse
Quartz
Sandstone
Simplex
Sketchy
Slate
Solar
Spacelab
Superhero
United
Vapor
Yeti
Zephyr

注意:请确保在项目中包含PerfectApp\Http\Cookie类以使用ThemeSelector