Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
shahzadamodassir committed Nov 14, 2024
1 parent 9881b0c commit 5a0ec6b
Show file tree
Hide file tree
Showing 28 changed files with 532 additions and 0 deletions.
18 changes: 18 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
root = true

[*]
charset = utf-8
end_of_line = lf
indent_size = 2
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true

[*.md]
trim_trailing_whitespace = false

[*.{yml,yaml}]
indent_size = 2

[docker-compose.yml]
indent_size = 2
20 changes: 20 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
* text=auto

/.editorconfig export-ignore
/.github export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore
/.mailmap export-ignore

/node_modules export-ignore
/vendor export-ignore
/tests export-ignore
/vendor-bin export-ignore

/CHANGELOG.md export-ignore
/README.md export-ignore
/AUTHORS export-ignore
/UPGRADING.md export-ignore
/CODE_OF_CONDUCT.md export-ignore
/CONTRIBUTING.md export-ignore
/SECURITY.md export-ignore
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
composer.lock
vendor
node_modules
vendor-bin
1 change: 1 addition & 0 deletions .mailmap
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Shahzada Modassir <[email protected]>
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Shahzada Modassir <[email protected]>
52 changes: 52 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
"name": "modassir/exception",
"description": "Exception library managing multiple exceptions with CLI support for creating, removing, and updating exceptions.",
"type": "library",
"license": "MIT",
"keywords": [
"exceptions",
"custom exceptions",
"error handling",
"exception",
"cli",
"exception manager",
"php exceptions"
],
"autoload": {
"psr-4": {
"Modassir\\Exception\\": "src/"
}
},
"require": {
"php": "^7.2 || ^8.0"
},
"bin": [
"excep"
],
"authors": [
{
"name": "Shahzada Modassir",
"email": "[email protected]",
"homepage": "https://github.com/shahzadamodassir"
}
],
"funding": [
{
"type": "YouTube",
"https": "https://youtube.com/@shahzadamodassir"
},
{
"type": "Patreon",
"url": "https://patreon.com/shahzadamodassir"
},
{
"type": "Github",
"url": "https://github.com/shahzadamodassir"
},
{
"type": "Opencollective",
"url": "https://opencollective.com/shahzadamodassir"
}
],
"minimum-stability": "stable"
}
18 changes: 18 additions & 0 deletions excep
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env php
<?php
declare(strict_types=1);

foreach([__DIR__.'/../vendor/autoload.php', __DIR__.'/../../vendor/autoload.php'] as $autoloader) {
if (\file_exists($autoloader)) {
define('INSTALLED_AUTOLOAD_EXCEPTION_PATH', $autoloader);
}
}

if (!defined('INSTALLED_AUTOLOAD_EXCEPTION_PATH')) {

}

require INSTALLED_AUTOLOAD_EXCEPTION_PATH;

Modassir\Exception\Exception::argv($argv)->run();
?>
218 changes: 218 additions & 0 deletions src/Exception.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
<?php

declare(strict_types=1);

/**
*
*/
namespace Modassir\Exception;

class Exception
{
private const CUSTOM_EXCEPTION_DIR = 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\\';

private $secondryClass;
private $primaryClass;
private $executor;

/**
*
* @param string $executor [required]
* @param string $primaryClass [required]
* @param string $secondryClass [required]
*
* @return void
*/
public function __construct($executor, $primaryClass, $secondryClass)
{
$this->executor = $executor;
$this->primaryClass = $primaryClass;
$this->secondryClass = $secondryClass;
}

/**
* @param array $argv [required]
* @return \Modassir\Exception\Exception
*/
public static function argv(array $argv)
{
\array_shift($argv);

$command = "-----------------------\n\n";
$commands = [
'Subclass:make CustomClass' => 'Creates a new custom Exception class extends with subClass.',
'CustomClass:rename NewClass' => 'Renamed existing custom Exception class.',
'NewClass:remove' => 'Removed existing custom Exception class.'
];

$maxLenght = \strlen(\array_keys($commands)[1]);

foreach($commands as $cmd => $desc) {
$command .= \sprintf(
"%s\x20 %s\n",
self::format($cmd.\str_repeat(" ", $maxLenght - \strlen($cmd)), false),
$desc
);
}

$command .="\n";

if ($argv === []) {
echo self::format("Command required:\n\n", true);
self::exit("Exception Command Help:\n%s", false, $command);
}

if (\count($argv) > 2) {
self::exit(
"\nError: Could not run command required #2 arguments but given [%s] extra.\n\n", true,
\implode(',', \array_slice($argv, 2))
);
}

$primary = \explode(':', $argv[0]);

if (\count($primary) !== 2) {
echo self::format( "\nError: Missing executor :target with [%s]", true, $argv[0]);
self::exit("\n\nException Command Help:\n%s", false, $command);
}

$secondry = end($argv);

return new self(\array_pop($primary), $primary[0], $secondry !== $argv[0] ? $secondry : null);
}

/**
* @param string $exception [required]
* @param string $newClass [required]
*
* @return void
*/
public function rename(string $exception, string $newClass) : void
{
if (!self::exists($exception)) {
self::exit(
"\nError: Could not rename Exception [%s] not found.\n\n", true, $exception
);
}

// Handle: Clousion Exception class.
if ($exception === $newClass) {
self::exit(
"\nError: Could not rename Exception [%s] already renamed.\n\n", true,
$exception
);
}

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

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

foreach($lines as $i => $line) {
if (\str_starts_with($line, $matches_signature)) {
$lines[$i] = \str_replace($exception, $newClass, $line);
break;
}
}

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

/**
* @param string $subClass [required]
* @param string $newClass [required]
*
* @return void
*/
public function make(string $subClass, string $newClass) : void
{
if (!\class_exists($subClass)) {
self::exit("\nError: Could not create Exception Invalid subclass [%s].\n\n", true, $subClass);
}

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

if (self::exists($newClass)) {
self::exit("\nError: Could not create Exception [%s] already exists.\n\n", true, $newClass);
}

$code = \sprintf("class %s extends %s\n{\n\t//\n}", $newClass, $subClass);
$use = \sprintf("%s%s;", self::CUSTOM_EXCEPTION_USE, $subClass);

$newException = \sprintf("<?php\n\ndeclare(strict_types=1);\n\n%s\n\n%s\n\n%s\n?>",
self::CUSTOM_EXCEPTION_NAMESPACE, $use, $code);

@\file_put_contents(\sprintf('%s/%s.php', $dir, $newClass), $newException);

self::exit("\nSuccess: Exception %s subclass of %s created!\n\n", false, $newClass, $subClass);
}

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

/**
* @param string $exception [required]
* @return void
*/
public function remove(string $exception) : void
{
if ($this->secondryClass) {
self::exit(
"\nError: Could not remove Exception required #2 arguments but given #3 [%s].\n\n", true,
$this->secondryClass
);
}

if (self::exists($exception)) {
\unlink(\sprintf('%s/%s.php', self::CUSTOM_EXCEPTION_DIR, $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);
}
}

/**
* @param string $format [required]
* @param bool $isError [required]
* @param string $values [required]
*
* @return void
*/
private static function exit(string $format, bool $isError = true, ...$values) : void
{
exit(self::format($format, $isError, ...$values));
}

/**
* @param string $format [required]
* @param bool $isError [required]
* @param string $values [required]
*
* @return string formated adjusted string with colored terminal show
*/
private static function format(string $format, bool $isError = true, ...$values) : string
{
$cmdColor = $isError ? '197;15;31' : '19;161;14';
return \sprintf(\sprintf("\033[38;2;%sm%s\033[0m", $cmdColor, $format), ...$values);
}

/**
*
*/
public function run() : void
{
if (!\method_exists($this, $this->executor)) {
self::exit("\nError: Could not run command invalid executor [%s].\n\n", true, $this->executor);
}

\call_user_func([$this, $this->executor], $this->primaryClass, $this->secondryClass);
}
}
?>
10 changes: 10 additions & 0 deletions src/Exception/ArithmeticErrorException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);
namespace Modassir\Exception\Exception;

class ArithmeticErrorException extends \ArithmeticError implements ExceptionInterface
{
//
}
?>
10 changes: 10 additions & 0 deletions src/Exception/BadFunctionCallException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);
namespace Modassir\Exception\Exception;

class BadFunctionCallException extends LogicException
{
//
}
?>
10 changes: 10 additions & 0 deletions src/Exception/BadMethodCallException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);
namespace Modassir\Exception\Exception;

class BadMethodCallException extends BadFunctionCallException
{
//
}
?>
10 changes: 10 additions & 0 deletions src/Exception/DivisionByZeroErrorExcepion.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);
namespace Modassir\Exception\Exception;

class DivisionByZeroErrorException extends \DivisionByZeroError implements ExceptionInterface
{
//
}
?>
Loading

0 comments on commit 5a0ec6b

Please sign in to comment.