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