-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
58f2243
commit 872ba2e
Showing
5 changed files
with
430 additions
and
6 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,7 +32,7 @@ | |
* @author kekefreedog <[email protected]> | ||
* @copyright 2022-2024 Kévin Zarshenas | ||
*/ | ||
class Composer{ | ||
class Composer { | ||
|
||
/** Constants | ||
****************************************************** | ||
|
@@ -460,6 +460,96 @@ public static function delete(array $values = []):bool{ | |
|
||
} | ||
|
||
/** | ||
* Remove value | ||
* | ||
* Set Value in composer file | ||
* | ||
* @param string $key Parameter of config to set | ||
* @param string $path Path of the config | ||
* @return void | ||
*/ | ||
public static function removeValue(string $key = "", string $file = "composer.json"):void { | ||
|
||
# Check if file exists into PATH | ||
if(array_key_exists($file, self::PATH)) | ||
|
||
# Set file | ||
$file = self::PATH[$file]; | ||
|
||
# Get mime type | ||
$fileData = Json::open($file); | ||
|
||
# Parse key | ||
$key = str_replace(self::SEPARATOR, "___", $key); | ||
|
||
# Explode keys | ||
$keys = explode("___", $key); | ||
|
||
# Check config file | ||
if(!$file || empty($keys)) | ||
|
||
# Stop script | ||
return; | ||
|
||
# Check if is array | ||
if(!is_array($fileData)) | ||
|
||
# New Exception | ||
throw new CrazyException( | ||
"Config \"".$keys[0]."\" isn't valid... Array waited !", | ||
500, | ||
[ | ||
"custom_code" => "composer-011", | ||
] | ||
); | ||
|
||
# Declare cursor | ||
$cursor = &$fileData; | ||
$parentCursor = null; | ||
|
||
# Iteration filedata | ||
$i=0;while(isset($keys[$i])){ | ||
|
||
# Check cursor.key isset | ||
if(!isset($cursor[$keys[$i]])) | ||
|
||
# Exit | ||
return; | ||
|
||
# Check if the last cursor | ||
$j = $i+1; | ||
if(!isset($keys[$j])) | ||
|
||
# Parent Cursor | ||
$parentCursor = &$cursor; | ||
|
||
else | ||
|
||
# Update the cursor | ||
$cursor = &$cursor[$keys[$i]]; | ||
|
||
$i++;} | ||
|
||
# Check parent cursor | ||
if($parentCursor){ | ||
|
||
# Last valid key | ||
$i--; | ||
|
||
# Unset value | ||
unset($parentCursor[$keys[$i]]); | ||
|
||
} | ||
|
||
# Set last resultCursor | ||
$result = $fileData; | ||
|
||
# Set result | ||
Json::set($file, $result, false, false); | ||
|
||
} | ||
|
||
/** | ||
* Process value | ||
* | ||
|
@@ -588,15 +678,15 @@ public static function requirePackage(string $package = "", bool $checkPackage = | |
]; | ||
|
||
# Add package in json in composer.json | ||
self::set($arrayToMerge); | ||
self::set($arrayToMerge, $file); | ||
|
||
# Check update Composer | ||
if($updateComposer) | ||
|
||
# Composer Update | ||
Composer::exec("update", "", false); | ||
|
||
} | ||
} | ||
|
||
/** | ||
* Require Package With Specific Version | ||
|
@@ -649,7 +739,7 @@ public static function requirePackageWithSpecificVersion(string $package = "", s | |
]; | ||
|
||
# Add package in json in composer.json | ||
self::set($arrayToMerge); | ||
self::set($arrayToMerge, $file); | ||
|
||
# Check update Composer | ||
if($updateComposer) | ||
|
@@ -659,6 +749,56 @@ public static function requirePackageWithSpecificVersion(string $package = "", s | |
|
||
} | ||
|
||
/** | ||
* Remove Package | ||
* | ||
* Remove package in composer config file | ||
* | ||
* @param string $package Package to remove in composer | ||
* @param bool $updateComposer Update composer | ||
* @param string $file Composer file | ||
* @return void | ||
*/ | ||
public static function removePackage(string|array $package = "", bool $updateComposer = true, string $file = "composer.json"):void { | ||
|
||
# Check package name | ||
if(!$package || empty($package)) | ||
|
||
# New error | ||
throw new CrazyException( | ||
"Composer package name \"".(is_string($package) ? $package : json_encode($package))."\” you want require looks strange, please respect \"vendor/package\" format !", | ||
500, | ||
[ | ||
"custom_code" => "composer-004", | ||
] | ||
); | ||
|
||
# Array to merge | ||
$require = self::get("require", $file); | ||
|
||
# check package | ||
if(is_string($package)) | ||
|
||
# Set package | ||
$package = [$package]; | ||
|
||
# Iteration of package | ||
foreach($package as $v) | ||
|
||
# Check if in require | ||
if(array_key_exists($v, $require)) | ||
|
||
# Remove package from require | ||
self::removeValue("require.$v", $file); | ||
|
||
# Check update Composer | ||
if($updateComposer) | ||
|
||
# Composer Update | ||
Composer::exec("update", "", false); | ||
|
||
} | ||
|
||
/** | ||
* Check Package Exists | ||
* | ||
|
@@ -733,5 +873,12 @@ private static function _readPath(string $path = ""):string { | |
return $result; | ||
|
||
} | ||
|
||
/** Public constants | ||
****************************************************** | ||
*/ | ||
|
||
/** @const separator */ | ||
public const SEPARATOR = [".", "___"]; | ||
|
||
} |
This file contains 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
Oops, something went wrong.