Skip to content

Commit eaa396c

Browse files
committed
Add support for custom date class via Date::use()
Laravel 5.8 introduced a feature to support a custom date class via `Date::use()`, see laravel/framework#25320 When e.g. using `Date::use(CarbonImmutable)` in a project, it means all date casts are not returning `\Illuminate\Support\Carbon` anymore but `\Carbon\CarbonImmutable`, which means all the generated type hints for dates are now wrong. This change tries to be still backwards compatible with Laravel < 5.8 which do not have the Date facade.
1 parent fcf3fae commit eaa396c

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

src/Console/ModelsCommand.php

+14-2
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,14 @@ class ModelsCommand extends Command
6363
*/
6464
protected $nullableColumns = [];
6565

66+
/**
67+
* During initializtion we use Laravels Date Facade to
68+
* determine the actual date class and store it here.
69+
*
70+
* @var string
71+
*/
72+
protected $dateClass;
73+
6674
/**
6775
* @param Filesystem $files
6876
*/
@@ -103,6 +111,10 @@ public function handle()
103111
}
104112
}
105113

114+
$this->dateClass = class_exists(\Illuminate\Support\Facades\Date::class)
115+
? '\\' . get_class(\Illuminate\Support\Facades\Date::now())
116+
: '\Illuminate\Support\Carbon';
117+
106118
$content = $this->generateDocs($model, $ignore);
107119

108120
if (!$this->write) {
@@ -284,7 +296,7 @@ protected function castPropertiesType($model)
284296
break;
285297
case 'date':
286298
case 'datetime':
287-
$realType = '\Illuminate\Support\Carbon';
299+
$realType = $this->dateClass;
288300
break;
289301
case 'collection':
290302
$realType = '\Illuminate\Support\Collection';
@@ -348,7 +360,7 @@ protected function getPropertiesFromTable($model)
348360
foreach ($columns as $column) {
349361
$name = $column->getName();
350362
if (in_array($name, $model->getDates())) {
351-
$type = '\Illuminate\Support\Carbon';
363+
$type = $this->dateClass;
352364
} else {
353365
$type = $column->getType()->getName();
354366
switch ($type) {

0 commit comments

Comments
 (0)