Completed
Push — try/jetpack-stories-block-mobi... ( 5d409e...2fea66 )
by
unknown
116:16 queued 106:28
created

CustomAutoloaderPlugin::determineSuffix()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 31

Duplication

Lines 14
Ratio 45.16 %

Importance

Changes 0
Metric Value
cc 6
nc 8
nop 0
dl 14
loc 31
rs 8.8017
c 0
b 0
f 0
1
<?php //phpcs:ignore WordPress.Files.FileName.NotHyphenatedLowercase
2
/**
3
 * Custom Autoloader Composer Plugin, hooks into composer events to generate the custom autoloader.
4
 *
5
 * @package automattic/jetpack-autoloader
6
 */
7
8
// phpcs:disable PHPCompatibility.Keywords.NewKeywords.t_useFound
9
// phpcs:disable PHPCompatibility.LanguageConstructs.NewLanguageConstructs.t_ns_separatorFound
10
// phpcs:disable PHPCompatibility.Keywords.NewKeywords.t_namespaceFound
11
// phpcs:disable WordPress.Files.FileName.NotHyphenatedLowercase
12
// phpcs:disable WordPress.Files.FileName.InvalidClassFileName
13
// phpcs:disable WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase
14
15
namespace Automattic\Jetpack\Autoloader;
16
17
use Composer\Composer;
18
use Composer\EventDispatcher\EventSubscriberInterface;
19
use Composer\IO\IOInterface;
20
use Composer\Plugin\PluginInterface;
21
use Composer\Script\Event;
22
use Composer\Script\ScriptEvents;
23
24
/**
25
 * Class CustomAutoloaderPlugin.
26
 *
27
 * @package automattic/jetpack-autoloader
28
 */
29
class CustomAutoloaderPlugin implements PluginInterface, EventSubscriberInterface {
30
31
	/**
32
	 * IO object.
33
	 *
34
	 * @var IOInterface IO object.
35
	 */
36
	private $io;
37
38
	/**
39
	 * Composer object.
40
	 *
41
	 * @var Composer Composer object.
42
	 */
43
	private $composer;
44
45
	/**
46
	 * Do nothing.
47
	 *
48
	 * @param Composer    $composer Composer object.
49
	 * @param IOInterface $io IO object.
50
	 */
51
	public function activate( Composer $composer, IOInterface $io ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
52
		$this->composer = $composer;
53
		$this->io       = $io;
54
	}
55
56
	/**
57
	 * Do nothing.
58
	 * phpcs:disable VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
59
	 *
60
	 * @param Composer    $composer Composer object.
61
	 * @param IOInterface $io IO object.
62
	 */
63
	public function deactivate( Composer $composer, IOInterface $io ) {
64
		/*
65
		 * Intentionally left empty. This is a PluginInterface method.
66
		 * phpcs:enable VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
67
		 */
68
	}
69
70
	/**
71
	 * Do nothing.
72
	 * phpcs:disable VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
73
	 *
74
	 * @param Composer    $composer Composer object.
75
	 * @param IOInterface $io IO object.
76
	 */
77
	public function uninstall( Composer $composer, IOInterface $io ) {
78
		/*
79
		 * Intentionally left empty. This is a PluginInterface method.
80
		 * phpcs:enable VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
81
		 */
82
	}
83
84
	/**
85
	 * Tell composer to listen for events and do something with them.
86
	 *
87
	 * @return array List of subscribed events.
88
	 */
89
	public static function getSubscribedEvents() {
90
		return array(
91
			ScriptEvents::POST_AUTOLOAD_DUMP => 'postAutoloadDump',
92
		);
93
	}
94
95
	/**
96
	 * Generate the custom autolaoder.
97
	 *
98
	 * @param Event $event Script event object.
99
	 */
100
	public function postAutoloadDump( Event $event ) {
101
		$config = $this->composer->getConfig();
102
103
		if ( 'vendor' !== $config->raw()['config']['vendor-dir'] ) {
104
			$this->io->writeError( "\n<error>An error occurred while generating the autoloader files:", true );
105
			$this->io->writeError( 'The project\'s composer.json or composer environment set a non-default vendor directory.', true );
106
			$this->io->writeError( 'The default composer vendor directory must be used.</error>', true );
107
			exit();
108
		}
109
110
		$installationManager = $this->composer->getInstallationManager();
111
		$repoManager         = $this->composer->getRepositoryManager();
112
		$localRepo           = $repoManager->getLocalRepository();
113
		$package             = $this->composer->getPackage();
114
		$optimize            = $event->getFlags()['optimize'];
115
		$suffix              = $this->determineSuffix();
116
117
		$generator = new AutoloadGenerator( $this->io );
118
119
		$generator->dump( $config, $localRepo, $package, $installationManager, 'composer', $optimize, $suffix );
120
		$this->generated = true;
121
	}
122
123
	/**
124
	 * Determine the suffix for the autoloader class.
125
	 *
126
	 * Reuses an existing suffix from vendor/autoload_packages.php or vendor/autoload.php if possible.
127
	 *
128
	 * @return string Suffix.
129
	 */
130
	private function determineSuffix() {
131
		$config     = $this->composer->getConfig();
132
		$vendorPath = $config->get( 'vendor-dir' );
133
134
		// Command line.
135
		$suffix = $config->get( 'autoloader-suffix' );
136
		if ( $suffix ) {
137
			return $suffix;
138
		}
139
140
		// Reuse our own suffix, if any.
141 View Code Duplication
		if ( is_readable( $vendorPath . '/autoload_packages.php' ) ) {
142
			// phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
143
			$content = file_get_contents( $vendorPath . '/autoload_packages.php' );
144
			if ( preg_match( '/^namespace Automattic\\\\Jetpack\\\\Autoloader\\\\jp([^;\s]+);/m', $content, $match ) ) {
145
				return $match[1];
146
			}
147
		}
148
149
		// Reuse Composer's suffix, if any.
150 View Code Duplication
		if ( is_readable( $vendorPath . '/autoload.php' ) ) {
151
			// phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
152
			$content = file_get_contents( $vendorPath . '/autoload.php' );
153
			if ( preg_match( '{ComposerAutoloaderInit([^:\s]+)::}', $content, $match ) ) {
154
				return $match[1];
155
			}
156
		}
157
158
		// Generate a random suffix.
159
		return md5( uniqid( '', true ) );
160
	}
161
162
}
163