- Added a new post-install script for composer which adds a project skeleton "ready to use". - Error 500 are now handled properly in production; waiting to see if adjustements will be required before adding other code pages.
92 lines
3.4 KiB
PHP
92 lines
3.4 KiB
PHP
<?php
|
|
|
|
namespace Lean;
|
|
|
|
use Composer\EventDispatcher\Event;
|
|
|
|
class Composer
|
|
{
|
|
public static function postInstall(Event $event) : void
|
|
{
|
|
$event->getIO()->write("post-install script executing ...");
|
|
|
|
if ( $event->getIO()->askConfirmation("Do you want to install Lean's Project Skeleton ? ( 'yes' or 'no' (default) ): ", false) ) {
|
|
$ns = trim(static::getNamespaceFromAutoload($event), '\\');
|
|
|
|
$replace = [
|
|
'%NAMESPACE%' => $ns,
|
|
'%ESCAPED_NAMESPACE%' => addslashes($ns),
|
|
'%APPKEY%' => strtolower(str_replace('\\', '.', $ns)),
|
|
'%RAND64%' => substr(base64_encode(random_bytes(100)), 0, 64),
|
|
];
|
|
|
|
$dir = static::createPath();
|
|
|
|
if ( is_writable($dir) ) {
|
|
$source = static::createPath('vendor', 'mcnd', 'lean', 'skeleton');
|
|
$dest = static::createPath('skeleton');
|
|
|
|
`cp -a $source $dest`;
|
|
|
|
$iterator = new \RecursiveDirectoryIterator($dest);
|
|
$iterator->setFlags(\RecursiveDirectoryIterator::SKIP_DOTS);
|
|
|
|
foreach (new \RecursiveIteratorIterator($iterator) as $file) {
|
|
if ( in_array($file->getExtension(), [ 'php', 'phtml', 'env' ]) ) {
|
|
$content = str_replace(array_keys($replace), array_values($replace), file_get_contents($file->getPathname()));
|
|
|
|
file_put_contents($file->getPathname(), $content);
|
|
}
|
|
}
|
|
|
|
foreach(scandir(static::createPath('skeleton')) as $item) {
|
|
if ( in_array($item, [ '.', '..' ]) ) continue;
|
|
|
|
rename(static::createPath('skeleton', $item), static::createPath($item));
|
|
}
|
|
|
|
rmdir($dest);
|
|
|
|
$event->getIO()->write("Installation completed.");
|
|
}
|
|
else {
|
|
$event->getIO()->writeError(sprintf("The user you are running this script with doesn't seem to have a writable permission on directory %s", static::createPath()));
|
|
}
|
|
}
|
|
}
|
|
|
|
protected static function readComposerJson(Event $event) : ? array
|
|
{
|
|
$path = static::createPath('composer.json');
|
|
|
|
return file_exists($path) ? json_decode(file_get_contents($path), true) : null;
|
|
}
|
|
|
|
protected static function getNamespaceFromAutoload(Event $event) : ? string
|
|
{
|
|
if ( null !== $composerJson = static::readComposerJson($event) ) {
|
|
if ( $psr4 = $composerJson['autoload']['psr-4'] ?? false ) {
|
|
foreach($psr4 as $ns => $directory) {
|
|
if ($directory === 'src/') {
|
|
return $ns;
|
|
}
|
|
}
|
|
}
|
|
else {
|
|
$event->getIO()->writeError("Your composer file do not seem to contains a valid psr-4 project of it's autoload array.");
|
|
}
|
|
}
|
|
else {
|
|
$event->getIO()->writeError("Composer.json file not found. Have this script been launch from the Composer CLI at the root of your project ?");
|
|
}
|
|
|
|
$event->getIO()->writeError("Your composer file do not seem to contains a valid psr-4 project pointing to the src folder.");
|
|
|
|
return null;
|
|
}
|
|
|
|
protected static function createPath(... $path) : string
|
|
{
|
|
return implode(DIRECTORY_SEPARATOR, array_merge([ $_SERVER['PWD'] ], $path));
|
|
}
|
|
} |