einenlum/php-stack-detector

一个用于检测项目PHP栈的包

2.0.1 2023-11-06 15:24 UTC

This package is auto-updated.

Last update: 2024-09-06 17:31:41 UTC


README

这个库允许在解析目录或GitHub远程仓库时轻松检测PHP栈(Wordpress、Laravel、Symfony…)及其版本。

目前支持的栈

  • Wordpress
  • Laravel
  • Symfony
  • Statamic
  • Craft CMS

安装

composer require einenlum/php-stack-detector

使用

<?php

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

use Einenlum\PhpStackDetector\Detector;
use Einenlum\PhpStackDetector\Factory\FilesystemDetectorFactory;
use Einenlum\PhpStackDetector\Factory\GithubDetectorFactory;
use Einenlum\PhpStackDetector\StackType;

// Local usage

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

$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();

$stack = $detector->getStack('symfony/demo');

$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();

$stack = $detector->getStack('einenlum/private-repo');

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

您也可以使用CLI进行测试。

php bin/detect-local.php ~/Prog/php/my_project/
Detected stack: laravel
Version: 10.19.0

php bin/detect-github.php 'symfony/demo'
Detected stack: symfony
Version: 6.3.0

建议在解析GitHub时使用访问令牌,以访问私有仓库或避免达到GitHub API限制。

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

使用Symfony

在您的services.yaml文件中声明工厂和检测器。

对于GitHub

services:
    Einenlum\PhpStackDetector\Factory\GithubDetectorFactory: ~

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

对于本地文件系统

services:
    Einenlum\PhpStackDetector\Factory\FilesystemDetectorFactory: ~

    Einenlum\PhpStackDetector\Detector:
        factory: ['@Einenlum\PhpStackDetector\Factory\FilesystemDetectorFactory', 'create']

测试

composer run test

贡献

每个栈都有自己的检测器,实现了StackDetectorInterface接口。如果栈使用composer,您可以使用PackageVersionProvider类。这将使用ComposerConfigProvider获取锁定文件或json配置。

它们都使用适配器,目前要么是FilesystemAdapter,要么是GithubAdapter

您可以添加自己的StackDetector,然后将其添加到HasStackDetectors特质的getStackDetectors方法中。

欢迎任何Pull Request!