Skip to content

Commit

Permalink
Merge pull request #32 from kylekatarnls/laravel-provider
Browse files Browse the repository at this point in the history
Laravel provider
  • Loading branch information
kylekatarnls authored Apr 8, 2019
2 parents 2a94828 + 87547fa commit c600eb5
Show file tree
Hide file tree
Showing 11 changed files with 1,453 additions and 13 deletions.
3 changes: 1 addition & 2 deletions .codeclimate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,4 @@ exclude_patterns:
- "src/Cmixin/Holidays/"
- "src/Cmixin/BusinessDay/Calendar/LunarCalendar.php"
- "src/Types/"
- "src/_ide_business_day_instantiated.php"
- "src/_ide_business_day_static.php"
- "types"
20 changes: 11 additions & 9 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
/tests export-ignore
/.codeclimate.yml export-ignore
/.editorconfig export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore
/.styleci.yml export-ignore
/.travis.yml export-ignore
/*.md export-ignore
/phpunit.xml.dist export-ignore
/tests export-ignore
/.codeclimate.yml export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore
/.multi-tester.yml export-ignore
/.travis.yml export-ignore
/*.md export-ignore
/types.php export-ignore
/prepare-names-files.php export-ignore
/phpmd.xml export-ignore
/phpunit.xml.dist export-ignore
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,23 @@ print_r(Carbon::getMonthBusinessDays('2019-06')); // Open days in June 2019
print_r(Carbon::parse('2019-06-10')->getMonthBusinessDays()); // Can be called from an instance
```

### Laravel

To enable business-day globally in Laravel, set default holidays settings in the config file **config/carbon.php**
(create this file if it does not exist yet):

```php
<?php return [
'holidays' => [
'region' => 'us',
'with' => [
'boss-birthday' => '09-26',
'last-monday' => '= last Monday of October',
],
],
];
```

### Note about timezones

When you set an holidays region, it does not change the timezone, so if January 1st is an holiday,
Expand Down
41 changes: 41 additions & 0 deletions src/Cmixin/BusinessDay/Laravel/ServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Cmixin\BusinessDay\Laravel;

use Closure;
use Cmixin\BusinessDay;

class ServiceProvider extends \Illuminate\Support\ServiceProvider
{
public function boot()
{
$config = $this->app->get('config')->get('carbon.holidays');

if ($config instanceof Closure) {
$config = $config($this->app);
}

if (is_array($config) && isset($config['region'])) {
$classes = array_filter(array(
'Carbon\Carbon',
'Carbon\CarbonImmutable',
'Illuminate\Support\Carbon',
), 'class_exists');

// @codeCoverageIgnoreStart
if (class_exists('Illuminate\Support\Facades\Date') &&
(($now = \Illuminate\Support\Facades\Date::now()) instanceof \DateTimeInterface) &&
!in_array($class = get_class($now), $classes)) {
$classes[] = $class;
}
// @codeCoverageIgnoreEnd

BusinessDay::enable($classes, $config['region'], isset($config['with']) ? $config['with'] : null);
}
}

public function register()
{
// Needed for Laravel < 5.3 compatibility
}
}
1 change: 1 addition & 0 deletions src/Types/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ public function writeHelpers($defaultClass, $source, $destination, $name = '_ide
'Carbon\Carbon',
'Carbon\CarbonImmutable',
'Illuminate\Support\Carbon',
'Illuminate\Support\Facades\Date',
);

$code = "<?php\n";
Expand Down
18 changes: 18 additions & 0 deletions tests/Laravel/App.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

class App
{
public $region = 'us';

public function get($name)
{
return $name === 'config' ? $this : function ($app) {
return array(
'region' => $app->region,
'with' => array(
'foo' => '09-07',
),
);
};
}
}
17 changes: 17 additions & 0 deletions tests/Laravel/ServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Illuminate\Support;

class ServiceProvider
{
/**
* @var \App
*/
public $app;

public function __construct()
{
include_once __DIR__.'/App.php';
$this->app = new \App();
}
}
34 changes: 34 additions & 0 deletions tests/Laravel/ServiceProviderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Tests\Carbon\Laravel;

use Carbon\Carbon;
use Cmixin\BusinessDay\Laravel\ServiceProvider;
use PHPUnit\Framework\TestCase;

class ServiceProviderTest extends TestCase
{
public function testBoot()
{
include_once __DIR__.'/ServiceProvider.php';
$service = new ServiceProvider();
$message = null;

Carbon::macro('isHoliday', null);

try {
Carbon::parse('2019-04-08')->isHoliday();
} catch (\BadMethodCallException $e) {
$message = $e->getMessage();
}

$this->assertSame('Method isHoliday does not exist.', $message);

$service->boot();

$this->assertSame('foo', Carbon::parse('2019-09-07')->getHolidayId());
$this->assertSame('us-national', Carbon::getHolidaysRegion());

$this->assertNull($service->register());
}
}
3 changes: 1 addition & 2 deletions types.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,5 @@
include __DIR__.'/vendor/autoload.php';
include __DIR__.'/src/Types/Generator.php';

$sources = __DIR__.'/src';
$generator = new \Types\Generator();
$generator->writeHelpers('Cmixin\BusinessDay', $sources, $sources);
$generator->writeHelpers('Cmixin\BusinessDay', __DIR__.'/src', __DIR__.'/types');
Loading

0 comments on commit c600eb5

Please sign in to comment.