Skip to content

Instantly share code, notes, and snippets.

@Hillzacky
Created May 14, 2024 17:48
Show Gist options
  • Save Hillzacky/be0cb2e9feb53f6543d395e3e5c2fa67 to your computer and use it in GitHub Desktop.
Save Hillzacky/be0cb2e9feb53f6543d395e3e5c2fa67 to your computer and use it in GitHub Desktop.
class Composer {
public static function install() {
$dependencies = $this->readComposerJson();
$this->pullDependencies($dependencies);
$this->createAutoload();
}
private function readComposerJson() {
// Membaca dan menguraikan composer.json
$composerJson = file_get_contents('composer.json');
$composerData = json_decode($composerJson, true);
return $composerData['require'];
}
private function pullDependencies($dependencies) {
// Mengunduh dan mengekstrak dependensi
foreach ($dependencies as $package => $version) {
// Misalnya, kita akan mengunduh dari GitHub
$url = "https://api.github.com/repos/{$package}/zipball/{$version}";
$zipFile = __DIR__ . "/{$package}-{$version}.zip";
// Mengunduh file zip
$contents = file_get_contents($url);
file_put_contents($zipFile, $contents);
// Mengekstrak file zip
$zip = new ZipArchive;
if ($zip->open($zipFile) === TRUE) {
$zip->extractTo(__DIR__ . '/vendor');
$zip->close();
unlink($zipFile);
} else {
echo 'Gagal mengekstrak';
}
}
}
private function updateAutoload() {
// Mengatur autoloaders secara manual
spl_autoload_register(function ($class) {
$file = __DIR__ . '/vendor/' . str_replace('\\', '/', $class) . '.php';
if (file_exists($file)) {
require_once $file;
return true;
}
return false;
});
}
private function createAutoload() {
$autoloadContent = '<?php' . PHP_EOL;
$autoloadContent .= 'foreach (glob(__DIR__ . "/vendor/*/*.php") as $file) {' . PHP_EOL;
$autoloadContent .= ' require_once $file;' . PHP_EOL;
$autoloadContent .= '}' . PHP_EOL;
$autoloadFile = __DIR__ . '/autoload.php';
file_put_contents($autoloadFile, $autoloadContent);
}
}
// Contoh penggunaan:
$composer = Composer::install();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment