diff --git a/src/Exception.php b/src/Exception.php index d2d4ca8..5fe15dc 100644 --- a/src/Exception.php +++ b/src/Exception.php @@ -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 + * @author Shahzadi Afsara + * + * @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\\'; @@ -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 @@ -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); @@ -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); } @@ -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); } @@ -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)); } /** @@ -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); @@ -215,4 +238,4 @@ public function run() : void \call_user_func([$this, $this->executor], $this->primaryClass, $this->secondryClass); } } -?> \ No newline at end of file +?>