Skip to content
This repository was archived by the owner on Apr 19, 2021. It is now read-only.

Commit c431ecb

Browse files
authored
Merge pull request #2 from timbroder/generators
Add Command and Handler Generators thanks to @timbroder
2 parents f7cca20 + c71a867 commit c431ecb

8 files changed

+387
-0
lines changed

Readme.md

+17
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,23 @@ Then you can modify each class name and they will be resolved from the laravel c
9494
];
9595
```
9696

97+
##Generators
98+
99+
You can generate Commands and Handlers automatically using artisan
100+
101+
```
102+
artisan make:tactician:command Foo
103+
artisan make:tactician:handler Foo
104+
```
105+
106+
This will create FooCommand and FooHandler and place them in the app/CommandBus/Commands and app/CommandBus/Handlers respectively
107+
108+
To run both at once
109+
110+
```
111+
artisan make:tactician Foo
112+
```
113+
97114
##Tests
98115

99116
To run the test in this package, navigate to the root folder of the project and run

src/Commands/MakeTacticianCommand.php

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace Joselfonseca\LaravelTactician\Commands;
4+
5+
use Illuminate\Console\Command;
6+
7+
class MakeTacticianCommand extends Command
8+
{
9+
/**
10+
* The name and signature of the console command.
11+
*
12+
* @var string
13+
*/
14+
protected $signature = 'make:tactician {name}';
15+
16+
/**
17+
* The console command description.
18+
*
19+
* @var string
20+
*/
21+
protected $description = 'Generate Tactician Command and Handler';
22+
23+
/**
24+
* Execute the console command.
25+
*
26+
* @return mixed
27+
*/
28+
public function handle()
29+
{
30+
$this->call('make:tactician:command', $this->getNameArgument());
31+
$this->call('make:tactician:handler', $this->getNameArgument());
32+
}
33+
34+
/**
35+
* @return array
36+
*/
37+
public function getNameArgument()
38+
{
39+
return ['name' => $this->argument('name')];
40+
}
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace Joselfonseca\LaravelTactician\Commands;
4+
5+
use Illuminate\Console\GeneratorCommand;
6+
7+
class MakeTacticianCommandCommand extends GeneratorCommand
8+
{
9+
/**
10+
* The console command name.
11+
*
12+
* @var string
13+
*/
14+
protected $name = 'make:tactician:command';
15+
16+
/**
17+
* The console command description.
18+
*
19+
* @var string
20+
*/
21+
protected $description = 'Create a new Tactician Command';
22+
23+
/**
24+
* Get the stub file for the generator.
25+
*
26+
* @return string
27+
*/
28+
protected function getStub()
29+
{
30+
return __DIR__.'/../../stubs/command.stub';
31+
}
32+
33+
/**
34+
* Get the default namespace for the class.
35+
*
36+
* @param string $rootNamespace
37+
* @return string
38+
*/
39+
protected function getDefaultNamespace($rootNamespace)
40+
{
41+
return "$rootNamespace\\CommandBus\\Commands";
42+
}
43+
44+
/**
45+
* Get the destination class path.
46+
*
47+
* @param string $name
48+
* @return string
49+
*/
50+
protected function getPath($name)
51+
{
52+
return parent::getPath($name.'Command');
53+
}
54+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace Joselfonseca\LaravelTactician\Commands;
4+
5+
use Illuminate\Console\GeneratorCommand;
6+
7+
class MakeTacticianHandlerCommand extends GeneratorCommand
8+
{
9+
/**
10+
* The console command name.
11+
*
12+
* @var string
13+
*/
14+
protected $name = 'make:tactician:handler';
15+
16+
/**
17+
* The console command description.
18+
*
19+
* @var string
20+
*/
21+
protected $description = 'Create a new Tactician Command Handler';
22+
23+
/**
24+
* Get the stub file for the generator.
25+
*
26+
* @return string
27+
*/
28+
protected function getStub()
29+
{
30+
return __DIR__.'/../../stubs/handler.stub';
31+
}
32+
33+
/**
34+
* Get the default namespace for the class.
35+
*
36+
* @param string $rootNamespace
37+
* @return string
38+
*/
39+
protected function getDefaultNamespace($rootNamespace)
40+
{
41+
return "$rootNamespace\\CommandBus\\Handlers";
42+
}
43+
44+
/**
45+
* Get the destination class path.
46+
*
47+
* @param string $name
48+
* @return string
49+
*/
50+
protected function getPath($name)
51+
{
52+
return parent::getPath($name.'Handler');
53+
}
54+
}

src/Providers/LaravelTacticianServiceProvider.php

+21
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
namespace Joselfonseca\LaravelTactician\Providers;
44

55
use Illuminate\Support\ServiceProvider;
6+
use Joselfonseca\LaravelTactician\Commands\MakeTacticianCommand;
7+
use Joselfonseca\LaravelTactician\Commands\MakeTacticianCommandCommand;
8+
use Joselfonseca\LaravelTactician\Commands\MakeTacticianHandlerCommand;
69

710
/**
811
* Class LaravelTacticianServiceProvider
@@ -23,6 +26,24 @@ public function register()
2326
$this->app->bind('League\Tactician\Handler\MethodNameInflector\MethodNameInflector', config('laravel-tactician.inflector'));
2427
$this->app->bind('League\Tactician\Handler\CommandNameExtractor\CommandNameExtractor', config('laravel-tactician.extractor'));
2528
$this->app->bind('Joselfonseca\LaravelTactician\CommandBusInterface', config('laravel-tactician.bus'));
29+
30+
// Register Command Generator
31+
$this->app->singleton('laravel-tactician.make.command', function ($app) {
32+
return new MakeTacticianCommandCommand($app['files']);
33+
});
34+
$this->commands('laravel-tactician.make.command');
35+
36+
// Register Handler Generator
37+
$this->app->singleton('laravel-tactician.make.handler', function ($app) {
38+
return new MakeTacticianHandlerCommand($app['files']);
39+
});
40+
$this->commands('laravel-tactician.make.handler');
41+
42+
// Register Comman+Handler Generator Command
43+
$this->app->singleton('laravel-tactician.make.tactician', function () {
44+
return new MakeTacticianCommand();
45+
});
46+
$this->commands('laravel-tactician.make.tactician');
2647
}
2748

2849
/**

stubs/command.stub

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace DummyNamespace;
4+
5+
/**
6+
* Class DummyClassCommand
7+
* @package DummyNamespace
8+
*/
9+
class DummyClassCommand
10+
{
11+
/**
12+
* DummyClassCommand constructor.
13+
*/
14+
public function __construct()
15+
{
16+
}
17+
}

stubs/handler.stub

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace DummyNamespace;
4+
5+
use App\CommandBus\Commands\DummyClassCommand;
6+
7+
/**
8+
* Class DummyClassHandler
9+
* @package App\CommandBus\Handlers
10+
*/
11+
class DummyClassHandler
12+
{
13+
/**
14+
* DummyClassHandler constructor.
15+
*/
16+
public function __construct()
17+
{
18+
}
19+
20+
/**
21+
* @param DummyClassCommand $command
22+
*/
23+
public function handle(DummyClassCommand $command)
24+
{
25+
}
26+
}

0 commit comments

Comments
 (0)