ottosmops/beacon
Composer 安装命令:
composer require ottosmops/beacon
包简介
A PHP library for parsing BEACON files
关键字:
README 文档
README
BEACON Parser
A PHP library for parsing BEACON files.
The package was heavily constructed with AI assistence. Please doublecheck if you want to use it in production.
Overview
BEACON is a data interchange format for large numbers of uniform links. A BEACON file contains meta fields and links that can be used to create connections between different datasets.
This library provides a PHP implementation for parsing BEACON files according to the BEACON specification.
Features
- Parse BEACON files from strings or file paths
- Extract meta fields and link data
- Support for all BEACON meta fields (PREFIX, TARGET, MESSAGE, RELATION, etc.)
- Link construction with URI pattern expansion
- Validation of BEACON file format
Installation
Install via Composer:
composer require ottosmops/beacon
Quick Start
Basic Usage
use BeaconParser\BeaconParser; // Parse from file $parser = new BeaconParser(); $beaconData = $parser->parseFile('path/to/beacon.txt'); // Parse from string $beaconContent = file_get_contents('beacon.txt'); $beaconData = $parser->parseString($beaconContent); // Access meta fields $description = $beaconData->getMetaField('DESCRIPTION'); $prefix = $beaconData->getMetaField('PREFIX'); // Access links $links = $beaconData->getLinks(); foreach ($links as $link) { echo $link->getSourceIdentifier() . ' -> ' . $link->getTargetIdentifier() . "\n"; }
Working with Links
// Get constructed links with full URIs $links = $beaconData->getConstructedLinks(); foreach ($links as $link) { echo "Source: " . $link->getSourceIdentifier() . "\n"; echo "Target: " . $link->getTargetIdentifier() . "\n"; echo "Relation: " . $link->getRelationType() . "\n"; if ($link->hasAnnotation()) { echo "Annotation: " . $link->getAnnotation() . "\n"; } echo "---\n"; }
BEACON Format
BEACON files consist of:
- Format indicator (
#FORMAT: BEACON) - Meta fields (lines starting with
#) - Empty lines (optional)
- Link lines
Example BEACON File
#FORMAT: BEACON
#PREFIX: http://example.org/
#TARGET: http://example.com/
#DESCRIPTION: Example link dump
#CREATOR: Example Organization
alice|About Alice
bob||http://example.net/bob
charlie|Charlie's Page|http://example.net/charlie
Meta Fields
The library supports all standard BEACON meta fields:
Link Construction Fields
PREFIX- URI pattern for source identifiersTARGET- URI pattern for target identifiersMESSAGE- Default link annotationRELATION- Relation type for linksANNOTATION- Meaning of link annotations
Link Dump Description Fields
DESCRIPTION- Human readable descriptionCREATOR- Creator name or URICONTACT- Contact informationHOMEPAGE- Website with additional informationFEED- Download URLTIMESTAMP- Last modification dateUPDATE- Update frequency
Dataset Fields
SOURCESET- Source dataset URITARGETSET- Target dataset URINAME- Target dataset nameINSTITUTION- Responsible organization
Link Construction
The library automatically constructs full URIs from link tokens using the meta fields:
- Source identifiers are constructed using the
PREFIXpattern - Target identifiers are constructed using the
TARGETpattern - Default values are applied when tokens are missing
- URI patterns support
{ID}and{+ID}expansions
Validation
The library includes a validator to check BEACON files.
Using the Validator
use BeaconParser\BeaconParser; $validator = new BeaconValidator(); // Validate from file $result = $validator->validateFile('beacon.txt'); // Validate from string $result = $validator->validateString($beaconContent); // Check if valid if ($result->isValid()) { echo "BEACON file is valid!\n"; } else { echo "BEACON file has errors:\n"; foreach ($result->getErrors() as $error) { echo "- $error\n"; } } // Show detailed report echo $result->getDetailedReport();
CLI Validation Tool
A command-line validation tool is included that supports both local files and URLs:
# Validate a local BEACON file php bin/validate-beacon.php example.beacon # Validate a BEACON file from URL php bin/validate-beacon.php https://example.org/beacon.txt # Validate with full path php bin/validate-beacon.php /path/to/beacon/file.txt # Show help php bin/validate-beacon.php --help
The CLI tool automatically detects whether the input is a URL (starting with http:// or https://) or a local file path and handles the validation accordingly.
URL Validation Features
- Automatic URL Detection: Recognizes HTTP/HTTPS URLs automatically
- Secure Downloads: Uses proper HTTP headers and timeout handling
- Error Handling: Provides clear error messages for HTTP errors (404, 500, etc.)
- Content Type Support: Accepts text/plain and other text formats
Validation Features
The validator checks for:
- File Structure: Format indicator, proper line endings, UTF-8 encoding
- Meta Fields: Known fields, proper syntax, valid values
- URI Validation: Valid URIs in appropriate fields
- Link Construction: Proper token handling and URI building
- Best Practices: Recommended fields, HTTPS usage, proper formatting
- Specification Compliance: Full adherence to BEACON specification
Exit codes:
0: File is valid1: File has validation errors2: File not found or other error
Usage Examples
CLI Usage Examples
# Validate local files php bin/validate-beacon.php example.beacon php bin/validate-beacon.php /path/to/beacon/file.txt # Validate remote files from URLs php bin/validate-beacon.php https://example.org/beacon.txt php bin/validate-beacon.php http://data.example.com/links.beacon # Show detailed help php bin/validate-beacon.php --help
Programming Examples
Example BEACON File
## Examples
### Sample BEACON File
```beacon
#FORMAT: BEACON
#PREFIX: http://example.org/people/
#TARGET: http://example.com/documents/
#DESCRIPTION: Example BEACON file mapping people to documents
#CREATOR: Example Organization
#RELATION: http://purl.org/dc/elements/1.1/contributor
alice
bob|author
charlie|editor|special-doc
diana||http://example.net/external-doc
Parsing Example
use BeaconParser\BeaconValidator; $parser = new BeaconParser(); $beaconData = $parser->parseFile('example.beacon'); // Get meta information echo "Description: " . $beaconData->getMetaField('DESCRIPTION') . "\n"; echo "Creator: " . $beaconData->getMetaField('CREATOR') . "\n"; // Process links foreach ($beaconData->getConstructedLinks() as $link) { echo $link->getSourceIdentifier() . " -> " . $link->getTargetIdentifier() . "\n"; if ($link->hasAnnotation()) { echo " Annotation: " . $link->getAnnotation() . "\n"; } }
Expected Output
Description: Example BEACON file mapping people to documents
Creator: Example Organization
http://example.org/people/alice -> http://example.com/documents/alice
http://example.org/people/bob -> http://example.com/documents/bob
Annotation: author
http://example.org/people/charlie -> http://example.com/documents/special-doc
Annotation: editor
http://example.org/people/diana -> http://example.net/external-doc
Validation Examples
use BeaconParser\BeaconValidator; $validator = new BeaconValidator(); // Validate a local file $result = $validator->validateFile('beacon.txt'); // Validate content from a URL programmatically $content = file_get_contents('https://example.org/beacon.txt'); $result = $validator->validateString($content); // Check validation results if ($result->isValid()) { echo "✅ BEACON file is valid\n"; } else { echo "❌ BEACON file has errors:\n"; foreach ($result->getErrors() as $error) { echo " - $error\n"; } } // Show detailed report echo $result->getDetailedReport();
Requirements
- PHP 8.0 or higher
Development
Running Tests
composer test
Code Style
composer phpcs
Static Analysis
composer phpstan
License
MIT License. See LICENSE file for details.
Links
ottosmops/beacon 适用场景与选型建议
ottosmops/beacon 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 08 月 17 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「library」 「parser」 「beacon」 「link-dump」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 ottosmops/beacon 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 ottosmops/beacon 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 ottosmops/beacon 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Easy to use SDK with grabber for multiple platforms at once like YouTube, Dailymotion, Facebook and more.
An MT940 bank statement parser for PHP
Inbox pattern process implementation for your Laravel Applications
Laravel integration for the jsonapi.org parser
A modern HTML DOM parser for PHP using DOMDocument and Symfony CssSelector.
Core library that defines common interfaces used by the rest of the intahwebz..
统计信息
- 总下载量: 2
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 19
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-08-17