Skip to content

Commit

Permalink
Laravel Facade (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
geoffreyrose authored Nov 17, 2024
1 parent 9d4d622 commit 976b474
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 19 deletions.
37 changes: 20 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
</div>

# 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
Expand Down Expand Up @@ -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
<?php
require 'vendor/autoload.php';

use USHolidays\Carbon;
```

#### Without Composer
...

```php
<?php
require 'path/to/nesbot/Carbon.php';
require 'path/to/geoffreyrose/Carbon.php';
$carbon = Carbon::create(2020, 1, 1);
$holidays = $carbon->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
Expand Down Expand Up @@ -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
Expand Down
12 changes: 10 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
}
}
11 changes: 11 additions & 0 deletions src/USHolidays/Facades/USHolidays.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace USHolidays\Facades;

class USHolidays extends \Illuminate\Support\Facades\Facade
{
protected static function getFacadeAccessor()
{
return \USHolidays\Carbon::class;
}
}
20 changes: 20 additions & 0 deletions src/USHolidays/ServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace USHolidays;

class ServiceProvider extends \Illuminate\Support\ServiceProvider
{
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
$this->app->singleton(\USHolidays\Carbon::class, function ($app) {
return new \USHolidays\Carbon();
});

$this->app->alias(\USHolidays\Carbon::class, 'usholidays');
}
}

0 comments on commit 976b474

Please sign in to comment.