Skip to content

Commit c79b21d

Browse files
committed
fix: errors related to missing config file
- Throws an exception when the config hasn't been published - Update README to guide publish config after installed
1 parent 1555eb4 commit c79b21d

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ Cài đặt Laravel Number To Words thông qua [Composer](https://getcomposer.or
2626
composer require phpviet/laravel-number-to-words
2727
```
2828

29+
Publish file cấu hình thông qua câu lệnh:
30+
31+
```php
32+
php artisan vendor:publish --provider="PHPViet\Laravel\NumberToWords\ServiceProvider" --tag="config"
33+
```
34+
2935
## Cách sử dụng
3036

3137
### Các tính năng của extension:
@@ -99,13 +105,7 @@ n2c(9492.15, ['đô', 'xen']);
99105

100106
> Nếu như bạn cảm thấy cách đọc ở trên ổn rồi thì hãy bỏ qua bước này.
101107
102-
Đầu tiên để thay đổi cách đọc số bạn cần phải publish file cấu hình thông qua câu lệnh:
103-
104-
```php
105-
php artisan vendor:publish --provider="PHPViet\Laravel\NumberToWords\ServiceProvider" --tag="config"
106-
```
107-
108-
Sau khi publish xong ta sẽ có được file config `config/n2w.php` như sau:
108+
Đầu tiên, bạn mở file config `config/n2w.php` như sau:
109109

110110
```php
111111
return [

src/N2WFacade.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Illuminate\Support\Facades\Facade;
1212
use InvalidArgumentException;
1313
use PHPViet\NumberToWords\DictionaryInterface;
14+
use LogicException;
1415

1516
/**
1617
* @method static string toWords($number)
@@ -33,6 +34,8 @@ class N2WFacade extends Facade
3334
*/
3435
protected static function getFacadeAccessor(): Transformer
3536
{
37+
static::checkConfigIsPublished();
38+
3639
$dictionary = static::$dictionary ?? static::getDefaultDictionary();
3740
$dictionary = static::makeDictionary($dictionary);
3841

@@ -63,4 +66,17 @@ protected static function makeDictionary(string $dictionary): DictionaryInterfac
6366

6467
return app()->make($dictionaryClass);
6568
}
69+
70+
/**
71+
* Throw an Exception when the config is not exist
72+
* Please run: php artisan vendor:publish --provider="PHPViet\Laravel\NumberToWords\ServiceProvider" --tag="config"
73+
*
74+
* @throws LogicException
75+
*/
76+
protected static function checkConfigIsPublished()
77+
{
78+
if (!config()->has('n2w')) {
79+
throw new LogicException("The config file is not found. You must publish the config before using it!");
80+
}
81+
}
6682
}

0 commit comments

Comments
 (0)