Skip to content
This repository has been archived by the owner on Dec 3, 2023. It is now read-only.

Commit

Permalink
symplify autoload
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Dec 25, 2018
1 parent 3dd8f55 commit 4d9ed30
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 75 deletions.
50 changes: 1 addition & 49 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,60 +49,12 @@
"thecodingmachine/phpstan-strict-rules": "0.10.6",
"tracy/tracy": "^2.5"
},
"autoload": {
"psr-4": {
"Symplify\\Autodiscovery\\": "packages/Autodiscovery/src",
"Symplify\\BetterPhpDocParser\\": "packages/BetterPhpDocParser/src",
"Symplify\\ChangelogLinker\\": "packages/ChangelogLinker/src",
"Symplify\\CodingStandard\\": "packages/CodingStandard/src",
"Symplify\\EasyCodingStandardTester\\": "packages/EasyCodingStandardTester/src",
"Symplify\\EasyCodingStandard\\": "packages/EasyCodingStandard/src",
"Symplify\\EasyCodingStandard\\ChangedFilesDetector\\": "packages/EasyCodingStandard/packages/ChangedFilesDetector/src",
"Symplify\\EasyCodingStandard\\Configuration\\": "packages/EasyCodingStandard/packages/Configuration/src",
"Symplify\\EasyCodingStandard\\FixerRunner\\": "packages/EasyCodingStandard/packages/FixerRunner/src",
"Symplify\\EasyCodingStandard\\SniffRunner\\": "packages/EasyCodingStandard/packages/SniffRunner/src",
"Symplify\\FlexLoader\\": "packages/FlexLoader/src",
"Symplify\\LatteToTwigConverter\\": "packages/LatteToTwigConverter/src",
"Symplify\\MonorepoBuilder\\": "packages/MonorepoBuilder/src",
"Symplify\\MonorepoBuilder\\Init\\": "packages/MonorepoBuilder/packages/Init/src",
"Symplify\\MonorepoBuilder\\Release\\": "packages/MonorepoBuilder/packages/Release/src",
"Symplify\\MonorepoBuilder\\Split\\": "packages/MonorepoBuilder/packages/Split/src",
"Symplify\\PHPStanExtensions\\": "packages/PHPStanExtensions/src",
"Symplify\\PackageBuilder\\": "packages/PackageBuilder/src",
"Symplify\\Statie\\": "packages/Statie/src",
"Symplify\\Statie\\Generator\\": "packages/Statie/packages/Generator/src",
"Symplify\\Statie\\Latte\\": "packages/Statie/packages/Latte/src",
"Symplify\\Statie\\Twig\\": "packages/Statie/packages/Twig/src",
"Symplify\\TokenRunner\\": "packages/TokenRunner/src"
}
},
"autoload-dev": {
"classmap": [
"packages/Autodiscovery/tests/KernelProjectDir"
],
"psr-4": {
"Symplify\\Util\\Composer\\": "util/Composer/src",
"Symplify\\Autodiscovery\\Tests\\": "packages/Autodiscovery/tests",
"Symplify\\BetterPhpDocParser\\Tests\\": "packages/BetterPhpDocParser/tests",
"Symplify\\ChangelogLinker\\Tests\\": "packages/ChangelogLinker/tests",
"Symplify\\CodingStandard\\Tests\\": "packages/CodingStandard/tests",
"Symplify\\EasyCodingStandardTester\\Tests\\": "packages/EasyCodingStandardTester/tests",
"Symplify\\EasyCodingStandard\\ChangedFilesDetector\\Tests\\": "packages/EasyCodingStandard/packages/ChangedFilesDetector/tests",
"Symplify\\EasyCodingStandard\\FixerRunner\\Tests\\": "packages/EasyCodingStandard/packages/FixerRunner/tests",
"Symplify\\EasyCodingStandard\\SniffRunner\\Tests\\": "packages/EasyCodingStandard/packages/SniffRunner/tests",
"Symplify\\EasyCodingStandard\\Tests\\": "packages/EasyCodingStandard/tests",
"Symplify\\FlexLoader\\Tests\\": "packages/FlexLoader/tests",
"Symplify\\LatteToTwigConverter\\Tests\\": "packages/LatteToTwigConverter/tests",
"Symplify\\MonorepoBuilder\\Split\\Tests\\": "packages/MonorepoBuilder/packages/Split/tests",
"Symplify\\MonorepoBuilder\\Tests\\": "packages/MonorepoBuilder/tests",
"Symplify\\PHPStanExtensions\\Tests\\": "packages/PHPStanExtensions/tests",
"Symplify\\PackageBuilder\\Tests\\": "packages/PackageBuilder/tests",
"Symplify\\Statie\\Generator\\Tests\\": "packages/Statie/packages/Generator/tests",
"Symplify\\Statie\\Latte\\Tests\\": "packages/Statie/packages/Latte/tests",
"Symplify\\Statie\\Tests\\": "packages/Statie/tests",
"Symplify\\Statie\\Twig\\Tests\\": "packages/Statie/packages/Twig/tests",
"Symplify\\Tests\\": "tests",
"Symplify\\TokenRunner\\Tests\\": "packages/TokenRunner/tests"
"Symplify\\Util\\Composer\\": "util/Composer/src"
}
},
"replace": {
Expand Down
89 changes: 63 additions & 26 deletions util/Composer/src/PackagePsr4Autoloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,54 +2,51 @@

namespace Symplify\Util\Composer;

use Composer\Package\RootPackageInterface;
use Composer\Script\Event;
use Nette\Utils\Strings;

final class PackagePsr4Autoloader
{
/**
* @var string
*/
private const MAIN_NAMESPACE = 'Symplify';

/**
* @var string
*/
private const PACKAGES = 'packages';

/**
* @see https://www.drupal.org/files/issues/vendor-classmap-2468499-14.patch
*/
public static function autoload(Event $event): void
{
// input
$packagesDirectory = 'packages';
$mainNamespace = 'Symplify';
$absolutePackagesDirectory = getcwd() . '/' . $packagesDirectory . '/*';
$absolutePackagesDirectory = getcwd() . '/' . self::PACKAGES . '/*';
$package = $event->getComposer()->getPackage();

// src
$srcDirectories = self::getDirectoriesInPath($absolutePackagesDirectory, 'src');
$namespaceToDirectory = self::createNamespaceToDirectory($srcDirectories, $mainNamespace, $packagesDirectory);
$autoload = $package->getAutoload();
$autoload['psr-4'] = array_merge($autoload['psr-4'], $namespaceToDirectory);
$package->setAutoload($autoload);

// tests
$testDirectories = self::getDirectoriesInPath($absolutePackagesDirectory, 'tests');
$namespaceToDirectory = self::createNamespaceToDirectory(
$testDirectories,
$mainNamespace,
$packagesDirectory,
'Tests'
);
$devAutoload = $package->getDevAutoload();
$devAutoload['psr-4'] = array_merge($devAutoload['psr-4'], $namespaceToDirectory);
$package->setDevAutoload($devAutoload);
self::autoloadSrc($absolutePackagesDirectory, $package);
self::autoloadTests($absolutePackagesDirectory, $package);
}

/**
* @return string[]
*/
private static function getDirectoriesInPath(string $directory, string $name): array
{
$globResult = self::globRecursive($directory, GLOB_ONLYDIR);

$directories = array_filter($globResult, function ($path) use ($name) {
// keep only dirs ending with "$name"
return Strings::match($path, '#\/' . preg_quote($name) . '$#') && ! Strings::contains($path, 'templates');
});

$directories = self::absolutizePaths($directories);

return self::relativizeToCwd($directories);
}

/**
* @see https://stackoverflow.com/a/12109100/1348344
* @return string[]
Expand All @@ -60,8 +57,10 @@ private static function globRecursive(string $pattern, int $flags = 0): array
foreach (glob(dirname($pattern) . '/*', GLOB_ONLYDIR | GLOB_NOSORT) as $dir) {
$files = array_merge($files, self::globRecursive($dir . '/' . basename($pattern), $flags));
}

return $files;
}

/**
* @param string[] $directories
* @return string[]
Expand All @@ -71,8 +70,10 @@ private static function relativizeToCwd(array $directories): array
foreach ($directories as $key => $directory) {
$directories[$key] = Strings::substring($directory, strlen(getcwd()) + 1);
}

return $directories;
}

/**
* @param string[] $paths
* @return string[]
Expand All @@ -85,8 +86,10 @@ private static function absolutizePaths(array $paths): array
$paths[$key] = $realpath;
}
}

return $paths;
}

/**
* From:
* "packages/SomePath/src"
Expand All @@ -99,22 +102,56 @@ private static function absolutizePaths(array $paths): array
*/
private static function createNamespaceToDirectory(
array $directories,
string $mainNamespace,
string $packagesDirectory,
string $namespaceSuffix = ''
): array {
$namespaceToDirectory = [];
foreach ($directories as $directory) {
$match = Strings::match($directory, '#' . $packagesDirectory . '/(?<namespacePart>.*?)/#');
$pattern = '#' . $packagesDirectory . '/(?<namespacePart>.*?)/(src|tests|packages)(/(?<secondNamespacePart>.*?)/)?#';

$match = Strings::match($directory, $pattern);
if (! isset($match['namespacePart'])) {
continue;
}
$namespace = $mainNamespace . '\\' . $match['namespacePart'] . '\\';

$namespace = self::MAIN_NAMESPACE . '\\' . $match['namespacePart'] . '\\';

// subpackage
if (isset($match['secondNamespacePart'])) {
$namespace .= $match['secondNamespacePart'] . '\\';
}

if ($namespaceSuffix) {
$namespace .= $namespaceSuffix . '\\';
}

$namespaceToDirectory[$namespace] = $directory;
}

return $namespaceToDirectory;
}
}

private static function autoloadSrc(string $absolutePackagesDirectory, RootPackageInterface $package): void
{
$srcDirectories = self::getDirectoriesInPath($absolutePackagesDirectory, 'src');
$namespaceToDirectory = self::createNamespaceToDirectory($srcDirectories, self::PACKAGES);

$autoload = $package->getAutoload();
$autoload['psr-4'] = array_merge($autoload['psr-4'] ?? [], $namespaceToDirectory);
$package->setAutoload($autoload);
}

private static function autoloadTests(string $absolutePackagesDirectory, RootPackageInterface $package): void
{
$testDirectories = self::getDirectoriesInPath($absolutePackagesDirectory, 'tests');
$namespaceToDirectory = self::createNamespaceToDirectory(
$testDirectories,
self::PACKAGES,
'Tests'
);

$devAutoload = $package->getDevAutoload();
$devAutoload['psr-4'] = array_merge($devAutoload['psr-4'] ?? [], $namespaceToDirectory);
$package->setDevAutoload($devAutoload);
}
}

0 comments on commit 4d9ed30

Please sign in to comment.