1 |
<?php
|
|
2 |
|
|
3 |
namespace Nuwave\Lighthouse\Console; |
|
4 |
|
|
5 |
use Illuminate\Console\GeneratorCommand; |
|
6 |
use InvalidArgumentException; |
|
7 |
|
|
8 |
abstract class LighthouseGeneratorCommand extends GeneratorCommand |
|
9 |
{
|
|
10 |
/**
|
|
11 |
* Get the desired class name from the input.
|
|
12 |
*
|
|
13 |
* As a typical workflow would be to write the schema first and then copy-paste
|
|
14 |
* a field name to generate a class for it, we uppercase it so the user does
|
|
15 |
* not run into unnecessary errors. You're welcome.
|
|
16 |
*/
|
|
17 |
protected function getNameInput(): string |
|
18 |
{
|
|
19 |
$name = $this->argument('name'); |
|
20 |
if (! is_string($name)) { |
|
21 |
throw new InvalidArgumentException('You must the name for the class to generate.'); |
|
22 |
}
|
|
23 |
|
|
24 |
return ucfirst(trim($name)); |
|
25 |
}
|
|
26 |
|
|
27 |
/**
|
|
28 |
* @param string $rootNamespace
|
|
29 |
*/
|
|
30 |
protected function getDefaultNamespace($rootNamespace): string |
|
31 |
{
|
|
32 |
$namespaces = config('lighthouse.namespaces.'.$this->namespaceConfigKey()); |
|
33 |
|
|
34 |
return static::commonNamespace((array) $namespaces); |
|
35 |
}
|
|
36 |
|
|
37 |
/**
|
|
38 |
* Get the config key that holds the default namespaces for the class.
|
|
39 |
*/
|
|
40 |
abstract protected function namespaceConfigKey(): string; |
|
41 |
|
|
42 |
/**
|
|
43 |
* Find the common namespace of a list of namespaces.
|
|
44 |
*
|
|
45 |
* For example, ['App\\Foo\\A', 'App\\Foo\\B'] would return 'App\\Foo'.
|
|
46 |
*
|
|
47 |
* @param array<string> $namespaces
|
|
48 |
*/
|
|
49 | 1 |
public static function commonNamespace(array $namespaces): string |
50 |
{
|
|
51 | 1 |
if ($namespaces === []) { |
52 | 1 |
throw new InvalidArgumentException( |
53 | 1 |
'A default namespace is required for code generation.'
|
54 |
);
|
|
55 |
}
|
|
56 |
|
|
57 | 1 |
if (count($namespaces) === 1) { |
58 | 1 |
return reset($namespaces); |
59 |
}
|
|
60 |
|
|
61 |
// Save the first namespace
|
|
62 | 1 |
$preferredNamespaceFallback = reset($namespaces); |
63 |
|
|
64 |
// If the strings are sorted, any prefix common to all strings
|
|
65 |
// will be common to the sorted first and last strings.
|
|
66 |
// All the strings in the middle can be ignored.
|
|
67 | 1 |
\Safe\sort($namespaces); |
68 |
|
|
69 | 1 |
$firstParts = explode('\\', reset($namespaces)); |
70 | 1 |
$lastParts = explode('\\', end($namespaces)); |
71 |
|
|
72 | 1 |
$matching = []; |
73 | 1 |
foreach ($firstParts as $i => $part) { |
74 |
// We ran out of elements to compare, so we reached the maximum common length
|
|
75 | 1 |
if (! isset($lastParts[$i])) { |
76 |
break; |
|
77 |
}
|
|
78 |
|
|
79 |
// We found an element that differs
|
|
80 | 1 |
if ($lastParts[$i] !== $part) { |
81 | 1 |
break; |
82 |
}
|
|
83 |
|
|
84 | 1 |
$matching [] = $part; |
85 |
}
|
|
86 |
|
|
87 |
// We could not determine a common part of the configured namespaces,
|
|
88 |
// so we just assume the user will prefer the first one in the list.
|
|
89 | 1 |
if ($matching === []) { |
90 | 1 |
return $preferredNamespaceFallback; |
91 |
}
|
|
92 |
|
|
93 | 1 |
return implode('\\', $matching); |
94 |
}
|
|
95 |
}
|
Read our documentation on viewing source code .