lean/src/Composer.php
2023-12-04 12:34:31 -05:00

135 lines
4.8 KiB
PHP

<?php
namespace Lean;
use Composer\EventDispatcher\Event;
use Kash\CacheInvalidator;
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.");
static::postUpdate($event);
}
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()));
}
}
}
public static function postUpdate(Event $event) : void
{
$event->getIO()->write("post-update script executing ...");
$path = static::createPath('var/cache/version.cache');
if ( is_writeable(dirname($path)) ) {
$event->getIO()->write("regenerating cache file version to invalidate cache for path '$path'");
new CacheInvalidator($path, true);
}
if ( file_exists($container = static::createPath('var/cache/di/CompiledContainer.php')) ) {
unlink($container);
}
if (function_exists("apcu_clear_cache")) {
apcu_clear_cache();
}
}
public static function readComposerLock() : false|array
{
static $content = null;
$path = static::createPath('composer.lock');
if (! file_exists($path) ) {
throw new \UnexpectedValueException("Composer file 'composer.lock' could not be found within your project's root folder.");
}
return $content ??= file_exists($path) ? json_decode(file_get_contents($path), true) : false;
}
public static function readComposerJson() : false|array
{
static $content = null;
$path = static::createPath('composer.json');
if (! file_exists($path) ) {
throw new \UnexpectedValueException("Composer file 'composer.json' could not be found within your project's root folder.");
}
return $content ??= file_exists($path) ? json_decode(file_get_contents($path), true) : false;
}
protected static function getNamespaceFromAutoload(Event $event) : ? string
{
if ( false !== $composerJson = static::readComposerJson() ) {
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'] ?? getenv('PROJECT_PATH') ], $path));
}
}