Skip to content

Commit 607184b

Browse files
committed
Update repository stub and add contract option stub
1 parent c64a3fa commit 607184b

File tree

7 files changed

+99
-76
lines changed

7 files changed

+99
-76
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
namespace {{namespace}};
4+
5+
use App\Models\{{model}};
6+
use Illuminate\Http\Request;
7+
use {{contractNamespace}}\{{contract}} as Contract;
8+
9+
class {{class}} implements Contract
10+
{
11+
/**
12+
* The {{model}} instance.
13+
*
14+
* @var App\Models\{{model}};
15+
*/
16+
protected ${{resourceLowercase}};
17+
18+
/**
19+
* {{model}} constructor.
20+
* @param {{model}} ${{resourceLowercase}}
21+
*/
22+
public function __construct({{model}} ${{resourceLowercase}})
23+
{
24+
$this->{{resourceLowercase}} = ${{resourceLowercase}};
25+
}
26+
27+
public function all()
28+
{
29+
return $this->{{resourceLowercase}}->all();
30+
}
31+
32+
/**
33+
* @param Request $request
34+
* @return mixed
35+
*/
36+
public function getColumn(Request $request)
37+
{
38+
// TODO: Implement getColumn() method.
39+
}
40+
41+
/**
42+
* @param Request $request
43+
* @return mixed
44+
*/
45+
public function getList(Request $request)
46+
{
47+
// TODO: Implement getList() method.
48+
}
49+
50+
/**
51+
* @param Request $request
52+
* @return mixed
53+
*/
54+
public function create(Request $request)
55+
{
56+
// TODO: Implement create() method.
57+
}
58+
59+
/**
60+
* @param Request $request
61+
* @param $id
62+
* @return mixed
63+
*/
64+
public function update(Request $request, $id)
65+
{
66+
// TODO: Implement update() method.
67+
}
68+
}

resources/stubs/repository.stub

Lines changed: 2 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -3,66 +3,11 @@
33
namespace {{namespace}};
44

55
use App\Models\{{model}};
6-
use Illuminate\Http\Request;
7-
use {{contractNamespace}}\{{contract}} as Contract;
86

9-
class {{class}} implements Contract
7+
class {{class}}
108
{
11-
/**
12-
* The {{model}} instance.
13-
*
14-
* @var App\Models\{{model}};
15-
*/
16-
protected ${{resourceLowercase}};
17-
18-
/**
19-
* {{model}} constructor.
20-
* @param {{model}} ${{resourceLowercase}}
21-
*/
22-
public function __construct({{model}} ${{resourceLowercase}})
23-
{
24-
$this->{{resourceLowercase}} = ${{resourceLowercase}};
25-
}
26-
279
public function all()
2810
{
29-
return $this->{{resourceLowercase}}->all();
30-
}
31-
32-
/**
33-
* @param Request $request
34-
* @return mixed
35-
*/
36-
public function getColumn(Request $request)
37-
{
38-
// TODO: Implement getColumn() method.
39-
}
40-
41-
/**
42-
* @param Request $request
43-
* @return mixed
44-
*/
45-
public function getList(Request $request)
46-
{
47-
// TODO: Implement getList() method.
48-
}
49-
50-
/**
51-
* @param Request $request
52-
* @return mixed
53-
*/
54-
public function create(Request $request)
55-
{
56-
// TODO: Implement create() method.
57-
}
58-
59-
/**
60-
* @param Request $request
61-
* @param $id
62-
* @return mixed
63-
*/
64-
public function update(Request $request, $id)
65-
{
66-
// TODO: Implement update() method.
11+
return {{model}}::all();
6712
}
6813
}

src/Commands/FileCommand.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ private function getFileName()
6363
*/
6464
public function handle()
6565
{
66-
// setup
6766
$this->setSettings();
6867
$this->getResourceName($this->getUrl(false));
6968

src/Commands/GeneratorCommand.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -367,10 +367,7 @@ protected function getDefaultNamespace($rootNamespace)
367367
protected function getStub()
368368
{
369369
$key = $this->getOptionStubKey();
370-
371-
// get the stub path
372370
$stub = config('generators.stubs.' . $key);
373-
374371
if ($stub === null) {
375372
$this->error('The stub does not exist in the config file - "' . $key . '"');
376373
exit;
@@ -389,7 +386,7 @@ protected function getOptionStubKey()
389386
$plain = $this->option('plain');
390387
$stub = $this->option('stub') . ($plain ? '_plain' : '');
391388

392-
// if no stub, we assume its the same as the type
389+
// if no stub, we assume it's the same as the type
393390
if (is_null($this->option('stub'))) {
394391
$stub = $this->option('type') . ($plain ? '_plain' : '');
395392
}

src/Commands/RepositoryCommand.php

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Bpocallaghan\Generators\Commands;
44

5+
use Symfony\Component\Console\Input\InputOption;
6+
57
class RepositoryCommand extends GeneratorCommand
68
{
79
/**
@@ -26,26 +28,31 @@ class RepositoryCommand extends GeneratorCommand
2628
protected $type = 'Repository';
2729

2830
/**
29-
* Add an extra option to use for generating the file
30-
* @var string
31+
* Execute the console command.
32+
*
33+
* @return void
3134
*/
32-
//protected $extraOption = 'contract';
35+
public function handle()
36+
{
37+
if (!$this->option('contract')) {
38+
parent::handle();
39+
} else {
40+
$this->call('generate:repository', [
41+
'name' => $this->argumentName(),
42+
'--stub' => 'repository_contract',
43+
]);
44+
}
45+
}
3346

3447
/**
3548
* Get the console command options.
3649
*
3750
* @return array
3851
*/
39-
/*protected function getOptions()
52+
protected function getOptions()
4053
{
4154
return array_merge([
42-
[
43-
'contract',
44-
null,
45-
InputOption::VALUE_OPTIONAL,
46-
'The name of the contract interface to implement.',
47-
'Contract'
48-
],
55+
['contract', 'c', InputOption::VALUE_NONE, 'Use the implements Contract Stub.'],
4956
], parent::getOptions());
50-
}*/
51-
}
57+
}
58+
}

src/config/config.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<?php
22

33
return [
4-
54
/*
65
|--------------------------------------------------------------------------
76
| The singular resource words that will not be pluralized
@@ -176,6 +175,7 @@
176175
'exception' => base_path() . '/vendor/bpocallaghan/generators/resources/stubs/exception.stub',
177176
'middleware' => base_path() . '/vendor/bpocallaghan/generators/resources/stubs/middleware.stub',
178177
'repository' => base_path() . '/vendor/bpocallaghan/generators/resources/stubs/repository.stub',
178+
'repository_contract' => base_path() . '/vendor/bpocallaghan/generators/resources/stubs/repository.contract.stub',
179179
'contract' => base_path() . '/vendor/bpocallaghan/generators/resources/stubs/contract.stub',
180180
'factory' => base_path() . '/vendor/bpocallaghan/generators/resources/stubs/factory.stub',
181181
'test' => base_path() . '/vendor/bpocallaghan/generators/resources/stubs/test.stub',

tests/RepositoryCommandTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,11 @@ public function generate_repository_with_option_test()
2121
$this->assertFileExists('app/Repositories/FooRepository.php');
2222
$this->assertFileExists('tests/Unit/Repositories/FooRepositoryTest.php');
2323
}
24+
25+
/** @test */
26+
public function generate_repository_with_contract_stub()
27+
{
28+
$this->artisan('generate:repository BookingRepository --contract');
29+
$this->assertFileExists('app/Repositories/BookingRepository.php');
30+
}
2431
}

0 commit comments

Comments
 (0)