proklung/framework-tools-bundle

为自定义 Symfony 提供的一些工具。

安装: 1

依赖: 1

建议者: 0

安全: 0

星标: 1

关注者: 1

分叉: 0

开放问题: 0

类型:symfony-bundle

1.5.5 2021-08-24 14:28 UTC

README

安装

  1. composer.json
  "repositories": [
    {
      "type": "git",
      "url": "https://github.com/proklung/framework-tools-bundle"
    }
  ]
  1. composer require proklung/framework-tools-bundle

详情

延迟事件调度器

基础

特别之处:“使用自定义 flusher 清除延迟事件”。

如果从 Bitrix 启动,则绑定到事件 OnEpilog 的监听器。

如果从 WordPress 启动,则绑定到钩子 shutdown 的监听器。

命令运行器

来自的分支。在不同进程中以包的形式运行命令。

使用示例

(new CommandRunner([
            new Process("my:command -q"),
            new Process("my:command2 -q"),
            new Process("my:command3 -q").
            new Process("my:command4 -q"),
            new Process("my:command5 -q"),
            new Process("my:command6 -q --env=$env"),
        ]))
            ->continueOnError(true)
            ->setIO($this->io)
            ->setLimit(3)
            ->run();
            

就是这样

class ExampleRunner extends Command
{
    /** @var SymfonyStyle */
    protected $io;

    /**
     * @inheritDoc
     */
    protected function configure()
    {
        $this->setName('runner:example')
             ->setDescription('runner example');
    }

    /**
     * @inheritDoc
     */
    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $this->io = new SymfonyStyle($input, $output);

        $this->io->writeln('Running runner example');

        sleep(5); # Sleep so user can abort update

        (new CommandRunner([
            new Process(['cache:clear', 'cache:clear --cache-type menu']),
        ]))
            ->continueOnError(true)
            ->setIO($this->io)
            ->setLimit(3)
            ->run();

        return 0;
    }
}

可锁定控制台命令

只能同时在一个实例中运行的命令。

use Prokl\FrameworkExtensionBundle\Services\Command\Lockable\AbstractLockableCommand;

class SomeCommand extends AbstractLockableCommand
{
   protected function configure()
   {
       $this->setName('lock:command')
            ->setDescription('Lock command')
    
       ;
    
       parent::configure();
   }

   protected function execute(InputInterface $input, OutputInterface $output) : int
   {
        $output->writeln('Start');
        sleep(100);
        $output->writeln('End');

        return 0;
   }
}

可以继承方法 getLockTtl() 来重写锁定时间(默认为 60 秒)。

通过 setter 机制通过 autowiring 机制获取此类命令的依赖。

控制台命令

缓存清理(Bitrix 和 WordPress)

php bin/console cache:clear 

简单的 Bitrix PSR-16 缓存

  bitrix.simple.cacher.configured:
    class: Prokl\FrameworkExtensionBundle\Services\Bitrix\Psr16Cache\BitrixCacher
    arguments: ['@Bitrix\Main\Data\Cache']
    calls:
      - setBaseDir: ['/guzzle_request']
      - setTtl: [3600]

方法

  • get
  • getMultiple
  • has
  • delete
  • deleteMultiple
  • clear
  • setMultiple
  • set - set($key, $value, $ttl = null)
  • getOrSet - getOrSet(string $key, callable $callable, $ttl = null)

为 Symfony Validator 定制的验证器

  • Email - 使用 Egulias\EmailValidator
  • Phone - 使用 giggsey/libphonenumber-for-php

辅助控制器

  • BinaryFileResponseTrait - 方法 returnFile(string $file) 会向浏览器返回 BinaryFileResponse 文件 $file,并自动确定 contentType。

通过 Symfony Notifier 向 WordPress 发送致命错误信息

  • 必须定义环境变量 ADMIN_EMAIL

  • 必须安装包 symfony/notifier。如果没有,则在编译时从容器中删除相应的服务。

  • 在根项目中必须有一个类服务,实现 Prokl\FrameworkExtensionBundle\Services\Wordpress\ErrorHandler\Contract\ErrorDbOperatorInterface,用于处理数据库记录(在我的例子中,保存了序列化异常的 md5)。

  • save - 将错误信息保存到数据库(或任何其他地方)。

  • has - 该错误记录是否在数据库中。

  • clearTable - 清除错误数据表。

  • 默认情况下,消息会发送到标记为 urgent 的通道。

  • 装饰器日志记录器。

主项目配置

  logger_notify_decorated:
    class: Prokl\FrameworkExtensionBundle\Services\Wordpress\Notifier\LoggerDecorator
    decorates: 'logger'
    arguments: ['@.inner', '@wp_notificator']

通过 Monolog 记录 WordPress 的 SQL 查询

服务 sql.logger.monolog。如果容器中没有服务 wpdb(wpdb 实例)和 logger,则日志记录器将被删除。

wp-config.php

define('SAVEQUERIES', true);

但我这样做了(在 .envSAVEQUERIES >= 0 或 1)

define('SAVEQUERIES', (bool)$_ENV['SAVEQUERIES'] ?? false);

某个地方

$sql = container()->get('sql.logger.monolog');
$sql->init();

请求日志将落入 Monolog 的常规日志中。