Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ Supported Stacks for now:
- 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

```
Expand All @@ -39,7 +49,7 @@ require_once __DIR__ . '/vendor/autoload.php';
use Einenlum\PhpStackDetector\Detector;
use Einenlum\PhpStackDetector\Factory\FilesystemDetectorFactory;
use Einenlum\PhpStackDetector\Factory\GithubDetectorFactory;
use Einenlum\PhpStackDetector\StackType;
use Einenlum\PhpStackDetector\Enum\StackType;

// Local usage

Expand Down Expand Up @@ -107,12 +117,20 @@ 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.
Expand All @@ -121,6 +139,10 @@ It is advised to use an access token for github parsing, to either access privat
GITHUB_ACCESS_TOKEN=my_token php bin/detect-github.php 'einenlum/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
Expand Down
7 changes: 7 additions & 0 deletions bin/detect-github.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,10 @@

echo 'Detected stack: '.$stack->type->value."\n";
echo 'Version: '.($stack->version ?: 'Unknown version')."\n";

$nodeConfig = $config->nodeConfiguration;

echo "\n";
echo 'Detected Node.js version: '.($nodeConfig?->version ?: 'Unknown version')."\n";
echo 'Detected Node.js requirements: '.($nodeConfig?->requirements ?: 'Unknown version')."\n";
echo 'Package Manager: '.($nodeConfig?->packageManager?->value ?: 'Unknown')."\n";
7 changes: 7 additions & 0 deletions bin/detect-local.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,10 @@

echo 'Detected stack: '.$stack->type->value."\n";
echo 'Version: '.($stack->version ?: 'Unknown version')."\n";

$nodeConfig = $config->nodeConfiguration;

echo "\n";
echo 'Detected Node.js version: '.($nodeConfig?->version ?: 'Unknown version')."\n";
echo 'Detected Node.js requirements: '.($nodeConfig?->requirements ?: 'Unknown version')."\n";
echo 'Package Manager: '.($nodeConfig?->packageManager?->value ?: 'Unknown')."\n";
6 changes: 2 additions & 4 deletions lib/Composer/ComposerConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Einenlum\PhpStackDetector\Composer;

use Einenlum\PhpStackDetector\DTO\Composer\ComposerConfig;
use Einenlum\PhpStackDetector\DTO\Enum\ComposerConfigType;
use Einenlum\PhpStackDetector\DirectoryCrawler\AdapterInterface;
use Einenlum\PhpStackDetector\Exception\CacheMissException;
use Einenlum\PhpStackDetector\Exception\ResourceNotFoundException;
Expand All @@ -22,10 +24,6 @@ public function __construct(
) {
}

/**
* This returns the content of the composer lock file if it exists,
* otherwise the composer json file if it exists, otherwise null.
*/
public function getComposerConfig(
ComposerConfigType $type,
string $baseUri,
Expand Down
3 changes: 3 additions & 0 deletions lib/Composer/PackageVersionProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

namespace Einenlum\PhpStackDetector\Composer;

use Einenlum\PhpStackDetector\DTO\Composer\PackageVersion;
use Einenlum\PhpStackDetector\DTO\Enum\ComposerConfigType;

readonly class PackageVersionProvider
{
public function __construct(private ComposerConfigProvider $configProvider)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

declare(strict_types=1);

namespace Einenlum\PhpStackDetector\Composer;
namespace Einenlum\PhpStackDetector\DTO\Composer;

use Einenlum\PhpStackDetector\DTO\Enum\ComposerConfigType;

readonly class ComposerConfig
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Einenlum\PhpStackDetector\Composer;
namespace Einenlum\PhpStackDetector\DTO\Composer;

use Einenlum\ComposerVersionParser\Parser;

Expand Down
2 changes: 1 addition & 1 deletion lib/DependencyTree.php → lib/DTO/DependencyTree.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Einenlum\PhpStackDetector;
namespace Einenlum\PhpStackDetector\DTO;

use Einenlum\PhpStackDetector\StackDetector\LunarDetector;
use Einenlum\PhpStackDetector\StackDetector\OctoberCMSDetector;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Einenlum\PhpStackDetector\Composer;
namespace Einenlum\PhpStackDetector\DTO\Enum;

enum ComposerConfigType: string
{
Expand Down
3 changes: 2 additions & 1 deletion lib/FullConfiguration.php → lib/DTO/FullConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

declare(strict_types=1);

namespace Einenlum\PhpStackDetector;
namespace Einenlum\PhpStackDetector\DTO;

readonly class FullConfiguration
{
public function __construct(
public PhpConfiguration $phpConfiguration,
public ?NodeConfiguration $nodeConfiguration,
public ?Stack $stack,
) {
}
Expand Down
17 changes: 17 additions & 0 deletions lib/DTO/NodeConfiguration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Einenlum\PhpStackDetector\DTO;

use Einenlum\PhpStackDetector\Enum\NodePackageManagerType;

readonly class NodeConfiguration
{
public function __construct(
public ?string $version,
public ?string $requirements,
public NodePackageManagerType $packageManager,
) {
}
}
2 changes: 1 addition & 1 deletion lib/PhpConfiguration.php → lib/DTO/PhpConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Einenlum\PhpStackDetector;
namespace Einenlum\PhpStackDetector\DTO;

readonly class PhpConfiguration
{
Expand Down
2 changes: 1 addition & 1 deletion lib/PhpVersion.php → lib/DTO/PhpVersion.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Einenlum\PhpStackDetector;
namespace Einenlum\PhpStackDetector\DTO;

readonly class PhpVersion
{
Expand Down
4 changes: 3 additions & 1 deletion lib/Stack.php → lib/DTO/Stack.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

declare(strict_types=1);

namespace Einenlum\PhpStackDetector;
namespace Einenlum\PhpStackDetector\DTO;

use Einenlum\PhpStackDetector\Enum\StackType;

readonly class Stack
{
Expand Down
13 changes: 13 additions & 0 deletions lib/Detector.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,17 @@

namespace Einenlum\PhpStackDetector;

use Einenlum\PhpStackDetector\DTO\FullConfiguration;
use Einenlum\PhpStackDetector\DTO\NodeConfiguration;
use Einenlum\PhpStackDetector\DTO\PhpConfiguration;
use Einenlum\PhpStackDetector\DTO\Stack;

readonly class Detector
{
/** @param StackDetectorInterface[] $stackDetectors */
public function __construct(
private PhpConfigurationDetector $phpConfigurationDetector,
private NodeConfigurationDetector $nodeConfigurationDetector,
private array $stackDetectors,
) {
}
Expand All @@ -18,10 +24,12 @@ public function getFullConfiguration(string $baseUri, ?string $subFolder = null)
$subFolder = $this->cleanSubFolder($subFolder);

$phpConfiguration = $this->getPhpConfiguration($baseUri, $subFolder);
$nodeConfiguration = $this->getNodeConfiguration($baseUri, $subFolder);
$stack = $this->getStack($baseUri, $subFolder);

return new FullConfiguration(
$phpConfiguration,
$nodeConfiguration,
$stack,
);
}
Expand All @@ -31,6 +39,11 @@ private function getPhpConfiguration(string $baseUri, ?string $subFolder = null)
return $this->phpConfigurationDetector->getPhpConfiguration($baseUri, $subFolder);
}

private function getNodeConfiguration(string $baseUri, ?string $subFolder = null): ?NodeConfiguration
{
return $this->nodeConfigurationDetector->getNodeConfiguration($baseUri, $subFolder);
}

/**
* @param string $baseUri The base URI of the project, e.g.
* /some/path/to/local/project
Expand Down
2 changes: 2 additions & 0 deletions lib/DirectoryCrawler/AdapterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ interface AdapterInterface
/** @throws ResourceNotFoundException */
public function getFileContent(string $baseUri, ?string ...$pathTree): string;

public function fileExists(string $baseUri, ?string ...$pathTree): bool;

public function directoryExists(string $baseUri, ?string ...$pathTree): bool;

/** @return array<int, string|null> */
Expand Down
7 changes: 7 additions & 0 deletions lib/DirectoryCrawler/FilesystemAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ public function getFileContent(string $baseUri, ?string ...$pathTree): string
return $content;
}

public function fileExists(string $baseUri, ?string ...$pathTree): bool
{
$fullUri = $this->getFullUri($baseUri, $pathTree);

return is_file($fullUri);
}

public function directoryExists(string $baseUri, ?string ...$pathTree): bool
{
$fullUri = $this->getFullUri($baseUri, $pathTree);
Expand Down
18 changes: 18 additions & 0 deletions lib/DirectoryCrawler/GithubAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,24 @@ public function getFileContent(string $baseUri, ?string ...$pathTree): string
return base64_decode($content['content']);
}

public function fileExists(string $baseUri, ?string ...$pathTree): bool
{
$config = $this->splitBaseUri($baseUri);

try {
$this->client->repo()->contents()->show(
$config['organization'],
$config['repository'],
$this->getPathTreeAsString($pathTree),
$config['reference']
);

return true;
} catch (RuntimeException $e) {
return false;
}
}

public function directoryExists(string $baseUri, ?string ...$pathTree): bool
{
$config = $this->splitBaseUri($baseUri);
Expand Down
14 changes: 14 additions & 0 deletions lib/Enum/NodePackageManagerType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace Einenlum\PhpStackDetector\Enum;

enum NodePackageManagerType: string
{
case NPM = 'npm';
case YARN = 'yarn';
case YARN_BERRY = 'yarn-berry';
case PNPM = 'pnpm';
case BUN = 'bun';
}
2 changes: 1 addition & 1 deletion lib/StackType.php → lib/Enum/StackType.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Einenlum\PhpStackDetector;
namespace Einenlum\PhpStackDetector\Enum;

enum StackType: string
{
Expand Down
14 changes: 13 additions & 1 deletion lib/Factory/FilesystemDetectorFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use Einenlum\PhpStackDetector\Composer\ComposerConfigProvider;
use Einenlum\PhpStackDetector\Detector;
use Einenlum\PhpStackDetector\DirectoryCrawler\FilesystemAdapter;
use Einenlum\PhpStackDetector\Node\PackageJsonProvider;
use Einenlum\PhpStackDetector\NodeConfigurationDetector;
use Einenlum\PhpStackDetector\PhpConfigurationDetector;

class FilesystemDetectorFactory
Expand All @@ -19,8 +21,18 @@ public function create(): Detector
$composerConfigProvider = new ComposerConfigProvider($adapter);

$phpConfigurationDetector = new PhpConfigurationDetector($composerConfigProvider);

$packageJsonProvider = new PackageJsonProvider($adapter);
$nodeConfigurationDetector = new NodeConfigurationDetector(
$packageJsonProvider,
$adapter
);
$stackDetectors = $this->getStackDetectors($composerConfigProvider, $adapter);

return new Detector($phpConfigurationDetector, $stackDetectors);
return new Detector(
$phpConfigurationDetector,
$nodeConfigurationDetector,
$stackDetectors
);
}
}
14 changes: 13 additions & 1 deletion lib/Factory/GithubDetectorFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use Einenlum\PhpStackDetector\Composer\ComposerConfigProvider;
use Einenlum\PhpStackDetector\Detector;
use Einenlum\PhpStackDetector\DirectoryCrawler\GithubAdapter;
use Einenlum\PhpStackDetector\Node\PackageJsonProvider;
use Einenlum\PhpStackDetector\NodeConfigurationDetector;
use Einenlum\PhpStackDetector\PhpConfigurationDetector;
use Github\Client;

Expand All @@ -21,8 +23,18 @@ public function create(?Client $client = null): Detector
$composerConfigProvider = new ComposerConfigProvider($adapter);
$phpConfigurationDetector = new PhpConfigurationDetector($composerConfigProvider);

$packageJsonProvider = new PackageJsonProvider($adapter);
$nodeConfigurationDetector = new NodeConfigurationDetector(
$packageJsonProvider,
$adapter
);

$stackDetectors = $this->getStackDetectors($composerConfigProvider, $adapter);

return new Detector($phpConfigurationDetector, $stackDetectors);
return new Detector(
$phpConfigurationDetector,
$nodeConfigurationDetector,
$stackDetectors
);
}
}
Loading
Loading