rohsyl/laraupdater
LaraUpdater 允许您的 Laravel 应用程序自动更新自身。
Requires
- php: >=7.1.3
- ext-json: *
- ext-zip: *
- guzzlehttp/guzzle: ^7.0.1
README
LaraUpdater
您的 Laravel 应用程序的自更新
LaraUpdater 允许您的 Laravel 应用程序自动更新自身!
当您发布应用程序时,维护它是最重要的,因此,为了修复错误以及实现新功能,可能需要发布更新。
您为多个用户部署应用程序
-
没有 LaraUpdate
您想要逐一联系他们并使用电子邮件或链接发送更新吗?...嗯...非常糟糕,因为每个具有管理员角色的用户都必须手动覆盖他在部署中的所有文件;或者,您必须手动访问所有部署(例如使用 FTP)并为他们安装更新。
-
使用 LaraUpdater
让您的应用程序(独立)检测到新更新可用,并将通知发送给管理员;此外,让您的应用程序安装它并处理所有相关步骤。
新版本变更日志
[感谢rohsyl]
- 通过实现
ILaraUpdaterPolicy改进权限管理 - 改进消息显示(《ob_flush》)
- 改进升级脚本
[感谢salihkiraz :) ]
- 支持多语言
- 添加示例视图
- 添加 Laravel 包自动发现
- 测试 Laravel 5.7
- 修复 zip 文件提取
功能
> 自更新
LaraUpdater 允许您的 Laravel 应用程序自我更新 :) 让您的应用程序(独立)检测到新更新可用,并将通知发送给管理员;此外,让您的应用程序安装它并处理所有相关步骤。
> 维护模式
LaraUpdate 从更新开始直到成功完成,使用 Laravel 原生命令激活维护模式。
> 安全性
您可以选择哪些用户(例如,只有管理员)可以执行应用程序的更新;此参数存储在 config/laraupdater.php 中,因此每个应用程序都可以独立设置其用户。此外,LaraUpdater 与 Laravel-Auth 兼容。
> 容错
在更新过程中,LaraUpdate 会备份所有被覆盖的文件,以便在发生错误时可以尝试自动恢复到之前的状态。如果恢复失败,您可以使用存储在系统根目录的备份进行手动维护。
> 支持升级脚本
LaraUpdate 可以导入 PHP 脚本来执行自定义操作(例如,在更新后创建数据库中的表);命令在更新步骤的最后执行。
入门指南
以下说明将帮助您在服务器上创建项目的副本,以便进行开发和测试。
先决条件
LaraUpdater 已使用 Laravel 5.7 测试。推荐的 Laravel 版本 >= 5.x
安装
此包可以通过 Composer 安装
composer require rohsyl/laraupdater
安装后,您必须执行以下步骤
1) 在 config/app.php 文件中添加服务提供者
'providers' => [
// ...
rohsyl\laraupdater\LaraUpdaterServiceProvider::class,
];
2) 在您的应用程序中发布 LaraUpdater
此步骤将复制配置文件到您的 Laravel 应用程序的配置目录中。
php artisan vendor:publish --provider="rohsyl\laraupdater\LaraUpdaterServiceProvider"
发布后,您可以通过config/laraupdater.php文件管理LaraUpdater的配置,它包含以下内容:
/*
* Temp folder to store update before to install it.
*/
'tmp_path' => '/../tmp',
/*
* URL where your updates are stored ( e.g. for a folder named 'updates', under http://site.com/yourapp ).
*/
'update_baseurl' => 'http://site.com/yourapp/updates',
/* ********************
* POST INSTALL SCRIPT
* ********************
* If this file exists in your app after the extraction of the archive and if
* it contains a laraupdater_post_upgrade($currentVersion, $lastVersion) method it will be executed.
*/
'post_upgrade_file_location' => 'update/update.php',
/*
* The name of the file in which the last version information are stored on your webserver
*/
'last_update_filename' => 'current.json',
/**
* The name of the file that contains the current version
* This file is located at the root of the project
*/
'current_filename' => 'version',
/*
* Set a middleware for every routes
* Only 'auth' NOT works (manage security using 'permissions' configuration)
*/
'middleware' => ['web', 'auth'],
'permissions' => [
/**
* Set which policy to check permissions
* You can create your own by implementing the
* pcinaglia\laraupdater\Policies\ILaraUpdaterPolicy interface
* and registering it here
*/
'policy' => pcinaglia\laraupdater\Policies\AllowUserIdLaraUpdaterPolicy::class,
'parameters' => [
/*
* This entry is related to the policy :
* pcinaglia\laraupdater\Policies\AllowUserIdLaraUpdaterPolicy
*
* If you are not using this policy, you can remove it.
*
* Set which users can perform an update;
* This parameter accepts: ARRAY(user_id) ,or FALSE => for example: [1] OR [1,3,0] OR false
* Generally, ADMIN have user_id=1; set FALSE to disable this check (not recommended)
*/
'allow_users_id' => [1],
]
],
3) 创建版本
为了存储应用程序的当前版本,您必须创建一个名为version的文本文件,并将其复制到Laravel应用程序的主文件夹中。例如,创建一个只包含以下内容的文件:
1.0.0
...只使用文件的第一行。当发布更新时,该文件将由LaraUpdate更新。
创建您的更新“仓库”
1) 创建存档
创建一个包含您想要在更新过程中替换的所有文件的.zip存档(使用与应用程序相同的结构来组织存档中的文件)。
1.1) 升级脚本(可选)
您可以创建一个名为update/update.php的PHP文件来执行自定义操作(例如,在数据库中创建一个新表)。此文件必须包含一个名为laraupdater_post_upgrade($currentVersion, $newlyInstalledVersion)的函数,并返回布尔值(将其实例执行状态传递给LaraUpdater),请参阅以下示例
<?php
function laraupdater_post_upgrade($currentVersion, $newlyInstalledVersion) {
command-1-to-connect-db
command-2-to-create-table
command-3-to-insert-data
return true;
}
?>
请注意,上述示例没有处理任何异常,因此其实例执行状态始终返回true(不推荐)。
2) 设置更新元数据
创建一个名为current.json的文件,如下所示
{
"version": "1.0.2",
"archive": "RELEASE-1.02.zip",
"description": "Minor bugs fix"
}
archive包含.zip存档的名称(参见步骤-1)。
3) 上传您的更新
将current.json和.zip存档上传到同一服务器文件夹中(该文件夹将托管更新)。
4) 配置您的应用程序
在config/laraupdater.php中设置将托管更新的服务器(参见安装说明)
例如,如果您在上传文件时使用
http://yoursites.com/updatesformyapp/RELEASE-1.02.zip
and http://yoursites.com/updatesformyapp/laraupdater.json
将'update_baseurl'设置为以下内容:'update_baseurl' => 'http://yoursites.com/updatesformyapp',
用法
LaraUpdater实现了三个主要方法,您可以通过路由调用它们
updater.check
返回''(不存在更新)或$version(如果存在更新,例如1.0.2)。
updater.currentVersion
返回应用程序的当前版本(从version.txt中获取)。
updater.update
它下载并安装最新可用的更新。此路由使用config/laraupdater.php下'allow_users_id'中的信息进行保护
建议不要直接使用这些路由,而是显示一个Alert来通知有更新可用;Alert可以包含一个按钮来执行更新,请参阅以下解决方案
(示例)Bootstrap 4和JQuery解决方案
将以下HTML代码添加到view/layout/app.blade.php中(建议在@yield('content')之前立即添加)
<div id="update_notification" style="display:none;" class="alert alert-info">
<button type="button" style="margin-left: 20px" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
现在,将以下JQuery脚本添加到view/layout/app.blade.php的末尾
<script>
$(document).ready(function() {
$.ajax({
type: 'GET',
url: 'updater.check',
async: false,
success: function(response) {
if(response != ''){
$('#update_notification').append('<strong>Update Available <span class="badge badge-pill badge-info">v. '+response+'</span></strong><a role="button" href="updater.update" class="btn btn-sm btn-info pull-right">Update Now</a>');
$('#update_notification').show();
}
}
});
});
</script>
<< END >> ...要测试它:发布一个更新并刷新页面以显示alert :-)
许可
该项目受MIT许可证许可 - 有关详细信息,请参阅LICENSE文件。
(MIT许可证 - 保证信息)软件按“原样”提供,不提供任何明示或暗示的保证,包括但不限于适销性、特定用途的适用性和非侵权性。在任何情况下,作者或版权所有者均不对任何索赔、损害或其他责任负责,无论是基于合同、侵权或其他方式,无论源于、涉及或与此软件或软件的使用或其他交易有关。
