juliangut / zf-maintenance
Zend Framework 维护模块
Requires
- php: >=5.3.3
- mtdowling/cron-expression: ~1.0
- zendframework/zendframework: ~2.3
Requires (Dev)
- phpmd/phpmd: ~2.2
- phpunit/phpunit: ~4.5
- sebastian/phpcpd: ~2.0
- squizlabs/php_codesniffer: ~2
- zendframework/zend-developer-tools: dev-master
Suggests
- zendframework/zend-developer-tools: If you want to see maintanenace status and schedule
This package is auto-updated.
Last update: 2024-09-15 11:51:49 UTC
README
Juliangut Zend Framework 维护模块
Zend Framework 2 的维护模块。
安装
- 最佳安装方式是使用Composer
php composer.phar require juliangut/zf-maintenance
或直接从 github 下载并将其放置在您的应用程序的 module/
目录中。
-
将
Jgut\Zf\Maintenance
模块添加到您的config/application.config.php
的模块部分 -
将
config\zf-maintenance.global.php.dist
复制到您的config
目录,并重命名为zf-maintenance.global.php
-
安装 zend-developer-tools(可选)
php composer.phar require zendframework/zend-developer-tools
配置
配置示例可以在 config\zf-maintenance.global.php.dist
中找到
use DateTime; return array( 'zf-maintenance' => array( // Strategy service to be used on maintenance 'strategy' => 'ZfMaintenanceStrategy', // Template for the maintenance strategy 'template' => 'zf-maintenance/maintenance', // Maintenance blocks access to application 'block' => true, /* * Maintenance providers * Different means to activate maintenance mode */ 'providers' => array( 'ZfMaintenanceConfigProvider' => array( 'active' => false, ), 'ZfMaintenanceConfigScheduledProvider' => array( 'start' => '2020-01-01 00:00:00', 'end' => new DateTime('2020-01-02 05:00:00'), 'message' => 'Undergoing scheduled maintenance tasks', ), 'ZfMaintenanceEnvironmentProvider' => array( 'variable' => 'zf-maintenance', 'value' => 'On', ), 'ZfMaintenanceFileProvider' => array( 'file' => __DIR__ . '/maintenance', 'message' => 'We are currently running maintenance proccesses', ), 'ZfMaintenanceCrontabProvider' => array( 'expression' => '0 0 1 * *', // @monthly 'interval' => 'PT1H', // 1 hour 'message' => 'We are currently running maintenance proccesses', ), ), /* * Exceptions to maintenance mode * Provides a way to bypass maintenance mode by fulfilling at least one of the conditions */ 'exclusions' => array( 'ZfMaintenanceIpExclusion' => array( '127.0.0.1', // Localhost '192.168.1.10', // Private network ), 'ZfMaintenanceRouteExclusion' => array( 'home', 'admin', ), ), ), );
策略
自定义策略来处理维护模式。
要创建自己的,只需扩展 Jgut\Zf\Maintenance\View\MaintenanceStrategy
模板
维护策略的模板文件
块
默认情况下,维护模式通过抛出 Zend\Mvc\MvcEvent::EVENT_DISPATCH_ERROR
(由 Jgut\Zf\Maintenance\View\MaintenanceStrategy
处理)来阻止应用程序继续执行
如果您不希望维护模式停止执行并显示维护页面,则将块设置为 false。
在您执行不需要关闭应用程序的维护任务时,例如 ddbb 备份时,可以使用此功能。
在这种情况下,可以使用 maintenanceMessage
视图助手来显示维护信息
提供者
维护模式提供者提供了不同的激活维护模式的方法
提供者的检查顺序与提供者数组中的顺序相同,当其中一个提供者处于活动状态时,其余提供者不再检查
常见属性
所有维护提供者都有一个用于维护策略页面的 message
属性
$provider = new ConfigProvider(); $provider->setMessage('custom maintenance mode message');
ConfigProvider
手动提供者,只需设置 active
属性即可设置维护模式
use Jgut\Zf\Maintenance\Provider\ConfigProvider; $provider = new ConfigProvider(); $provider->setActive(true);
EnvironmentProvider
环境变量检查提供者,检查环境变量以设置维护模式
use Jgut\Zf\Maintenance\Provider\EnvironmentProvider; putenv('zf-maintenance=On'); $provider = new EnvironmentProvider(); $provider->setVar('zf-maintenance'); $provider->setValue('On');
FileProvider
文件提供者,验证文件的存在以设置维护模式
use Jgut\Zf\Maintenance\Provider\FileProvider; $provider = new FileProvider(); $provider->setFile(__DIR__ . '/maintenance_file');
计划提供者
实现 Jgut\Zf\Maintenance\Provider\ScheduledProviderInterface
的任何提供者都将用于确定未来的维护情况,并在 scheduledMaintenance
视图助手以及 zend-developer-tools 中使用
ConfigScheduledProvider
手动安排维护时间段
维护模式将在由 start
和 end
属性提供的时间段内设置(有效的 DateTime 字符串或对象)。
如果只提供了 start
,则维护模式启动后不会停止。如果只提供了 end
,则维护模式将从此时开始,直到结束时间
use Jgut\Zf\Maintenance\Provider\ConfigScheduledProvider; use DateTime; $provider = new ConfigScheduledProvider(); $provider->setStart('2020-01-01 00:00:00'); $provider->setEnd(new DateTime('2020-01-01 05:00:00')),
CrontabProvider
基于 CRON 表达式语法 的计划维护
* * * * * *
| | | | | |
| | | | | +--- Year [optional]
| | | | +-------- Day of week (0-7) (Sunday=0|7)
| | | +------------- Month (1-12)
| | +------------------ Day of month (1-31)
| +----------------------- Hour (0-23)
+---------------------------- Minute (0-59)
维护模式将在由 CRON 表达式和 interval
属性提供的时间段内设置(有效的 DateInterval 规范字符串)。
use Jgut\Zf\Maintenance\Provider\CrontabProvider; $provider = new CrontabProvider(); $provider->setExpression('@monthly'); // 0 0 1 * * $provider->setInterval('PT1H'), // 1 hour // Maintenance will be ON the 1st of every month at 0:00 during 1 hour
使用 Michael Dowling (mtdowling) cron-expression
排除项
绕过维护模式的条件
排除项的检查方式与提供者相同,检查顺序与排除项数组中的顺序相同,当其中一个排除项处于活动状态时(isExcluded),其余排除项不再检查
IpExclusion
排除 IP 以设置维护模式
use Jgut\Zf\Maintenance\Exclusion\IpExclusion; use Zend\Http\PhpEnvironment\RemoteAddress; $excludedIps = array( '127.0.0.1', '192.168.1.10', ); $exclusion = new IpExclusion($excludedIps, new RemoteAddress);
RuteExclusion
排除路由以设置维护模式
use Jgut\Zf\Maintenance\Exclusion\RouteExclusion; use Zend\Mvc\Router\RouteMatch; $excludedRoutes = array( 'routeName', array( 'controller' => 'controllerName', ), array( 'controller' => 'controllerName', 'action' => 'actionName', ), ); $exclusion = new RouteExclusion($excludedRoutes, new RouteMatch);
视图助手
维护信息
maintenanceMessage
将返回当前活动维护提供者的信息,如果不在维护模式下,则返回空字符串
允许在非阻塞状态下的维护或在适用排除规则的用户中显示维护信息
通常在通用模板上使用此助手作为应用程序标题或页脚的信息区域。请注意,如果在维护阻塞模式下,所有未绑定排除规则的所有请求将被重定向到维护页面
$maintenanceMessage = $this->maintenanceMessage(); if ($maintenanceMessage !== '') { sprintf('<div class="alert alert-info">%s</div>', $maintenanceMessage); }
计划维护
scheduledMaintenance
将返回一个数组,其中包含下一个计划维护的时间段
$maintenance = $this->scheduledMaintenance(); // Start or end can be null if not provided /* array( 'start' => \DateTime, 'end' => \DateTime, ); */
ZendDeveloperTools集成
存在一个收集器jgut-zf-maintenance-collector
,用于ZendDeveloperTools,显示当前的维护状态和未来的计划维护时间段
贡献
发现了一个错误或有一个功能请求?请创建一个新问题。在创建新问题之前查看现有问题
请参阅文件CONTRIBUTING.md
许可证
在BSD-3-Clause许可证下发布。
请参阅源代码中包含的文件LICENSE,以获取许可证条款的副本