c975l/services-bundle

c975L 套件使用的常用服务

v4.2.2 2024-09-10 07:45 UTC

README

ServicesBundle 执行以下操作

  • 定义 c975L 套件使用的服务,
  • 定义 c975L 套件使用的翻译,
  • 作为“额外奖励”还包含 .sh 脚本(见底部),

ServicesBundle 专用网页.

ServicesBundle API 文档.

安装套件

步骤 1:下载套件

使用 Composer 安装库

    composer require c975l/services-bundle

步骤 2:启用套件

然后,将套件添加到项目 app/AppKernel.php 文件中注册的套件列表中,以启用该套件

<?php
class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = [
            // ...
            new c975L\ServicesBundle\c975LServicesBundle(),
        ];
    }
}

如何使用

通过其接口调用所需的服务并使用其方法。

调整图像大小

如果您想调整图像大小,可以这样做

use c975L\ServicesBundle\Service\ServiceImageInterface;

class YourClass
{
    private $imageService;

    public yourMethod(ServiceImageInterface $imageService)
    {
        //Do your stuff...

        //Resizes image
        $imageService->resize($file, string $folder, string $filename, string $format = 'jpg', int $finalHeight = 400, int $compression = 75, bool $square = false, $stamp = null);
    }
}

创建 Flash 消息

如果您想创建一个 Flash 消息,可以这样做

use c975L\ServicesBundle\Service\ServiceToolsInterface;

class YourClass
{
    private $toolsService;

    public yourMethod(ServiceToolsInterface $toolsService)
    {
        //Do your stuff...

        //Create flash
        $toolsService->createFlash(string $translationDomain = null, string $text, string $style = 'success', array $options = array());
    }
}

.sh 脚本

这些脚本与 Symfony 无关,而是与其生产步骤有关,用于 GitHookPostUpdate.sh 及其备份 BackupXXX.sh。**它们是为在 Synfony 4(flex) 结构和 GNU/Linux 服务器上工作而编写的。更多信息请见下文。

GitHookPostUpdate.sh

此脚本应在 Git 仓库更新后(通过 git pull)运行,为此,应在 .git/hooks/post-update 文件中放置以下代码

#!/bin/bash
Folder="$( cd "$(dirname "${BASH_SOURCE[0]}")"; pwd -P )";
#YOUR_PHP_VERSION is the name of the php binary you will use i.e. `php-7.3`
source $Folder/../../PATH_TO_ROOT_FOLDER/vendor/c975l/services-bundle/Scripts/GitHookPostUpdate.sh YOUR_PHP_VERSION;
exit 0

ImportSqlFile.sh

此脚本在您将一些 SQL 查询存储在文件中以允许直接导入到 MySql 服务器时很有用。脚本会在处理之前重命名导入的文件(必须是 "/var/tmp/sqlFile.sql"),以避免冲突,并在之后使用日期和时间重命名它。您可以通过以下代码简单地添加一个新的 cron

MAILTO=YOUR_EMAIL_ADDRESS
*/20    *       *       *       *       bash ~/run.as/httpdocs/vendor/c975l/services-bundle/Scripts/ImportSqlFile.sh 1> /dev/null

它还会删除 7 天前的文件。它使用 /config/backup_config.cnf 中定义的数据,见下文。

BackupXXX.sh

这些脚本是用于网站备份的,详细说明如下。备份文件存储在 /var/backup/{year}/{year-month}/{year-month-day}。文件使用以下方案命名:"[MYSQL|WEBSITE]_[NAME]_[YYYY-MM-DD]_[HH-II]_[WithoutArchives|Archives|Complete|Partial].tar.bz2"。

您可以将它们包含在 crontab 中,如下所示,以在 06 到 22 点之间每小时的 15 分钟执行

MAILTO=YOUR_EMAIL_ADDRESS
15       6-22       *       *       *       bash /server_path_website/vendor/c975l/services-bundle/Scripts/BackupXXX.sh

如果出现错误,将通过 cron 发送电子邮件(每天只发送一次,在配置文件中指定的时刻),以总结备份操作。

您必须创建一个配置文件 /config/backup_config.cnf,包含以下数据(无空格)请注意要将此文件添加到 .gitignore

[client]
user=DB_USER
password=DB_PASSWORD
host=DB_HOST
[config]
website=WEBSITE_NAME
database=DATABASE_NAME
day=DAY_FOR_COMPLETE_BACKUP
hour=HOUR_FOR_COMPLETE_BACKUP This hour has to be one of which the cron will be launched otherwise it will never be reached

BackupServer.sh

此脚本将调用 BackupMysql.shBackupFiles.sh 的调用分组,以允许只有一个 crontab,但它们可以单独调用。

BackupMysql.sh

此脚本对 MySql 服务器中的表进行备份。每次运行时,所有表都进行 mysqldump(逐个,允许按表恢复),除了名为 _archives 的表,它每天在 /config/backup_config.cnf 中指定的时刻执行一次。同时,也会在相同的时刻进行整个数据库的 mysqldump,以允许使用单个文件恢复。命名格式为 "NAME_-_TABLE.sql"。

BackupFolders.sh

本脚本会对public文件夹进行备份。每周进行一次完整备份,其他时间进行部分备份(仅备份新文件和较新文件)。您可以在名为/config/backup_exclude.cnf的文件中指定要排除的模式列表,用换行符分隔,例如*/folder_to_exclude

Twig 扩展

使用提供的 Twig 扩展 RouteExists,您可以通过 {% if route_exists('YOUR_ROUTE_TO_CHECK') %} 检查路由是否可用。

使用提供的 Twig 扩展 TemplateExists,您可以通过 {% if template_exists('YOUR_TEMPLATE_TO_CHECK') %} 检查模板是否可用。

列表

您可以使用提供的列表

  • 扩展
  • 机器人进行校验。可以通过以下代码调用它们(需要c975L/ConfigBundle
use c975L\ConfigBundle\Service\ConfigServiceInterface;

class YourClass
{
    private $configService;

    public function __construct(ConfigServiceInterface $configService)
    {
        $this->configService = $configService;
    }

    public function yourMethod()
    {
        $extensions = file($this->configService->getContainerParameter('kernel.project_dir') . '/../vendor/c975l/services-bundle/Lists/extensions.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
        if (in_array('txt', $extensions)) {
            //Do your stuff
        }
    }
}

如果这个项目能帮助您缩短开发时间,您可以通过顶部“赞助”按钮来赞助我:)