-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1799 from hydephp/create-invalid-configuration-ex…
…ception-class [2.x] Add an invalid configuration exception class
- Loading branch information
Showing
3 changed files
with
76 additions
and
0 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
47 changes: 47 additions & 0 deletions
47
packages/framework/src/Framework/Exceptions/InvalidConfigurationException.php
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 |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Hyde\Framework\Exceptions; | ||
|
||
use Hyde\Facades\Filesystem; | ||
use InvalidArgumentException; | ||
|
||
use function assert; | ||
use function explode; | ||
use function realpath; | ||
|
||
class InvalidConfigurationException extends InvalidArgumentException | ||
{ | ||
public function __construct(string $message = 'Invalid configuration detected.', ?string $namespace = null, ?string $key = null) | ||
{ | ||
if ($namespace && $key) { | ||
[$this->file, $this->line] = $this->findConfigLine($namespace, $key); | ||
} | ||
|
||
parent::__construct($message); | ||
} | ||
|
||
/** | ||
* @experimental Please report any issues with this method to the authors at https://github.com/hydephp/develop/issues | ||
* | ||
* @return array{string, int} | ||
*/ | ||
protected function findConfigLine(string $namespace, string $key): array | ||
{ | ||
$file = realpath("config/$namespace.php"); | ||
$contents = Filesystem::getContents($file); | ||
$lines = explode("\n", $contents); | ||
|
||
foreach ($lines as $line => $content) { | ||
if (str_contains($content, "'$key' =>")) { | ||
break; | ||
} | ||
} | ||
|
||
assert($file !== false); | ||
assert(isset($line)); | ||
|
||
return [$file, $line + 1]; | ||
} | ||
} |
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