einenlum/php-stack-detector

A package to detect the PHP stack of a project

2.0.1 2023-11-06 15:24 UTC

This package is auto-updated.

Last update: 2024-11-06 17:53:21 UTC


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.

Supported Stacks for now:

  • Wordpress
  • Laravel
  • Symfony
  • Statamic
  • Craft CMS

Install

composer require einenlum/php-stack-detector

Usage

<?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

You can also use the CLI to test it.

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

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 'einenlum/private-repo'
Detected stack: laravel
Version: 10.19.0

Usage with Symfony

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

For Github:

services:
    Einenlum\PhpStackDetector\Factory\GithubDetectorFactory: ~

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

For local filesystem:

services:
    Einenlum\PhpStackDetector\Factory\FilesystemDetectorFactory: ~

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

Tests

composer run test

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!