Skip to content

Commit 786d4bc

Browse files
committed
Resource Command - add 'create repository and contract' for the resource
1 parent 460e99d commit 786d4bc

9 files changed

+321
-18
lines changed

readme.md

+1
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ php artisan generate:resource bar --schema="title:string, body:text, slug:string
174174

175175
- This will generate a Bar model, BarsController, resources views (in config), create_bars_table migration, BarTableSeeder
176176
- In the config there is a `resource_views` array, you can specify the views that you want to generate there, just make sure the stub exist.
177+
- This will also ask you to generate the 'repository - contract pattern' files.
177178

178179
### Repository
179180
```

resources/stubs/contract.stub

+29-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,35 @@
22

33
namespace {{namespace}};
44

5-
class {{class}}
5+
use Illuminate\Http\Request;
6+
7+
/**
8+
* Interface {{class}}
9+
*/
10+
interface {{class}}
611
{
12+
/**
13+
* @param Request $request
14+
* @return mixed
15+
*/
16+
public function getColumn(Request $request);
17+
18+
/**
19+
* @param Request $request
20+
* @return mixed
21+
*/
22+
public function getList(Request $request);
23+
24+
/**
25+
* @param Request $request
26+
* @return mixed
27+
*/
28+
public function create(Request $request);
729

30+
/**
31+
* @param Request $request
32+
* @param $id
33+
* @return mixed
34+
*/
35+
public function update(Request $request, $id);
836
}
+118
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
<?php
2+
3+
namespace {{namespace}};
4+
5+
use Redirect;
6+
use {{rootNamespace}}Http\Requests;
7+
use {{rootNamespace}}Models\{{model}};
8+
use Illuminate\Http\Request;
9+
use {{contractNamespace}}\{{contract}};
10+
use {{rootNamespace}}Http\Controllers\Admin\AdminController;
11+
12+
class {{class}} extends AdminController
13+
{
14+
/**
15+
* The {{contract}} instance.
16+
*
17+
* @var {{contractNamespace}}\{{contract}}
18+
*/
19+
protected ${{resourceLowercase}};
20+
21+
/**
22+
* {{contract}} constructor.
23+
* @param {{contract}} ${{resourceLowercase}}
24+
*/
25+
public function __construct({{contract}} ${{resourceLowercase}})
26+
{
27+
$this->{{resourceLowercase}} = ${{resourceLowercase}};
28+
}
29+
30+
/**
31+
* Display a listing of {{resource}}.
32+
*
33+
* @return Response
34+
*/
35+
public function index()
36+
{
37+
save_resource_url();
38+
39+
return $this->view('{{viewPath}}.index')->with('items', {{model}}::all());
40+
}
41+
42+
/**
43+
* Show the form for creating a new {{resource}}.
44+
*
45+
* @return Response
46+
*/
47+
public function create()
48+
{
49+
return $this->view('{{viewPath}}.create_edit');
50+
}
51+
52+
/**
53+
* Store a newly created {{resource}} in storage.
54+
*
55+
* @param Request $request
56+
* @return Response
57+
*/
58+
public function store(Request $request)
59+
{
60+
$this->validate($request, {{model}}::$rules, {{model}}::$messages);
61+
62+
$this->createEntry({{model}}::class, $request->all());
63+
64+
return redirect_to_resource();
65+
}
66+
67+
/**
68+
* Display the specified {{resource}}.
69+
*
70+
* @param {{model}} ${{resource}}
71+
* @return Response
72+
*/
73+
public function show({{model}} ${{resource}})
74+
{
75+
return $this->view('{{viewPath}}.show')->with('item', ${{resource}});
76+
}
77+
78+
/**
79+
* Show the form for editing the specified {{resource}}.
80+
*
81+
* @param {{model}} ${{resource}}
82+
* @return Response
83+
*/
84+
public function edit({{model}} ${{resource}})
85+
{
86+
return $this->view('{{viewPath}}.create_edit')->with('item', ${{resource}});
87+
}
88+
89+
/**
90+
* Update the specified {{resource}} in storage.
91+
*
92+
* @param {{model}} ${{resource}}
93+
* @param Request $request
94+
* @return Response
95+
*/
96+
public function update({{model}} ${{resource}}, Request $request)
97+
{
98+
$this->validate($request, {{model}}::$rules, {{model}}::$messages);
99+
100+
$this->updateEntry(${{resource}}, $request->all());
101+
102+
return redirect_to_resource();
103+
}
104+
105+
/**
106+
* Remove the specified {{resource}} from storage.
107+
*
108+
* @param {{model}} ${{resource}}
109+
* @param Request $request
110+
* @return Response
111+
*/
112+
public function destroy({{model}} ${{resource}}, Request $request)
113+
{
114+
$this->deleteEntry(${{resource}}, $request);
115+
116+
return redirect_to_resource();
117+
}
118+
}

resources/stubs/repository.stub

+49-1
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,22 @@
33
namespace {{namespace}};
44

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

7-
class {{class}}
9+
class {{class}} implements Contract
810
{
11+
/**
12+
* The {{model}} instance.
13+
*
14+
* @var App\Models\{{model}};
15+
*/
916
protected ${{resourceLowercase}};
1017

18+
/**
19+
* {{model}} constructor.
20+
* @param {{model}} ${{resourceLowercase}}
21+
*/
1122
public function __construct({{model}} ${{resourceLowercase}})
1223
{
1324
$this->{{resourceLowercase}} = ${{resourceLowercase}};
@@ -17,4 +28,41 @@ class {{class}}
1728
{
1829
return $this->{{resourceLowercase}}->all();
1930
}
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+
}
2068
}

src/Commands/FileCommand.php

+6
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,12 @@ protected function buildClass($name)
173173
// console command name
174174
$stub = str_replace('{{command}}', $this->option('command'), $stub);
175175

176+
// contract file name
177+
$stub = str_replace('{{contract}}', $this->getContractName(), $stub);
178+
179+
// contract namespace
180+
$stub = str_replace('{{contractNamespace}}', $this->getContractNamespace(), $stub);
181+
176182
return $stub;
177183
}
178184

src/Commands/GeneratorCommand.php

+36
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,42 @@ protected function getCollectionUpperName($name = null)
221221
return $name;
222222
}
223223

224+
/**
225+
* Get the name of the contract
226+
* @param null $name
227+
* @return string
228+
*/
229+
protected function getContractName($name = null)
230+
{
231+
$name = isset($name) ? $name : $this->resource;
232+
233+
$name = str_singular(ucwords(camel_case(class_basename($name))));
234+
235+
return $name . config('generators.settings.contract.postfix');
236+
}
237+
238+
/**
239+
* Get the namespace of where contract was created
240+
* @param bool $withApp
241+
* @return string
242+
*/
243+
protected function getContractNamespace($withApp = true)
244+
{
245+
// get path from settings
246+
$path = config('generators.settings.contract.namespace') . '\\';
247+
248+
// dont add the default namespace if specified not to in config
249+
$path .= str_replace('/', '\\', $this->getArgumentPath());
250+
251+
$pieces = array_map('ucfirst', explode('/', $path));
252+
253+
$namespace = ($withApp === true ? $this->getAppNamespace() : '') . implode('\\', $pieces);
254+
255+
$namespace = rtrim(ltrim(str_replace('\\\\', '\\', $namespace), '\\'), '\\');
256+
257+
return $namespace;
258+
}
259+
224260
/**
225261
* Get the path to the view file
226262
*

src/Commands/RepositoryCommand.php

+24
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,28 @@ class RepositoryCommand extends GeneratorCommand
2424
* @var string
2525
*/
2626
protected $type = 'Repository';
27+
28+
/**
29+
* Add an extra option to use for generating the file
30+
* @var string
31+
*/
32+
//protected $extraOption = 'contract';
33+
34+
/**
35+
* Get the console command options.
36+
*
37+
* @return array
38+
*/
39+
/*protected function getOptions()
40+
{
41+
return array_merge([
42+
[
43+
'contract',
44+
null,
45+
InputOption::VALUE_OPTIONAL,
46+
'The name of the contract interface to implement.',
47+
'Contract'
48+
],
49+
], parent::getOptions());
50+
}*/
2751
}

0 commit comments

Comments
 (0)