Skip to content

Commit f12222a

Browse files
authored
Merge pull request #1 from Einenlum/2.x
Version 2
2 parents c45f95e + 5945595 commit f12222a

31 files changed

+829
-1850
lines changed

Diff for: .gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
composer.phar
2+
composer.lock
23
/vendor/
34

45
# Commit your application's lock file https://getcomposer.org/doc/01-basic-usage.md#commit-your-composer-lock-file-to-version-control

Diff for: README.md

+70-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# PHP Stack Detector
22

3-
This library allows to easily detect the PHP stack (Wordpress, Laravel, Symfony…) and the version used, when parsing a directory.
3+
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.
44

55
Supported Stacks for now:
66

@@ -24,9 +24,14 @@ composer require einenlum/php-stack-detector
2424
require_once __DIR__ . '/vendor/autoload.php';
2525

2626
use Einenlum\PhpStackDetector\Detector;
27+
use Einenlum\PhpStackDetector\Factory\FilesystemDetectorFactory;
28+
use Einenlum\PhpStackDetector\Factory\GithubDetectorFactory;
2729
use Einenlum\PhpStackDetector\StackType;
2830

29-
$detector = Detector::create();
31+
// Local usage
32+
33+
$factory = new FilesystemDetectorFactory();
34+
$detector = $factory->create();
3035
$stack = $detector->getStack('/path/to/a/symfony/directory');
3136

3237
$stack->type === StackType::SYMFONY;
@@ -38,16 +43,74 @@ $stack->version; // null
3843

3944
$stack = $detector->getStack('/path/to/an/unknown/stack/directory');
4045
$stack; // null
46+
47+
// For Github usage
48+
49+
$factory = new GithubDetectorFactory();
50+
$detector = $factory->create();
51+
52+
$stack = $detector->getStack('symfony/demo');
53+
54+
$stack->type === StackType::SYMFONY;
55+
$stack->version; // 6.3.0
56+
57+
// You can also pass an already authenticated Github Client
58+
$client = new \Github\Client();
59+
$client->authenticate('some_access_token', null, \Github\AuthMethod::ACCESS_TOKEN);
60+
$detector = $factory->create();
61+
62+
$stack = $detector->getStack('einenlum/private-repo');
63+
64+
$stack->type === StackType::SYMFONY;
65+
$stack->version; // 6.3.0
4166
```
4267

4368
You can also use the CLI to test it.
4469

4570
```
46-
php bin/detect.php ~/Prog/php/my_project/
71+
php bin/detect-local.php ~/Prog/php/my_project/
72+
Detected stack: laravel
73+
Version: 10.19.0
74+
75+
php bin/detect-github.php 'symfony/demo'
76+
Detected stack: symfony
77+
Version: 6.3.0
78+
```
79+
80+
It is advised to use an access token for github parsing, to either access private repositories or avoid reaching Github API limit.
81+
82+
```
83+
GITHUB_ACCESS_TOKEN=my_token php bin/detect-github.php 'einenlum/private-repo'
4784
Detected stack: laravel
4885
Version: 10.19.0
4986
```
5087

88+
### Usage with Symfony
89+
90+
Declare the factory and the detector in your `services.yaml` file.
91+
92+
For Github:
93+
94+
```yaml
95+
services:
96+
Einenlum\PhpStackDetector\Factory\GithubDetectorFactory: ~
97+
98+
Einenlum\PhpStackDetector\Detector:
99+
factory: ['@Einenlum\PhpStackDetector\Factory\GithubDetectorFactory', 'create']
100+
arguments:
101+
$client: '@Github\Client'
102+
```
103+
104+
For local filesystem:
105+
106+
```yaml
107+
services:
108+
Einenlum\PhpStackDetector\Factory\FilesystemDetectorFactory: ~
109+
110+
Einenlum\PhpStackDetector\Detector:
111+
factory: ['@Einenlum\PhpStackDetector\Factory\FilesystemDetectorFactory', 'create']
112+
```
113+
51114
## Tests
52115
53116
```
@@ -58,7 +121,10 @@ composer run test
58121

59122
Each stack has its own Detector implementing a [StackDetectorInterface](src/StackDetectorInterface.php).
60123
If the stack uses composer you can use the [PackageVersionProvider](src/Composer/PackageVersionProvider.php) class.
124+
This will use a [ComposerConfigProvider](src/Composer/ComposerConfigProvider.php) to get the lock or the json config.
125+
126+
All of them use an Adapter, that is for now either [FilesystemAdapter](src/DirectoryCrawler/FilesystemAdapter.php) or [GithubAdapter](src/DirectoryCrawler/GithubAdapter.php)
61127

62-
You can add your own StackDetector and then add it to the `create` method of the [Detector](src/Detector.php).
128+
You can add your own StackDetector and then add it to the `getStackDetectors` method of the [HasStackDetectors](src/Factory/HasStackDetectors.php) trait.
63129

64130
Any Pull Request welcome!

Diff for: bin/detect-github.php

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
require_once __DIR__ . '/../vendor/autoload.php';
4+
5+
use Einenlum\PhpStackDetector\Detector;
6+
use Einenlum\PhpStackDetector\Factory\GithubDetectorFactory;
7+
use Github\AuthMethod;
8+
9+
$factory = new GithubDetectorFactory();
10+
11+
$accessToken = getenv('GITHUB_ACCESS_TOKEN');
12+
if ($accessToken) {
13+
$client = new \Github\Client();
14+
$client->authenticate($accessToken, null, AuthMethod::ACCESS_TOKEN);
15+
$detector = $factory->create($client);
16+
} else {
17+
$detector = $factory->create();
18+
}
19+
20+
$directory = $argv[1] ?? null;
21+
if (null === $directory) {
22+
echo 'Please provide a directory to scan' . "\n";
23+
exit(1);
24+
}
25+
26+
$subDirectory = $argv[2] ?? null;
27+
28+
$stack = $detector->getStack($directory, $subDirectory);
29+
30+
if (null === $stack) {
31+
echo 'No stack detected' . "\n";
32+
exit(0);
33+
}
34+
35+
echo 'Detected stack: ' . $stack->type->value . "\n";
36+
echo 'Version: ' . ($stack->version ?: 'Unknown version') . "\n";

Diff for: bin/detect.php renamed to bin/detect-local.php

+7-4
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,20 @@
22

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

5-
use Einenlum\PhpStackDetector\Detector;
5+
use Einenlum\PhpStackDetector\Factory\FilesystemDetectorFactory;
66

7-
$detector = Detector::create();
7+
$factory = new FilesystemDetectorFactory();
8+
$detector = $factory->create();
89

910
$directory = $argv[1] ?? null;
10-
if (null === $directory || !is_dir($directory)) {
11+
if (null === $directory) {
1112
echo 'Please provide a directory to scan' . "\n";
1213
exit(1);
1314
}
1415

15-
$stack = $detector->getStack($directory);
16+
$subDirectory = $argv[2] ?? null;
17+
18+
$stack = $detector->getStack($directory, $subDirectory);
1619

1720
if (null === $stack) {
1821
echo 'No stack detected' . "\n";

Diff for: composer.json

+9-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@
2424
"phpstan/phpstan": "^1.10"
2525
},
2626
"require": {
27-
"einenlum/composer-version-parser": "^1.0"
27+
"einenlum/composer-version-parser": "^1.0",
28+
"knplabs/github-api": "^3.12",
29+
"symfony/http-client": "^6.3",
30+
"nyholm/psr7": "^1.8"
2831
},
2932
"scripts": {
3033
"unit-test": "phpunit tests",
@@ -33,5 +36,10 @@
3336
"@unit-test",
3437
"@static-analysis"
3538
]
39+
},
40+
"config": {
41+
"allow-plugins": {
42+
"php-http/discovery": true
43+
}
3644
}
3745
}

0 commit comments

Comments
 (0)