Skip to content

Commit

Permalink
Update Exception.php
Browse files Browse the repository at this point in the history
  • Loading branch information
shahzadamodassir authored Nov 14, 2024
1 parent c70eac3 commit f351ee1
Showing 1 changed file with 30 additions and 7 deletions.
37 changes: 30 additions & 7 deletions src/Exception.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,28 @@
declare(strict_types=1);

/**
* Exception library managing multiple exceptions with CLI support for creating,
* removing, and updating exceptions.
*
* The (exception) Github repository
* @see https://github.com/lazervel/exception
*
* @author Shahzada Modassir <[email protected]>
* @author Shahzadi Afsara <[email protected]>
*
* @copyright (c) Shahzada Modassir
* @copyright (c) Shahzadi Afsara
*
* @license MIT License
* @see https://github.com/lazervel/exception/blob/main/LICENSE
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Modassir\Exception;

class Exception
{
private const CUSTOM_EXCEPTION_DIR = CUSTOM_EXCEPTION_DIR ?? __DIR__.DIRECTORY_SEPARATOR.'Custom';
private const CUSTOM_EXCEPTION_DIR = __DIR__.DIRECTORY_SEPARATOR.'Custom';
private const CUSTOM_EXCEPTION_NAMESPACE = 'namespace Modassir\\Exception\\Custom;';
private const CUSTOM_EXCEPTION_USE = 'use Modassir\\Exception\\Exception\\';

Expand All @@ -32,6 +47,14 @@ public function __construct($executor, $primaryClass, $secondryClass)
$this->secondryClass = $secondryClass;
}

/**
* @return string custom directory location.
*/
private function CED() : string
{
return \defined('CUSTOM_EXCEPTION_DIR') ? CUSTOM_EXCEPTION_DIR : $this->CED();
}

/**
* @param array $argv [required]
* @return \Modassir\Exception\Exception
Expand Down Expand Up @@ -105,7 +128,7 @@ public function rename(string $exception, string $newClass) : void
);
}

$lines = @\file(($path = \sprintf('%s/%s.php', self::CUSTOM_EXCEPTION_DIR, $exception)));
$lines = @\file(($path = \sprintf('%s/%s.php', $this->CED(), $exception)));

$matches_signature = \sprintf('class %s extends', $exception);

Expand All @@ -117,7 +140,7 @@ public function rename(string $exception, string $newClass) : void
}

@\file_put_contents($path, \join("", $lines));
\rename($path, \sprintf('%s/%s.php', self::CUSTOM_EXCEPTION_DIR, $newClass));
\rename($path, \sprintf('%s/%s.php', $this->CED(), $newClass));
self::exit("\nSuccess: Exception [%s] has been renamed.\n\n", false, $newClass);
}

Expand All @@ -133,7 +156,7 @@ public function make(string $subClass, string $newClass) : void
self::exit("\nError: Could not create Exception Invalid subclass [%s].\n\n", true, $subClass);
}

if (($dir = self::CUSTOM_EXCEPTION_DIR) && !\is_dir($dir)) {
if (($dir = $this->CED()) && !\is_dir($dir)) {
\mkdir($dir);
}

Expand All @@ -154,7 +177,7 @@ public function make(string $subClass, string $newClass) : void

private function exists(string $exception) : bool
{
return \file_exists(\sprintf('%s/%s.php', self::CUSTOM_EXCEPTION_DIR, $exception));
return \file_exists(\sprintf('%s/%s.php', $this->CED(), $exception));
}

/**
Expand All @@ -171,7 +194,7 @@ public function remove(string $exception) : void
}

if (self::exists($exception)) {
\unlink(\sprintf('%s/%s.php', self::CUSTOM_EXCEPTION_DIR, $exception));
\unlink(\sprintf('%s/%s.php', $this->CED(), $exception));
self::exit("\nSuccess: Exception [%s] has been removed.\n\n", false, $exception);
} else {
self::exit("\nError: Could not remove Exception [%s] is not found.\n\n", true, $exception);
Expand Down Expand Up @@ -215,4 +238,4 @@ public function run() : void
\call_user_func([$this, $this->executor], $this->primaryClass, $this->secondryClass);
}
}
?>
?>

0 comments on commit f351ee1

Please sign in to comment.