Skip to content

Commit 76853fd

Browse files
committed
Make:rule Artisan command.
Allows quick creation of custom validation rules.
1 parent bbfdb60 commit 76853fd

File tree

3 files changed

+104
-0
lines changed

3 files changed

+104
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace Illuminate\Foundation\Console;
4+
5+
use Illuminate\Console\GeneratorCommand;
6+
7+
class RuleMakeCommand extends GeneratorCommand
8+
{
9+
/**
10+
* The console command name.
11+
*
12+
* @var string
13+
*/
14+
protected $name = 'make:rule';
15+
16+
/**
17+
* The console command description.
18+
*
19+
* @var string
20+
*/
21+
protected $description = 'Create a new validation rule';
22+
23+
/**
24+
* The type of class being generated.
25+
*
26+
* @var string
27+
*/
28+
protected $type = 'Rule';
29+
30+
/**
31+
* Get the stub file for the generator.
32+
*
33+
* @return string
34+
*/
35+
protected function getStub()
36+
{
37+
return __DIR__.'/stubs/rule.stub';
38+
}
39+
40+
/**
41+
* Get the default namespace for the class.
42+
*
43+
* @param string $rootNamespace
44+
* @return string
45+
*/
46+
protected function getDefaultNamespace($rootNamespace)
47+
{
48+
return $rootNamespace.'\Rules';
49+
}
50+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace DummyNamespace;
4+
5+
use Illuminate\Contracts\Validation\Rule;
6+
7+
class DummyClass implements Rule
8+
{
9+
/**
10+
* Create a new rule instance.
11+
*
12+
* @return void
13+
*/
14+
public function __construct()
15+
{
16+
//
17+
}
18+
19+
/**
20+
* Determine if the validation rule passes.
21+
*
22+
* @param string $attribute
23+
* @param mixed $value
24+
* @return bool
25+
*/
26+
public function passes($attribute, $value)
27+
{
28+
//
29+
}
30+
31+
/**
32+
* Get the validation error message.
33+
*
34+
* @return string
35+
*/
36+
public function message()
37+
{
38+
return 'The validation error message.';
39+
}
40+
}

src/Illuminate/Foundation/Providers/ArtisanServiceProvider.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Illuminate\Queue\Console\FailedTableCommand;
1515
use Illuminate\Foundation\Console\AppNameCommand;
1616
use Illuminate\Foundation\Console\JobMakeCommand;
17+
use Illuminate\Foundation\Console\RuleMakeCommand;
1718
use Illuminate\Database\Console\Seeds\SeedCommand;
1819
use Illuminate\Foundation\Console\MailMakeCommand;
1920
use Illuminate\Foundation\Console\OptimizeCommand;
@@ -144,6 +145,7 @@ class ArtisanServiceProvider extends ServiceProvider
144145
'QueueFailedTable' => 'command.queue.failed-table',
145146
'QueueTable' => 'command.queue.table',
146147
'RequestMake' => 'command.request.make',
148+
'RuleMake' => 'command.rule.make',
147149
'SeederMake' => 'command.seeder.make',
148150
'SessionTable' => 'command.session.table',
149151
'Serve' => 'command.serve',
@@ -725,6 +727,18 @@ protected function registerRequestMakeCommand()
725727
});
726728
}
727729

730+
/**
731+
* Register the command.
732+
*
733+
* @return void
734+
*/
735+
protected function registerRuleMakeCommand()
736+
{
737+
$this->app->singleton('command.rule.make', function ($app) {
738+
return new RuleMakeCommand($app['files']);
739+
});
740+
}
741+
728742
/**
729743
* Register the command.
730744
*

0 commit comments

Comments
 (0)