| 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 |
|