承接 divineomega/cachetphp 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

divineomega/cachetphp

最新稳定版本:v2.1.0

Composer 安装命令:

composer require divineomega/cachetphp

包简介

cachet.php is a PHP client library for the Cachet status page

README 文档

README

Packagist Downloads Tests

cachet.php is a PHP client library for the Cachet status page (https://cachethq.io/).

This library could be useful for displaying details from your Cachet status page in other systems, such as a monitoring dashboard, a client ticketing system or any other web applications.

If you want to grab cachet.php with Composer, take a look on Packagist: https://packagist.org/packages/jord-jd/cachetphp

Quick Start

Before starting, install the cachet.php library via Composer.

Create CachetInstance object

Now, you need to create a CachetInstance object that represents your installation of Cachet. You can do this like so:

require_once 'vendor/autoload.php';

use \JordJD\CachetPHP\Factories\CachetInstanceFactory;

// The API token for the demo instance is 9yMHsdioQosnyVK4iCVR.
$cachetInstance = CachetInstanceFactory::create('https://demo.cachethq.io/api/v1/', '9yMHsdioQosnyVK4iCVR');

Retrieving Cachet elements

Retrieving data from the various elements of your Cachet instance is easy. Just call the appropriate getter method on your $cachetInstance object. The Cachet install will be contacted and an array of request appropriate objects be returned.

$components = $cachetInstance->getAllComponents();         // Components
$incidents = $cachetInstance->getAllIncidents();           // Incidents
$incidentUpdates = $incidents[0]->getAllIncidentUpdates(); // Incident Updates (Cachet 2.4.0 or above required)
$metrics = $cachetInstance->getAllMetrics();               // Metrics
$metricPoints = $metrics[0]->getAllMetricPoints();         // Metric Points
$subscribers = $cachetInstance->getAllSubscribers();       // Subscribers

To retrieve a single Cachet element by its ID, you can use code similar to the following method.

// Get incident by id
$incident = $cachetInstance->getIncidentById($incidentId);

Sorting Cachet elements

If you wish to sort your results, you can use the following syntax. This works for components, incidents, metrics, metric points and subscribers.

// Get components sorted by name ascending
$components = $cachetInstance->getAllComponents('name', 'asc');

getAll* methods automatically follow paginated responses from Cachet and return a single combined result set.

Reading from Cachet element objects

Reading data from retrieved Cachet element objects is easy. Just access their public member variables.

Here's an example of outputting the id, name, description and status of a Cachet component.

// Get components
$components = $cachetInstance->getAllComponents();

// Display components
foreach ($components as $component) {
    echo $component->id.' - '.$component->name.' - '.$component->description.' - '.$component->status;
    echo "<br/>";
}

See the official Cachet documentation for information on the variables available for each type of Cachet element.

Creating Cachet elements

You can create a Cachet element, such as a component or incident very easily using the cachet.php library. This involves creating an associative array of the details for the Cachet elements and then passing this to the relevant creation method.

The following example shows you how to make a simple test component.

$componentDetails = ['name' => 'Test Component '.rand(1, 99999), 'status' => 1];

$component = $cachetInstance->createComponent($componentDetails);

echo $component->id.' - '.$component->name.' - '.$component->description.' - '.$component->status;

The more comprehensive example below shows how to create an incident and add an incident update to it. Please note that Cachet 2.4.0 or above required if you wish to use incident updates.

$incidentDetails = ['name' => 'Test Incident '.rand(1, 99999), 'message' => 'Incident message '.rand(1, 99999), 'status' => 1, 'visible' => 1];

$incident = $cachetInstance->createIncident($incidentDetails);

$incidentUpdateDetails = ['status' => 2, 'message' => 'Test incident update '.rand(1, 99999)];

$incidentUpdate = $incident->createIncidentUpdate($incidentUpdateDetails);

echo $incidentUpdate->id.' - '.$incidentUpdate->incident_id.' - '.$incidentUpdate->status.' - '.$incidentUpdate->message;

Updating Cachet elements

The cachet.php allows you to make changes to a Cachet element and saving those changes back to your Cachet install. This is done by directly changing the Cachet element's public member variables, and then calling the object's save() method.

The following example shows how to change the name and status of a component, then save the changes.

// Get components
$components = $cachetInstance->getAllComponents();

// Change component details
$component[0]->name = 'My awesome component';
$component[0]->status = 1;
$component[0]->save();

Updating Component Status via Incident and Incident Updates

If you create an incident with a component_id, you can also include a component_status to change the component's status at the same time as creating the incident. See the example below.

$incidentDetails = ['name' => 'Test Incident '.rand(1, 99999), 'message' => 'Incident message '.rand(1, 99999), 'status' => 1, 'visible' => 1,
    'component_id' => 1, 'component_status' => 1];

$incident = $cachetInstance->createIncident($incidentDetails);

When creating incident updates, you can also specify a component_status to change the component's status when the incident update is created. See the example below.

$incidentUpdateDetails = ['status' => 2, 'message' => 'Test incident update '.rand(1, 99999), 'component_status' => 2];

$incidentUpdate = $incident->createIncidentUpdate($incidentUpdateDetails);

You can also change a component status via an incident or incident update by changing the component_status on the related object and saving, as shown below in the exmaples below. Note that this will only work if the incident was created with an assoicated component_id.

$incident->component_status = 2;
$incident->save();
$incidentUpdate->component_status = 3;
$incidentUpdate->save();

Deleting Cachet elements

To delete a Cachet element from your Cachet install, you simply need to call the delete() method on the appropriate Cachet element object.

For example, to delete an incident you could do the following.

// Get incidents
$incidents = $cachetInstance->getAllIncidents();

// Delete the first one
$incidents[0]->delete();

Contact

If you've found a bug or a new feature, please report it as a GitHub issue.

For other queries, I'm available on Twitter (Jordan Hall).

divineomega/cachetphp 适用场景与选型建议

divineomega/cachetphp 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 13.73k 次下载、GitHub Stars 达 37, 最近一次更新时间为 2015 年 05 月 20 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「page」 「javascript」 「api」 「library」 「JS」 「status」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 divineomega/cachetphp 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 divineomega/cachetphp 我们能提供哪些服务?
定制开发 / 二次开发

基于 divineomega/cachetphp 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 13.73k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 37
  • 点击次数: 13
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 37
  • Watchers: 3
  • Forks: 5
  • 开发语言: PHP

其他信息

  • 授权协议: LGPL-3.0-only
  • 更新时间: 2015-05-20