Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Experimental autoload strategy #60

Merged
merged 5 commits into from
Feb 23, 2024
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
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,31 @@ be installed.
}
```

### Autoload (strategy)

You may have some problems with symlinks and recursion when developing packages inside another application or package,
for that, you can use `experimental:autoload` strategy.

This strategy will create a simple copy of your `composer.json` in `packages/vendor` directory to do a symlink from your
original `vendor` directory.

To activate it, you should change your `packages/composer.json`.

```json
{
"extra": {
"composer-plug-and-play": {
"autoload-dev": ["dex/fake"],
"require-dev": ["dex/fake"],
"strategy": "experimental:autoload"
}
}
}
```

You must add to `autoload-dev` the packages that you want to map its autoload and add to `require-dev` the packages
that you want to require its dev dependencies.

## License

[Composer Plug and Play](https://github.com/edersoares/composer-plug-and-play/) is licensed under the MIT license.
Expand Down
2 changes: 2 additions & 0 deletions fixtures/autoload-strategy/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}
11 changes: 11 additions & 0 deletions fixtures/autoload-strategy/packages/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"config": {
"allow-plugins": true
},
"extra": {
"composer-plug-and-play": {
"strategy": "experimental:autoload",
"autoload-dev": ["dex/fake"]
}
}
}
19 changes: 19 additions & 0 deletions fixtures/autoload-strategy/packages/dex/fake/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "dex/fake",
"require": {
"composer/composer": "*"
},
"require-dev": {
"dex/composer-plug-and-play": "@dev"
},
"autoload": {
"psr-4": {
"Dex\\Fake\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Dex\\Fake\\Tests\\": "tests/"
}
}
}
9 changes: 7 additions & 2 deletions src/Commands/ResetCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Dex\Composer\PlugAndPlay\Commands;

use Composer\Command\BaseCommand;
use Composer\Util\Filesystem;
use Dex\Composer\PlugAndPlay\PlugAndPlayInterface;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
Expand All @@ -24,14 +25,18 @@ protected function execute(InputInterface $input, OutputInterface $output): int
{
$this->outputPluginUse($output);

$filesystem = new Filesystem();

$filesystem->removeDirectoryPhp(PlugAndPlayInterface::PACKAGES_VENDOR);

if (file_exists(PlugAndPlayInterface::FILENAME)) {
unlink(PlugAndPlayInterface::FILENAME);
$filesystem->unlink(PlugAndPlayInterface::FILENAME);
}

$lock = str_replace('.json', '.lock', PlugAndPlayInterface::FILENAME);

if (file_exists($lock)) {
unlink($lock);
$filesystem->unlink($lock);
}

return 0;
Expand Down
54 changes: 51 additions & 3 deletions src/Composer/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Composer\Json\JsonFile;
use Composer\Json\JsonValidationException;
use Composer\PartialComposer;
use Composer\Util\Filesystem;
use Dex\Composer\PlugAndPlay\PlugAndPlayInterface;
use InvalidArgumentException;
use Seld\JsonLint\ParsingException;
Expand All @@ -17,6 +18,8 @@ class Factory extends ComposerFactory implements PlugAndPlayInterface
{
private static bool $loaded = false;

private Filesystem $filesystem;

/**
* Restart factory.
*/
Expand Down Expand Up @@ -57,11 +60,11 @@ private function saveComposerPlugAndPlayFile(array $data): void
/**
* Creates a repository item.
*/
private function createRepositoryItem(string $package): array
private function createRepositoryItem(string $url): array
{
return [
'type' => 'path',
'url' => './' . dirname($package),
'url' => $url,
'symlink' => true
];
}
Expand Down Expand Up @@ -175,9 +178,17 @@ private function definePluggedAndIgnoredPackages(IOInterface $io, array &$plugge
{
$ignore = $localConfig['extra']['composer-plug-and-play']['ignore'] ?? [];
$requireDev = $localConfig['extra']['composer-plug-and-play']['require-dev'] ?? [];
$autoloadDev = $localConfig['extra']['composer-plug-and-play']['autoload-dev'] ?? [];
$strategy = $localConfig['extra']['composer-plug-and-play']['strategy'] ?? 'default';
$isExperimental = $strategy === 'experimental:autoload';

$packages = glob(self::PATH);

if ($isExperimental) {
$this->filesystem()->removeDirectoryPhp(PlugAndPlayInterface::PACKAGES_VENDOR);
$this->filesystem()->ensureDirectoryExists(PlugAndPlayInterface::PACKAGES_VENDOR);
}

foreach ($packages as $package) {
$data = $this->loadJsonFile($io, $package);

Expand All @@ -187,6 +198,20 @@ private function definePluggedAndIgnoredPackages(IOInterface $io, array &$plugge
continue;
}

if ($isExperimental) {
foreach ($data['autoload']['psr-4'] ?? [] as $namespace => $directory) {
$localConfig['autoload']['psr-4'][$namespace] = dirname($package) . DIRECTORY_SEPARATOR . $directory;
}

if (in_array($data['name'], $autoloadDev)) {
foreach ($data['autoload-dev']['psr-4'] ?? [] as $namespace => $directory) {
$localConfig['autoload-dev']['psr-4'][$namespace] = dirname($package) . DIRECTORY_SEPARATOR . $directory;
}
}

$this->experimentalAutoloadStrategy($data);
}

// TODO show dev dependencies required
if (in_array($data['name'], $requireDev)) {
foreach ($data['require-dev'] ?? [] as $pack => $version) {
Expand All @@ -196,11 +221,34 @@ private function definePluggedAndIgnoredPackages(IOInterface $io, array &$plugge

$plugged[] = $data['name'];

$url = './' . dirname($package);

if ($isExperimental) {
$url = './' . str_replace('packages', PlugAndPlayInterface::PACKAGES_VENDOR, dirname($package));
}

$localConfig['require'][$data['name']] = '@dev';
$localConfig['repositories'][] = $this->createRepositoryItem($package);
$localConfig['repositories'][] = $this->createRepositoryItem($url);
}
}

private function filesystem(): Filesystem
{
return $this->filesystem ??= new Filesystem();
}

private function experimentalAutoloadStrategy(array $data): void
{
$this->filesystem()->ensureDirectoryExists(PlugAndPlayInterface::PACKAGES_VENDOR . $data['name']);

unset($data['autoload']);
unset($data['autoload-dev']);

$json = json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) . PHP_EOL;

file_put_contents(PlugAndPlayInterface::PACKAGES_VENDOR . $data['name'] . '/composer.json', $json);
}

/**
* Write in output which packages are plugged and witch are ignored.
*/
Expand Down
2 changes: 2 additions & 0 deletions src/PlugAndPlayInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,7 @@ interface PlugAndPlayInterface

public const PACKAGES_PATH = 'packages';

public const PACKAGES_VENDOR = 'packages/vendor';

public const PATH = 'packages/*/*/composer.json';
}
44 changes: 44 additions & 0 deletions tests/Composer/Factory/AutoloadStrategyTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

beforeEach()
->fixture('autoload-strategy')
->prepare();

afterEach()
->cleanup();

test('factory', function () {
$this->factory();

$this->assertGeneratedJsonEquals([
'config' => [
'allow-plugins' => true,
],
'extra' => [
'composer-plug-and-play' => [
'strategy' => 'experimental:autoload',
'autoload-dev' => ['dex/fake'],
]
],
'autoload' => [
'psr-4' => [
'Dex\\Fake\\' => 'packages/dex/fake/src/',
]
],
'autoload-dev' => [
'psr-4' => [
'Dex\\Fake\\Tests\\' => 'packages/dex/fake/tests/',
]
],
'require' => [
'dex/fake' => '@dev',
],
'repositories' => [
[
'type' => 'path',
'url' => './packages/vendor/dex/fake',
'symlink' => true,
],
]
]);
});
Loading