tschope/flightlogger-php 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

tschope/flightlogger-php

Composer 安装命令:

composer require tschope/flightlogger-php

包简介

A PHP SDK for integrating with FlightLogger GraphQL API using Saloon

README 文档

README

A PHP SDK for integrating with FlightLogger's GraphQL API, built using Saloon.

Description

This package simplifies integration with the FlightLogger API, allowing you to fetch information about users, students, classes, flights, and training in a simple and intuitive way.

Installation

You can install the package via Composer:

composer require tschope/flightlogger-php

Requirements

  • PHP 8.1 or higher
  • cURL extension enabled

Configuration

Basic Configuration (Pure PHP)

First, you need to obtain your API token from FlightLogger. Then, you can initialize the connector in three ways:

1. Passing the token directly to the constructor

<?php

use Tschope\FlightLogger\FlightLoggerConnector;

$connector = new FlightLoggerConnector('your-api-token-here');

2. Using environment variable

Add to your .env file:

FLIGHTLOGGER_API_TOKEN=your-api-token-here

Then initialize without passing the token:

<?php

use Tschope\FlightLogger\FlightLoggerConnector;

// Token will be automatically read from environment variable
$connector = new FlightLoggerConnector();

Laravel Configuration

The package has automatic integration with Laravel through auto-discovery.

1. Publish the configuration file (optional)

php artisan vendor:publish --tag=flightlogger-config

This will create the config/flightlogger.php file.

2. Configure in .env

Add to your .env file:

FLIGHTLOGGER_API_TOKEN=your-api-token-here

3. Use via Dependency Injection

In Laravel, you can inject the connector directly into your controllers or services:

<?php

namespace App\Http\Controllers;

use Tschope\FlightLogger\FlightLoggerConnector;
use Tschope\FlightLogger\Requests\Users\GetUsersRequest;

class FlightLoggerController extends Controller
{
    public function __construct(
        protected FlightLoggerConnector $flightLogger
    ) {}

    public function index()
    {
        $request = new GetUsersRequest(['limit' => 10]);
        $response = $this->flightLogger->send($request);

        return response()->json($response->json());
    }
}

4. Or use Laravel's helper

use Tschope\FlightLogger\FlightLoggerConnector;

$connector = app(FlightLoggerConnector::class);

Configuration Priority

The package looks for the API token in the following order:

  1. Token passed to constructor (highest priority)
  2. Laravel configuration file (config/flightlogger.php)
  3. Environment variable FLIGHTLOGGER_API_TOKEN

If no token is found, a RuntimeException will be thrown.

Usage

Fetching Users

List all users

use Tschope\FlightLogger\Requests\Users\GetUsersRequest;

$request = new GetUsersRequest([
    'limit' => 50,
    'offset' => 0,
    'orderBy' => 'firstName',
    'orderDirection' => 'ASC'
]);

$response = $connector->send($request);
$data = $response->json();

// Accessing data
foreach ($data['data']['users']['edges'] as $edge) {
    $user = $edge['node'];
    echo "Name: {$user['firstName']} {$user['lastName']}\n";
    echo "Email: {$user['email']}\n";
}

Fetch a specific user

use Tschope\FlightLogger\Requests\Users\GetUserRequest;

$request = new GetUserRequest('user-id-here');
$response = $connector->send($request);
$user = $response->json()['data']['user'];

echo "Name: {$user['firstName']} {$user['lastName']}\n";

Fetching Classes (Groups of Students)

use Tschope\FlightLogger\Requests\Classes\GetClassesRequest;

$request = new GetClassesRequest([
    'first' => 20
]);

$response = $connector->send($request);
$data = $response->json();

foreach ($data['data']['classes']['edges'] as $edge) {
    $class = $edge['node'];
    echo "Class: {$class['name']}\n";

    // Listing students in the class
    foreach ($class['users'] as $student) {
        echo "  - {$student['firstName']} {$student['lastName']}\n";
    }
}

Fetching Flights

use Tschope\FlightLogger\Requests\Flights\GetFlightsRequest;

$request = new GetFlightsRequest([
    'first' => 50,
    'from' => '2024-01-01T00:00:00Z',
    'to' => '2024-12-31T23:59:59Z'
]);

$response = $connector->send($request);
$data = $response->json();

foreach ($data['data']['flights']['edges'] as $edge) {
    $flight = $edge['node'];
    echo "Flight: {$flight['id']}\n";
    echo "Type: {$flight['flightType']}\n";
    echo "Aircraft: {$flight['aircraft']['registration']}\n";
    echo "From: {$flight['departureAirport']['icao']} to {$flight['arrivalAirport']['icao']}\n";

    // Pilot/instructor information
    if (!empty($flight['primaryLog'])) {
        $pilot = $flight['primaryLog']['user'];
        echo "Pilot: {$pilot['firstName']} {$pilot['lastName']}\n";
    }

    echo "\n";
}

Fetching Trainings

use Tschope\FlightLogger\Requests\Trainings\GetTrainingsRequest;

$request = new GetTrainingsRequest([
    'first' => 30,
    'status' => ['APPROVED', 'PENDING']
]);

$response = $connector->send($request);
$data = $response->json();

foreach ($data['data']['trainings']['edges'] as $edge) {
    $training = $edge['node'];
    echo "Training: {$training['name']}\n";
    echo "Status: {$training['status']}\n";

    // Instructor information
    if (!empty($training['instructor'])) {
        echo "Instructor: {$training['instructor']['firstName']} {$training['instructor']['lastName']}\n";
    }

    // Student information
    if (!empty($training['student'])) {
        echo "Student: {$training['student']['firstName']} {$training['student']['lastName']}\n";
    }

    // Total time
    $hours = $training['totalSeconds'] / 3600;
    echo "Total hours: " . number_format($hours, 2) . "h\n";

    echo "\n";
}

Custom Fields

You can customize the fields returned by queries by passing an array of fields as the second parameter:

$request = new GetUsersRequest(
    ['limit' => 10],
    ['id', 'firstName', 'lastName', 'email', 'phone']
);

For nested fields (like relationships), use GraphQL syntax:

$request = new GetFlightsRequest(
    ['first' => 10],
    [
        'id',
        'flightType',
        'aircraft {
          registration
          model
          type
        }',
        'primaryLog {
          user {
            firstName
            lastName
          }
          role
        }'
    ]
);

Pagination

FlightLogger queries use cursor-based pagination. You can navigate through results using the after, before, first, and last parameters:

// First page
$request = new GetFlightsRequest(['first' => 20]);
$response = $connector->send($request);
$data = $response->json();

// Check if there's a next page
if ($data['data']['flights']['pageInfo']['hasNextPage']) {
    $endCursor = $data['data']['flights']['pageInfo']['endCursor'];

    // Fetch next page
    $nextRequest = new GetFlightsRequest([
        'first' => 20,
        'after' => $endCursor
    ]);
    $nextResponse = $connector->send($nextRequest);
}

Error Handling

try {
    $response = $connector->send($request);

    if ($response->failed()) {
        echo "Request error: " . $response->status() . "\n";
        echo $response->body();
    } else {
        $data = $response->json();
        // Process data
    }
} catch (\Exception $e) {
    echo "Error: " . $e->getMessage() . "\n";
}

Contributing

Contributions are welcome! This package was created to start with the most common endpoints, but the FlightLogger API has many other available endpoints.

To add new endpoints:

  1. Create a new class that extends GraphQLRequest or GraphQLMutation
  2. Implement the getQuery() (or getMutation()) and getVariables() methods
  3. Define default fields in getDefaultFields()
  4. Add tests and documentation

Example structure for a new endpoint:

<?php

namespace Tschope\FlightLogger\Requests\ResourceName;

use Tschope\FlightLogger\Requests\GraphQLRequest;

class GetResourceRequest extends GraphQLRequest
{
    protected array $filters;
    protected array $fields;

    public function __construct(array $filters = [], array $fields = null)
    {
        $this->filters = $filters;
        $this->fields = $fields ?? $this->getDefaultFields();
    }

    protected function getQuery(): string
    {
        // Your GraphQL query here
    }

    protected function getVariables(): array
    {
        return $this->filters;
    }

    protected function getDefaultFields(): array
    {
        // Default query fields
    }
}

License

This package is open-source and available under the MIT license.

Links

Support

If you encounter any issues or have suggestions, please open an issue on GitHub.

Credits

Developed by tschope

This is an unofficial package and is not affiliated with FlightLogger.

tschope/flightlogger-php 适用场景与选型建议

tschope/flightlogger-php 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.62k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 11 月 16 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 tschope/flightlogger-php 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-11-16