Skip to content

Commit 4f5b00d

Browse files
author
Andrey Helldar
committed
[6.x] Added make console command of custom Cast
1 parent f9f9b07 commit 4f5b00d

File tree

3 files changed

+92
-0
lines changed

3 files changed

+92
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace Illuminate\Foundation\Console;
4+
5+
use Illuminate\Console\GeneratorCommand;
6+
7+
class CastMakeCommand extends GeneratorCommand
8+
{
9+
/**
10+
* The console command name.
11+
*
12+
* @var string
13+
*/
14+
protected $name = 'make:cast';
15+
16+
/**
17+
* The console command description.
18+
*
19+
* @var string
20+
*/
21+
protected $description = 'Create a new cast mutator';
22+
23+
/**
24+
* The type of class being generated.
25+
*
26+
* @var string
27+
*/
28+
protected $type = 'Cast';
29+
30+
/**
31+
* Get the stub file for the generator.
32+
*
33+
* @return string
34+
*/
35+
protected function getStub()
36+
{
37+
return __DIR__.'/stubs/cast.stub';
38+
}
39+
40+
/**
41+
* Get the default namespace for the class.
42+
*
43+
* @param string $rootNamespace
44+
*
45+
* @return string
46+
*/
47+
protected function getDefaultNamespace($rootNamespace)
48+
{
49+
return $rootNamespace.'\Casts';
50+
}
51+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace DummyNamespace;
4+
5+
use Illuminate\Contracts\Database\Eloquent\Castable;
6+
7+
class DummyClass implements Castable
8+
{
9+
/**
10+
* Get a given attribute from the model.
11+
*
12+
* @param mixed $value
13+
*
14+
* @return mixed
15+
*/
16+
public function get($value)
17+
{
18+
return $value;
19+
}
20+
21+
/**
22+
* Set a given attribute on the model.
23+
*
24+
* @param mixed $value
25+
*
26+
* @return mixed
27+
*/
28+
public function set($value)
29+
{
30+
return $value;
31+
}
32+
}

src/Illuminate/Foundation/Providers/ArtisanServiceProvider.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Illuminate\Database\Console\Seeds\SeedCommand;
1414
use Illuminate\Database\Console\Seeds\SeederMakeCommand;
1515
use Illuminate\Database\Console\WipeCommand;
16+
use Illuminate\Foundation\Console\CastMakeCommand;
1617
use Illuminate\Foundation\Console\ChannelMakeCommand;
1718
use Illuminate\Foundation\Console\ClearCompiledCommand;
1819
use Illuminate\Foundation\Console\ConfigCacheCommand;
@@ -130,6 +131,7 @@ class ArtisanServiceProvider extends ServiceProvider implements DeferrableProvid
130131
'MailMake' => 'command.mail.make',
131132
'MiddlewareMake' => 'command.middleware.make',
132133
'ModelMake' => 'command.model.make',
134+
'CastMake' => 'command.cast.make',
133135
'NotificationMake' => 'command.notification.make',
134136
'NotificationTable' => 'command.notification.table',
135137
'ObserverMake' => 'command.observer.make',
@@ -486,6 +488,13 @@ protected function registerModelMakeCommand()
486488
});
487489
}
488490

491+
protected function registerCastMakeCommand()
492+
{
493+
$this->app->singleton('command.cast.make', function ($app) {
494+
return new CastMakeCommand($app['files']);
495+
});
496+
}
497+
489498
/**
490499
* Register the command.
491500
*

0 commit comments

Comments
 (0)