Skip to content

Commit

Permalink
Replace jenssegers/date with Carbon 2.0
Browse files Browse the repository at this point in the history
Replaces #518, related: eaa29fe
  • Loading branch information
Luke Towers committed Sep 4, 2020
1 parent 93388c8 commit 7c6ef31
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 12 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"symfony/yaml": "^3.4",
"twig/twig": "~2.0",
"league/csv": "~9.1",
"jenssegers/date": "~3.5",
"nesbot/carbon": "^2.0",
"laravel/framework": "~6.0",
"laravel/tinker": "~2.0"
},
Expand Down
53 changes: 52 additions & 1 deletion src/Argon/Argon.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,61 @@
<?php namespace October\Rain\Argon;

use Jenssegers\Date\Date as DateBase;
use Carbon\Carbon as DateBase;

/**
* Umbrella class.
*/
class Argon extends DateBase
{
/**
* Function to call instead of format.
*
* @var string|callable|null
*/
protected static $formatFunction = 'translatedFormat';

/**
* Function to call instead of createFromFormat.
*
* @var string|callable|null
*/
protected static $createFromFormatFunction = 'createFromFormatWithCurrentLocale';

/**
* Function to call instead of parse.
*
* @var string|callable|null
*/
protected static $parseFunction = 'parseWithCurrentLocale';

public static function parseWithCurrentLocale($time = null, $timezone = null)
{
if (is_string($time)) {
$time = static::translateTimeString($time, static::getLocale(), 'en');
}

return parent::rawParse($time, $timezone);
}

public static function createFromFormatWithCurrentLocale($format, $time = null, $timezone = null)
{
if (is_string($time)) {
$time = static::translateTimeString($time, static::getLocale(), 'en');
}

return parent::rawCreateFromFormat($format, $time, $timezone);
}

/**
* Get the language portion of the locale.
*
* @param string $locale
* @return string
*/
public static function getLanguageFromLocale($locale)
{
$parts = explode('_', str_replace('-', '_', $locale));

return $parts[0];
}
}
19 changes: 17 additions & 2 deletions src/Argon/ArgonServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

class ArgonServiceProvider extends ServiceProvider
{
/**
* @var bool Indicates if loading of the provider is deferred.
*/
protected $defer = false;

/**
* Bootstrap the application events.
*
Expand All @@ -30,18 +35,28 @@ protected function setArgonLocale($locale)
}

/**
* Split the locale and use it as the fallback.
* Get the locale to use as the fallback
*/
protected function getFallbackLocale($locale)
{
if ($position = strpos($locale, '-')) {
$target = substr($locale, 0, $position);
$resource = __DIR__ . '/../../../../jenssegers/date/src/Lang/'.$target.'.php';
$resource = __DIR__ . '/../../../../nesbot/carbon/src/Carbon/Lang/'.$target.'.php';
if (file_exists($resource)) {
return $target;
}
}

return $this->app['config']->get('app.fallback_locale');
}

/**
* Get the services provided by the provider.
*
* @return array
*/
public function provides()
{
return ['Date'];
}
}
3 changes: 1 addition & 2 deletions src/Argon/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@

Argon is a simple PHP API extension for DateTime, and is the cousin of Carbon. The goal of this library is to create a solution for displaying localized dates and introducing more robust features.

This library acts as an umbrella for the Date library:
https://github.com/jenssegers/date
This library used to act as an umbrella for the Date library (https://github.com/jenssegers/date) but Carbon 2 means that it can now just extend Carbon 2 directly.
9 changes: 3 additions & 6 deletions src/Foundation/Application.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
<?php namespace October\Rain\Foundation;

use Closure;
use Config;
use Str;
use Config;
use Closure;
use Throwable;
use Carbon\Laravel\ServiceProvider as CarbonServiceProvider;
use Jenssegers\Date\DateServiceProvider as JessengersDateServiceProvider;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Collection;
use Illuminate\Foundation\Application as ApplicationBase;
Expand All @@ -16,6 +14,7 @@
use October\Rain\Router\RoutingServiceProvider;
use October\Rain\Foundation\Providers\LogServiceProvider;
use October\Rain\Foundation\Providers\MakerServiceProvider;
use Carbon\Laravel\ServiceProvider as CarbonServiceProvider;
use October\Rain\Foundation\Providers\ExecutionContextProvider;

class Application extends ApplicationBase
Expand Down Expand Up @@ -84,8 +83,6 @@ protected function registerBaseServiceProviders()
$this->register(new ExecutionContextProvider($this));

$this->register(new CarbonServiceProvider($this));

$this->register(new JessengersDateServiceProvider($this));
}

/**
Expand Down

0 comments on commit 7c6ef31

Please sign in to comment.