1 | <?php |
||
26 | class ChangelogsPlugin implements PluginInterface, EventSubscriberInterface |
||
27 | { |
||
28 | const EXTRA_KEY = 'composer-changelogs'; |
||
29 | |||
30 | /** @var Composer */ |
||
31 | private $composer; |
||
32 | |||
33 | /** @var IOInterface */ |
||
34 | private $io; |
||
35 | |||
36 | /** @var Outputter */ |
||
37 | private $outputter; |
||
38 | |||
39 | /** @var ConfigLocator */ |
||
40 | private $configLocator; |
||
41 | |||
42 | /** @var Config */ |
||
43 | private $config; |
||
44 | |||
45 | /** @var int */ |
||
46 | private static $postUpdatePriority = -1; |
||
47 | |||
48 | /** |
||
49 | * {@inheritdoc} |
||
50 | */ |
||
51 | 14 | public function activate(Composer $composer, IOInterface $io) |
|
52 | { |
||
53 | 14 | $this->composer = $composer; |
|
54 | 14 | $this->io = $io; |
|
55 | 14 | $this->configLocator = new ConfigLocator($composer); |
|
56 | |||
57 | 14 | $this->setupConfig(); |
|
58 | 14 | $this->autoloadNeededClasses(); |
|
59 | |||
60 | 14 | $this->outputter = Factory::createOutputter($this->config->getGitlabHosts()); |
|
61 | 14 | } |
|
62 | |||
63 | /** |
||
64 | * {@inheritdoc} |
||
65 | */ |
||
66 | 6 | public static function getSubscribedEvents() |
|
67 | { |
||
68 | return [ |
||
69 | 6 | PackageEvents::POST_PACKAGE_UPDATE => [ |
|
70 | 6 | ['postPackageOperation'], |
|
71 | 6 | ], |
|
72 | 6 | PackageEvents::POST_PACKAGE_INSTALL => [ |
|
73 | 6 | ['postPackageOperation'], |
|
74 | 6 | ], |
|
75 | 6 | PackageEvents::POST_PACKAGE_UNINSTALL => [ |
|
76 | 6 | ['postPackageOperation'], |
|
77 | 6 | ], |
|
78 | 6 | ScriptEvents::POST_UPDATE_CMD => [ |
|
79 | 6 | ['postUpdate', static::$postUpdatePriority], |
|
|
|||
80 | 6 | ], |
|
81 | 6 | ]; |
|
82 | } |
||
83 | |||
84 | /** |
||
85 | * @param PackageEvent $event |
||
86 | */ |
||
87 | 10 | public function postPackageOperation(PackageEvent $event) |
|
88 | { |
||
89 | 10 | $operation = $event->getOperation(); |
|
90 | |||
91 | 10 | $this->outputter->addOperation($operation); |
|
92 | 10 | } |
|
93 | |||
94 | /** |
||
95 | * @param Event $event |
||
96 | */ |
||
97 | 10 | public function postUpdate(Event $event) |
|
98 | { |
||
99 | 10 | $this->io->write($this->outputter->getOutput()); |
|
100 | |||
101 | 10 | $this->handleCommit(); |
|
102 | 10 | } |
|
103 | |||
104 | /** |
||
105 | * This method ensures all the classes required to make the plugin working |
||
106 | * are loaded. |
||
107 | * |
||
108 | * It's required to avoid composer looking for classes no longer existing |
||
109 | * (after the plugin is updated or removed for example). |
||
110 | * |
||
111 | * Lot of classes (like operation handlers, url generators, Outputter, etc) |
||
112 | * do not need this because they are already autoloaded at the activation |
||
113 | * of the plugin. |
||
114 | */ |
||
115 | 14 | private function autoloadNeededClasses() |
|
126 | |||
127 | 14 | private function setupConfig() |
|
128 | { |
||
129 | 14 | $builder = new ConfigBuilder(); |
|
130 | |||
131 | 14 | $this->config = $builder->build( |
|
132 | 14 | $this->configLocator->getConfig(self::EXTRA_KEY), |
|
133 | 14 | $this->configLocator->getPath(self::EXTRA_KEY) |
|
134 | 14 | ); |
|
135 | |||
136 | 14 | static::$postUpdatePriority = $this->config->getPostUpdatePriority(); |
|
137 | |||
138 | 14 | if (count($builder->getWarnings()) > 0) { |
|
139 | $this->io->writeError('<error>Invalid config for composer-changelogs plugin:</error>'); |
||
140 | foreach ($builder->getWarnings() as $warning) { |
||
141 | $this->io->write(' ' . $warning); |
||
142 | } |
||
143 | } |
||
144 | 14 | } |
|
145 | |||
146 | 10 | private function handleCommit() |
|
164 | |||
165 | 6 | private function doCommit() |
|
166 | { |
||
167 | 6 | if (!$this->config->getCommitBinFile()) { |
|
168 | $this->io->writeError('<error>No "commit-bin-file" for composer-changelogs plugin. Commit not done.</error>'); |
||
169 | |||
184 | } |
||
185 |
Let’s assume you have a class which uses late-static binding:
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()
on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClass
to useself
instead: