installer = new Manager( $io, $composer ); $composer->getInstallationManager()->addInstaller( $this->installer ); } /** * Deactivates the installer plugin. * * @param Composer $composer the Composer global instance. * @param IOInterface $io the IO interface global instance. */ public function deactivate( Composer $composer, IOInterface $io ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable $composer->getInstallationManager()->removeInstaller( $this->installer ); } /** * Uninstalls the installer plugin. * * @param Composer $composer the Composer global instance. * @param IOInterface $io the IO interface global instance. */ public function uninstall( Composer $composer, IOInterface $io ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable } /** * Tell composer to listen for events and do something with them. * * @return array List of subscribed events. */ public static function getSubscribedEvents() { return array( ScriptEvents::POST_INSTALL_CMD => 'generateManifest', ScriptEvents::POST_UPDATE_CMD => 'generateManifest', ); } /** * Generate the assets manifest. * * @param Event $event Script event object. */ public function generateManifest( Event $event ) { $composer = $event->getComposer(); $filesystem = new Filesystem(); $io = $event->getIO(); $io->info( 'Generating jetpack-library i18n map' ); $extra = $composer->getPackage()->getExtra(); if ( isset( $extra['wp-plugin-slug'] ) ) { $todomain = $extra['wp-plugin-slug']; $totype = 'plugins'; } elseif ( isset( $extra['wp-theme-slug'] ) ) { $todomain = $extra['wp-theme-slug']; $totype = 'themes'; } elseif ( isset( $extra['beta-plugin-slug'] ) ) { $todomain = $extra['beta-plugin-slug']; $totype = 'plugins'; } elseif ( isset( $extra['beta-theme-slug'] ) ) { $todomain = $extra['beta-theme-slug']; $totype = 'themes'; } else { $io->warning( 'Skipping jetpack-library i18n map generation, .extra.wp-plugin-slug / .extra.wp-theme-slug is not set in composer.json' ); $filesystem->remove( 'jetpack_vendor/i18n-map.php' ); return; } $data = array( 'domain' => $todomain, 'type' => $totype, 'packages' => array(), 'paths' => array(), ); foreach ( $composer->getRepositoryManager()->getLocalRepository()->getCanonicalPackages() as $package ) { if ( $package->getType() !== 'jetpack-library' ) { continue; } $ver = $package->getVersion(); if ( isset( $extra['branch-alias'][ $ver ] ) ) { $ver = $extra['branch-alias'][ $ver ]; } // Composer's `getVersion()` seems to like to return a 4-component version, while semver wants only 3 components. Strip any extra components. $ver = preg_replace( '/^(\d+\.\d+\.\d+)(?:\.\d+)+/', '$1', $ver ); if ( ! preg_match( '/^\d+\.\d+\.\d+(?:-[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*)?(?:\+[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*)?$/', $ver ) ) { // Invalid version, skip it. $ver = '0.0.0'; } $extra = $package->getExtra(); if ( empty( $extra['textdomain'] ) ) { $io->info( " {$package->getName()} ($ver): no textdomain set" ); } elseif ( $extra['textdomain'] === $todomain ) { // Nothing to alias: the package's strings are already in the // plugin's domain, and emitting a self-alias would make // Assets::filter_gettext() recurse infinitely on any // untranslated string in it. The path is still needed — it is // the prefix WordPress hashes to name the package's JavaScript // translation files — so it goes in `paths`, which registers // only that. $data['paths'][ $extra['textdomain'] ] = array( 'path' => 'jetpack_vendor/' . $package->getPrettyName(), 'ver' => $ver, ); $io->info( " {$package->getName()} ($ver): textdomain {$extra['textdomain']} matches the plugin domain, recording its path without an alias" ); } else { $data['packages'][ $extra['textdomain'] ] = array( 'path' => 'jetpack_vendor/' . $package->getPrettyName(), 'ver' => $ver, ); $io->info( " {$package->getName()} ($ver): textdomain is {$extra['textdomain']}, path is jetpack_vendor/{$package->getPrettyName()}" ); } } ksort( $data['packages'] ); ksort( $data['paths'] ); $code = " \n\s*array\(/', ' => array(', $code ); $filesystem->ensureDirectoryExists( 'jetpack_vendor' ); $filesystem->filePutContentsIfModified( 'jetpack_vendor/i18n-map.php', $code ); } }