PluginProbe
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF / 1.10
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF v1.10
2.3.4 2.3.3 2.3.2 2.3.1 2.3.0 2.2.9 2.2.8 trunk 1.10 1.3.3 1.3.4 1.3.5 1.3.5.1 1.3.5.2 1.3.6 1.3.6.1 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.5 All 103 releases
imagify / vendor / dangoodman / composer-for-wordpress / ComposerForWordpress.php

ComposerForWordpress.php in Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF 1.10, at vendor/dangoodman/composer-for-wordpress/ComposerForWordpress.php

76 lines 2.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Dangoodman\ComposerForWordpress;
3
4 use Composer\Composer;
5 use Composer\EventDispatcher\EventSubscriberInterface;
6 use Composer\IO\IOInterface;
7 use Composer\Plugin\PluginInterface;
8 use Composer\Script\Event;
9 use Composer\Script\ScriptEvents;
10
11
12 class ComposerForWordpress implements PluginInterface, EventSubscriberInterface
13 {
14 public function activate(Composer $composer, IOInterface $io)
15 {
16 }
17
18 public function deactivate(Composer $composer, IOInterface $io)
19 {
20 }
21
22 public function uninstall(Composer $composer, IOInterface $io)
23 {
24 }
25
26 public static function getSubscribedEvents()
27 {
28 return array(
29 ScriptEvents::POST_AUTOLOAD_DUMP => array(
30 array('onPostAutoloadDump', 0)
31 ),
32 );
33 }
34
35 public function onPostAutoloadDump(Event $event)
36 {
37 $composerConfig = $event->getComposer()->getConfig();
38 $composerAutoloadDir = "{$composerConfig->get('vendor-dir')}/composer";
39
40 $classLoader = "{$composerAutoloadDir}/ClassLoader.php";
41 $autoloadReal = "{$composerAutoloadDir}/autoload_real.php";
42 $autoloadStatic = "{$composerAutoloadDir}/autoload_static.php";
43
44 $suffix = $composerConfig->get('classloader-suffix') ?: md5(uniqid('', true));
45
46 self::replaceInFiles(
47 array($classLoader, $autoloadReal),
48 '/Composer\\\\Autoload(;|\\\\(?!ComposerStaticInit))/',
49 "Composer\\Autoload{$suffix}\$1"
50 );
51
52 self::replaceInFiles(
53 array($autoloadStatic),
54 array(
55 '/\bClassLoader\b/'
56 => "ClassLoader{$suffix}",
57 '/'.preg_quote("\nnamespace Composer\\Autoload;\n", '/').'/'
58 => "$0\nuse Composer\\Autoload{$suffix}\\ClassLoader as ClassLoader{$suffix};\n\n",
59 )
60 );
61 }
62
63 private static function replaceInFiles(array $files, $search, $replace = null)
64 {
65 if (func_num_args() == 3) {
66 $search = array($search => $replace);
67 }
68
69 foreach ($files as $file) {
70 $contents = file_get_contents($file);
71 $contents = preg_replace(array_keys($search), array_values($search), $contents);
72 file_put_contents($file, $contents);
73 }
74 }
75 }
76