AutoloadFileWriter.php
11 months ago
AutoloadGenerator.php
11 months ago
AutoloadProcessor.php
11 months ago
CustomAutoloaderPlugin.php
11 months ago
ManifestGenerator.php
11 months ago
autoload.php
11 months ago
class-autoloader-handler.php
11 months ago
class-autoloader-locator.php
11 months ago
class-autoloader.php
11 months ago
class-container.php
11 months ago
class-hook-manager.php
11 months ago
class-latest-autoloader-guard.php
11 months ago
class-manifest-reader.php
11 months ago
class-path-processor.php
11 months ago
class-php-autoloader.php
11 months ago
class-plugin-locator.php
11 months ago
class-plugins-handler.php
11 months ago
class-shutdown-handler.php
11 months ago
class-version-loader.php
11 months ago
class-version-selector.php
11 months ago
index.php
11 months ago
CustomAutoloaderPlugin.php
97 lines
| 1 | <?php |
| 2 | namespace Automattic\Jetpack\Autoloader; |
| 3 | if (!defined('ABSPATH')) exit; |
| 4 | use Composer\Composer; |
| 5 | use Composer\EventDispatcher\EventSubscriberInterface; |
| 6 | use Composer\IO\IOInterface; |
| 7 | use Composer\Plugin\PluginInterface; |
| 8 | use Composer\Script\Event; |
| 9 | use Composer\Script\ScriptEvents; |
| 10 | class CustomAutoloaderPlugin implements PluginInterface, EventSubscriberInterface { |
| 11 | private $io; |
| 12 | private $composer; |
| 13 | public function activate( Composer $composer, IOInterface $io ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
| 14 | $this->composer = $composer; |
| 15 | $this->io = $io; |
| 16 | } |
| 17 | public function deactivate( Composer $composer, IOInterface $io ) { |
| 18 | } |
| 19 | public function uninstall( Composer $composer, IOInterface $io ) { |
| 20 | } |
| 21 | public static function getSubscribedEvents() { |
| 22 | return array( |
| 23 | ScriptEvents::POST_AUTOLOAD_DUMP => 'postAutoloadDump', |
| 24 | ); |
| 25 | } |
| 26 | public function postAutoloadDump( Event $event ) { |
| 27 | // When the autoloader is not required by the root package we don't want to execute it. |
| 28 | // This prevents unwanted transitive execution that generates unused autoloaders or |
| 29 | // at worst throws fatal executions. |
| 30 | if ( ! $this->isRequiredByRoot() ) { |
| 31 | return; |
| 32 | } |
| 33 | $config = $this->composer->getConfig(); |
| 34 | if ( 'vendor' !== $config->raw()['config']['vendor-dir'] ) { |
| 35 | $this->io->writeError( "\n<error>An error occurred while generating the autoloader files:", true ); |
| 36 | $this->io->writeError( 'The project\'s composer.json or composer environment set a non-default vendor directory.', true ); |
| 37 | $this->io->writeError( 'The default composer vendor directory must be used.</error>', true ); |
| 38 | exit( 0 ); |
| 39 | } |
| 40 | $installationManager = $this->composer->getInstallationManager(); |
| 41 | $repoManager = $this->composer->getRepositoryManager(); |
| 42 | $localRepo = $repoManager->getLocalRepository(); |
| 43 | $package = $this->composer->getPackage(); |
| 44 | $optimize = $event->getFlags()['optimize']; |
| 45 | $suffix = $this->determineSuffix(); |
| 46 | $generator = new AutoloadGenerator( $this->io ); |
| 47 | $generator->dump( $this->composer, $config, $localRepo, $package, $installationManager, 'composer', $optimize, $suffix ); |
| 48 | } |
| 49 | private function determineSuffix() { |
| 50 | $config = $this->composer->getConfig(); |
| 51 | $vendorPath = $config->get( 'vendor-dir' ); |
| 52 | // Command line. |
| 53 | $suffix = $config->get( 'autoloader-suffix' ); |
| 54 | if ( $suffix ) { |
| 55 | return $suffix; |
| 56 | } |
| 57 | // Reuse our own suffix, if any. |
| 58 | if ( is_readable( $vendorPath . '/autoload_packages.php' ) ) { |
| 59 | $content = file_get_contents( $vendorPath . '/autoload_packages.php' ); |
| 60 | if ( preg_match( '/^namespace Automattic\\\\Jetpack\\\\Autoloader\\\\jp([^;\s]+?)(?:\\\\al[^;\s]+)?;/m', $content, $match ) ) { |
| 61 | return $match[1]; |
| 62 | } |
| 63 | } |
| 64 | // Reuse Composer's suffix, if any. |
| 65 | if ( is_readable( $vendorPath . '/autoload.php' ) ) { |
| 66 | $content = file_get_contents( $vendorPath . '/autoload.php' ); |
| 67 | if ( preg_match( '{ComposerAutoloaderInit([^:\s]+)::}', $content, $match ) ) { |
| 68 | return $match[1]; |
| 69 | } |
| 70 | } |
| 71 | // Generate a random suffix. |
| 72 | return md5( uniqid( '', true ) ); |
| 73 | } |
| 74 | private function isRequiredByRoot() { |
| 75 | $package = $this->composer->getPackage(); |
| 76 | $requires = $package->getRequires(); |
| 77 | if ( ! is_array( $requires ) ) { // @phan-suppress-current-line PhanRedundantCondition -- Earlier Composer versions may not have guaranteed this. |
| 78 | $requires = array(); |
| 79 | } |
| 80 | $devRequires = $package->getDevRequires(); |
| 81 | if ( ! is_array( $devRequires ) ) { // @phan-suppress-current-line PhanRedundantCondition -- Earlier Composer versions may not have guaranteed this. |
| 82 | $devRequires = array(); |
| 83 | } |
| 84 | $requires = array_merge( $requires, $devRequires ); |
| 85 | if ( empty( $requires ) ) { |
| 86 | $this->io->writeError( "\n<error>The package is not required and this should never happen?</error>", true ); |
| 87 | exit( 0 ); |
| 88 | } |
| 89 | foreach ( $requires as $require ) { |
| 90 | if ( 'automattic/jetpack-autoloader' === $require->getTarget() ) { |
| 91 | return true; |
| 92 | } |
| 93 | } |
| 94 | return false; |
| 95 | } |
| 96 | } |
| 97 |