8000 [Feature-WIP] Add generator changes and fix for Laravel 5.4 · volldigital/laravel-json-api@655a4de · GitHub
[go: up one dir, main page]

Skip to content

Commit 655a4de

Browse files
committed
[Feature-WIP] Add generator changes and fix for Laravel 5.4
1 parent 0ce299d commit 655a4de

File tree

3 files changed

+57
-16
lines changed

3 files changed

+57
-16
lines changed

src/Api/Repository.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,15 @@ public function __construct(Config $config, Factory $factory)
5959
$this->factory = $factory;
6060
}
6161

62+
/**
63+
* @param $apiName
64+
* @return bool
65+
*/
66+
public function isApi($apiName)
67+
{
68+
return !empty($this->retrieveConfig($apiName));
69+
}
70+
6271
/**
6372
* @param $apiName
6473
* @param string|null $host

src/Console/Commands/AbstractGeneratorCommand.php

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,19 @@ public function __construct(Filesystem $files, Repository $apiRepository)
8888
$this->stubsDirectory = __DIR__ . '/../../../stubs';
8989
}
9090

91+
/**
92+
* @return bool|null
93+
*/
94+
public function fire()
95+
{
96+
if (!$this->apiRepository->isApi($api = $this->argument('api'))) {
97+
$this->error("JSON API '$api' does not exist.");
98+
return 1;
99+
}
100+
101+
return (parent::fire() !== false) ? 0 : 1;
102+
}
103+
91104
/**
92105
* Get the desired class name from the input.
93106
*
@@ -103,10 +116,24 @@ protected function getNameInput()
103116
}
104117

105118
/**
106-
* @param string $name
107-
* @return mixed
119+
* Laravel 5.3 name parsing.
120+
*
121+
* @param $name
122+
* @return string
123+
* @todo remove when dropping support for Laravel 5.3
108124
*/
109125
protected function parseName($name)
126+
{
127+
return $this->qualifyClass($name);
128+
}
129+
130+
/**
131+
* Laravel 5.4 name parsing.
132+
*
133+
* @param string $name
134+
* @return string
135+
*/
136+
protected function qualifyClass($name)
110137
{
111138
return call_user_func(
112139
Fqn::class . '::' . $this->type,

src/Console/Commands/MakeResourceCommand.php

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -97,19 +97,22 @@ public function handle()
9797
}
9898

9999
// Run commands that cannot accept Eloquent parameters.
100-
$this->runCommandsWithParameters($commands->only([
101-
'make:json-api:validators',
102-
]), $resourceParameter);
100+
$notEloquent = ['make:json-api:validators'];
101+
102+
if (!$this->runCommandsWithParameters($commands->only($notEloquent), $resourceParameter)) {
103+
return 1;
104+
}
103105

104106
// Run commands that can accept Eloquent parameters.
105-
$this->runCommandsWithParameters($commands->only([
106-
'make:json-api:adapter',
107-
'make:json-api:hydrator',
108-
'make:json-api:schema',
109-
]), $eloquentParameters);
107+
$eloquent = ['make:json-api:adapter', 'make:json-api:hydrator', 'make:json-api:schema'];
108+
109+
if (!$this->runCommandsWithParameters($commands->only($eloquent), $eloquentParameters)) {
110+
return 1;
111+
}
110112

111113
// Give the user a digial high-five.
112114
$this->comment('All done, keep doing what you do.');
115+
113116
return 0;
114117
}
115118

@@ -118,7 +121,6 @@ public function handle()
118121
*
119122
* @param Collection $commands
120123
* @param string $type
121-
*
122124
* @return Collection
123125
*/
124126
private function filterCommands(Collection $commands, $type)
@@ -139,14 +141,17 @@ private function filterCommands(Collection $commands, $type)
139141
*
140142
* @param Collection $commands
141143
* @param array $parameters
142-
*
143-
* @return void
144+
* @return bool
144145
*/
145146
private function runCommandsWithParameters(Collection $commands, array $parameters)
146147
{
147-
$commands->keys()->each(function ($command) use ($parameters) {
148-
$this->call($command, $parameters);
149-
});
148+
foreach ($commands->keys() as $command) {
149+
if (0 !== $this->call($command, $parameters)) {
150+
56A3 return false;
151+
}
152+
}
153+
154+
return true;
150155
}
151156

152157
}

0 commit comments

Comments
 (0)
0