PluginProbe
QuadMenu – Mega Menu / 3.3.6
QuadMenu – Mega Menu v3.3.6
3.3.8 3.3.7 3.3.6 trunk 1.8.4 1.8.5 1.8.7 1.8.8 1.8.9 1.9.0 1.9.2 1.9.3 1.9.4 1.9.5 1.9.6 1.9.7 1.9.8 1.9.9 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 All 88 releases
quadmenu / vendor / automattic / jetpack-composer-plugin / src / class-plugin.php

class-plugin.php in QuadMenu – Mega Menu 3.3.6, at vendor/automattic/jetpack-composer-plugin/src/class-plugin.php

155 lines 5.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Main class file for the Composer plugin that will implement a custom installer for Jetpack
4 * packages.
5 *
6 * @see https://getcomposer.org/doc/articles/custom-installers.md
7 * @package automattic/jetpack-composer-plugin
8 * */
9
10 namespace Automattic\Jetpack\Composer;
11
12 use Composer\Composer;
13 use Composer\EventDispatcher\EventSubscriberInterface;
14 use Composer\IO\IOInterface;
15 use Composer\Plugin\PluginInterface;
16 use Composer\Script\Event;
17 use Composer\Script\ScriptEvents;
18 use Composer\Util\Filesystem;
19
20 /**
21 * This class is the entry point for the installer plugin. The Composer
22 * installation mechanism registers the plugin by calling its activate method.
23 */
24 class Plugin implements PluginInterface, EventSubscriberInterface {
25
26 /**
27 * Installer instance.
28 *
29 * @var Manager|null
30 */
31 private $installer;
32
33 /**
34 * Activates the installer plugin at installation time.
35 *
36 * @param Composer $composer the Composer global instance.
37 * @param IOInterface $io the IO interface global instance.
38 */
39 public function activate( Composer $composer, IOInterface $io ) {
40 $this->installer = new Manager( $io, $composer );
41 $composer->getInstallationManager()->addInstaller( $this->installer );
42 }
43
44 /**
45 * Deactivates the installer plugin.
46 *
47 * @param Composer $composer the Composer global instance.
48 * @param IOInterface $io the IO interface global instance.
49 */
50 public function deactivate( Composer $composer, IOInterface $io ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
51 $composer->getInstallationManager()->removeInstaller( $this->installer );
52 }
53
54 /**
55 * Uninstalls the installer plugin.
56 *
57 * @param Composer $composer the Composer global instance.
58 * @param IOInterface $io the IO interface global instance.
59 */
60 public function uninstall( Composer $composer, IOInterface $io ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
61 }
62
63 /**
64 * Tell composer to listen for events and do something with them.
65 *
66 * @return array List of subscribed events.
67 */
68 public static function getSubscribedEvents() {
69 return array(
70 ScriptEvents::POST_INSTALL_CMD => 'generateManifest',
71 ScriptEvents::POST_UPDATE_CMD => 'generateManifest',
72 );
73 }
74
75 /**
76 * Generate the assets manifest.
77 *
78 * @param Event $event Script event object.
79 */
80 public function generateManifest( Event $event ) {
81 $composer = $event->getComposer();
82 $filesystem = new Filesystem();
83 $io = $event->getIO();
84 $io->info( 'Generating jetpack-library i18n map' );
85
86 $extra = $composer->getPackage()->getExtra();
87 if ( isset( $extra['wp-plugin-slug'] ) ) {
88 $todomain = $extra['wp-plugin-slug'];
89 $totype = 'plugins';
90 } elseif ( isset( $extra['wp-theme-slug'] ) ) {
91 $todomain = $extra['wp-theme-slug'];
92 $totype = 'themes';
93 } elseif ( isset( $extra['beta-plugin-slug'] ) ) {
94 $todomain = $extra['beta-plugin-slug'];
95 $totype = 'plugins';
96 } elseif ( isset( $extra['beta-theme-slug'] ) ) {
97 $todomain = $extra['beta-theme-slug'];
98 $totype = 'themes';
99 } else {
100 $io->warning( 'Skipping jetpack-library i18n map generation, .extra.wp-plugin-slug / .extra.wp-theme-slug is not set in composer.json' );
101 $filesystem->remove( 'jetpack_vendor/i18n-map.php' );
102 return;
103 }
104
105 $data = array(
106 'domain' => $todomain,
107 'type' => $totype,
108 'packages' => array(),
109 );
110 foreach ( $composer->getRepositoryManager()->getLocalRepository()->getCanonicalPackages() as $package ) {
111 if ( $package->getType() !== 'jetpack-library' ) {
112 continue;
113 }
114
115 $ver = $package->getVersion();
116 if ( isset( $extra['branch-alias'][ $ver ] ) ) {
117 $ver = $extra['branch-alias'][ $ver ];
118 }
119
120 // Composer's `getVersion()` seems to like to return a 4-component version, while semver wants only 3 components. Strip any extra components.
121 $ver = preg_replace( '/^(\d+\.\d+\.\d+)(?:\.\d+)+/', '$1', $ver );
122
123 if ( ! preg_match( '/^\d+\.\d+\.\d+(?:-[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*)?(?:\+[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*)?$/', $ver ) ) {
124 // Invalid version, skip it.
125 $ver = '0.0.0';
126 }
127
128 $extra = $package->getExtra();
129 if ( empty( $extra['textdomain'] ) ) {
130 $io->info( " {$package->getName()} ($ver): no textdomain set" );
131 } else {
132 $data['packages'][ $extra['textdomain'] ] = array(
133 'path' => 'jetpack_vendor/' . $package->getPrettyName(),
134 'ver' => $ver,
135 );
136 $io->info( " {$package->getName()} ($ver): textdomain is {$extra['textdomain']}, path is jetpack_vendor/{$package->getPrettyName()}" );
137 }
138 }
139
140 ksort( $data['packages'] );
141
142 $code = "<?php\n";
143 $code .= "// i18n-map.php @generated by automattic/jetpack-composer-plugin\n";
144 // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_var_export
145 $code .= 'return ' . var_export( $data, true ) . ";\n";
146
147 // Fixup syntax a little.
148 $code = str_replace( 'array (', 'array(', $code );
149 $code = preg_replace( '/ => \n\s*array\(/', ' => array(', $code );
150
151 $filesystem->ensureDirectoryExists( 'jetpack_vendor' );
152 $filesystem->filePutContentsIfModified( 'jetpack_vendor/i18n-map.php', $code );
153 }
154 }
155