定制 einenlum/php-stack-detector 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

einenlum/php-stack-detector

Composer 安装命令:

composer require einenlum/php-stack-detector

包简介

A package to detect the PHP stack and node config of a project

README 文档

README

This library allows to easily detect the PHP stack (Wordpress, Laravel, Symfony…) and the version used, when parsing a directory or ar Github remote repository.

The library is still in an early stage and subject to changes, but it follows SemVer. As long as you stick to a major version, you should be safe.

Supported Stacks for now:

  • Bolt CMS
  • Cakephp
  • Codeigniter
  • Craft CMS
  • Drupal
  • Grav CMS
  • Kirby CMS
  • Laravel
  • Leaf PHP
  • Lunar
  • October CMS
  • Shopware
  • Statamic
  • Symfony
  • Tempest
  • Twill
  • Typo3 CMS
  • Winter CMS
  • Wordpress

It also detects if the repository uses nodeJS. If so, it detects node version (through .nvmrc or .node-version), node version requirements, and package manager.

Detected package managers:

  • npm
  • pnpm
  • bun
  • yarn
  • yarn berry

Install

composer require fortrabbit/stack-detector

Usage

<?php

require_once __DIR__ . '/vendor/autoload.php';

use fortrabbit\StackDetector\Detector;
use fortrabbit\StackDetector\Factory\FilesystemDetectorFactory;
use fortrabbit\StackDetector\Factory\GithubDetectorFactory;
use fortrabbit\StackDetector\Enum\StackType;

// Local usage

$factory = new FilesystemDetectorFactory();
$detector = $factory->create();
$config = $detector->getFullConfiguration('/path/to/a/symfony/directory');

$phpConfiguration = $config->phpConfiguration;

// value from config platform php
$phpConfiguration->phpVersion->version; // 8.3
// value from require php
$phpConfiguration->phpVersion->requirements; // ^7.2|^8.0
$phpConfiguration->requiredExtensions; // ['intl', 'curl']

$stack = $config->stack;

$stack->type === StackType::SYMFONY;
$stack->version; // 5.4

$stack = $detector->getStack('/path/to/an/unknown/symfony/version/directory');
$stack->type === StackType::SYMFONY;
$stack->version; // null

$stack = $detector->getStack('/path/to/an/unknown/stack/directory');
$stack; // null

// For Github usage

$factory = new GithubDetectorFactory();
$detector = $factory->create();

$config = $detector->getFullConfiguration('/path/to/a/symfony/directory');

$phpConfiguration = $config->phpConfiguration;
// value from config platform php
$phpConfiguration->phpVersion->version; // 8.3
// value from require php
$phpConfiguration->phpVersion->requirements; // ^7.2|^8.0
$phpConfiguration->requiredExtensions; // ['intl', 'curl']

$stack = $config->stack;

$stack->type === StackType::SYMFONY;
$stack->version; // 6.3.0

// You can also pass an already authenticated Github Client
$client = new \Github\Client();
$client->authenticate('some_access_token', null, \Github\AuthMethod::ACCESS_TOKEN);
$detector = $factory->create($client);

$config = $detector->getFullConfiguration('fortrabbit/private-repo');

// optionally: detect the stack on a specific branch 
$config = $detector->getFullConfiguration('fortrabbit/private-repo:branch-name');

You can also use the CLI to test it.

php bin/detect-local.php ~/Prog/php/my_project/
Detected PHP version: Unknown version
Detected PHP requirements: ^8.4
Required extensions: ctype, iconv, redis, sodium
Detected stack: laravel
Version: 10.19.0

Detected Node.js version: 22.0
Detected Node.js requirements: Unknown version
Package Manager: bun

php bin/detect-github.php 'symfony/demo'
Detected PHP version: 8.4
Detected PHP requirements: ^8.3
Required extensions: ctype, iconv
Detected stack: symfony
Version: 6.3.0

Detected Node.js version: Unknown version
Detected Node.js requirements: Unknown version
Package Manager: npm

It is advised to use an access token for github parsing, to either access private repositories or avoid reaching Github API limit.

GITHUB_ACCESS_TOKEN=my_token php bin/detect-github.php 'fortrabbit/private-repo'
Detected stack: laravel
Version: 10.19.0

Detected Node.js version: Unknown version
Detected Node.js requirements: Unknown version
Package Manager: npm

Usage with Symfony

Declare the factory and the detector in your services.yaml file.

For Github:

services:
    fortrabbit\StackDetector\Factory\GithubDetectorFactory: ~

    fortrabbit\StackDetector\Detector:
        factory: ['@fortrabbit\StackDetector\Factory\GithubDetectorFactory', 'create']
        arguments:
            $client: '@Github\Client'

For local filesystem:

services:
    fortrabbit\StackDetector\Factory\FilesystemDetectorFactory: ~

    fortrabbit\StackDetector\Detector:
        factory: ['@fortrabbit\StackDetector\Factory\FilesystemDetectorFactory', 'create']

Tests

composer run test

Coding standards

# check CS
composer run test-cs
# fix CS
composer run fix-cs

Run interactive shell with docker

docker run --rm -it -v "$PWD":/app -w /app  php:8.4-cli bash

Contribute

Each stack has its own Detector implementing a StackDetectorInterface. If the stack uses composer you can use the PackageVersionProvider class. This will use a ComposerConfigProvider to get the lock or the json config.

All of them use an Adapter, that is for now either FilesystemAdapter or GithubAdapter

You can add your own StackDetector and then add it to the getStackDetectors method of the HasStackDetectors trait.

Any Pull Request welcome!

einenlum/php-stack-detector 适用场景与选型建议

einenlum/php-stack-detector 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 14 次下载、GitHub Stars 达 4, 最近一次更新时间为 2023 年 10 月 13 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 4
  • Watchers: 1
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-10-13