1
|
|
|
<?php |
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\IO\IOInterface; |
19
|
|
|
use Composer\Script\Event; |
20
|
|
|
use Composer\Script\ScriptEvents; |
21
|
|
|
use Composer\Plugin\PluginInterface; |
22
|
|
|
use Composer\EventDispatcher\EventSubscriberInterface; |
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
|
|
|
* Tell composer to listen for events and do something with them. |
58
|
|
|
* |
59
|
|
|
* @return array List of succribed events. |
60
|
|
|
*/ |
61
|
|
|
public static function getSubscribedEvents() { |
62
|
|
|
return array( |
63
|
|
|
ScriptEvents::POST_AUTOLOAD_DUMP => 'postAutoloadDump', |
64
|
|
|
); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Generate the custom autolaoder. |
69
|
|
|
* |
70
|
|
|
* @param Event $event Script event object. |
71
|
|
|
*/ |
72
|
|
|
public function postAutoloadDump( Event $event ) { |
73
|
|
|
|
74
|
|
|
$installationManager = $this->composer->getInstallationManager(); |
75
|
|
|
$repoManager = $this->composer->getRepositoryManager(); |
76
|
|
|
$localRepo = $repoManager->getLocalRepository(); |
77
|
|
|
$package = $this->composer->getPackage(); |
78
|
|
|
$config = $this->composer->getConfig(); |
79
|
|
|
$optimize = true; |
80
|
|
|
$suffix = $config->get( 'autoloader-suffix' ) |
81
|
|
|
? $config->get( 'autoloader-suffix' ) |
82
|
|
|
: md5( uniqid( '', true ) ); |
83
|
|
|
|
84
|
|
|
$generator = new AutoloadGenerator( $this->io ); |
85
|
|
|
|
86
|
|
|
$generator->dump( $config, $localRepo, $package, $installationManager, 'composer', $optimize, $suffix ); |
87
|
|
|
$this->generated = true; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
} |
91
|
|
|
|