proklung / framework-tools-bundle
为自定义 Symfony 提供的一些工具。
Requires
- php: >=7.3 || ^8.0
- ext-json: *
- denismitr/translit: ~3
- doctrine/common: ^3.1
- egulias/email-validator: ^2 | ^3.1
- giggsey/libphonenumber-for-php: ^8.12
- psr/simple-cache: ^1.0
- symfony/config: ^4.4 || ^5.0
- symfony/console: ^4.4 || ^5.0
- symfony/dependency-injection: ^4.4 || ^5.0
- symfony/filesystem: ^4.4 || ^5.0
- symfony/http-kernel: ^4.4 || ^5.0
- symfony/lock: ^4.4 || ^5.0
- symfony/mime: ^4.4 || ^5.0
- symfony/process: ^4.4 || ^5.0
- symfony/validator: ^4.4 || ^5.0
Requires (Dev)
- brain/monkey: ^2.4.2
- icanhazstring/composer-unused: ^0.7.5
- johnpbloch/wordpress-core: @stable
- proklung/phpunit-testing-tools: ^1.3.0
This package is auto-updated.
Last update: 2024-09-24 21:07:05 UTC
README
安装
- composer.json
"repositories": [ { "type": "git", "url": "https://github.com/proklung/framework-tools-bundle" } ]
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);
但我这样做了(在 .env
中 SAVEQUERIES
>= 0 或 1)
define('SAVEQUERIES', (bool)$_ENV['SAVEQUERIES'] ?? false);
某个地方
$sql = container()->get('sql.logger.monolog'); $sql->init();
请求日志将落入 Monolog 的常规日志中。