Skip to content

Commit

Permalink
Make:rule Artisan command.
Browse files Browse the repository at this point in the history
Allows quick creation of custom validation rules.
  • Loading branch information
taylorotwell committed Jun 25, 2017
1 parent bbfdb60 commit 76853fd
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 0 deletions.
50 changes: 50 additions & 0 deletions src/Illuminate/Foundation/Console/RuleMakeCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace Illuminate\Foundation\Console;

use Illuminate\Console\GeneratorCommand;

class RuleMakeCommand extends GeneratorCommand
{
/**
* The console command name.
*
* @var string
*/
protected $name = 'make:rule';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Create a new validation rule';

/**
* The type of class being generated.
*
* @var string
*/
protected $type = 'Rule';

/**
* Get the stub file for the generator.
*
* @return string
*/
protected function getStub()
{
return __DIR__.'/stubs/rule.stub';
}

/**
* Get the default namespace for the class.
*
* @param string $rootNamespace
* @return string
*/
protected function getDefaultNamespace($rootNamespace)
{
return $rootNamespace.'\Rules';
}
}
40 changes: 40 additions & 0 deletions src/Illuminate/Foundation/Console/stubs/rule.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace DummyNamespace;

use Illuminate\Contracts\Validation\Rule;

class DummyClass implements Rule
{
/**
* Create a new rule instance.
*
* @return void
*/
public function __construct()
{
//
}

/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value)
{
//
}

/**
* Get the validation error message.
*
* @return string
*/
public function message()
{
return 'The validation error message.';
}
}
14 changes: 14 additions & 0 deletions src/Illuminate/Foundation/Providers/ArtisanServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Illuminate\Queue\Console\FailedTableCommand;
use Illuminate\Foundation\Console\AppNameCommand;
use Illuminate\Foundation\Console\JobMakeCommand;
use Illuminate\Foundation\Console\RuleMakeCommand;
use Illuminate\Database\Console\Seeds\SeedCommand;
use Illuminate\Foundation\Console\MailMakeCommand;
use Illuminate\Foundation\Console\OptimizeCommand;
Expand Down Expand Up @@ -144,6 +145,7 @@ class ArtisanServiceProvider extends ServiceProvider
'QueueFailedTable' => 'command.queue.failed-table',
'QueueTable' => 'command.queue.table',
'RequestMake' => 'command.request.make',
'RuleMake' => 'command.rule.make',
'SeederMake' => 'command.seeder.make',
'SessionTable' => 'command.session.table',
'Serve' => 'command.serve',
Expand Down Expand Up @@ -725,6 +727,18 @@ protected function registerRequestMakeCommand()
});
}

/**
* Register the command.
*
* @return void
*/
protected function registerRuleMakeCommand()
{
$this->app->singleton('command.rule.make', function ($app) {
return new RuleMakeCommand($app['files']);
});
}

/**
* Register the command.
*
Expand Down

0 comments on commit 76853fd

Please sign in to comment.