swp / updater-bundle
为 ahilles107/updater 提供集成,为您的应用程序更新提供简单方法。
Requires
- php: >=5.4
- ahilles107/updater: 1.0.x-dev
- friendsofsymfony/rest-bundle: >=1.7.1
- jms/serializer-bundle: ^1.0
- nelmio/api-doc-bundle: @stable
- symfony/framework-bundle: ~2.0
- vierbergenlars/php-semver: ^3.0
- willdurand/hateoas-bundle: ^1.0
Requires (Dev)
- akeneo/phpspec-skip-example-extension: ^1.2@dev
- phpspec/phpspec: ~2.0
- phpunit/php-code-coverage: @stable
- phpunit/phpunit: ~4.5@stable
Suggests
- guzzlehttp/guzzle: Allows to send more advanced HTTP requests (requires PHP >= 5.5.0)
This package is auto-updated.
Last update: 2020-01-26 16:25:47 UTC
README
为 updater 提供集成,为您的应用程序更新提供简单方法。
安装
安装是一个8步的过程
- 下载 SWPUpdaterBundle
- 启用包及其依赖项
- 创建自己的
Version
类或使用现有类 - 导入 SWPUpdaterBundle 路由文件
- 配置 FOSRestBundle
- 配置 SensioFrameworkExtraBundle
- 配置 Symfony FrameworkBundle
- 配置 SWPUpdaterBundle
第1步:使用 Composer 安装 SWPUpdaterBundle
运行以下 composer require 命令
$ php composer.phar require swp/updater-bundle
第2步:启用包及其依赖项
在 AppKernel.php
中启用包及其所有依赖项(FOSRestBundle, JMSSerializerBundle, NelmioApiDocBundle)
<?php // app/AppKernel.php public function registerBundles() { $bundles = array( // ... new SWP\UpdaterBundle\SWPUpdaterBundle(), new FOS\RestBundle\FOSRestBundle(), new JMS\SerializerBundle\JMSSerializerBundle(), new Nelmio\ApiDocBundle\NelmioApiDocBundle(), ); }
第3步:创建自己的 Version
类或使用现有类
此包需要您的自定义 Version
类来读取当前应用程序的版本并根据该版本应用可用更新。您必须为您的应用程序创建自己的 Version
类,该类必须实现此包提供的 SWP\UdaterBundle\Version\VersionInterface
接口。下面是示例。
<?php namespace Acme\DemoBundle\Version; use SWP\UpdaterBundle\Version\VersionInterface; final class Version implements VersionInterface { private $version = '0.0.1'; public function getVersion() { return $this->version; } public function setVersion($version) { $this->version = $version; return $this; } }
第4步:导入 SWPUpdaterBundle 路由文件
您必须导入 SWPUpdaterBundle 路由文件。您可以使用 YAML 或 XML 格式。
YAML
# app/config/routing.yml swp_updater: resource: "@SWPUpdaterBundle/Resources/config/routing.yml" prefix: /
XML
<!-- app/config/routing.xml --> <import resource="@SWPUpdaterBundle/Resources/config/routing.xml" prefix="/" />
第5步:配置 FOSRestBundle
FOSRestBundle 将自动为您安装。您只需进行配置。SWPUpdaterBundle 提供了 API 端点来获取和发布适当的数据。为了使用 SWPUpdaterBundle 提供的 API,您必须正确配置 FOSRestBundle。
FOSRestBundle 提供了各种工具,可以快速使用 Symfony2 开发 RESTful API。以下示例提供了可用的 YAML 配置。
感谢 NelmioApiDocBundle,SWPUpdaterBundle API 文档可以在以下 URL 下获得: http://example.com/api/doc
有关 FOSRestBundle 的配置信息,请参阅 文档
# app/config/config.yml fos_rest: routing_loader: default_format: json view: formats: json: true view_response_listener: 'force' format_listener: rules: - { path: '^/api', priorities: ['json'], fallback_format: json, prefer_extension: true } - { path: '^/', stop: true } exception: codes: 'Symfony\Component\Routing\Exception\ResourceNotFoundException': 404 messages: 'Symfony\Component\Routing\Exception\ResourceNotFoundException': true
第6步:配置 SensioFrameworkExtraBundle
Symfony FrameworkBundle 将自动为您安装。您只需进行配置。
# app/config/config.yml sensio_framework_extra: view: { annotations: false }
第7步:配置 Symfony FrameworkBundle
Symfony FrameworkBundle 将自动为您安装。您只需进行配置。
# app/config/config.yml framework: templating: engines: ['twig']
步骤 8:配置 SWPUpdaterBundle
现在您已经有了自己的 Version
类,是时候根据您的需求配置 Bundle 了。
将以下参数添加到您的 parameters.yml
文件中。
# app/config/parameters.yml parameters: swp_updater.version.class: "Acme\DemoBundle\Version\Version"
将以下配置添加到您的 config.yml
文件中。
# app/config/config.yml swp_updater: version_class: %swp_updater.version.class% client: base_uri: http://example.com
在此阶段,该 Bundle 已准备好供您的应用程序使用。
添加自定义 http 客户端选项
SWPUpdaterBundle 使用 Guzzle 从提供更新包信息的外部服务器获取数据。您可以通过简单地添加一个选项数组作为参数来为您的 http 客户端添加自定义 Guzzle 选项/头部。以下示例显示了如何添加自定义 curl 选项。
# app/config/parameters.yml parameters: swp_updater.client.options: curl: # http://guzzle.readthedocs.org/en/latest/faq.html#how-can-i-add-custom-curl-options 10203: # integer value of CURLOPT_RESOLVE - "example.com:localhost" # This will resolve the host example.com to your localhost
更多详细信息请参阅 Guzzle 文档。
更改默认目录
Http 客户端使用两种类型的目录
- 临时目录 - 指定更新包将被下载和解压缩的位置,默认为
app/cache/<env>
,其中<env>
可以是dev
、test
或prod
。 - 目标目录 - 应该更新的目录,默认为当前应用程序目录。
可以通过在 config.yml 文件中设置 temp_dir
和 target_dir
选项来更改这些目录。
# app/config/config.yml swp_updater: temp_dir: "/some/temp/dir" target_dir: "/some/target/dir"
为 SWPUpdaterBundle 启用 Monolog 通道
可以启用一个单独的 Monolog 通道,所有日志都会转发到该通道。这样,您将有一个单独的 SWPUpdaterBundle 日志文件,该文件将保存在 app/logs/
目录下,并以 updater_<env>-<current_date>.log
命名。默认情况下,单独的通道是禁用的。您可以通过将 monolog_channel
选项设置为 true
并配置 Monolog 来启用它。
# app/config/config.yml swp_updater: monolog_channel: true monolog: channels: ["updater"] handlers: updater: type: rotating_file # creates a new file every day (https://symfony.ac.cn/doc/current/cookbook/logging/monolog.html#how-to-rotate-your-log-files) path: %kernel.logs_dir%/updater_%kernel.environment%.log level: debug max_files: 10 channels: ["updater"]
更多详细信息请参阅 Monolog 文档。
更改客户端的类型
SWPUpdaterBundle 支持两种客户端从更新服务器下载更新包
- PHP (file_get_contents)
- Guzzle
可以通过在 Bundle 配置中定义客户端的 type
来在这两个客户端之间切换。可用的类型有
- guzzle
- 默认
当 type
没有定义时,默认使用 PHP (默认) 客户端。
# app/config/config.yml swp_updater: version_class: %swp_updater.version.class% client: base_uri: http://example.com type: guzzle # or default