Skip to content

Commit

Permalink
Autoloader: AutoloadGenerator no longer extends Composer's AutoloadGe…
Browse files Browse the repository at this point in the history
…nerator class (#17813)

* Autoloader: AutoloadGenerator no longer extends Composer's AutoloadGenerator class

* Use the latest stable composer release in the "Build dashboard" Travis build

Remove the composer self-update 2.0.6 command.

* Autoloader: fix typo in AutoloadGenerator

* Update packages/autoloader/src/AutoloadGenerator.php

Co-authored-by: Brad Jorsch <[email protected]>

* Autoloader: fix the AutoloadGenerator $composer parameter

Co-authored-by: Brad Jorsch <[email protected]>
  • Loading branch information
kbrown9 and anomiex authored Nov 19, 2020
1 parent f263069 commit 3ae959f
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 14 deletions.
5 changes: 2 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ php:
- "7.3"
- "7.4snapshot"

before_install:
- composer self-update 2.0.6

env:
global:
# Global variable is re-defined in matrix-include -list
Expand Down Expand Up @@ -50,6 +47,8 @@ matrix:
name: "Build dashboard & extensions"
language: node_js
env: WP_TRAVISCI="yarn build-concurrently"
before_install:
- composer self-update

# Disable for now until we fix all the spelling issues
# - name: "Spell check Markdown files"
Expand Down
128 changes: 119 additions & 9 deletions packages/autoloader/src/AutoloadGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,28 @@
// phpcs:disable WordPress.WP.AlternativeFunctions.file_system_read_file_put_contents
// phpcs:disable WordPress.WP.AlternativeFunctions.file_system_read_fopen
// phpcs:disable WordPress.WP.AlternativeFunctions.file_system_read_fwrite
// phpcs:disable WordPress.NamingConventions.ValidFunctionName.MethodNameInvalid
// phpcs:disable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
// phpcs:disable WordPress.NamingConventions.ValidVariableName.InterpolatedVariableNotSnakeCase
// phpcs:disable WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase
// phpcs:disable WordPress.NamingConventions.ValidVariableName.PropertyNotSnakeCase


namespace Automattic\Jetpack\Autoloader;

use Composer\Autoload\AutoloadGenerator as BaseGenerator;
use Composer\Autoload\ClassMapGenerator;
use Composer\Composer;
use Composer\Config;
use Composer\Installer\InstallationManager;
use Composer\IO\IOInterface;
use Composer\Package\PackageInterface;
use Composer\Repository\InstalledRepositoryInterface;
use Composer\Util\Filesystem;
use Composer\Util\PackageSorter;

/**
* Class AutoloadGenerator.
*/
class AutoloadGenerator extends BaseGenerator {
class AutoloadGenerator {

const COMMENT = <<<AUTOLOADER_COMMENT
/**
Expand Down Expand Up @@ -66,15 +67,17 @@ public function __construct( IOInterface $io = null ) {
/**
* Dump the Jetpack autoloader files.
*
* @param Composer $composer The Composer object.
* @param Config $config Config object.
* @param InstalledRepositoryInterface $localRepo Installed Reposetories object.
* @param InstalledRepositoryInterface $localRepo Installed Repository object.
* @param PackageInterface $mainPackage Main Package object.
* @param InstallationManager $installationManager Manager for installing packages.
* @param string $targetDir Path to the current target directory.
* @param bool $scanPsrPackages Whether or not PSR packages should be converted to a classmap.
* @param string $suffix The autoloader suffix.
*/
public function dump(
Composer $composer,
Config $config,
InstalledRepositoryInterface $localRepo,
PackageInterface $mainPackage,
Expand All @@ -85,7 +88,7 @@ public function dump(
) {
$this->filesystem->ensureDirectoryExists( $config->get( 'vendor-dir' ) );

$packageMap = $this->buildPackageMap( $installationManager, $mainPackage, $localRepo->getCanonicalPackages() );
$packageMap = $composer->getAutoloadGenerator()->buildPackageMap( $installationManager, $mainPackage, $localRepo->getCanonicalPackages() );
$autoloads = $this->parseAutoloads( $packageMap, $mainPackage );

// Convert the autoloads into a format that the manifest generator can consume more easily.
Expand All @@ -106,6 +109,117 @@ public function dump(
}
}

/**
* Compiles an ordered list of namespace => path mappings
*
* @param array $packageMap Array of array(package, installDir-relative-to-composer.json).
* @param PackageInterface $mainPackage Main package instance.
*
* @return array The list of path mappings.
*/
public function parseAutoloads( array $packageMap, PackageInterface $mainPackage ) {
$rootPackageMap = array_shift( $packageMap );

$sortedPackageMap = $this->sortPackageMap( $packageMap );
$sortedPackageMap[] = $rootPackageMap;
array_unshift( $packageMap, $rootPackageMap );

$psr0 = $this->parseAutoloadsType( $packageMap, 'psr-0', $mainPackage );
$psr4 = $this->parseAutoloadsType( $packageMap, 'psr-4', $mainPackage );
$classmap = $this->parseAutoloadsType( array_reverse( $sortedPackageMap ), 'classmap', $mainPackage );
$files = $this->parseAutoloadsType( $sortedPackageMap, 'files', $mainPackage );

krsort( $psr0 );
krsort( $psr4 );

return array(
'psr-0' => $psr0,
'psr-4' => $psr4,
'classmap' => $classmap,
'files' => $files,
);
}

/**
* Sorts packages by dependency weight
*
* Packages of equal weight retain the original order
*
* @param array $packageMap The package map.
* @return array
*/
protected function sortPackageMap( array $packageMap ) {
$packages = array();
$paths = array();

foreach ( $packageMap as $item ) {
list( $package, $path ) = $item;
$name = $package->getName();
$packages[ $name ] = $package;
$paths[ $name ] = $path;
}

$sortedPackages = PackageSorter::sortPackages( $packages );

$sortedPackageMap = array();

foreach ( $sortedPackages as $package ) {
$name = $package->getName();
$sortedPackageMap[] = array( $packages[ $name ], $paths[ $name ] );
}

return $sortedPackageMap;
}

/**
* Returns the file identifier.
*
* @param PackageInterface $package The package instance.
* @param string $path The path.
*/
protected function getFileIdentifier( PackageInterface $package, $path ) {
return md5( $package->getName() . ':' . $path );
}

/**
* Returns the path code for the given path.
*
* @param Filesystem $filesystem The filesystem instance.
* @param string $basePath The base path.
* @param string $vendorPath The vendor path.
* @param string $path The path.
*
* @return string The path code.
*/
protected function getPathCode( Filesystem $filesystem, $basePath, $vendorPath, $path ) {
if ( ! $filesystem->isAbsolutePath( $path ) ) {
$path = $basePath . '/' . $path;
}
$path = $filesystem->normalizePath( $path );

$baseDir = '';
if ( 0 === strpos( $path . '/', $vendorPath . '/' ) ) {
$path = substr( $path, strlen( $vendorPath ) );
$baseDir = '$vendorDir';

if ( false !== $path ) {
$baseDir .= ' . ';
}
} else {
$path = $filesystem->normalizePath( $filesystem->findShortestPath( $basePath, $path, true ) );
if ( ! $filesystem->isAbsolutePath( $path ) ) {
$baseDir = '$baseDir . ';
$path = '/' . $path;
}
}

if ( strpos( $path, '.phar' ) !== false ) {
$baseDir = "'phar://' . " . $baseDir;
}

return $baseDir . ( ( false !== $path ) ? var_export( $path, true ) : '' );
}

/**
* This function differs from the composer parseAutoloadsType in that beside returning the path.
* It also return the path and the version of a package.
Expand All @@ -121,10 +235,6 @@ public function dump(
protected function parseAutoloadsType( array $packageMap, $type, PackageInterface $mainPackage ) {
$autoloads = array();

if ( 'psr-4' !== $type && 'classmap' !== $type && 'files' !== $type && 'psr-0' !== $type ) {
return parent::parseAutoloadsType( $packageMap, $type, $mainPackage );
}

foreach ( $packageMap as $item ) {
list($package, $installPath) = $item;
$autoload = $package->getAutoload();
Expand Down
3 changes: 1 addition & 2 deletions packages/autoloader/src/CustomAutoloaderPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,7 @@ public function postAutoloadDump( Event $event ) {
$suffix = $this->determineSuffix();

$generator = new AutoloadGenerator( $this->io );

$generator->dump( $config, $localRepo, $package, $installationManager, 'composer', $optimize, $suffix );
$generator->dump( $this->composer, $config, $localRepo, $package, $installationManager, 'composer', $optimize, $suffix );
$this->generated = true;
}

Expand Down

0 comments on commit 3ae959f

Please sign in to comment.