Skip to content

Commit

Permalink
Implement extension removal
Browse files Browse the repository at this point in the history
  • Loading branch information
kekefreedog committed Apr 6, 2024
1 parent 58f2243 commit 872ba2e
Show file tree
Hide file tree
Showing 5 changed files with 430 additions and 6 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,26 @@ Delete router type
php vendor/kzarshenas/crazyphp/bin/CrazyCommand delete routerType
```

## Extension

New router type

```sh
php vendor/kzarshenas/crazyphp/bin/CrazyCommand new extension
```

Update extension

```sh
php vendor/kzarshenas/crazyphp/bin/CrazyCommand update extension
```

Delete router type

```sh
php vendor/kzarshenas/crazyphp/bin/CrazyCommand delete extension
```

## Trash

Clean trash
Expand Down
17 changes: 16 additions & 1 deletion src/Cli/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
/**
* Dependances
*/
use CrazyPHP\Exception\CrazyException;
use League\CLImate\CLImate;

/**
Expand Down Expand Up @@ -63,11 +64,25 @@ public function __construct(array $valueCollection = []){
foreach($valueCollection as $value){

# Check select is callable
if(isset($value['select']) && is_callable($value['select']))
if(isset($value['select']) && is_callable($value['select'])){

# Set select
$value['select'] = $value['select']();

# Check if empty
if(empty($value['select']))

# New error
throw new CrazyException(
"No items found",
500,
[
"custom_code" => "form-001"
]
);

}

# Check select is callable
if(isset($value['default']) && is_callable($value['default']))

Expand Down
155 changes: 151 additions & 4 deletions src/Library/File/Composer.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
* @author kekefreedog <[email protected]>
* @copyright 2022-2024 Kévin Zarshenas
*/
class Composer{
class Composer {

/** Constants
******************************************************
Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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
*
Expand Down Expand Up @@ -733,5 +873,12 @@ private static function _readPath(string $path = ""):string {
return $result;

}

/** Public constants
******************************************************
*/

/** @const separator */
public const SEPARATOR = [".", "___"];

}
89 changes: 88 additions & 1 deletion src/Model/Extension/Delete.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
use CrazyPHP\Interface\CrazyCommand;
use CrazyPHP\Library\File\Composer;
use CrazyPHP\Library\Array\Arrays;
use CrazyPHP\Library\File\Trash;
use CrazyPHP\Library\File\File;

/**
* Delete Extension
Expand Down Expand Up @@ -209,7 +211,22 @@ public function runGetExtension():self {
*/
public function runRemoveExtensionFromConfig():self {

print_r($this->data);
# Check extensions to remove
if(isset($this->data["toRemove"]) && !empty($this->data["toRemove"])){

# Get extensions installed
$extensionsInstalled = FileConfig::getValue("Extension.installed");

# Iteration of extension to remove
foreach($this->data["toRemove"] as $extensionName => $extension)

# Check if in extensionsInstalled
if(array_key_exists($extensionName, $extensionsInstalled))

# Remove extension
FileConfig::removeValue("Extension.installed.$extensionName");

}

# Return instance
return $this;
Expand All @@ -225,6 +242,29 @@ public function runRemoveExtensionFromConfig():self {
*/
public function runBackupScripts():self {

# Check extensions to remove
if(isset($this->data["toRemove"]) && !empty($this->data["toRemove"]))

# Iteration of extension to remove
foreach($this->data["toRemove"] as $extensionName => $extension)

# Check script
if(isset($extension["scripts"]) && !empty($extension["scripts"]))

# Iteration scripts
foreach($extension["scripts"] as $script)

# Check destination
if(isset($script["destination"]) && File::exists($script["destination"])){

# Set key
$hierarchy = "Extension/$extensionName";

# Send to trash
Trash::send($script["destination"], $hierarchy, false);

}

# Return instance
return $this;

Expand All @@ -238,6 +278,24 @@ public function runBackupScripts():self {
* @return self
*/
public function runRemoveScripts():self {

# Check extensions to remove
if(isset($this->data["toRemove"]) && !empty($this->data["toRemove"]))

# Iteration of extension to remove
foreach($this->data["toRemove"] as $extension)

# Check script
if(isset($extension["scripts"]) && !empty($extension["scripts"]))

# Iteration scripts
foreach($extension["scripts"] as $script)

# Check destination
if(isset($script["destination"]) && File::exists($script["destination"]))

# Remove file
File::remove($script["destination"]);

# Return instance
return $this;
Expand All @@ -252,6 +310,35 @@ public function runRemoveScripts():self {
* @return self
*/
public function runRemoveDependances():self {

# Check extensions to remove
if(isset($this->data["toRemove"]) && !empty($this->data["toRemove"]))

# Iteration of extension to remove
foreach($this->data["toRemove"] as $extensionName => $extension)

# Check script
if(isset($extension["dependencies"]) && !empty($extension["dependencies"]))

# Iteration of scripts
foreach($extension['dependencies'] as $manager => $packages)

# Check package
if(!empty($packages))

# Iteration packages
foreach($packages as $package => $version)

# Check manager
if($manager === "composer"){

# Append package
Composer::removePackage($package, false);

# Set manager true
$this->data["managers"]["composer"] = true;

}

# Return instance
return $this;
Expand Down
Loading

0 comments on commit 872ba2e

Please sign in to comment.