From 976b4746ac8b6bc8646deb0314e3696ad7e5ee7f Mon Sep 17 00:00:00 2001 From: Geoffrey Rose Date: Sun, 17 Nov 2024 00:44:58 -0800 Subject: [PATCH] Laravel Facade (#25) --- README.md | 37 +++++++++++++++------------ composer.json | 12 +++++++-- src/USHolidays/Facades/USHolidays.php | 11 ++++++++ src/USHolidays/ServiceProvider.php | 20 +++++++++++++++ 4 files changed, 61 insertions(+), 19 deletions(-) create mode 100644 src/USHolidays/Facades/USHolidays.php create mode 100644 src/USHolidays/ServiceProvider.php diff --git a/README.md b/README.md index d775f44..f4a95f6 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ [![License](https://img.shields.io/github/license/geoffreyrose/us-holidays?style=flat-square)](https://github.com/geoffreyrose/us-holidays/blob/master/LICENSE) -# Carbon Support for US Holidays +# PHP: Adds Carbon Support for US Holidays + Laravel Facade This extends [Carbon](http://carbon.nesbot.com/) and adds support for 42 US holidays. ## Full Documentation @@ -68,29 +68,32 @@ This extends [Carbon](http://carbon.nesbot.com/) and adds support for 42 US holi ### Usage -#### With Composer +#### Installation ``` -$ composer require geoffreyrose/us-holidays +composer require geoffreyrose/us-holidays ``` +### With Plain PHP ```php -getHolidaysByYear(); +``` -use USHolidays\Carbon; +### With Laravel Facade +Laravel uses Package Auto-Discovery, which doesn't require you to manually add the ServiceProvider and Facade. +```php +$holidays = USHolidays::getHolidaysByYear(); ``` +## Examples + +**Note all examples below use Plain PHP (use USHolidays\Carbon) but can be swapped with Laravel Facade (USHolidays)** + ### Get Holiday By Year See [documentation](https://geoffreyrose.github.io/us-holidays/#getHolidaysByYear) for more details @@ -394,23 +397,23 @@ composer install Use locally installed carbon version ``` -$ ./vendor/bin/phpunit +./vendor/bin/phpunit // or with coverage -$ XDEBUG_MODE=coverage ./vendor/bin/phpunit +XDEBUG_MODE=coverage ./vendor/bin/phpunit ``` ---- Test against Carbon v2 ``` -$ ./tests/carbon-2.sh +./tests/carbon-2.sh ``` Test against Carbon v3 ``` -$ ./tests/carbon-3.sh +./tests/carbon-3.sh ``` ### See It Used in the Wild diff --git a/composer.json b/composer.json index c3d38d9..69f247b 100644 --- a/composer.json +++ b/composer.json @@ -22,7 +22,15 @@ "phpunit/phpunit": "^5|^6|^7.5|^8.0|^9.0|^10.0", "php-coveralls/php-coveralls": "^2.5" }, - "config": { - "discard-changes": true + "minimum-stability": "dev", + "extra": { + "laravel": { + "providers": [ + "USHolidays\\ServiceProvider" + ], + "aliases": { + "USHolidays": "USHolidays\\Facades\\USHolidays" + } + } } } diff --git a/src/USHolidays/Facades/USHolidays.php b/src/USHolidays/Facades/USHolidays.php new file mode 100644 index 0000000..aba69c7 --- /dev/null +++ b/src/USHolidays/Facades/USHolidays.php @@ -0,0 +1,11 @@ +app->singleton(\USHolidays\Carbon::class, function ($app) { + return new \USHolidays\Carbon(); + }); + + $this->app->alias(\USHolidays\Carbon::class, 'usholidays'); + } +}