Created
November 23, 2016 22:45
-
-
Save xphere/18400f43cd76d130a87197a0d676d8df to your computer and use it in GitHub Desktop.
Dump paths defined in composer.json into lib/paths.php to load in the kernel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Composer\Script\Event; | |
class PathDumper | |
{ | |
static $defaults = [ | |
'symfony-app-dir' => 'boot', | |
'symfony-bin-dir' => 'bin', | |
'symfony-config-dir' => 'etc', | |
'symfony-test-dir' => 'tests', | |
'symfony-var-dir' => 'var', | |
'symfony-web-dir' => 'public', | |
]; | |
public static function dumpKernelPaths(Event $event) | |
{ | |
$composer = $event->getComposer(); | |
$rootPackage = $composer->getPackage(); | |
$extra = self::addDefaultValues($rootPackage->getExtra()); | |
$rootPackage->setExtra($extra); | |
$projectDir = getcwd(); | |
$paths = self::extractPaths($extra, $projectDir); | |
$config = $composer->getConfig(); | |
$vendorDir = $config->get('vendor-dir'); | |
self::updatePaths($vendorDir, $paths); | |
} | |
private static function extractPaths(array $extra, $basePath) | |
{ | |
$paths = []; | |
foreach ($extra as $key => $value) { | |
if (preg_match('~^symfony-([^-]+)-dir$~', $key, $matches)) { | |
$paths[$matches[1]] = $basePath . '/' . trim($value, '/'); | |
} | |
} | |
return $paths; | |
} | |
private static function addDefaultValues(array $extra) | |
{ | |
return array_merge(self::$defaults, $extra); | |
} | |
private static function updatePaths($directory, array $paths) | |
{ | |
$dump = var_export($paths, true); | |
file_put_contents( | |
"{$directory}/paths.php", | |
"<?php return {$dump};" | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment